diff --git a/web/public/cloud/invoice-detail-runtime.js b/web/public/cloud/invoice-detail-runtime.js index b467ba34..254f5b91 100644 --- a/web/public/cloud/invoice-detail-runtime.js +++ b/web/public/cloud/invoice-detail-runtime.js @@ -896,6 +896,13 @@ async function createInvoicePayment(invoice, method, container, paymentResult) { container.querySelectorAll('button').forEach(button => { button.disabled = true; }); const submit = byId('invoice-payment-submit'); if (submit) submit.disabled = true; + // A durable provider order is an earlier payment attempt. An explicit new + // click must not reuse that attempt's key, while an unresolved HTTP/task + // submission keeps its key so transport retries remain idempotent. + if (firstText(state.activePayment?.status).toLowerCase() === 'pending' + && state.activePayment?.payment) { + clearInvoicePaymentKey(invoice.id, method.id); + } const idempotencyKey = pendingInvoicePaymentKey(invoice.id, method.id); try { const result = await api(`/api/v1/invoices/${encodeURIComponent(invoice.id)}/payments`, { @@ -918,6 +925,7 @@ async function createInvoicePayment(invoice, method, container, paymentResult) { // A terminal task result is newer than every status GET already in // flight. Project it immediately so a late response cannot restore // an older payment attempt over the new instructions. + clearInvoicePaymentKey(invoice.id, method.id); state.appliedReadSequence = ++state.readSequence; state.activePayment = { status: 'pending', diff --git a/web/tests/CloudInvoiceDetailContractTest.php b/web/tests/CloudInvoiceDetailContractTest.php index 157c2b9f..5f06f3f6 100644 --- a/web/tests/CloudInvoiceDetailContractTest.php +++ b/web/tests/CloudInvoiceDetailContractTest.php @@ -401,6 +401,8 @@ $providerButtonsDisabled = strpos($providerPayment, "container.querySelectorAll('button').forEach(button => { button.disabled = true; });"); $providerIdempotency = strpos($providerPayment, 'pendingInvoicePaymentKey(invoice.id, method.id)'); +$providerFreshAttempt = strpos($providerPayment, + 'clearInvoicePaymentKey(invoice.id, method.id);'); $providerRequest = strpos($providerPayment, 'api(`/api/v1/invoices/${encodeURIComponent(invoice.id)}/payments`'); $assert(str_contains($providerPayment, @@ -413,16 +415,21 @@ $assert(!str_contains($providerPayment, 'confirm(') && $providerBusyGuard !== false && $providerBusySet !== false && $providerButtonsDisabled !== false + && $providerFreshAttempt !== false && $providerIdempotency !== false && $providerRequest !== false && $providerBusyGuard < $providerBusySet && $providerBusySet < $providerButtonsDisabled + && $providerButtonsDisabled < $providerFreshAttempt + && $providerFreshAttempt < $providerIdempotency && $providerButtonsDisabled < $providerIdempotency && $providerIdempotency < $providerRequest && str_contains($providerPayment, 'const observer = observeTask(result, {') && str_contains($providerPayment, 'await observer.done;') && str_contains($providerPayment, 'state.appliedReadSequence = ++state.readSequence;') + && substr_count($providerPayment, + 'clearInvoicePaymentKey(invoice.id, method.id);') >= 3 && str_contains($providerPayment, "status: 'pending'") && str_contains($providerPayment, 'state.mutationBusy = false;'), 'Provider payment must keep controls fenced until durable creation is terminal and reject stale reads.'); @@ -455,12 +462,12 @@ $savePaymentAttempt = $section($invoicePaymentService, 'private static function saveAttempt(', 'public static function markProviderOrdersTerminal('); $assert(str_contains($enqueuePayment, + "->where('request_idempotency_key', \$key)") + && !str_contains($enqueuePayment, "->where('request_fingerprint', \$fingerprint)") - && str_contains($enqueuePayment, "->where('provider', \$method)") - && str_contains($savePaymentAttempt, "->where('provider', \$provider)") - && !str_contains($savePaymentAttempt, - '该账单已有支付订单或结果不明的尝试'), - 'An unfinished order must not block switching to a different provider.'); + && !str_contains($enqueuePayment, "->where('provider', \$method)") + && !str_contains($savePaymentAttempt, "->where('provider', \$provider)"), + 'Only an exact idempotency retry may reuse a payment attempt.'); $assert(!str_contains($legacyRuntime, 'function loadInvoiceDetail(') && !str_contains($legacyRuntime, 'function renderInvoiceDetail(') && !str_contains($legacyRuntime, 'INVOICE_PAYMENT_STORAGE_PREFIX'),