Skip to content

fix(core): serialise refunds and captures on an order - #2925

Merged
glennjacobs merged 1 commit into
2.xfrom
fix/serialise-order-refunds-and-captures
Nov 24, 2026
Merged

glennjacobs merged 1 commit into
2.xfrom
fix/serialise-order-refunds-and-captures

Conversation

@glennjacobs

Copy link
Copy Markdown
Contributor

Fixes #2920.

Problem

RefundOrder checked the remaining refundable quantity and the available-to-refund balance with no lock, then called the driver and recorded the line allocation in a separate transaction. Concurrent refunds on one order all passed the same checks. CaptureOrder has the same check-then-act shape, and its execute() didn't check for an existing capture at all — only the panel's canRun() did.

Reproduced on current 2.x against MySQL 8.4: 8 processes refunding the last unit of a single-line order (quantity 1, 180.00 captured) at the same instant.

Before After
Refunds (3–4 runs each) 2–7 refunds committed (up to 1,260.00 against 180.00), refunded_quantity up to 3 of 1, 1–4 1213 Deadlock errors after the refund had committed 1 refund, 1 RefundLine, 7 rejected with "exceeds the remaining refundable quantity"
Captures (8 parallel on an offline intent) 8 captures, 1,440.00 against a 180.00 intent 1 capture, 7 rejected with "Order has already been captured."

Change

  • RefundOrder::execute() runs in a transaction that locks the order row first, the way RecordPayment already does. The checks, the driver refund and the line allocation run under that lock and commit together, so a failed allocation can no longer leave a committed refund with no lines. Serialising the refunds also removes the refund_lines / refunded_quantity lock-upgrade deadlock. OrderRefunded is dispatched after the commit.
  • CaptureOrder::execute() does the same, and rejects the capture with an OrderActionException when the order already has a successful capture. That matches what CaptureOrder::canRun() already reported to the panel.

Trade-offs

  • The gateway call (Stripe, PayPal) now happens while the order row is locked. A second refund or capture on the same order waits for that call to finish. Other orders are not affected.
  • If the database commit fails after the gateway succeeded, the refund or capture record is lost. Before this change, the line allocation could already fail that way.

Tests

  • Added a CaptureOrderTest case for capturing an order that already has a successful capture. It fails without this change.
  • No test for the race itself: a single-process Pest test can't run two requests at once, and a test that only asserts the lock query would test implementation detail. The before/after numbers above come from parallel PHP processes against MySQL.
  • core, panel, filament, stripe and paypal suites pass; PHPStan and Pint are clean.

Not in this PR

PayPal's refund idempotency key is reference:refund:amount (PaypalPaymentType::refund()). Two separate partial refunds of the same amount on one capture may be treated by PayPal as the same request, while Lunar records two refund transactions. Not verified against PayPal.

RefundOrder and CaptureOrder checked the order's balance and line
quantities without a lock, so concurrent requests all passed the same
checks. Both now lock the order row in a transaction, the way
RecordPayment does, and check under that lock. A refund and its line
allocation commit together, OrderRefunded fires after commit, and a
capture is rejected once the order has a successful capture.

Fixes #2920

Amp-Thread-ID: https://ampcode.com/threads/T-bf44572d-9e22-4754-a0b7-22cb4af86b69
Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant