Repository navigation
fix(core): serialise refunds and captures on an order - #2925
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2920.
Problem
RefundOrderchecked 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.CaptureOrderhas the same check-then-act shape, and itsexecute()didn't check for an existing capture at all — only the panel'scanRun()did.Reproduced on current
2.xagainst MySQL 8.4: 8 processes refunding the last unit of a single-line order (quantity 1, 180.00 captured) at the same instant.refunded_quantityup to 3 of 1, 1–41213 Deadlockerrors after the refund had committedRefundLine, 7 rejected with "exceeds the remaining refundable quantity"Change
RefundOrder::execute()runs in a transaction that locks the order row first, the wayRecordPaymentalready 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 therefund_lines/refunded_quantitylock-upgrade deadlock.OrderRefundedis dispatched after the commit.CaptureOrder::execute()does the same, and rejects the capture with anOrderActionExceptionwhen the order already has a successful capture. That matches whatCaptureOrder::canRun()already reported to the panel.Trade-offs
Tests
CaptureOrderTestcase for capturing an order that already has a successful capture. It fails without this change.core,panel,filament,stripeandpaypalsuites 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.