From 691478d3e797be4645f0a2a888c8350915b90e5c Mon Sep 17 00:00:00 2001 From: Dave Shoreman Date: Mon, 23 Dec 2024 20:31:31 +0000 Subject: [PATCH] Support setting custom Nordigen access duration If agreements are already available and no custom `access_days` is passed, it will simply take the first match regardless of duration. When a new agreement is needed, it defaults to the max supported. --- app/Helpers/Bank/Nordigen/Nordigen.php | 13 ++++++++----- app/Http/Controllers/Bank/NordigenController.php | 10 ++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Helpers/Bank/Nordigen/Nordigen.php b/app/Helpers/Bank/Nordigen/Nordigen.php index 54abadf7c7..dbfb4295d9 100644 --- a/app/Helpers/Bank/Nordigen/Nordigen.php +++ b/app/Helpers/Bank/Nordigen/Nordigen.php @@ -92,14 +92,14 @@ class Nordigen * accepted: ?string, * }[] EndUserAgreement list */ - public function firstValidAgreement(string $institutionId, int $txDays): ?array + public function firstValidAgreement(string $institutionId, int $accessDays, int $txDays): ?array { $requiredScopes = ['balances', 'details', 'transactions']; try { return Arr::first( $this->client->endUserAgreement->getEndUserAgreements()['results'], - function (array $eua) use ($institutionId, $requiredScopes, $txDays): bool { + function (array $eua) use ($institutionId, $requiredScopes, $accessDays, $txDays): bool { $expiresAt = $eua['accepted'] ? (new \DateTimeImmutable($eua['accepted']))->add( new \DateInterval("P{$eua['access_valid_for_days']}D") ) : false; @@ -107,6 +107,7 @@ class Nordigen return $eua['institution_id'] === $institutionId && $eua['accepted'] === null && $eua['max_historical_days'] >= $txDays + && $eua['access_valid_for_days'] >= $accessDays && !array_diff($requiredScopes, $eua['access_scope'] ?? []); }, null @@ -137,13 +138,15 @@ class Nordigen * accepted: string * }|null Agreement details */ - public function createAgreement(array $institution, int $transactionDays): array + public function createAgreement(array $institution, int $accessDays, int $transactionDays): array { $txDays = $transactionDays < 30 ? 30 : $transactionDays; - $max = $institution['transaction_total_days']; + $maxAccess = $institution['max_access_valid_for_days']; + $maxTx = $institution['transaction_total_days']; return $this->client->endUserAgreement->createEndUserAgreement( - maxHistoricalDays: $txDays > $max ? $max : $txDays, + accessValidForDays: $accessDays > $maxAccess ? $maxAccess : $accessDays, + maxHistoricalDays: $txDays > $maxTx ? $maxTx : $txDays, institutionId: $institution['id'], ); } diff --git a/app/Http/Controllers/Bank/NordigenController.php b/app/Http/Controllers/Bank/NordigenController.php index 7b7c727ac0..0470dcd369 100644 --- a/app/Http/Controllers/Bank/NordigenController.php +++ b/app/Http/Controllers/Bank/NordigenController.php @@ -86,15 +86,17 @@ class NordigenController extends BaseController $nordigenAccount = $nordigen->getAccount($integration->nordigen_account_id); $euaId = preg_replace($match, '${1}', $nordigenAccount['error']); - // Fetch the old agreement and maintain its history setting - $data['tx_days'] = $nordigen->getAgreement($euaId)['max_historical_days']; + // Fetch the old agreement and maintain its access/history settings + $agreement = $nordigen->getAgreement($euaId); + $data['access_days'] = $agreement['max_access_valid_for_days']; + $data['tx_days'] = $agreement['max_historical_days']; } try { $txDays = $data['tx_days'] ?? 0; - $agreement = $nordigen->firstValidAgreement($institution['id'], $txDays) - ?? $nordigen->createAgreement($institution, $txDays); + $agreement = $nordigen->firstValidAgreement($institution['id'], $data['access_days'] ?? 0, $txDays) + ?? $nordigen->createAgreement($institution, $data['access_days'] ?? 9999, $txDays); } catch (\Exception $e) { $debug = "{$e->getMessage()} ({$e->getCode()})";