1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Helpers/Bank/Nordigen/Nordigen.php

170 lines
5.6 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
*
* Institutions: Are Banks or Payment-Providers, which manages bankaccounts.
*
* Accounts: Accounts are existing bank_accounts at a specific institution.
*
* Requisitions: Are registered/active user-flows to authenticate one or many accounts. After completition, the accoundId could be used to fetch data for this account. After the access expires, the user could create a new requisition to connect accounts again.
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
2024-01-12 10:58:55 +01:00
use App\Services\Email\Email;
use App\Models\BankIntegration;
use App\Services\Email\EmailObject;
use Illuminate\Support\Facades\App;
2023-11-30 16:00:50 +01:00
use App\Helpers\Bank\Nordigen\Transformer\AccountTransformer;
2023-12-11 16:13:26 +01:00
use App\Helpers\Bank\Nordigen\Transformer\TransactionTransformer;
2024-01-12 10:58:55 +01:00
use Illuminate\Mail\Mailables\Address;
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()
2023-11-30 16:00:50 +01:00
{
2023-12-11 16:13:26 +01:00
$this->test_mode = config('ninja.nordigen.test_mode');
2024-01-14 05:05:00 +01:00
if (!(config('ninja.nordigen.secret_id') && config('ninja.nordigen.secret_key'))) {
throw new \Exception('missing nordigen credentials');
2024-01-14 05:05:00 +01:00
}
$this->client = new \Nordigen\NordigenPHP\API\NordigenClient(config('ninja.nordigen.secret_id'), config('ninja.nordigen.secret_key'));
2023-12-26 04:31:03 +01:00
$this->client->createAccessToken();
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()
{
2024-01-14 05:05:00 +01:00
if ($this->test_mode) {
return [$this->client->institution->getInstitution($this->sandbox_institutionId)];
2024-01-14 05:05:00 +01:00
}
2023-12-01 14:30:33 +01:00
2023-11-30 16:00:50 +01:00
return $this->client->institution->getInstitutions();
}
2023-12-01 14:30:33 +01:00
// requisition-section
2023-12-27 04:05:19 +01:00
public function createRequisition(string $redirect, string $initutionId, string $reference, string $userLanguage)
2023-12-01 14:30:33 +01:00
{
2024-01-14 05:05:00 +01:00
if ($this->test_mode && $initutionId != $this->sandbox_institutionId) {
2023-12-01 14:30:33 +01:00
throw new \Exception('invalid institutionId while in test-mode');
2024-01-14 05:05:00 +01:00
}
2023-12-01 14:30:33 +01:00
2023-12-27 04:05:19 +01:00
return $this->client->requisition->createRequisition($redirect, $initutionId, null, $reference, $userLanguage);
2023-12-01 14:30:33 +01:00
}
public function getRequisition(string $requisitionId)
{
try {
return $this->client->requisition->getRequisition($requisitionId);
} catch (\Exception $e) {
2024-01-14 05:05:00 +01:00
if (strpos($e->getMessage(), "Invalid Requisition ID") !== false) {
return false;
2024-01-14 05:05:00 +01:00
}
throw $e;
}
2023-12-01 14:30:33 +01:00
}
// 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);
2024-01-14 05:05:00 +01:00
2023-12-12 07:23:53 +01:00
} catch (\Exception $e) {
2024-01-14 05:05:00 +01:00
if (strpos($e->getMessage(), "Invalid Account ID") !== false) {
return false;
2024-01-14 05:05:00 +01:00
}
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
}
2024-01-14 05:05:00 +01:00
2023-12-26 00:24:42 +01:00
/**
* isAccountActive
*
* @param string $account_id
* @return bool
*/
public function isAccountActive(string $account_id): bool
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") {
nlog('nordigen account was not in status ready. accountId: ' . $account_id . ' status: ' . $account["status"]);
2023-12-01 14:30:33 +01:00
return false;
}
2023-12-01 14:30:33 +01:00
return true;
} catch (\Exception $e) {
2024-01-14 05:05:00 +01:00
if (strpos($e->getMessage(), "Invalid Account ID") !== false) {
2023-12-12 07:23:53 +01:00
return false;
2024-01-14 05:05:00 +01:00
}
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
}
2024-01-14 05:05:00 +01:00
2023-12-09 17:16:01 +01:00
/**
2023-12-26 00:24:42 +01:00
* getTransactions
*
* @param string $accountId
* @param string $dateFrom
* @return array
2023-12-09 17:16:01 +01:00
*/
2023-12-26 00:24:42 +01:00
public function getTransactions(string $accountId, string $dateFrom = null): array
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
}
2024-01-12 10:58:55 +01:00
public function disabledAccountEmail(BankIntegration $bank_integration): void
{
App::setLocale($bank_integration->company->getLocale());
$mo = new EmailObject();
$mo->subject = ctrans('texts.nordigen_requisition_subject');
$mo->body = ctrans('texts.nordigen_requisition_body');
$mo->text_body = ctrans('texts.nordigen_requisition_body');
$mo->company_key = $bank_integration->company->company_key;
$mo->html_template = 'email.template.generic';
$mo->to = [new Address($bank_integration->company->owner()->email, $bank_integration->company->owner()->present()->name())];
$mo->email_template_body = 'nordigen_requisition_body';
$mo->email_template_subject = 'nordigen_requisition_subject';
Email::dispatch($mo, $bank_integration->company);
}
2023-11-30 16:00:50 +01:00
}