fix(payment): rotate invoice payment keys after accepted attempts

go
codex 2 hours ago
parent e5064183a0
commit 88f69aa1d7

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

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

Loading…
Cancel
Save