From 717c08a8cf71cfa22d9fc2b415eeaeefb667cda9 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 22 Nov 2026 19:39:53 +0800 Subject: [PATCH] fix(payment): project the latest invoice payment attempt --- .../Cloud/CloudInvoiceDetailPageService.php | 39 +++++++++++++++---- web/tests/CloudInvoicePaymentResumeTest.php | 29 ++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/web/app/Services/Cloud/CloudInvoiceDetailPageService.php b/web/app/Services/Cloud/CloudInvoiceDetailPageService.php index 68a9ebe2..4f6ac4f1 100644 --- a/web/app/Services/Cloud/CloudInvoiceDetailPageService.php +++ b/web/app/Services/Cloud/CloudInvoiceDetailPageService.php @@ -83,15 +83,38 @@ final class CloudInvoiceDetailPageService ->orderByDesc('updated_at')->orderByDesc('id') ->sharedLock() ->first(); - $jobQuery = CloudJobService::blockingQueryForInvoice($invoiceId) - ->where('user_id', $userId); - if ($payment !== null) { - $jobQuery->where('request_idempotency_key', - (string) $payment->idempotency_key); + $jobFields = ['id', 'user_id', 'invoice_id', 'kind', + 'request_idempotency_key', 'phase', 'outcome', + 'effect_resolution', 'safe_to_resubmit', 'updated_at']; + $latestBlockingJob = CloudJobService::blockingQueryForInvoice($invoiceId) + ->where('user_id', $userId)->orderByDesc('id')->sharedLock() + ->first($jobFields); + $paymentJob = $payment === null ? null + : \App\Models\CloudJob::where('invoice_id', $invoiceId) + ->where('user_id', $userId)->where('kind', 'payment_create') + ->where('request_idempotency_key', + (string) $payment->idempotency_key) + ->orderByDesc('id')->sharedLock()->first($jobFields); + $paymentBlockingJob = $payment === null ? null + : CloudJobService::blockingQueryForInvoice($invoiceId) + ->where('user_id', $userId) + ->where('request_idempotency_key', + (string) $payment->idempotency_key) + ->orderByDesc('id')->sharedLock()->first($jobFields); + $jobIsNewer = $latestBlockingJob !== null && ( + $payment === null + || ($paymentJob !== null + && (int) $latestBlockingJob->id > (int) $paymentJob->id) + || ($paymentJob === null + && (int) $latestBlockingJob->updated_at + > (int) $payment->updated_at) + ); + if ($jobIsNewer) { + $payment = null; + $job = $latestBlockingJob; + } else { + $job = $paymentBlockingJob; } - $job = $jobQuery->orderByDesc('id')->sharedLock() - ->first(['id', 'user_id', 'invoice_id', 'kind', 'phase', - 'outcome', 'effect_resolution', 'safe_to_resubmit']); if ($job === null && $payment === null) { return null; } diff --git a/web/tests/CloudInvoicePaymentResumeTest.php b/web/tests/CloudInvoicePaymentResumeTest.php index b02f6de4..47bc5905 100644 --- a/web/tests/CloudInvoicePaymentResumeTest.php +++ b/web/tests/CloudInvoicePaymentResumeTest.php @@ -249,6 +249,35 @@ $assert(($pendingStatus['invoice']['id'] ?? null) === '21' === true, 'Lightweight invoice polling omitted the active provider payment.'); +Capsule::table('cloud_jobs')->insert([ + 'id' => 32, + 'order_id' => null, + 'invoice_id' => 21, + 'instance_id' => null, + 'user_id' => 1, + 'kind' => 'payment_create', + 'request_idempotency_key' => 'stripe-fresh-attempt-key-0002', + 'phase' => 'accepted', + 'outcome' => null, + 'attempts' => 0, + 'updated_at' => $now + 1, + 'error_code' => null, + 'effect_resolution' => 'not_applicable', + 'safe_to_resubmit' => null, + 'sealed_payload' => 'not-readable-by-page', +]); +$freshAttempt = CloudInvoiceDetailPageService::forUser(21, 1, $user); +$assert(($freshAttempt['active_payment']['status'] ?? null) === 'creating' + && ($freshAttempt['active_payment']['task_id'] ?? null) === '32' + && ($freshAttempt['active_payment']['payment'] ?? null) === null, + 'A newer payment attempt was hidden behind older provider instructions.'); +Capsule::table('cloud_jobs')->where('id', 32)->update([ + 'phase' => 'terminal', + 'outcome' => 'succeeded', + 'effect_resolution' => 'applied', + 'safe_to_resubmit' => 0, +]); + Capsule::table('cloud_invoice_payments')->where('id', 41)->update([ 'status' => 'expired', 'updated_at' => $now + 1,