From c9282b1bd0ff1ea849f4dd3453fb160c19998797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ertu=C4=9Frul=20Keyvan?= Date: Mon, 30 Nov 2026 01:16:23 +0300 Subject: [PATCH] fix(bank-payments): match the payment reference the transfer instructions print The bank transfer gateway asks the customer to write payment_ref() (the prefix and the invoice ID, e.g. INV34) in the transfer title. The matcher only recognised the invoice number, so a customer who followed the instructions was never matched by number and could not reach auto_ready. Either one in the title now counts as the number match. --- .../BankPayments/Services/InvoiceMatcher.php | 7 +++- .../BankPayments/InvoiceMatcherTest.php | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/Addons/BankPayments/Services/InvoiceMatcher.php b/modules/Addons/BankPayments/Services/InvoiceMatcher.php index e2f11a00f..949717e87 100644 --- a/modules/Addons/BankPayments/Services/InvoiceMatcher.php +++ b/modules/Addons/BankPayments/Services/InvoiceMatcher.php @@ -154,8 +154,13 @@ public function scoreInvoice(BankTransaction $transaction, Invoice $invoice, ?Ba $referenceTokens = $this->referenceTokens($transaction->reference); $balance = round((float) $invoice->amountDue(), 2); + // The bank transfer instructions ask the customer to write the payment + // reference (payment_ref(): prefix and invoice ID, e.g. INV34), not + // the invoice number, so either one in the title identifies the invoice. $invoiceNumber = $this->normalizeNumber((string) $invoice->invoice_num); - $numberMatch = $invoiceNumber !== '' && in_array($invoiceNumber, $referenceTokens, true); + $paymentReference = $this->normalizeNumber(payment_ref($invoice)); + $numberMatch = ($invoiceNumber !== '' && in_array($invoiceNumber, $referenceTokens, true)) + || ($paymentReference !== '' && in_array($paymentReference, $referenceTokens, true)); $amountMatch = $balance > 0.009 && abs($balance - $amount) <= 0.01; $learnedMatch = $profile !== null && (int) $profile->client_id === (int) $invoice->client_id; $clientMatch = $learnedMatch || $this->clientMatches($sender, $invoice->client); diff --git a/tests/Feature/BankPayments/InvoiceMatcherTest.php b/tests/Feature/BankPayments/InvoiceMatcherTest.php index da850ea9e..3c61cd949 100644 --- a/tests/Feature/BankPayments/InvoiceMatcherTest.php +++ b/tests/Feature/BankPayments/InvoiceMatcherTest.php @@ -83,6 +83,42 @@ function matcherInvoice(Client $client, array $overrides = []): Invoice ->and(Transaction::count())->toBe(0); }); +it('recognises the payment reference the bank transfer instructions print', function () { + $client = Client::factory()->create(['company_name' => 'Acme Sp. z o.o.']); + $invoice = matcherInvoice($client, ['invoice_num' => 'FV/2026/11/0042']); + $transaction = matcherTransaction(['reference' => 'Payment '.payment_ref($invoice)]); + + $result = app(InvoiceMatcher::class)->match($transaction); + + expect(payment_ref($invoice))->toBe('INV'.$invoice->id) + ->and($result['status'])->toBe('auto_ready') + ->and($result['score'])->toBe(90) + ->and(BankMatch::first()->invoice_id)->toBe($invoice->id) + ->and(BankMatch::first()->criteria['number_match'])->toBeTrue(); +}); + +it('recognises a payment reference with a custom prefix', function () { + Setting::set('PaymentReferencePrefix', 'PAY'); + $client = Client::factory()->create(['company_name' => 'Acme Sp. z o.o.']); + $invoice = matcherInvoice($client, ['invoice_num' => 'FV/2026/11/0043']); + $transaction = matcherTransaction(['reference' => 'pay-'.$invoice->id]); + + $result = app(InvoiceMatcher::class)->match($transaction); + + expect($result['status'])->toBe('auto_ready') + ->and(BankMatch::first()->criteria['number_match'])->toBeTrue(); +}); + +it('does not take a longer payment reference for a shorter one', function () { + $client = Client::factory()->create(['company_name' => 'Acme Sp. z o.o.']); + $invoice = matcherInvoice($client, ['invoice_num' => 'FV/2026/11/0044']); + $transaction = matcherTransaction(['reference' => 'INV'.$invoice->id.'9']); + + app(InvoiceMatcher::class)->match($transaction); + + expect(BankMatch::where('invoice_id', $invoice->id)->first()?->criteria['number_match'])->toBeFalse(); +}); + it('shows an amount and payer match as a 56 percent review candidate', function () { $client = Client::factory()->create(['company_name' => 'MOONSTONE MAGDALENA NOWARA']); $invoice = matcherInvoice($client, [