1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Helpers/Bank/Yodlee/Yodlee.php

368 lines
11 KiB
PHP
Raw Normal View History

2022-07-28 06:09:13 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-07-28 06:09:13 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
2022-07-28 07:07:35 +02:00
namespace App\Helpers\Bank\Yodlee;
2022-07-28 06:09:13 +02:00
2022-08-11 04:39:43 +02:00
use App\Exceptions\YodleeApiException;
2022-08-08 01:15:31 +02:00
use App\Helpers\Bank\Yodlee\Transformer\AccountTransformer;
2022-08-08 04:46:41 +02:00
use App\Helpers\Bank\Yodlee\Transformer\IncomeTransformer;
2022-07-28 07:07:35 +02:00
use Illuminate\Support\Facades\Http;
2022-08-10 03:56:46 +02:00
use Illuminate\Support\Str;
2023-02-16 02:36:09 +01:00
2022-07-28 06:09:13 +02:00
class Yodlee
{
2022-08-08 00:26:27 +02:00
public bool $test_mode = false;
2022-07-28 07:07:35 +02:00
2022-08-05 05:45:53 +02:00
private string $api_endpoint = 'https://production.api.yodlee.com/ysl';
2022-07-28 07:07:35 +02:00
2022-08-17 03:52:16 +02:00
private string $dev_api_endpoint = 'https://sandbox.api.yodlee.com/ysl';
2024-01-14 05:05:00 +01:00
2022-08-10 03:56:46 +02:00
private string $test_api_endpoint = 'https://development.api.yodlee.com/ysl';
2022-07-28 07:07:35 +02:00
2022-08-17 03:52:16 +02:00
public string $dev_fast_track_url = 'https://fl4.sandbox.yodlee.com/authenticate/restserver/fastlink';
2022-08-10 03:56:46 +02:00
public string $test_fast_track_url = 'https://fl4.preprod.yodlee.com/authenticate/USDevexPreProd3-449/fastlink?channelAppName=usdevexpreprod3';
public string $production_track_url = 'https://fl4.prod.yodlee.com/authenticate/USDevexProd3-331/fastlink?channelAppName=usdevexprod3';
2022-07-28 07:07:35 +02:00
protected string $client_id;
protected string $client_secret;
protected string $admin_name;
2022-08-08 00:26:27 +02:00
protected ?string $bank_account_id;
public function __construct(?string $bank_account_id = null)
2022-07-28 07:07:35 +02:00
{
2022-08-08 00:26:27 +02:00
$this->bank_account_id = $bank_account_id;
2022-07-28 07:07:35 +02:00
$this->client_id = config('ninja.yodlee.client_id');
$this->client_secret = config('ninja.yodlee.client_secret');
$this->admin_name = config('ninja.yodlee.admin_name');
2022-08-15 22:03:12 +02:00
$this->test_mode = config('ninja.yodlee.test_mode');
2022-08-17 03:52:16 +02:00
config('ninja.yodlee.dev_mode') ? $this->setDevUrl() : null;
2022-07-28 07:07:35 +02:00
}
2022-08-10 03:56:46 +02:00
public function getFastTrackUrl()
{
2023-02-16 02:36:09 +01:00
if (config('ninja.yodlee.dev_mode')) {
return $this->dev_fast_track_url;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2022-08-10 03:56:46 +02:00
return $this->test_mode ? $this->test_fast_track_url : $this->production_track_url;
}
2022-08-08 00:26:27 +02:00
public function setTestMode()
{
$this->test_mode = true;
return $this;
}
2022-08-17 03:52:16 +02:00
public function setDevUrl()
{
$this->test_api_endpoint = $this->dev_api_endpoint;
$this->api_endpoint = $this->dev_api_endpoint;
return $this;
}
2022-08-08 00:26:27 +02:00
public function getEndpoint()
{
return $this->test_mode ? $this->test_api_endpoint : $this->api_endpoint;
}
/**
* If we do not pass in a user
* we pass in the admin username instead
*/
public function getAccessToken($is_admin = false)
2022-07-28 06:09:13 +02:00
{
2023-02-16 02:36:09 +01:00
if ($is_admin) {
2022-07-28 09:33:47 +02:00
$user = $this->admin_name;
2023-02-16 02:36:09 +01:00
} else {
2022-08-08 00:26:27 +02:00
$user = $this->bank_account_id ?: $this->admin_name;
2023-02-16 02:36:09 +01:00
}
2022-07-28 09:33:47 +02:00
2023-02-16 02:36:09 +01:00
$response = $this->bankFormRequest('/auth/token', 'post', [], ['loginName' => $user]);
2022-08-11 04:39:43 +02:00
2022-07-28 08:29:42 +02:00
return $response->token->accessToken;
}
2022-08-10 03:56:46 +02:00
public function createUser($company)
2022-07-28 08:29:42 +02:00
{
2022-08-08 00:26:27 +02:00
$token = $this->getAccessToken(true);
2022-07-28 08:29:42 +02:00
$user['user'] = [
2022-08-10 03:56:46 +02:00
'loginName' => Str::uuid(),
2022-07-28 08:29:42 +02:00
];
2023-02-16 02:36:09 +01:00
/*
{
"user": {
"preferences": {
"dateFormat": "string",
"timeZone": "string",
"currency": "USD",
"locale": "en_US"
},
"address": {
"zip": "string",
"country": "string",
"address3": "string",
"address2": "string",
"city": "string",
"address1": "string",
"state": "string"
},
"loginName": "string",
"name": {
"middle": "string",
"last": "string",
"fullName": "string",
"first": "string"
},
"email": "string",
"segmentName": "string"
}
}
*/
2022-08-06 08:58:48 +02:00
2022-08-08 00:26:27 +02:00
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->post($this->getEndpoint(). "/user/register", $user);
2022-08-06 08:58:48 +02:00
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-08-06 08:58:48 +02:00
return $response->object();
2023-02-16 02:36:09 +01:00
}
2022-08-06 08:58:48 +02:00
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-11 04:39:43 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
2022-07-28 07:07:35 +02:00
}
2022-08-08 00:26:27 +02:00
public function getAccounts($params = [])
2022-07-28 09:33:47 +02:00
{
2022-08-08 00:26:27 +02:00
$token = $this->getAccessToken();
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/accounts", $params);
2022-08-05 05:45:53 +02:00
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-08-08 01:15:31 +02:00
$at = new AccountTransformer();
return $at->transform($response->object());
2022-08-11 04:39:43 +02:00
}
2022-08-05 05:45:53 +02:00
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-11 06:19:35 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
2022-08-11 06:19:35 +02:00
}
public function getAccount($account_id)
{
$token = $this->getAccessToken();
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/accounts/{$account_id}", []);
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
return true;
2023-02-16 02:36:09 +01:00
}
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
return false;
2023-02-16 02:36:09 +01:00
}
}
public function getAccountSummary($account_id)
{
$token = $this->getAccessToken();
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/accounts/{$account_id}", []);
if ($response->successful()) {
return $response->object();
}
if ($response->failed()) {
return false;
}
}
2022-08-11 06:19:35 +02:00
public function deleteAccount($account_id)
{
$token = $this->getAccessToken();
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->delete($this->getEndpoint(). "/accounts/{$account_id}", []);
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-08-11 06:19:35 +02:00
return true;
}
2022-07-28 09:33:47 +02:00
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-11 04:39:43 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
2022-07-28 09:33:47 +02:00
}
2022-08-08 00:26:27 +02:00
public function getTransactions($params = [])
2022-07-30 04:10:42 +02:00
{
2022-08-08 00:26:27 +02:00
$token = $this->getAccessToken();
2024-01-14 05:05:00 +01:00
2022-08-08 00:26:27 +02:00
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/transactions", $params);
2022-07-30 04:10:42 +02:00
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-08-08 04:46:41 +02:00
$it = new IncomeTransformer();
return $it->transform($response->object());
}
2022-08-05 05:45:53 +02:00
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-11 04:39:43 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
2022-07-30 04:10:42 +02:00
}
2022-08-17 08:37:05 +02:00
public function getTransactionCount($params = [])
{
$token = $this->getAccessToken();
2024-01-14 05:05:00 +01:00
2022-08-17 08:37:05 +02:00
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/transactions/count", $params);
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-08-17 08:37:05 +02:00
return $response->object();
}
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-17 08:37:05 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
}
2022-08-17 08:37:05 +02:00
2022-08-08 00:26:27 +02:00
public function getTransactionCategories($params = [])
2022-07-28 07:07:35 +02:00
{
2022-08-08 00:26:27 +02:00
$token = $this->getAccessToken();
2022-07-28 07:07:35 +02:00
2022-08-08 00:26:27 +02:00
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/transactions/categories", $params);
2022-07-28 08:29:42 +02:00
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-07-28 08:29:42 +02:00
return $response->object();
2023-02-16 02:36:09 +01:00
}
2022-07-28 08:29:42 +02:00
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-11 04:39:43 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
2022-07-28 08:29:42 +02:00
}
2022-08-18 01:27:28 +02:00
private function bankFormRequest(string $uri, string $verb, array $data, array $headers)
2022-07-28 08:29:42 +02:00
{
2022-08-08 00:26:27 +02:00
$response = Http::withHeaders($this->getFormHeaders($headers))->asForm()->{$verb}($this->getEndpoint() . $uri, $this->buildBody());
2022-07-28 07:07:35 +02:00
2023-02-16 02:36:09 +01:00
if ($response->successful()) {
2022-07-28 07:07:35 +02:00
return $response->object();
2023-02-16 02:36:09 +01:00
}
2022-07-28 07:07:35 +02:00
2023-02-16 02:36:09 +01:00
if ($response->failed()) {
2022-08-11 04:39:43 +02:00
throw new YodleeApiException($response->body());
2023-02-16 02:36:09 +01:00
}
2022-07-28 07:07:35 +02:00
}
private function getHeaders($data = [])
{
return array_merge($data, [
'Api-Version' => '1.1',
2022-07-28 08:29:42 +02:00
'ContentType' => 'application/json'
]);
}
private function getFormHeaders($data = [])
{
return array_merge($data, [
'Api-Version' => '1.1',
2022-07-28 07:07:35 +02:00
]);
}
private function buildBody()
{
return [
'clientId' => $this->client_id,
'secret' => $this->client_secret,
];
2022-07-28 06:09:13 +02:00
}
2023-08-10 04:17:37 +02:00
/**
* updateEligibility
2023-10-26 04:57:44 +02:00
*
2023-08-10 04:17:37 +02:00
* ALLOW_UPDATE
* ALLOW_UPDATE_WITH_CREDENTIALS
* DISALLOW_UPDATE
*/
/**
* additionalStatus
2023-10-26 04:57:44 +02:00
*
2023-08-10 04:17:37 +02:00
* LOGIN_IN_PROGRESS
* DATA_RETRIEVAL_IN_PROGRESS
* ACCT_SUMMARY_RECEIVED
* AVAILABLE_DATA_RETRIEVED
* PARTIAL_DATA_RETRIEVED
* DATA_RETRIEVAL_FAILED
* DATA_NOT_AVAILABLE
* ACCOUNT_LOCKED
* ADDL_AUTHENTICATION_REQUIRED
* BETA_SITE_DEV_IN_PROGRESS
* CREDENTIALS_UPDATE_NEEDED
* INCORRECT_CREDENTIALS
* PROPERTY_VALUE_NOT_AVAILABLE
* INVALID_ADDL_INFO_PROVIDED
* REQUEST_TIME_OUT
* SITE_BLOCKING_ERROR
* UNEXPECTED_SITE_ERROR
* SITE_NOT_SUPPORTED
* SITE_UNAVAILABLE
* TECH_ERROR
* USER_ACTION_NEEDED_AT_SITE
* SITE_SESSION_INVALIDATED
* NEW_AUTHENTICATION_REQUIRED
* DATASET_NOT_SUPPORTED
* ENROLLMENT_REQUIRED_FOR_DATASET
* CONSENT_REQUIRED
* CONSENT_EXPIRED
* CONSENT_REVOKED
* INCORRECT_OAUTH_TOKEN
* MIGRATION_IN_PROGRESS
2023-10-26 04:57:44 +02:00
*/
2023-08-10 04:17:37 +02:00
/**
* IN_PROGRESS LOGIN_IN_PROGRESS Provider login is in progress.
* IN_PROGRESS USER_INPUT_REQUIRED Provider site requires MFA-based authentication and needs user input for login.
* IN_PROGRESS LOGIN_SUCCESS Provider login is successful.
* IN_PROGRESS ACCOUNT_SUMMARY_RETRIEVED Account summary info may not have the complete info of accounts that are available in the provider site. This depends on the sites behaviour. Account summary info may not be available at all times.
* FAILED NEVER_INITIATED The add or update provider account was not triggered due to techincal reasons. This is a rare occurrence and usually resolves quickly.
* FAILED LOGIN_FAILED Provider login failed.
* FAILED REQUEST_TIME_OUT The process timed out.
* FAILED DATA_RETRIEVAL_FAILED All accounts under the provider account failed with same or different errors, though login was successful.
* FAILED No additional status or information will be provided when there are errors other than the ones listed above.
* PARTIAL_SUCCESS PARTIAL_DATA_RETRIEVED DATA_RETRIEVAL_FAILED_PARTIALLY One/few accounts data gathered and one/few accounts failed.
* PARTIAL_SUCCESS PARTIAL_DATA_RETRIEVED_REM_SCHED DATA_RETRIEVAL_FAILED_PARTIALLY One/few accounts data gathered One/few accounts failed
* SUCCESS All accounts under the provider was added or updated successfully.
*/
2023-10-26 04:57:44 +02:00
/**
* updateEligibility
*
* ALLOW_UPDATE The status indicates that the account is eligible for the next update and applies to both MFA and non-MFA accounts. For MFA-based accounts, the user may have to provide the MFA details during account refresh.
* ALLOW_UPDATE_WITH_CREDENTIALS The status indicates updating or refreshing the account by directing the user to edit the provided credentials.
* DISALLOW_UPDATE The status indicates the account is not eligible for the update or refresh process due to a site issue or a technical error.
*/
2023-08-10 04:17:37 +02:00
2022-07-28 06:09:13 +02:00
}