2018-10-01 19:24:24 +02:00
|
|
|
<?php
|
2018-10-05 17:54:51 +02:00
|
|
|
/**
|
|
|
|
|
* FinTS.php
|
2019-10-02 06:37:26 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
2018-10-05 17:54:51 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-10-05 17:54:51 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2018-10-05 17:54:51 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-10-05 17:54:51 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-10-05 17:54:51 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-10-05 17:54:51 +02:00
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
2018-10-01 19:24:24 +02:00
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\FinTS;
|
|
|
|
|
|
2018-10-03 13:57:17 +02:00
|
|
|
use Fhp\Model\SEPAAccount;
|
2018-10-01 19:24:24 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-10-03 13:56:53 +02:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2018-10-01 19:24:24 +02:00
|
|
|
|
2018-10-05 17:54:51 +02:00
|
|
|
/**
|
2019-07-31 16:53:09 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-10-05 17:54:51 +02:00
|
|
|
* Class FinTS
|
|
|
|
|
*/
|
2018-10-01 19:24:24 +02:00
|
|
|
class FinTS
|
|
|
|
|
{
|
|
|
|
|
/** @var \Fhp\FinTs */
|
|
|
|
|
private $finTS;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $config
|
2018-10-05 17:54:51 +02:00
|
|
|
*
|
2018-10-01 19:24:24 +02:00
|
|
|
* @throws FireflyException
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $config)
|
|
|
|
|
{
|
2018-10-05 17:54:51 +02:00
|
|
|
if (!isset($config['fints_url'], $config['fints_port'], $config['fints_bank_code'], $config['fints_username'], $config['fints_password'])) {
|
|
|
|
|
throw new FireflyException('Constructed FinTS with incomplete config.');
|
|
|
|
|
}
|
2018-10-01 19:24:24 +02:00
|
|
|
$this->finTS = new \Fhp\FinTs(
|
|
|
|
|
$config['fints_url'],
|
|
|
|
|
$config['fints_port'],
|
|
|
|
|
$config['fints_bank_code'],
|
|
|
|
|
$config['fints_username'],
|
2019-08-25 07:25:01 +02:00
|
|
|
Crypt::decrypt($config['fints_password']) // verified
|
2018-10-01 19:24:24 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:54:51 +02:00
|
|
|
/**
|
|
|
|
|
* @return bool|string
|
|
|
|
|
*/
|
2018-10-01 19:24:24 +02:00
|
|
|
public function checkConnection()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->finTS->getSEPAAccounts();
|
2018-10-05 17:54:51 +02:00
|
|
|
|
2018-10-01 19:24:24 +02:00
|
|
|
return true;
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
return $exception->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $accountNumber
|
2018-10-05 17:54:51 +02:00
|
|
|
*
|
2018-10-03 13:57:17 +02:00
|
|
|
* @return SEPAAccount
|
2018-10-01 19:24:24 +02:00
|
|
|
* @throws FireflyException
|
|
|
|
|
*/
|
2019-02-12 21:49:28 +01:00
|
|
|
public function getAccount(string $accountNumber): SEPAAccount
|
2018-10-01 19:24:24 +02:00
|
|
|
{
|
|
|
|
|
$accounts = $this->getAccounts();
|
2018-10-05 17:54:51 +02:00
|
|
|
$filteredAccounts = array_filter(
|
|
|
|
|
$accounts, function (SEPAAccount $account) use ($accountNumber) {
|
|
|
|
|
return $account->getAccountNumber() === $accountNumber;
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-02-12 21:01:12 +01:00
|
|
|
if (1 !== count($filteredAccounts)) {
|
|
|
|
|
throw new FireflyException(sprintf('Cannot find account with number "%s"', $accountNumber));
|
2018-10-01 19:24:24 +02:00
|
|
|
}
|
2018-10-05 17:54:51 +02:00
|
|
|
|
2018-10-05 16:19:48 +02:00
|
|
|
return reset($filteredAccounts);
|
2018-10-01 19:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:54:51 +02:00
|
|
|
/**
|
|
|
|
|
* @return SEPAAccount[]
|
|
|
|
|
* @throws FireflyException
|
|
|
|
|
*/
|
2019-02-12 21:49:28 +01:00
|
|
|
public function getAccounts(): ?array
|
2018-10-05 17:54:51 +02:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return $this->finTS->getSEPAAccounts();
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
throw new FireflyException($exception->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-01 19:24:24 +02:00
|
|
|
/**
|
2018-10-03 13:57:17 +02:00
|
|
|
* @param SEPAAccount $account
|
2018-10-05 17:54:51 +02:00
|
|
|
* @param \DateTime $from
|
|
|
|
|
* @param \DateTIme $to
|
|
|
|
|
*
|
2018-10-01 19:24:24 +02:00
|
|
|
* @return \Fhp\Model\StatementOfAccount\StatementOfAccount|null
|
|
|
|
|
* @throws FireflyException
|
|
|
|
|
*/
|
2018-10-03 13:57:17 +02:00
|
|
|
public function getStatementOfAccount(SEPAAccount $account, \DateTime $from, \DateTIme $to)
|
2018-10-01 19:24:24 +02:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return $this->finTS->getStatementOfAccount($account, $from, $to);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
throw new FireflyException($exception->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-31 07:48:23 +01:00
|
|
|
}
|