Repository navigation
Prevent refunds from exceeding a payment's amount - #6737
Merged
AlistairNorman merged 1 commit intoNov 19, 2026
Merged
AlistairNorman merged 1 commit into
AlistairNorman merged 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6737 +/- ##
=======================================
Coverage 92.68% 92.68%
=======================================
Files 1082 1082
Lines 22007 22018 +11
=======================================
+ Hits 20397 20408 +11
Misses 1610 1610 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jarednorman
requested changes
Nov 17, 2026
| validates :amount, numericality: true | ||
| validates :source, presence: true, if: :source_required? | ||
| validates :payment_method, presence: true | ||
| validate :amount_not_less_than_refunds, if: :persisted? |
Member
There was a problem hiding this comment.
This will break existing invalid payments, making it impossible to update them. What about :will_save_change_to_amount? as the condition?
Contributor
Author
There was a problem hiding this comment.
I switched the condition to will_save_change_to_amount?. Thanks for catching that bug
A pending payment can be refunded, and a later partial `capture!` (or an amount update) would then lower its amount below what was refunded, leaving `credit_allowed` and the order's `payment_total` negative. - Validate that a payment's amount is not less than the sum of its refunds. The check only runs when the amount is changing, so payments that already have more refunded than their amount (created before this fix) can still be updated. - Refuse a `capture!` for less than the refunded amount before calling the gateway, so no money is captured for a payment we would reject. Fixes solidusio#6708 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VickTor61
force-pushed
the
VickTor61/refunds-exceed-payment-amount
branch
from
November 18, 2026 06:25
1876165 to
d9c756d
Compare
jarednorman
approved these changes
Nov 18, 2026
AlistairNorman
approved these changes
Nov 19, 2026
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.
Summary
Fixes #6708
A payment could end up with more refunded than its amount. Solidus allows refunds on
pending(authorized but not yet captured) payments, and the refund is only checked againstcredit_allowedwhen it is created. A later partialcapture!, or an update to the payment'samount, could then lower the amount below what was already refunded. For example:pending).credit_allowedis 6.00.capture!(200)captures $2.00 and setsamountto 2.00.The result is
credit_allowed == -4.00and an orderpayment_totalof -4.00 (outstanding balance 10.00 on a 6.00 order).This PR makes "refunds never exceed the payment's amount" hold:
Spree::Paymentvalidation: a persisted payment'samountcan't be less than the sum of its refunds. This covers amount updates from the API, the admin, or custom code.capture!: a capture for less than the refunded amount is refused with aSpree::Core::GatewayError, before the gateway is called. A validation alone wouldn't be enough here.capture!runsupdate!(amount: captured_amount)afterpayment_method.capture, so it would fail only after the money had already been captured.Both error messages are localized in
core/config/locales/en.yml.Notes for reviewers
capture!rather thanreturn false, so the reason is visible to whoever triggered the capture. Happy to change it if returningfalseis preferred.capture!#6708 is intentionally left out of this PR to keep it focused.capture!records the capture event and updatesamountbeforehandle_response, so a capture the gateway rejects still records a captured amount. I can follow up separately.Tests
New specs in
core/spec/models/spree/payment_spec.rb:The "refused" and "invalid" specs fail without this change and pass with it. The core payment specs, the API payment request specs, and the backend admin payments and refunds controller specs pass locally.
Checklist
Check out our PR guidelines for more details.
The following are mandatory for all PRs:
The following are not always needed:
🤖 Generated with Claude Code