From e5064183a00fa9fe1f639aa07984dccdf8a8bc7f Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 22 Nov 2026 19:36:39 +0800 Subject: [PATCH] fix(payment): allow fresh provider attempts for unpaid invoices --- .../Cloud/CloudInvoicePaymentService.php | 51 ------------------- web/tests/CloudInvoiceLifecycleTest.php | 13 +++-- 2 files changed, 9 insertions(+), 55 deletions(-) diff --git a/web/app/Services/Cloud/CloudInvoicePaymentService.php b/web/app/Services/Cloud/CloudInvoicePaymentService.php index aa9143ea..b5725053 100644 --- a/web/app/Services/Cloud/CloudInvoicePaymentService.php +++ b/web/app/Services/Cloud/CloudInvoicePaymentService.php @@ -95,44 +95,6 @@ final class CloudInvoicePaymentService 409 ); } - $sameMethodJob = CloudJobService::blockingQueryForInvoice( - (int) $locked->id - )->where('user_id', (int) $user->id) - ->where('request_fingerprint', $fingerprint) - ->orderByDesc('id')->lockForUpdate()->first(); - if ($sameMethodJob !== null) { - CloudInvoicePayment::where('invoice_id', (int) $locked->id) - ->where('user_id', (int) $user->id) - ->where('idempotency_key', - (string) $sameMethodJob->request_idempotency_key) - ->whereIn('status', [ - 'creating', 'pending', 'creation_unknown', - ])->update(['updated_at' => time()]); - return $sameMethodJob; - } - $sameMethodPayment = CloudInvoicePayment::where( - 'invoice_id', (int) $locked->id - )->where('user_id', (int) $user->id) - ->where('provider', $method) - ->whereIn('status', - ['creating', 'pending', 'creation_unknown']) - ->orderByDesc('updated_at')->orderByDesc('id') - ->lockForUpdate()->first(); - if ($sameMethodPayment !== null) { - $sameMethodPayment->updated_at = time(); - $sameMethodPayment->save(); - $sameMethodPaymentJob = \App\Models\CloudJob::where( - 'invoice_id', (int) $locked->id - )->where('user_id', (int) $user->id) - ->where('kind', 'payment_create') - ->where('request_idempotency_key', - (string) $sameMethodPayment->idempotency_key) - ->orderByDesc('id')->lockForUpdate()->first(); - if ($sameMethodPaymentJob !== null) { - return $sameMethodPaymentJob; - } - throw new CloudError('该支付方式已有未完成订单,请刷新后继续', 409); - } if (!PaymentCheckoutQuotes::canQuote($method)) { throw new PaymentRateUnavailable(); } @@ -478,19 +440,6 @@ final class CloudInvoicePaymentService string $orderNo, string $idempotency, string $amount, int $userId, int $now): void { - // The invoice row is already locked by recordAttempt(). Keep at most - // one unfinished order per provider, while allowing the customer to - // switch to another provider before either order is paid. - if (CloudInvoicePayment::where('invoice_id', $invoiceId) - ->where('idempotency_key', '<>', $idempotency) - ->where('provider', $provider) - ->whereIn('status', ['creating', 'pending', 'creation_unknown']) - ->lockForUpdate()->exists()) { - throw new CloudError( - '该支付方式已有未完成订单,请继续原订单或选择其他方式', - 409 - ); - } $payment = new CloudInvoicePayment(); $payment->invoice_id = $invoiceId; $payment->user_id = $userId; diff --git a/web/tests/CloudInvoiceLifecycleTest.php b/web/tests/CloudInvoiceLifecycleTest.php index f05078a2..a329901b 100644 --- a/web/tests/CloudInvoiceLifecycleTest.php +++ b/web/tests/CloudInvoiceLifecycleTest.php @@ -445,12 +445,17 @@ namespace { 'invoice-switch-test' ); $assert((int) $paytaroJob->id !== (int) $yipayJob->id - && (int) $sameProviderJob->id === (int) $paytaroJob->id + && (int) $sameProviderJob->id !== (int) $paytaroJob->id + && (int) $sameProviderJob->id !== (int) $yipayJob->id && (int) $sameRequestJob->id === (int) $paytaroJob->id && CloudJob::where('invoice_id', (int) $second->id) - ->where('kind', 'payment_create')->count() === 2, - 'An unfinished provider task blocked switching or duplicated one provider.'); - CloudJob::whereIn('id', [(int) $paytaroJob->id, (int) $yipayJob->id]) + ->where('kind', 'payment_create')->count() === 3, + 'A fresh provider attempt was reused or an exact retry was duplicated.'); + CloudJob::whereIn('id', [ + (int) $paytaroJob->id, + (int) $yipayJob->id, + (int) $sameProviderJob->id, + ]) ->update([ 'phase' => 'terminal', 'outcome' => 'succeeded',