diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 3a514b79d4..061490542a 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -124,6 +124,10 @@ class RefundPayment } } else { $this->payment->refunded += $net_refund; + + if($this->refund_reversed_invoice){ + $this->payment->refunded += $this->credits_used; + } } $this->payment->setRefundMeta($this->refund_data); diff --git a/tests/Feature/CreditTest.php b/tests/Feature/CreditTest.php index b317c86365..608c38d560 100644 --- a/tests/Feature/CreditTest.php +++ b/tests/Feature/CreditTest.php @@ -45,6 +45,116 @@ class CreditTest extends TestCase $this->makeTestData(); } + public function testNewCreditDeletionAfterInvoiceReversalAndPaymentRefund() + { + $c = Client::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'balance' => 0, + 'paid_to_date' => 0, + ]); + + $ii = new InvoiceItem(); + $ii->cost = 100; + $ii->quantity = 1; + $ii->product_key = 'xx'; + $ii->notes = 'yy'; + + $i = \App\Models\Invoice::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'tax_name1' => '', + 'tax_name2' => '', + 'tax_name3' => '', + 'tax_rate1' => 0, + 'tax_rate2' => 0, + 'tax_rate3' => 0, + 'discount' => 0, + 'line_items' => [ + $ii + ], + 'status_id' => 1, + ]); + + $repo = new InvoiceRepository(); + $repo->save([], $i); + + $i = $i->calc()->getInvoice(); + $i = $i->service()->markPaid()->save(); //paid + + $payment = $i->payments()->first(); + + $this->assertNotNull($payment); + + $this->assertEquals(0, $i->balance); + $this->assertEquals(100, $i->amount); + + + $credit_array = $i->withoutRelations()->toArray(); + $credit_array['invoice_id'] = $i->hashed_id; + $credit_array['client_id'] = $c->hashed_id; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/credits', $credit_array); + + $response->assertStatus(200); //reversal - credit created. + + $arr = $response->json(); + $credit = \App\Models\Credit::find($this->decodePrimaryKey($arr['data']['id'])); + + $this->assertNotNull($credit); + $payment = $payment->fresh(); + + $i = $i->fresh(); + + $this->assertEquals(\App\Models\Invoice::STATUS_REVERSED, $i->status_id); + $this->assertTrue($payment->credits()->exists()); + + $client = $i->client; + + $this->assertEquals(100, $client->credit_balance); + + + $refund_payload = [ + 'id' => $payment->hashed_id, + 'amount' => 100, + 'date' => '2020/12/12', + 'invoices' => [ + + ], + 'credits' => [ + [ + 'invoice_id' => $credit->hashed_id, + 'amount' => 100, + ], + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments/refund', $refund_payload); + + $response->assertStatus(200); + + $credit = $credit->fresh(); + $payment = $payment->fresh(); + $client = $client->fresh(); + + $this->assertEquals(0, $client->credit_balance); + $this->assertEquals(0, $client->paid_to_date); + $this->assertEquals(0, $client->balance); + + $this->assertEquals(100, $payment->refunded); + $this->assertEquals(\App\Models\Payment::STATUS_REFUNDED, $payment->status_id); + $this->assertEquals(0, $credit->balance); + $this->assertEquals(Credit::STATUS_APPLIED, $credit->status_id); + + } + public function testNewCreditDeletionAfterInvoiceReversal() { $c = Client::factory()->create([