diff --git a/tests/Feature/GoCardlessInstantBankPaymentTest.php b/tests/Feature/GoCardlessInstantBankPaymentTest.php index 7a7148f357..04dfeab420 100644 --- a/tests/Feature/GoCardlessInstantBankPaymentTest.php +++ b/tests/Feature/GoCardlessInstantBankPaymentTest.php @@ -12,6 +12,11 @@ namespace Tests\Feature; use App\Models\CompanyGateway; +use App\Models\GatewayType; +use App\Models\Invoice; +use App\Models\PaymentHash; +use App\Models\PaymentType; +use App\Utils\Traits\MakesHash; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Routing\Middleware\ThrottleRequests; use Tests\MockAccountData; @@ -24,6 +29,7 @@ class GoCardlessInstantBankPaymentTest extends TestCase { use DatabaseTransactions; use MockAccountData; + use MakesHash; private array $mock = [ 'events' => @@ -121,6 +127,105 @@ class GoCardlessInstantBankPaymentTest extends TestCase // mock the invoice and the payment hash + } + + public function testInvoiceDelayedNotificationPayment() + { + + $gocardlesspayment = new \stdClass; + $links = new \stdClass; + $links->mandate = "my_mandate"; + $gocardlesspayment->links = $links; + $gocardlesspayment->id = "gocardless_payment_id"; + + + $invoice = Invoice::factory()->create( + [ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id + ]); + + $invoice->status_id = Invoice::STATUS_SENT; + $invoice->calc()->getInvoice()->save(); + + + $data_object = json_decode('{"invoices":[{"invoice_id":"xx","amount":0,"due_date":"","invoice_number":"0","additional_info":"2022-07-18"}],"credits":0,"amount_with_fee":15,"client_id":23,"billing_request":"BRQ005YJ7GHF","billing_request_flow":"xxdfdf"}'); + + $invoice_object = end($data_object->invoices); + $invoice_object->invoice_id = $invoice->hashed_id; + $invoice_object->invoice_number = $invoice->number; + $invoice_object->amount = $invoice->amount; + $data_object->invoices = [$invoice_object]; + $data_object->client = $this->client->id; + $data_object->amount_with_fee = $invoice->amount; + + $payment_hash = new PaymentHash(); + $payment_hash->hash = "1234567890abc"; + $payment_hash->fee_total = 0; + $payment_hash->fee_invoice_id = $invoice->hashed_id; + $payment_hash->data = $data_object; + $payment_hash->save(); + + + $this->assertIsArray($data_object->invoices); + $this->assertIsObject(end($data_object->invoices)); + $this->assertEquals(1, count($data_object->invoices)); + + $test_invoice_object = end($data_object->invoices); + + $this->assertEquals($invoice->hashed_id, $test_invoice_object->invoice_id); + $this->assertEquals($invoice->balance, $test_invoice_object->amount); + + + foreach($this->mock['events'] as $event) + { + + if($event['action'] == 'fulfilled' && array_key_exists('billing_request', $event['links'])) { + + $hash = PaymentHash::whereJsonContains('data->billing_request', $event['links']['billing_request'])->first(); + + $this->assertNotNull($hash); + $this->assertEquals('1234567890abc', $hash->hash); + + $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($hash->invoices(), 'invoice_id')))->withTrashed()->get(); + + $this->assertNotNull($invoices); + $this->assertEquals(1, $invoices->count()); + + // remove all paid invoices + $invoices->filter(function ($invoice){ + return $invoice->isPayable(); + }); + + $this->assertEquals(1, $invoices->count()); + + + $data = [ + 'payment_method' => $gocardlesspayment->links->mandate, + 'payment_type' => PaymentType::INSTANT_BANK_PAY, + 'amount' => $hash->data->amount_with_fee, + 'transaction_reference' => $gocardlesspayment->id, + 'gateway_type_id' => GatewayType::INSTANT_BANK_PAY, + ]; + + + $this->assertEquals('my_mandate', $data['payment_method']); + $this->assertEquals('gocardless_payment_id', $data['transaction_reference']); + $this->assertEquals($invoice->balance, $data['amount']); + } + + } + + + + + + + + + + }