diff --git a/app/Helpers/Bank/Nordigen/Http/Client.php b/app/Helpers/Bank/Nordigen/Http/NordigenClient.php similarity index 92% rename from app/Helpers/Bank/Nordigen/Http/Client.php rename to app/Helpers/Bank/Nordigen/Http/NordigenClient.php index df51cb660e..075dd9a4be 100644 --- a/app/Helpers/Bank/Nordigen/Http/Client.php +++ b/app/Helpers/Bank/Nordigen/Http/NordigenClient.php @@ -8,15 +8,14 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -class Client +class NordigenClient { - private string $baseUrl; - private string $accessToken; + private string $baseUrl = 'https://bankaccountdata.gocardless.com/api/v2'; + private PendingRequest $httpClient; - public function __construct(string $baseUrl, string $accessToken) + public function __construct(private string $accessToken) { - $this->baseUrl = rtrim($baseUrl, '/'); $this->accessToken = $accessToken; $this->httpClient = Http::withHeaders([ 'Authorization' => "Bearer {$this->accessToken}", @@ -39,7 +38,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/requisitions/", $params); - return $this->handlePaginatedResponse($response, 'requisitions'); + return $this->handlePaginatedResponse($response); } /** @@ -94,6 +93,7 @@ class Client do { $requisitions = $this->getRequisitions($limit, $offset); + nlog($requisitions); if ($requisitions->isEmpty()) { break; } @@ -122,7 +122,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/agreements/", $params); - return $this->handlePaginatedResponse($response, 'agreements'); + return $this->handlePaginatedResponse($response); } /** @@ -198,14 +198,11 @@ class Client */ public function getInstitutions(int $limit = 100, ?string $offset = null): Collection { - $params = ['limit' => $limit]; - if ($offset) { - $params['offset'] = $offset; - } + $params = []; $response = $this->httpClient->get("{$this->baseUrl}/institutions/", $params); - return $this->handlePaginatedResponse($response, 'institutions'); + return $this->handlePaginatedResponse($response); } /** @@ -230,7 +227,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/institutions/", $params); - return $this->handlePaginatedResponse($response, 'institutions'); + return $this->handlePaginatedResponse($response); } /** @@ -273,7 +270,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/enduser-agreements/", $params); - return $this->handlePaginatedResponse($response, 'enduser_agreements'); + return $this->handlePaginatedResponse($response); } /** @@ -330,7 +327,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/accounts/", $params); - return $this->handlePaginatedResponse($response, 'accounts'); + return $this->handlePaginatedResponse($response); } /** @@ -447,7 +444,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/accounts/{$accountId}/transactions/", $params); - return $this->handlePaginatedResponse($response, 'transactions'); + return $this->handlePaginatedResponse($response); } /** @@ -475,7 +472,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/transactions/", $params); - return $this->handlePaginatedResponse($response, 'transactions'); + return $this->handlePaginatedResponse($response); } /** @@ -527,7 +524,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/accounts/{$accountId}/transactions/", $params); - return $this->handlePaginatedResponse($response, 'transactions'); + return $this->handlePaginatedResponse($response); } /** @@ -550,7 +547,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/accounts/{$accountId}/transactions/", $params); - return $this->handlePaginatedResponse($response, 'transactions'); + return $this->handlePaginatedResponse($response); } /** @@ -573,7 +570,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/accounts/{$accountId}/transactions/", $params); - return $this->handlePaginatedResponse($response, 'transactions'); + return $this->handlePaginatedResponse($response); } /** @@ -598,7 +595,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/accounts/{$accountId}/transactions/", $params); - return $this->handlePaginatedResponse($response, 'transactions'); + return $this->handlePaginatedResponse($response); } // ==================== PAYMENTS ==================== @@ -615,7 +612,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/payments/", $params); - return $this->handlePaginatedResponse($response, 'payments'); + return $this->handlePaginatedResponse($response); } /** @@ -672,7 +669,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/mandates/", $params); - return $this->handlePaginatedResponse($response, 'mandates'); + return $this->handlePaginatedResponse($response); } /** @@ -729,7 +726,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/refunds/", $params); - return $this->handlePaginatedResponse($response, 'refunds'); + return $this->handlePaginatedResponse($response); } /** @@ -766,7 +763,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/events/", $params); - return $this->handlePaginatedResponse($response, 'events'); + return $this->handlePaginatedResponse($response); } /** @@ -793,7 +790,7 @@ class Client $response = $this->httpClient->get("{$this->baseUrl}/webhooks/", $params); - return $this->handlePaginatedResponse($response, 'webhooks'); + return $this->handlePaginatedResponse($response); } /** @@ -841,20 +838,16 @@ class Client /** * Handle paginated response */ - private function handlePaginatedResponse(Response $response, string $dataKey): Collection + private function handlePaginatedResponse(Response $response): Collection { if (!$response->successful()) { $this->logError('Paginated request failed', $response); return collect(); } - $data = $response->json(); - - if (!isset($data[$dataKey])) { - return collect(); - } - - return collect($data[$dataKey]); + $data = $response->json()['results']; + + return collect($data); } /** @@ -875,7 +868,8 @@ class Client */ private function logError(string $message, Response $response): void { - Log::error($message, [ + nlog([ + 'message' => $message, 'status' => $response->status(), 'body' => $response->body(), 'headers' => $response->headers() diff --git a/app/Helpers/Bank/Nordigen/Nordigen.php b/app/Helpers/Bank/Nordigen/Nordigen.php index 4f185b0c43..1dfb02a83f 100644 --- a/app/Helpers/Bank/Nordigen/Nordigen.php +++ b/app/Helpers/Bank/Nordigen/Nordigen.php @@ -177,6 +177,21 @@ class Nordigen ); } + + public function validAgreement($institution_id, $_accounts) + { + + $nc = new \App\Helpers\Bank\Nordigen\Http\NordigenClient($this->client->getAccessToken()); + $requisitions = $nc->getAllRequisitions(); + + $requisitions->filter(function($requisition) use ($institution_id, $_accounts){ + if($requisition['institution_id'] == $institution_id && !empty(array_intersect($requisition['accounts'], $_accounts))){ + return $requisition; + } + }); + + } + public function getRequisition(string $requisitionId) { try { @@ -196,11 +211,30 @@ class Nordigen try { $out = new \stdClass(); - $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']); + if($out->metadata['status'] == 'READY'){ + $out->data = $this->client->account($account_id)->getAccountDetails()['account']; + $out->balances = $this->client->account($account_id)->getAccountBalances()['balances']; + } + else{ + + $out->data = [ + 'iban' => $out->metadata['iban'], + 'ownerName' => $out->metadata['owner_name'], + ]; + $out->balances = [ + [ + 'balanceType' => '', + 'balanceAmount' => [ + 'amount' => 0, + 'currency' => '', + ], + ], + ]; + } + $it = new AccountTransformer(); return $it->transform($out); diff --git a/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php b/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php index f06279a9f7..d8b8227a90 100644 --- a/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php +++ b/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php @@ -139,7 +139,7 @@ class TransactionTransformer implements BankRevenueInterface (array_key_exists('creditorName', $transaction) ? $transaction['creditorName'] : null); - return [ + $data = [ 'transaction_id' => 0, 'nordigen_transaction_id' => $transactionId, 'amount' => abs($amount), @@ -153,8 +153,16 @@ class TransactionTransformer implements BankRevenueInterface 'base_type' => $base_type, ]; + // $data['currency_code'] = $this->makeHash($data); + + return $data; } + // private function makeHash($data) + // { + // return hash('sha1', $data['amount'].$data['date'].$data['description'].$data['participant'].$data['participant_name'].$data['base_type']); + // } + private function convertCurrency(string $code) { diff --git a/app/Http/Controllers/Bank/NordigenV2Controller.php b/app/Http/Controllers/Bank/NordigenV2Controller.php deleted file mode 100644 index eaf11d117f..0000000000 --- a/app/Http/Controllers/Bank/NordigenV2Controller.php +++ /dev/null @@ -1,63 +0,0 @@ -json(['message' => 'Not yet authenticated with Nordigen Bank Integration service'], 400); - } - - $nordigen = new Nordigen(); - - return response()->json($nordigen->getInstitutions()); - } - -} \ No newline at end of file diff --git a/app/Http/Requests/Nordigen/ConnectNordigenBankIntegrationRequest.php b/app/Http/Requests/Nordigen/ConnectNordigenBankIntegrationRequest.php index 9adb5104b7..78f02caf7c 100644 --- a/app/Http/Requests/Nordigen/ConnectNordigenBankIntegrationRequest.php +++ b/app/Http/Requests/Nordigen/ConnectNordigenBankIntegrationRequest.php @@ -46,6 +46,10 @@ class ConnectNordigenBankIntegrationRequest extends Request $input['institution_id'] = $context['institution_id']; } + if(isset($context['bank_account_id'])){ + $input['bank_account_id'] = $context['bank_account_id']; + } + $input['redirect'] = ($context['is_react'] ?? false) ? config('ninja.react_url') . '/#/settings/bank_accounts' : config('ninja.app_url'); diff --git a/app/Models/BankTransaction.php b/app/Models/BankTransaction.php index ceab4dd8dc..72e691a7b1 100644 --- a/app/Models/BankTransaction.php +++ b/app/Models/BankTransaction.php @@ -90,7 +90,8 @@ class BankTransaction extends BaseModel 'vendor_id', 'amount', 'participant', - 'participant_name' + 'participant_name', + 'currency_code' ];