fix(payment): project the latest invoice payment attempt

go
codex 2 hours ago
parent 88f69aa1d7
commit 717c08a8cf

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

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

Loading…
Cancel
Save