From ef388a0c7777c40d60f60f18ba7ec3e83fbcf89f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 14 Jun 2024 14:31:26 +1000 Subject: [PATCH] Filters for bank accounts --- app/Filters/BankTransactionFilters.php | 22 +++++ .../gateways/paypal/ppcp/card.blade.php | 98 ++++++++++++++++++- tests/Feature/Bank/BankTransactionTest.php | 92 +++++++++++++++++ 3 files changed, 211 insertions(+), 1 deletion(-) diff --git a/app/Filters/BankTransactionFilters.php b/app/Filters/BankTransactionFilters.php index 4a7db04e8b70..e3c62da1ef30 100644 --- a/app/Filters/BankTransactionFilters.php +++ b/app/Filters/BankTransactionFilters.php @@ -115,6 +115,28 @@ class BankTransactionFilters extends QueryFilters return $this->builder; } + + /** + * Filters the list based on Bank Accounts. + * + * @param string $ids Comma Separated List of bank account ids + * @return Builder + */ + public function bank_integration_ids(string $ids = ''): Builder + { + if(strlen($ids) == 0) + return $this->builder; + + $ids = $this->transformKeys(explode(",", $ids)); + + $this->builder->where(function ($query) use ($ids) { + $query->whereIn('bank_integration_id', $ids); + }); + + return $this->builder; + + } + /** * Sorts the list based on $sort. * diff --git a/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php b/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php index bae844c8b19f..86ffefbbb290 100644 --- a/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php +++ b/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php @@ -94,7 +94,101 @@ const clientId = "{{ $client_id }}"; const orderId = "{!! $order_id !!}"; - const buttons = paypal.Buttons(); + const buttons = paypal.Buttons({ + client: clientId, + createOrder: function(data, actions) { + return orderId; + }, + onApprove: function(data, actions) { + + const { liabilityShift, orderID } = data; + if(liabilityShift) { + + /* Handle liability shift. More information in 3D Secure response parameters */ + if(liabilityShift == 'NO') { + + document.getElementById('errors').textContent = `Sorry, your transaction could not be processed, Please try a different payment method.`; + document.getElementById('errors').hidden = false; + + return; + } + + } + + let storeCard = document.querySelector('input[name=token-billing-checkbox]:checked'); + + if (storeCard) { + document.getElementById("store_card").value = storeCard.value; + } + + document.getElementById("gateway_response").value =JSON.stringify( data ); + + formData = JSON.stringify(Object.fromEntries(new FormData(document.getElementById("server_response")))), + + fetch('{{ route('client.payments.response') }}', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + "X-Requested-With": "XMLHttpRequest", + "X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content + }, + body: formData, + }) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok ' + response.statusText); + } + return response.json(); + }) + .then(data => { + + var errorDetail = Array.isArray(data.details) && data.details[0]; + + if (errorDetail && ['INSTRUMENT_DECLINED', 'PAYER_ACTION_REQUIRED'].includes(errorDetail.issue)) { + return actions.restart(); + } + + if(data.redirect){ + window.location.href = data.redirect; + return; + } + + document.getElementById("gateway_response").value =JSON.stringify( data ); + document.getElementById("server_response").submit(); + }) + .catch(error => { + console.error('Error:', error); + + document.getElementById('errors').textContent = `Sorry, your transaction could not be processed...\n\n${error.message}`; + document.getElementById('errors').hidden = false; + + }); + + }, + onCancel: function() { + + window.location.href = "/client/invoices/"; + }, + // onError: function(error) { + + + // console.log("submit catch"); + // const errorM = parseError(error); + + // console.log(errorM); + + // const msg = handle422Error(errorM); + + // document.getElementById('errors').textContent = `Sorry, your transaction could not be processed...\n\n${msg.description}`; + // document.getElementById('errors').hidden = false; + + // }, + onClick: function (){ + + } + + + }); @if(strlen($pp_client_reference) > 1) buttons.render('#paypal-button-container'); @@ -243,6 +337,8 @@ }).catch((error) => { + console.log(error); + let msg; if(!['INVALID_NUMBER','INVALID_CVV','INVALID_EXPIRY'].includes(error.message)) diff --git a/tests/Feature/Bank/BankTransactionTest.php b/tests/Feature/Bank/BankTransactionTest.php index 60312d8c9f59..68d820fc9a62 100644 --- a/tests/Feature/Bank/BankTransactionTest.php +++ b/tests/Feature/Bank/BankTransactionTest.php @@ -40,6 +40,98 @@ class BankTransactionTest extends TestCase ); } + public function testBankIntegrationFilters() + { + + $bi = BankIntegrationFactory::create($this->company->id, $this->user->id, $this->account->id); + $bi->bank_account_name = "Bank1"; + $bi->save(); + + $bt = BankTransactionFactory::create($this->company->id, $this->user->id); + $bt->bank_integration_id = $bi->id; + $bt->status_id = BankTransaction::STATUS_UNMATCHED; + $bt->description = 'Fuel'; + $bt->amount = 10; + $bt->currency_code = $this->client->currency()->code; + $bt->date = now()->format('Y-m-d'); + $bt->transaction_id = 1234567890; + $bt->category_id = 10000003; + $bt->base_type = 'DEBIT'; + $bt->save(); + + + $bi2 = BankIntegrationFactory::create($this->company->id, $this->user->id, $this->account->id); + $bi2->bank_account_name = "Bank2"; + $bi2->save(); + + $bt = BankTransactionFactory::create($this->company->id, $this->user->id); + $bt->bank_integration_id = $bi2->id; + $bt->status_id = BankTransaction::STATUS_UNMATCHED; + $bt->description = 'Fuel'; + $bt->amount = 20; + $bt->currency_code = $this->client->currency()->code; + $bt->date = now()->format('Y-m-d'); + $bt->transaction_id = 1234567890; + $bt->category_id = 10000003; + $bt->base_type = 'DEBIT'; + $bt->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->getJson('/api/v1/bank_transactions'); + + $response->assertStatus(200); + + $arr = $response->json(); + + $transaction_count = count($arr['data']); + + $this->assertGreaterThan(1, $transaction_count); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->getJson('/api/v1/bank_transactions?bank_integration_ids='.$bi->hashed_id); + + $response->assertStatus(200); + + $arr = $response->json(); + + $transaction_count = count($arr['data']); + + $this->assertCount(1, $arr['data']); + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->getJson('/api/v1/bank_transactions?bank_integration_ids='.$bi2->hashed_id); + + $response->assertStatus(200); + + $arr = $response->json(); + + $transaction_count = count($arr['data']); + + $this->assertCount(1, $arr['data']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->getJson('/api/v1/bank_transactions?bank_integration_ids='.$bi2->hashed_id.",".$bi->hashed_id); + + $response->assertStatus(200); + + $arr = $response->json(); + + $transaction_count = count($arr['data']); + + $this->assertCount(2, $arr['data']); + + + } + public function testLinkMultipleExpensesWithDeleteToTransaction() { $data = [];