fix(payment): allow fresh provider attempts for unpaid invoices

go
codex 2 hours ago
parent aef0bd7152
commit e5064183a0

@ -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;

@ -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',

Loading…
Cancel
Save