From 73c73d1f5c100019b73dc29840d953655029533d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 20 Mar 2024 16:52:14 +1100 Subject: [PATCH] Fixes for applied and unapplied amounts after a refund --- .../Payment/ValidRefundableRequest.php | 3 - .../PaymentAppliedValidAmount.php | 5 +- tests/Feature/RefundTest.php | 128 ++++++++++++++++++ 3 files changed, 132 insertions(+), 4 deletions(-) diff --git a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php index 49530bca72a1..093aba14c839 100644 --- a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php +++ b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php @@ -57,9 +57,6 @@ class ValidRefundableRequest implements Rule if ($payment->invoices()->exists()) { $this->checkInvoice($payment->invoices, $request_invoices); - // foreach ($payment->invoices as $paymentable_invoice) { - // $this->checkInvoice($paymentable_invoice, $request_invoices); - // } } foreach ($request_invoices as $request_invoice) { diff --git a/app/Http/ValidationRules/PaymentAppliedValidAmount.php b/app/Http/ValidationRules/PaymentAppliedValidAmount.php index c6257fde8d1f..06f83315b288 100644 --- a/app/Http/ValidationRules/PaymentAppliedValidAmount.php +++ b/app/Http/ValidationRules/PaymentAppliedValidAmount.php @@ -61,7 +61,10 @@ class PaymentAppliedValidAmount implements Rule $payment_amounts = 0; $invoice_amounts = 0; - $payment_amounts = $payment->amount - $payment->refunded - $payment->applied; + // $payment_amounts = $payment->amount - $payment->refunded - $payment->applied; + + //20-03-2024 - applied amounts are never tainted by refunded amount. + $payment_amounts = $payment->amount - $payment->applied; if (request()->has('credits') && is_array(request()->input('credits')) diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index 3b5b246da49c..002a3eb43256 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -11,6 +11,7 @@ namespace Tests\Feature; +use App\DataMapper\InvoiceItem; use App\Factory\ClientFactory; use App\Factory\CreditFactory; use App\Factory\InvoiceFactory; @@ -59,6 +60,133 @@ class RefundTest extends TestCase // $this->withoutExceptionHandling(); } + public function testRefundAndAppliedAmounts() + { + + +$data = [ + 'amount' => 500, + 'client_id' => $this->client->hashed_id, + 'date' => '2020/12/12', + +]; + +$response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, +])->postJson('/api/v1/payments', $data); + +$response->assertStatus(200); + +$arr = $response->json(); + +$payment_id = $arr['data']['id']; + +$item = new InvoiceItem; +$item->cost = 300; +$item->quantity = 1; + +$i = Invoice::factory() +->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id, + 'line_items' => [$item], + 'discount' => 0, + 'tax_name1' => '', + 'tax_name2' => '', + 'tax_name3' => '', + 'tax_rate1' => 0, + 'tax_rate2' => 0, + 'tax_rate3' => 0, +]); + +$i->calc()->getInvoice(); +$i->service()->markSent()->save(); + +$this->assertEquals(300, $i->balance); + +$data = [ + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $i->hashed_id, + 'amount' => 300 + ], + ] +]; + +$response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, +])->putJson('/api/v1/payments/'.$payment_id, $data); + +$response->assertStatus(200); + +$i = $i->fresh(); + +$this->assertEquals(0, $i->balance); + +$payment = Payment::find($this->decodePrimaryKey($payment_id)); + +$this->assertNotNull($payment); +$this->assertEquals(500, $payment->amount); +$this->assertEquals(300, $payment->applied); +$this->assertEquals(0, $payment->refunded); + +$data = [ + 'id' => $this->encodePrimaryKey($payment->id), + 'invoices' => [ + [ + 'invoice_id' => $i->hashed_id, + 'amount' => $i->amount, + ], + ], + 'date' => '2020/12/12', +]; + +$response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, +])->postJson('/api/v1/payments/refund', $data); + +$response->assertStatus(200); + +$payment = $payment->fresh(); +$i = $i->fresh(); + +$this->assertEquals(300, $payment->refunded); +$this->assertEquals(300, $i->balance); +$this->assertEquals(2, $i->status_id); + + +$data = [ + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $i->hashed_id, + 'amount' => 200 + ], + ] +]; + +$response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, +])->putJson('/api/v1/payments/'.$payment_id, $data); + +$response->assertStatus(200); + +$payment = $payment->fresh(); +$i = $i->fresh(); + +$this->assertEquals(300, $payment->refunded); +$this->assertEquals(100, $i->balance); +$this->assertEquals(3, $i->status_id); +$this->assertEquals(500, $payment->applied); + + } + /** * Test that a simple payment of $50 * is able to be refunded.