1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Helpers/Bank/Nordigen/Nordigen.php

112 lines
3.9 KiB
PHP
Raw Normal View History

2023-11-30 16:00:50 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2023-12-01 14:30:33 +01:00
*
* Documentation of Api-Usage: https://developer.gocardless.com/bank-account-data/overview
2023-11-30 16:00:50 +01:00
*/
2023-12-01 14:30:33 +01:00
namespace App\Helpers\Bank\Nordigen;
2023-11-30 16:00:50 +01:00
use App\Exceptions\NordigenApiException;
use App\Helpers\Bank\Nordigen\Transformer\AccountTransformer;
2023-12-11 16:13:26 +01:00
use App\Helpers\Bank\Nordigen\Transformer\TransactionTransformer;
use Log;
use Nordigen\NordigenPHP\Exceptions\NordigenExceptions\NordigenException;
2023-11-30 16:00:50 +01:00
class Nordigen
{
2023-12-11 16:13:26 +01:00
public bool $test_mode; // https://developer.gocardless.com/bank-account-data/sandbox
2023-11-30 16:00:50 +01:00
2023-12-01 14:30:33 +01:00
public string $sandbox_institutionId = "SANDBOXFINANCE_SFIN0000";
2023-11-30 16:00:50 +01:00
2023-12-01 14:30:33 +01:00
protected \Nordigen\NordigenPHP\API\NordigenClient $client;
2023-11-30 16:00:50 +01:00
public function __construct(string $secret_id, string $secret_key)
2023-11-30 16:00:50 +01:00
{
2023-12-11 16:13:26 +01:00
$this->test_mode = config('ninja.nordigen.test_mode');
$this->client = new \Nordigen\NordigenPHP\API\NordigenClient($secret_id, $secret_key);
$this->client->createAccessToken(); // access_token is valid 24h -> so we dont have to implement a refresh-cycle
2023-11-30 16:00:50 +01:00
}
2023-12-01 14:30:33 +01:00
// metadata-section for frontend
2023-11-30 16:00:50 +01:00
public function getInstitutions()
{
2023-12-01 14:30:33 +01:00
if ($this->test_mode)
return (array) $this->client->institution->getInstitution($this->sandbox_institutionId);
2023-11-30 16:00:50 +01:00
return $this->client->institution->getInstitutions();
}
2023-12-01 14:30:33 +01:00
// requisition-section
public function createRequisition(string $redirect, string $initutionId, string $reference)
2023-12-01 14:30:33 +01:00
{
if ($this->test_mode && $initutionId != $this->sandbox_institutionId)
throw new \Exception('invalid institutionId while in test-mode');
return $this->client->requisition->createRequisition($redirect, $initutionId, null, $reference); // we dont reuse existing requisitions, to prevent double usage of them. see: deleteAccount
2023-12-01 14:30:33 +01:00
}
public function getRequisition(string $requisitionId)
{
return $this->client->requisition->getRequisition($requisitionId);
}
// TODO: return null on not found
2023-12-01 14:30:33 +01:00
public function getAccount(string $account_id)
2023-11-30 16:00:50 +01:00
{
2023-12-12 07:23:53 +01:00
try {
$out = new \stdClass();
2023-11-30 16:00:50 +01:00
2023-12-12 07:23:53 +01:00
$out->data = $this->client->account($account_id)->getAccountDetails()["account"];
$out->metadata = $this->client->account($account_id)->getAccountMetaData();
$out->balances = $this->client->account($account_id)->getAccountBalances()["balances"];
$out->institution = $this->client->institution->getInstitution($out->metadata["institution_id"]);
2023-11-30 16:00:50 +01:00
2023-12-12 07:23:53 +01:00
$it = new AccountTransformer();
return $it->transform($out);
} catch (\Exception $e) {
if (strpos($e->getMessage(), "Invalid Account ID") !== false)
return null;
2023-11-30 16:00:50 +01:00
2023-12-12 07:23:53 +01:00
throw $e;
}
2023-11-30 16:00:50 +01:00
}
2023-12-01 14:30:33 +01:00
public function isAccountActive(string $account_id)
2023-11-30 16:00:50 +01:00
{
2023-12-01 14:30:33 +01:00
try {
$account = $this->client->account($account_id)->getAccountMetaData();
if ($account["status"] != "READY")
return false;
return true;
} catch (\Exception $e) {
2023-12-12 07:23:53 +01:00
if (strpos($e->getMessage(), "Invalid Account ID") !== false)
return false;
2023-12-01 14:30:33 +01:00
2023-12-12 07:23:53 +01:00
throw $e;
}
2023-11-30 16:00:50 +01:00
}
2023-12-09 17:16:01 +01:00
/**
* this method returns booked transactions from the bank_account, pending transactions are not part of the result
* @todo @turbo124 should we include pending transactions within the integration-process and mark them with a specific category?!
*/
2023-12-01 14:30:33 +01:00
public function getTransactions(string $accountId, string $dateFrom = null)
2023-11-30 16:00:50 +01:00
{
2023-12-09 17:16:01 +01:00
$transactionResponse = $this->client->account($accountId)->getAccountTransactions($dateFrom);
2023-12-11 16:13:26 +01:00
$it = new TransactionTransformer();
2023-12-09 17:16:01 +01:00
return $it->transform($transactionResponse);
2023-12-01 14:30:33 +01:00
}
2023-11-30 16:00:50 +01:00
}