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.
This commit is contained in:
Dave Shoreman 2024-12-23 20:31:31 +00:00
parent 5f75b6a9e1
commit 691478d3e7
No known key found for this signature in database
GPG Key ID: C920D1D63709F443
2 changed files with 14 additions and 9 deletions

View File

@ -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'],
);
}

View File

@ -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()})";