From 5732794681a6726617d32c40e128b83a88b28e31 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 26 Sep 2024 16:06:16 +1000 Subject: [PATCH] Fixes for inbound validation of payments --- .../Requests/Payment/StorePaymentRequest.php | 7 ++-- .../Import/Quickbooks/QuickbooksTest.php | 14 ++++---- tests/Feature/PaymentTest.php | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 0387396baab6..23eb235ffca2 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -107,7 +107,7 @@ class StorePaymentRequest extends Request $input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); } - if (array_key_exists('amount', $value)) { + if (array_key_exists('amount', $value) && is_numeric($value['amount'])) { $invoices_total += $value['amount']; } } @@ -121,7 +121,10 @@ class StorePaymentRequest extends Request foreach ($input['credits'] as $key => $value) { if (isset($value['credit_id']) && is_string($value['credit_id'])) { $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); - $credits_total += $value['amount']; + + if (array_key_exists('amount', $value) && is_numeric($value['amount'])) { + $credits_total += $value['amount']; + } } } } diff --git a/tests/Feature/Import/Quickbooks/QuickbooksTest.php b/tests/Feature/Import/Quickbooks/QuickbooksTest.php index 039aed0f401b..4846179ca46a 100644 --- a/tests/Feature/Import/Quickbooks/QuickbooksTest.php +++ b/tests/Feature/Import/Quickbooks/QuickbooksTest.php @@ -52,13 +52,15 @@ class QuickbooksTest extends TestCase $this->faker = \Faker\Factory::create(); } + + public function createQbProduct() { $service_product = Product::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->company->owner()->id, 'notes' => $this->faker->sentence(), - 'product_key' => $this->faker->word(63), + 'product_key' => \Illuminate\Support\Str::random(64), 'tax_id' => 2, ]); @@ -67,7 +69,7 @@ class QuickbooksTest extends TestCase 'company_id' => $this->company->id, 'user_id' => $this->company->owner()->id, 'notes' => $this->faker->sentence(), - 'product_key' => $this->faker->word(63), + 'product_key' => \Illuminate\Support\Str::random(64), 'tax_id' => 1, ]); @@ -347,14 +349,14 @@ class QuickbooksTest extends TestCase "DueDate" => $i->due_date, "TotalAmt" => $i->amount, "DocNumber" => $i->number, - "ApplyTaxAfterDiscount" => false, + // "ApplyTaxAfterDiscount" => false, "GlobalTaxCalculation" => "TaxExcluded", // This tells QuickBooks to calculate taxes "TxnTaxDetail" => [ "UseAutomatedSalesTax" => true, - // "TxnTaxCodeRef" => [ - // "value" => "TAX" // Use the appropriate tax code for your QuickBooks account + "TxnTaxCodeRef" => [ + "value" => "SALES_TAX_STUB" // Use the appropriate tax code for your QuickBooks account // "DefaultTaxRateRef" => [ - // ] + ], ] // "Note" => $this->invoice->public_notes, ]; diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index fa028184bd07..0b44556b6aee 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -62,6 +62,38 @@ class PaymentTest extends TestCase ); } + public function testNullPaymentAmounts() + { + + $data = [ + 'amount' => "null", + 'client_id' => "null", + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'amount' => "null", + ], + ], + 'credits' => [ + [ + 'credit_id' => $this->invoice->hashed_id, + 'amount' => "null", + ], + ], + 'date' => '2020/12/11', + 'idempotency_key' => 'xx', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments/', $data); + + $response->assertStatus(422); + + } + + public function testIdempotencyTrigger() {