1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Helpers/Bank/Yodlee/Yodlee.php

159 lines
3.8 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
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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-07-28 07:07:35 +02:00
use Illuminate\Support\Facades\Http;
2022-07-28 06:09:13 +02:00
class Yodlee
{
2022-07-28 07:07:35 +02:00
public bool $test_mode;
private string $api_endpoint = '';
private string $test_api_endpoint = 'https://sandbox.api.yodlee.com/ysl';
2022-07-28 08:29:42 +02:00
public string $fast_track_url = 'https://fl4.sandbox.yodlee.com/authenticate/restserver/fastlink';
2022-07-28 07:07:35 +02:00
protected string $client_id;
protected string $client_secret;
protected string $admin_name;
public function __construct(bool $test_mode = false)
{
$this->test_mode = $test_mode;
if($this->test_mode)
$this->api_endpoint = $this->test_api_endpoint;
$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-07-28 09:33:47 +02:00
public function getAccessToken($user = false)
2022-07-28 06:09:13 +02:00
{
2022-07-28 09:33:47 +02:00
if(!$user)
$user = $this->admin_name;
$response = $this->bankFormRequest('/auth/token', 'post', [], ['loginName' => $user]);
2022-07-28 08:29:42 +02:00
return $response->token->accessToken;
}
public function createUser()
{
2022-07-28 09:33:47 +02:00
$token = $this->getAccessToken();
2022-07-28 08:29:42 +02:00
$user['user'] = [
'loginName' => 'test123',
];
2022-07-28 09:33:47 +02:00
return $this->bankRequest('/user/register', 'post', $user, ['Authorization' => $token]);
2022-07-28 08:29:42 +02:00
2022-07-28 07:07:35 +02:00
}
2022-07-28 09:33:47 +02:00
public function getAccounts($token)
{
2022-07-30 03:43:24 +02:00
$response = $this->bankRequest('/accounts', 'get', [], ["Authorization" => "Bearer {$token}"]);
2022-07-28 09:33:47 +02:00
return $response;
}
2022-07-30 04:10:42 +02:00
public function getTransactions($token)
{
2022-08-05 05:04:02 +02:00
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->api_endpoint. "/transactions", ['categoryType' => 'EXPENSE']);
2022-08-05 04:58:45 +02:00
// $response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->api_endpoint. "/transactions");
2022-07-30 04:10:42 +02:00
if($response->successful())
return $response->object();
if($response->failed())
return $response->body();
}
public function getTransactionCategories($token)
{
$response = $this->bankRequest('/transactions/categories', 'get', [], ["Authorization" => "Bearer {$token}"]);
return $response;
}
2022-07-28 09:33:47 +02:00
private function bankRequest(string $uri, string $verb, array $data = [], array $headers = [])
2022-07-28 07:07:35 +02:00
{
2022-07-28 09:33:47 +02:00
$response = Http::withHeaders($this->getHeaders($headers))->{$verb}($this->api_endpoint . $uri, $this->buildBody());
2022-07-28 08:29:42 +02:00
if($response->successful())
return $response->object();
if($response->failed())
return $response->body();
}
2022-07-28 09:33:47 +02:00
private function bankFormRequest(string $uri, string $verb, array $data = [], array $headers)
2022-07-28 08:29:42 +02:00
{
2022-07-28 09:33:47 +02:00
$response = Http::withHeaders($this->getFormHeaders($headers))->asForm()->{$verb}($this->api_endpoint . $uri, $this->buildBody());
2022-07-28 07:07:35 +02:00
if($response->successful())
return $response->object();
if($response->failed())
return $response->body();
}
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
}
}