mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Transaction transformers
This commit is contained in:
parent
b7fbfe6531
commit
38704c6ce5
17
app/Helpers/Bank/AccountTransformerInterface.php
Normal file
17
app/Helpers/Bank/AccountTransformerInterface.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
namespace App\Helpers\Bank;
|
||||
|
||||
interface AccountTransformerInterface
|
||||
{
|
||||
public function transform($accounts);
|
||||
}
|
@ -13,5 +13,5 @@ namespace App\Helpers\Bank;
|
||||
|
||||
interface BankRevenueInterface
|
||||
{
|
||||
|
||||
public function transform($transaction);
|
||||
}
|
||||
|
@ -10,6 +10,8 @@
|
||||
*/
|
||||
|
||||
namespace App\Helpers\Bank\Yodlee\Transformer;
|
||||
|
||||
use App\Helpers\Bank\AccountTransformerInterface;
|
||||
|
||||
/**
|
||||
[0] => stdClass Object
|
||||
@ -59,10 +61,11 @@ namespace App\Helpers\Bank\Yodlee\Transformer;
|
||||
)
|
||||
*/
|
||||
|
||||
class AccountTransformer
|
||||
class AccountTransformer implements AccountTransformerInterface
|
||||
{
|
||||
|
||||
public function transform($yodlee_account){
|
||||
public function transform($yodlee_account)
|
||||
{
|
||||
|
||||
$data = [];
|
||||
|
||||
@ -76,7 +79,7 @@ class AccountTransformer
|
||||
|
||||
public function transformAccount($account)
|
||||
{
|
||||
nlog($account);
|
||||
|
||||
return [
|
||||
'id' => $account->id,
|
||||
'account_type' => $account->CONTAINER,
|
||||
|
@ -10,6 +10,8 @@
|
||||
*/
|
||||
|
||||
namespace App\Helpers\Bank\Yodlee\Transformer;
|
||||
|
||||
use App\Helpers\Bank\BankRevenueInterface;
|
||||
|
||||
/**
|
||||
"date": "string",
|
||||
@ -70,11 +72,77 @@ namespace App\Helpers\Bank\Yodlee\Transformer;
|
||||
"holdingDescription": "string",
|
||||
"isin": "string",
|
||||
"status": "POSTED"
|
||||
*/
|
||||
|
||||
(
|
||||
[CONTAINER] => bank
|
||||
[id] => 103953585
|
||||
[amount] => stdClass Object
|
||||
(
|
||||
[amount] => 480.66
|
||||
[currency] => USD
|
||||
)
|
||||
|
||||
class IncomeTransformer
|
||||
[categoryType] => UNCATEGORIZE
|
||||
[categoryId] => 1
|
||||
[category] => Uncategorized
|
||||
[categorySource] => SYSTEM
|
||||
[highLevelCategoryId] => 10000017
|
||||
[createdDate] => 2022-08-04T21:50:17Z
|
||||
[lastUpdated] => 2022-08-04T21:50:17Z
|
||||
[description] => stdClass Object
|
||||
(
|
||||
[original] => CHEROKEE NATION TAX TA TAHLEQUAH OK
|
||||
)
|
||||
|
||||
[isManual] =>
|
||||
[sourceType] => AGGREGATED
|
||||
[date] => 2022-08-03
|
||||
[transactionDate] => 2022-08-03
|
||||
[postDate] => 2022-08-03
|
||||
[status] => POSTED
|
||||
[accountId] => 12331794
|
||||
[runningBalance] => stdClass Object
|
||||
(
|
||||
[amount] => 480.66
|
||||
[currency] => USD
|
||||
)
|
||||
|
||||
[checkNumber] => 998
|
||||
)
|
||||
*/
|
||||
|
||||
class IncomeTransformer implements BankRevenueInterface
|
||||
{
|
||||
|
||||
public function transform($transaction)
|
||||
{
|
||||
|
||||
$data = [];
|
||||
|
||||
foreach($transaction->transaction as $transaction)
|
||||
{
|
||||
$data[] = $this->transformTransaction($transaction);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function transformTransaction($transaction)
|
||||
{
|
||||
|
||||
return [
|
||||
'id' => $transaction->id,
|
||||
'amount' => $transaction->amount->amount,
|
||||
'currency' => $transaction->amount->currency,
|
||||
'account_type' => $transaction->CONTAINER,
|
||||
'category_id' => $transaction->categoryId,
|
||||
'category_type' => $transaction->categoryType,
|
||||
'date' => $transaction->date,
|
||||
'account_id' => $transaction->accountId,
|
||||
'description' => $transaction->description->original,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace App\Helpers\Bank\Yodlee;
|
||||
|
||||
use App\Helpers\Bank\Yodlee\Transformer\AccountTransformer;
|
||||
use App\Helpers\Bank\Yodlee\Transformer\IncomeTransformer;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class Yodlee
|
||||
@ -73,7 +74,7 @@ class Yodlee
|
||||
|
||||
$response = $this->bankFormRequest('/auth/token', 'post', [], ['loginName' => $user]);
|
||||
//catch failures here
|
||||
nlog($response);
|
||||
// nlog($response);
|
||||
return $response->token->accessToken;
|
||||
}
|
||||
|
||||
@ -160,8 +161,12 @@ class Yodlee
|
||||
|
||||
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/transactions", $params);
|
||||
|
||||
if($response->successful())
|
||||
return $response->object();
|
||||
if($response->successful()){
|
||||
// return $response->object();
|
||||
$it = new IncomeTransformer();
|
||||
return $it->transform($response->object());
|
||||
|
||||
}
|
||||
|
||||
if($response->failed())
|
||||
return $response->body();
|
||||
|
@ -455,16 +455,44 @@ class BankIntegrationController extends BaseController
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function remote_accounts(AdminBankIntegrationRequest $request)
|
||||
public function remoteAccounts(AdminBankIntegrationRequest $request)
|
||||
{
|
||||
// As yodlee is the first integration we don't need to perform switches yet, however
|
||||
// if we add additional providers we can reuse this class
|
||||
|
||||
$bank_account_id = auth()->user()->account->bank_integration_account_id;
|
||||
|
||||
$yodlee = new Yodlee();
|
||||
$yodlee->setTestMode(true);
|
||||
if(!$bank_account_id)
|
||||
return response()->json(['message' => 'Not yet authenticated with Bank Integration service'], 400);
|
||||
|
||||
$yodlee = new Yodlee($bank_account_id);
|
||||
|
||||
$accounts = $yodlee->getAccounts();
|
||||
|
||||
return response()->json($accounts, 200);
|
||||
}
|
||||
|
||||
public function getTransactions(AdminBankIntegrationRequest $request)
|
||||
{
|
||||
//handle API failures we have only accounts for success
|
||||
|
||||
$bank_account_id = auth()->user()->account->bank_integration_account_id;
|
||||
|
||||
if(!$bank_account_id)
|
||||
return response()->json(['message' => 'Not yet authenticated with Bank Integration service'], 400);
|
||||
|
||||
$yodlee = new Yodlee($bank_account_id);
|
||||
|
||||
$data = [
|
||||
'CONTAINER' => 'bank',
|
||||
'categoryType' => 'INCOME, UNCATEGORIZE',
|
||||
'top' => 500,
|
||||
'fromDate' => '2000-10-10', /// YYYY-MM-DD
|
||||
];
|
||||
|
||||
$transactions = $yodlee->getTransactions($data);
|
||||
|
||||
return response()->json($transactions, 200)
|
||||
|
||||
$yodlee->getAccounts($bank_account_id);
|
||||
}
|
||||
}
|
@ -107,7 +107,9 @@ Route::group(['middleware' => ['throttle:10,1','api_secret_check','email_db']],
|
||||
Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale'], 'prefix' => 'api/v1', 'as' => 'api.'], function () {
|
||||
Route::put('accounts/{account}', [AccountController::class, 'update'])->name('account.update');
|
||||
Route::resource('bank_integrations', BankIntegrationController::class); // name = (clients. index / create / show / update / destroy / edit
|
||||
|
||||
Route::post('bank_integrations/remote_accounts', [BankIntegrationController::class, 'remoteAccounts'])->name('bank_integrations.remote_accounts');
|
||||
Route::post('bank_integrations/transactions', [BankIntegrationController::class, 'getTransactions'])->name('bank_integrations.transactions');
|
||||
|
||||
Route::post('check_subdomain', [SubdomainController::class, 'index'])->name('check_subdomain');
|
||||
Route::get('ping', [PingController::class, 'index'])->name('ping');
|
||||
Route::get('health_check', [PingController::class, 'health'])->name('health_check');
|
||||
|
@ -234,7 +234,7 @@ class YodleeApiTest extends TestCase
|
||||
$yodlee->setTestMode();
|
||||
|
||||
$transactions = $yodlee->getTransactionCategories();
|
||||
//
|
||||
|
||||
// nlog($transactions);
|
||||
|
||||
$this->assertIsArray($transactions->transactionCategory);
|
||||
@ -350,8 +350,6 @@ class YodleeApiTest extends TestCase
|
||||
|
||||
$accounts = $yodlee->getAccounts();
|
||||
|
||||
nlog($accounts);
|
||||
|
||||
$this->assertIsArray($accounts);
|
||||
}
|
||||
|
||||
@ -416,4 +414,25 @@ nlog($accounts);
|
||||
}
|
||||
|
||||
|
||||
public function testGetTransactionsWithParams()
|
||||
{
|
||||
|
||||
$yodlee = new Yodlee('sbMem62e1e69547bfb1');
|
||||
$yodlee->setTestMode();
|
||||
|
||||
$data = [
|
||||
'CONTAINER' => 'bank',
|
||||
'categoryType' => 'INCOME, UNCATEGORIZE',
|
||||
'top' => 500,
|
||||
'fromDate' => '2000-10-10', /// YYYY-MM-DD
|
||||
];
|
||||
|
||||
$accounts = $yodlee->getTransactions($data);
|
||||
|
||||
|
||||
nlog($accounts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user