1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Http/Controllers/Bank/YodleeController.php

118 lines
3.6 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
*/
namespace App\Http\Controllers\Bank;
2022-07-28 08:29:42 +02:00
use App\Helpers\Bank\Yodlee\Yodlee;
2022-07-28 06:09:13 +02:00
use App\Http\Controllers\BaseController;
2022-08-10 03:56:46 +02:00
use App\Http\Requests\Yodlee\YodleeAuthRequest;
use App\Jobs\Bank\ProcessBankTransactions;
2022-08-11 06:19:35 +02:00
use App\Models\BankIntegration;
2022-07-28 06:09:13 +02:00
use Illuminate\Http\Request;
class YodleeController extends BaseController
{
2022-08-10 03:56:46 +02:00
public function auth(YodleeAuthRequest $request)
2022-07-28 06:09:13 +02:00
{
2022-08-06 10:11:43 +02:00
// create a user at this point
// use the one time token here to pull in the actual user
2022-08-15 22:03:12 +02:00
// store the user_account_id on the accounts table
2022-08-06 10:11:43 +02:00
2022-08-10 03:56:46 +02:00
$yodlee = new Yodlee();
$company = $request->getCompany();
2022-08-10 03:56:46 +02:00
if($company->account->bank_integration_account_id){
2022-08-15 22:03:12 +02:00
2022-08-10 03:56:46 +02:00
$flow = 'edit';
2022-08-15 22:03:12 +02:00
2022-08-10 03:56:46 +02:00
$token = $company->account->bank_integration_account_id;
2022-08-15 22:03:12 +02:00
2022-08-10 03:56:46 +02:00
}
else{
2022-08-15 22:03:12 +02:00
2022-08-10 03:56:46 +02:00
$flow = 'add';
2022-08-15 22:03:12 +02:00
2022-08-10 03:56:46 +02:00
$response = $yodlee->createUser($company);
$token = $response->user->loginName;
$company->account->bank_integration_account_id = $token;
2022-08-15 22:03:12 +02:00
2022-08-10 03:56:46 +02:00
$company->push();
}
2022-08-10 11:49:27 +02:00
$yodlee = new Yodlee($token);
2022-08-10 03:56:46 +02:00
if($request->has('window_closed') && $request->input("window_closed") == "true")
$this->getAccounts($company, $token);
2022-07-28 06:09:13 +02:00
$data = [
2022-08-10 03:56:46 +02:00
'access_token' => $yodlee->getAccessToken(),
'fasttrack_url' => $yodlee->getFastTrackUrl(),
2022-08-15 22:03:12 +02:00
'config_name' => config('ninja.yodlee.config_name'),
2022-08-10 03:56:46 +02:00
'flow' => $flow,
2022-08-10 11:49:27 +02:00
'company' => $company,
'account' => $company->account,
'completed' => $request->has('window_closed') ? true : false,
2022-07-28 06:09:13 +02:00
];
return view('bank.yodlee.auth', $data);
}
private function getAccounts($company, $token)
{
$yodlee = new Yodlee($token);
$accounts = $yodlee->getAccounts();
foreach($accounts as $account)
{
if(!BankIntegration::where('bank_account_id', $account['id'])->where('company_id', $company->id)->exists())
{
$bank_integration = new BankIntegration();
$bank_integration->company_id = $company->id;
$bank_integration->account_id = $company->account_id;
$bank_integration->user_id = $company->owner()->id;
$bank_integration->bank_account_id = $account['id'];
$bank_integration->bank_account_type = $account['account_type'];
$bank_integration->bank_account_name = $account['account_name'];
$bank_integration->bank_account_status = $account['account_status'];
$bank_integration->bank_account_number = $account['account_number'];
$bank_integration->provider_id = $account['provider_id'];
$bank_integration->provider_name = $account['provider_name'];
$bank_integration->nickname = $account['nickname'];
$bank_integration->balance = $account['current_balance'];
$bank_integration->currency = $account['account_currency'];
$bank_integration->save();
}
}
$company->account->bank_integrations->each(function ($bank_integration) use ($company){
ProcessBankTransactions::dispatch($company->account->bank_integration_account_id, $bank_integration);
});
}
2022-07-28 06:09:13 +02:00
}