From d98fd30add805df28ad391f838c092106a985cfc Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 28 Jul 2022 15:07:35 +1000 Subject: [PATCH] Auth tokens --- app/Helpers/Bank/Yodlee/Yodlee.php | 67 +++++++++++++++++++++++++++- config/ninja.php | 6 ++- tests/Feature/Bank/YodleeApiTest.php | 40 +++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/Bank/YodleeApiTest.php diff --git a/app/Helpers/Bank/Yodlee/Yodlee.php b/app/Helpers/Bank/Yodlee/Yodlee.php index 5dc13ed34a87..1a4b47692629 100644 --- a/app/Helpers/Bank/Yodlee/Yodlee.php +++ b/app/Helpers/Bank/Yodlee/Yodlee.php @@ -9,13 +9,78 @@ * @license https://www.elastic.co/licensing/elastic-license */ -namespace App\Helpers\Yodlee; +namespace App\Helpers\Bank\Yodlee; +use Illuminate\Support\Facades\Http; + class Yodlee { + public bool $test_mode; + + private string $api_endpoint = ''; + + private string $test_api_endpoint = 'https://sandbox.api.yodlee.com/ysl'; + + protected string $fast_track_url = 'https://fl4.sandbox.yodlee.com/authenticate/restserver/fastlink'; + + 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'); + + } + public function getAccessToken() { + return $this->bankRequest('/auth/token', 'post'); + } + + private function bankRequest(string $uri, string $verb, array $data = []) + { + nlog($this->getHeaders()); + nlog($this->buildBody()); + nlog($this->api_endpoint . $uri); + + $response = Http::withHeaders($this->getHeaders())->asForm()->{$verb}($this->api_endpoint . $uri, $this->buildBody()); + + if($response->successful()) + return $response->object(); + + if($response->failed()) + return $response->body(); + + } + + private function getHeaders($data = []) + { + return array_merge($data, [ + 'Api-Version' => '1.1', + 'loginName' => $this->admin_name + ]); + } + + private function buildBody() + { + + return [ + 'clientId' => $this->client_id, + 'secret' => $this->client_secret, + ]; } diff --git a/config/ninja.php b/config/ninja.php index d0670923e0b2..d6ad129dac02 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -200,5 +200,9 @@ return [ 'twilio_account_sid' => env('TWILIO_ACCOUNT_SID',false), 'twilio_auth_token' => env('TWILIO_AUTH_TOKEN',false), 'twilio_verify_sid' => env('TWILIO_VERIFY_SID',false), - + 'yodlee' => [ + 'client_id' => env('YODLEE_CLIENT_ID',false), + 'client_secret' => env('YODLEE_CLIENT_SECRET', false), + 'admin_name' => env('YODLEE_LOGIN_ADMIN_NAME', false), + ], ]; diff --git a/tests/Feature/Bank/YodleeApiTest.php b/tests/Feature/Bank/YodleeApiTest.php new file mode 100644 index 000000000000..12b7289df5db --- /dev/null +++ b/tests/Feature/Bank/YodleeApiTest.php @@ -0,0 +1,40 @@ +getAccessToken(); + + nlog($access_token); + + $this->assertNotNull($access_token); + } + +}