mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-04 02:54:35 -04:00
fixes for sync transactions
This commit is contained in:
parent
1d0506a832
commit
e12fb1f765
@ -21,6 +21,7 @@ namespace App\Helpers\Bank\Nordigen;
|
|||||||
|
|
||||||
use App\Helpers\Bank\Nordigen\Transformer\AccountTransformer;
|
use App\Helpers\Bank\Nordigen\Transformer\AccountTransformer;
|
||||||
use App\Helpers\Bank\Nordigen\Transformer\TransactionTransformer;
|
use App\Helpers\Bank\Nordigen\Transformer\TransactionTransformer;
|
||||||
|
use Log;
|
||||||
|
|
||||||
class Nordigen
|
class Nordigen
|
||||||
{
|
{
|
||||||
@ -111,6 +112,8 @@ class Nordigen
|
|||||||
{
|
{
|
||||||
$transactionResponse = $this->client->account($accountId)->getAccountTransactions($dateFrom);
|
$transactionResponse = $this->client->account($accountId)->getAccountTransactions($dateFrom);
|
||||||
|
|
||||||
|
Log::info($transactionResponse);
|
||||||
|
|
||||||
$it = new TransactionTransformer();
|
$it = new TransactionTransformer();
|
||||||
return $it->transform($transactionResponse);
|
return $it->transform($transactionResponse);
|
||||||
}
|
}
|
||||||
|
@ -86,17 +86,28 @@ class TransactionTransformer implements BankRevenueInterface
|
|||||||
if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction))
|
if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction))
|
||||||
throw new \Exception('invalid dataset');
|
throw new \Exception('invalid dataset');
|
||||||
|
|
||||||
|
// description could be in varios places
|
||||||
|
$description = '';
|
||||||
|
if (array_key_exists('bank_remittanceInformationStructured', $transaction))
|
||||||
|
$description = $transaction["bank_remittanceInformationStructured"];
|
||||||
|
else if (array_key_exists('bank_remittanceInformationStructuredArray', $transaction))
|
||||||
|
$description = implode($transaction["bank_remittanceInformationStructured"], '\r\n');
|
||||||
|
else if (array_key_exists('remittanceInformationUnstructured', $transaction))
|
||||||
|
$description = $transaction["remittanceInformationUnstructured"];
|
||||||
|
else
|
||||||
|
Log::warning("Missing description for the following transaction: " . json_encode($transaction));
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'transaction_id' => 'nordigen:' . $transaction["transactionId"],
|
'transaction_id' => $transaction["transactionId"],
|
||||||
'amount' => abs((int) $transaction["transactionAmount"]["amount"]),
|
'amount' => abs((int) $transaction["transactionAmount"]["amount"]),
|
||||||
'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]),
|
'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]),
|
||||||
'category_id' => 0, // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
|
'category_id' => 0, // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
|
||||||
'category_type' => $transaction["additionalInformation"], // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
|
'category_type' => array_key_exists('additionalInformation', $transaction) ? $transaction["additionalInformation"] : '', // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
|
||||||
'date' => $transaction["bookingDate"],
|
'date' => $transaction["bookingDate"],
|
||||||
'description' => array_key_exists('bank_remittanceInformationStructured', $transaction) ? $transaction["bank_remittanceInformationStructured"] : (array_key_exists('bank_remittanceInformationStructuredArray', $transaction) ? implode($transaction["bank_remittanceInformationStructured"], '\r\n') : ''),
|
'description' => $description,
|
||||||
// 'description' => `IBAN: ${elem . json["bank_debtorAccount"] && elem . json["bank_debtorAccount"]["iban"] ? elem . json["bank_debtorAccount"]["iban"] : ' -'}\nVerwendungszweck: ${elem . json["bank_remittanceInformationStructured"] || ' -'}\nName: ${elem . json["bank_debtorName"] || ' -'}`, // 2 fields to get data from (structured and structuredArray (have to be joined))
|
// 'description' => `IBAN: ${elem . json["bank_debtorAccount"] && elem . json["bank_debtorAccount"]["iban"] ? elem . json["bank_debtorAccount"]["iban"] : ' -'}\nVerwendungszweck: ${elem . json["bank_remittanceInformationStructured"] || ' -'}\nName: ${elem . json["bank_debtorName"] || ' -'}`, // 2 fields to get data from (structured and structuredArray (have to be joined))
|
||||||
// TODO: debitor name & iban & bic
|
// TODO: debitor name & iban & bic
|
||||||
'base_type' => (int) $transaction["transactionAmount"]["amount"] > 0 ? 'DEBIT' : 'CREDIT',
|
'base_type' => (int) $transaction["transactionAmount"]["amount"] <= 0 ? 'DEBIT' : 'CREDIT',
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ use App\Utils\Traits\MakesHash;
|
|||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Log;
|
||||||
|
|
||||||
class BankIntegrationController extends BaseController
|
class BankIntegrationController extends BaseController
|
||||||
{
|
{
|
||||||
@ -199,13 +200,13 @@ class BankIntegrationController extends BaseController
|
|||||||
|
|
||||||
$user_account = $user->account;
|
$user_account = $user->account;
|
||||||
|
|
||||||
|
// if (Cache::get("throttle_polling:{$user_account->key}")) // @todo uncomment for PR
|
||||||
|
// return response()->json(BankIntegration::query()->company(), 200);
|
||||||
|
|
||||||
$this->refreshAccountsYodlee($user);
|
$this->refreshAccountsYodlee($user);
|
||||||
|
|
||||||
$this->refreshAccountsNordigen($user);
|
$this->refreshAccountsNordigen($user);
|
||||||
|
|
||||||
if (Cache::get("throttle_polling:{$user_account->key}"))
|
|
||||||
return response()->json(BankIntegration::query()->company(), 200);
|
|
||||||
|
|
||||||
// Processing transactions for each bank account
|
// Processing transactions for each bank account
|
||||||
if (Ninja::isHosted() && $user->account->bank_integration_yodlee_account_id)
|
if (Ninja::isHosted() && $user->account->bank_integration_yodlee_account_id)
|
||||||
$user_account->bank_integrations->where("integration_type", BankIntegration::INTEGRATION_TYPE_YODLEE)->where('auto_sync', true)->each(function ($bank_integration) use ($user_account) {
|
$user_account->bank_integrations->where("integration_type", BankIntegration::INTEGRATION_TYPE_YODLEE)->where('auto_sync', true)->each(function ($bank_integration) use ($user_account) {
|
||||||
@ -346,6 +347,8 @@ class BankIntegrationController extends BaseController
|
|||||||
|
|
||||||
if (config("ninja.nordigen.secret_id") && config("ninja.nordigen.secret_key") && (Ninja::isSelfHost() || (Ninja::isHosted() && $account->isPaid() && $account->plan == 'enterprise'))) {
|
if (config("ninja.nordigen.secret_id") && config("ninja.nordigen.secret_key") && (Ninja::isSelfHost() || (Ninja::isHosted() && $account->isPaid() && $account->plan == 'enterprise'))) {
|
||||||
$account->bank_integrations()->where('integration_type', BankIntegration::INTEGRATION_TYPE_NORDIGEN)->where('auto_sync', true)->cursor()->each(function ($bank_integration) {
|
$account->bank_integrations()->where('integration_type', BankIntegration::INTEGRATION_TYPE_NORDIGEN)->where('auto_sync', true)->cursor()->each(function ($bank_integration) {
|
||||||
|
Log::info($bank_integration);
|
||||||
|
|
||||||
(new ProcessBankTransactionsNordigen($bank_integration))->handle();
|
(new ProcessBankTransactionsNordigen($bank_integration))->handle();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,6 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
private BankIntegration $bank_integration;
|
private BankIntegration $bank_integration;
|
||||||
private string $secret_id;
|
|
||||||
private string $secret_key;
|
|
||||||
|
|
||||||
private ?string $from_date;
|
private ?string $from_date;
|
||||||
|
|
||||||
@ -80,7 +78,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
try {
|
try {
|
||||||
$this->updateAccount();
|
$this->updateAccount();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
nlog("{$this->secret_id} - exited abnormally => " . $e->getMessage());
|
nlog("{$this->bank_integration->account->key} - exited abnormally => " . $e->getMessage());
|
||||||
|
|
||||||
$content = [
|
$content = [
|
||||||
"Processing transactions for account: {$this->bank_integration->account->key} failed",
|
"Processing transactions for account: {$this->bank_integration->account->key} failed",
|
||||||
@ -89,7 +87,8 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
];
|
];
|
||||||
|
|
||||||
$this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja();
|
$this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja();
|
||||||
return;
|
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
if (!$this->nordigen_account)
|
if (!$this->nordigen_account)
|
||||||
return;
|
return;
|
||||||
@ -100,7 +99,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
try {
|
try {
|
||||||
$this->processTransactions();
|
$this->processTransactions();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
nlog("{$this->secret_id} - exited abnormally => " . $e->getMessage());
|
nlog("{$this->bank_integration->account->key} - exited abnormally => " . $e->getMessage());
|
||||||
|
|
||||||
$content = [
|
$content = [
|
||||||
"Processing transactions for account: {$this->bank_integration->account->key} failed",
|
"Processing transactions for account: {$this->bank_integration->account->key} failed",
|
||||||
@ -109,7 +108,8 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
];
|
];
|
||||||
|
|
||||||
$this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja();
|
$this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja();
|
||||||
return;
|
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -122,10 +122,12 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
private function updateAccount()
|
private function updateAccount()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Log::info("try to execute updateAccount");
|
||||||
if (!$this->nordigen->isAccountActive($this->bank_integration->nordigen_account_id)) {
|
if (!$this->nordigen->isAccountActive($this->bank_integration->nordigen_account_id)) {
|
||||||
$this->bank_integration->disabled_upstream = true;
|
$this->bank_integration->disabled_upstream = true;
|
||||||
$this->bank_integration->save();
|
$this->bank_integration->save();
|
||||||
$this->stop_loop = false;
|
$this->stop_loop = false;
|
||||||
|
Log::info("account inactive");
|
||||||
// @turbo124 @todo send email for expired account
|
// @turbo124 @todo send email for expired account
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -176,7 +178,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
|
|||||||
|
|
||||||
foreach ($transactions as $transaction) {
|
foreach ($transactions as $transaction) {
|
||||||
|
|
||||||
if (BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $this->company->id)->withTrashed()->exists())
|
if (BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $this->company->id)->where('bank_integration_id', $this->bank_integration->id)->withTrashed()->exists())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
//this should be much faster to insert than using ::create()
|
//this should be much faster to insert than using ::create()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user