test_mode = config('ninja.nordigen.test_mode'); if (!(config('ninja.nordigen.secret_id') && config('ninja.nordigen.secret_key'))) { throw new \Exception('missing nordigen credentials'); } $this->client = new \Nordigen\NordigenPHP\API\NordigenClient(config('ninja.nordigen.secret_id'), config('ninja.nordigen.secret_key')); $this->client->createAccessToken(); } // metadata-section for frontend public function getInstitutions() { if ($this->test_mode) { return [$this->client->institution->getInstitution($this->sandbox_institutionId)]; } return $this->client->institution->getInstitutions(); } // requisition-section public function createRequisition(string $redirect, string $initutionId, string $reference, string $userLanguage) { 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, $userLanguage); } public function getRequisition(string $requisitionId) { try { return $this->client->requisition->getRequisition($requisitionId); } catch (\Exception $e) { if (strpos($e->getMessage(), "Invalid Requisition ID") !== false) { return false; } throw $e; } } // TODO: return null on not found public function getAccount(string $account_id) { 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"]); $it = new AccountTransformer(); return $it->transform($out); } catch (\Exception $e) { nlog("Nordigen getAccount() failed => {$account_id} => " . $e->getMessage()); return false; } } /** * isAccountActive * * @param string $account_id * @return bool */ public function isAccountActive(string $account_id): bool { 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"]); return false; } return true; } catch (\Exception $e) { if (strpos($e->getMessage(), "Invalid Account ID") !== false) { return false; } throw $e; } } /** * getTransactions * * @param string $accountId * @param string $dateFrom * @return array */ public function getTransactions(Company $company, string $accountId, string $dateFrom = null): array { $transactionResponse = $this->client->account($accountId)->getAccountTransactions($dateFrom); $it = new TransactionTransformer($company); return $it->transform($transactionResponse); } 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); } }