Skip to content

fix(refund): prevent over-refund via late provider success - #1

Closed
mooder1 wants to merge 2 commits into
crossben:mainfrom
mooder1:fix/over-refund-settlement-guards
Closed

mooder1 wants to merge 2 commits into
crossben:mainfrom
mooder1:fix/over-refund-settlement-guards

Conversation

@mooder1

@mooder1 mooder1 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • CRITICAL: A refund transitioning FAILED → REFUNDED (late provider confirmation) was excluded from committedAmount(), allowing a second refund to consume the freed space. The cumulative refunded total could exceed the payment amount, causing direct financial loss.
  • committedAmount() now includes FAILED refunds that have a provider_reference (the provider saw the request and may still confirm it)
  • RefundTransitions raises late_refund and refund_exceeds_payment alerts when a late success is detected and when the total exceeds the payment
  • Settlement.settleRefund() and Settlement.settlePayout() now verify confirmedAmount against expected, matching the existing guard in settlePayment()

Reproduction scenario (before fix)

  1. Payment of 10 000 XOF succeeds
  2. Refund A of 10 000 created, provider returns Unknown (timeout after accept), provider_reference set
  3. Reconciler settles A as FAILED via provider status API
  4. committedAmount() returns 0 (FAILED excluded)
  5. Refund B of 10 000 created (validation passes: remaining = 10 000)
  6. Provider later confirms A as REFUNDED (late success), FAILED → REFUNDED = APPLY
  7. Ledger posting created without cumulative check
  8. Result: 20 000 XOF refunded on a 10 000 XOF payment

Changes

File What
Alerts.java Add late_refund and refund_exceeds_payment alert types
RefundRepository.java committedAmount includes FAILED refunds with provider_reference
RefundTransitions.java Alert on late success, alert if cumulative exceeds payment
Settlement.java Amount mismatch verification for refunds and payouts

Design notes

  • The state machine (FAILED → REFUNDED = APPLY) is intentionally permissive per ADR: losing real money is worse than a surprised app. The fix prevents creating a second refund during the vulnerability window, and raises alerts for operator visibility if the race was already lost.
  • payments.find() is used instead of payments.lock() in RefundTransitions to avoid reversing the documented lock order (payment → refund). The payment amount is immutable, so a snapshot read is sufficient.
  • FAILED refunds without provider_reference (definitively rejected before the provider processed them) remain excluded from committedAmount, preserving the existing retry behavior tested in RefundApiTest.a_rejected_refund_is_failed_and_frees_the_amount_again.

Test plan

  • Existing RefundApiTest.partial_refunds_are_allowed_up_to_the_payment_amount passes
  • Existing RefundApiTest.concurrent_refunds_can_never_exceed_what_was_paid passes
  • Existing RefundApiTest.a_rejected_refund_is_failed_and_frees_the_amount_again passes (rejected refunds still free the amount)
  • New test: a FAILED refund with provider_reference blocks further refunds for the same amount
  • New test: settleRefund and settlePayout reject amount mismatches with AMOUNT_MISMATCH
  • ./mvnw verify clean

A refund that transitions FAILED -> REFUNDED (late provider confirmation)
was excluded from committedAmount, allowing a second refund to be created
for the same amount. The total refunded could then exceed the payment.

Three fixes:
- committedAmount now includes FAILED refunds that have a provider
  reference, since the provider may still confirm them
- RefundTransitions raises an alert on late refund and on cumulative
  exceeding the payment amount
- Settlement verifies confirmed amounts for refunds and payouts, matching
  the existing check in settlePayment
@mooder1
mooder1 force-pushed the fix/over-refund-settlement-guards branch from d8cadd3 to 88d4164 Compare November 17, 2026 20:53
- YoonLateSuccess now matches late_refund alongside late_success/late_payout
- New critical alert YoonRefundExceedsPayment for over-refund detection
- Runbook: amount mismatch section covers refunds and payouts, new section
  for refund-exceeds-payment with recovery steps

crossben commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Thanks a lot for this, @mooder1 — the diagnosis is right and the reproduction is exactly the hole: a refund settled FAILED that the provider later confirms REFUNDED frees its amount in the meantime, so a second refund can push the total past the payment. Good catch, and good call using payments.find() instead of a lock to keep the payment → refund lock order.

Before this can go in, a few things:

  1. Tests. The test plan lists two new tests but none are in the diff. For money paths we need them in the same change: a FAILED refund with a provider_reference blocking a second refund; the late FAILED → REFUNDED path raising late_refund (and refund_exceeds_payment when the total goes over); settleRefund / settlePayout returning AMOUNT_MISMATCH and leaving the status unchanged.
  2. Refunds that really failed. With status <> 'FAILED' OR provider_reference IS NOT NULL, a refund the provider accepted and then genuinely failed (Stripe can do this) keeps its amount reserved forever — the merchant can never refund that amount again through Yoon. That may be the right trade-off, but it's a visible API change: it needs to be documented, tested, and in the CHANGELOG, and an operator probably needs a way to release it once they've checked with the provider.
  3. Payout amount check. The new guard is only safe if every adapter's payoutStatus reports the amount the recipient received (Wave reports receive_amount, which matches what we send). DexPay payouts haven't run against the real API yet, so we can't tell whether its amount includes fees; if it does, every DexPay payout would sit in AMOUNT_MISMATCH. A per-adapter test pinning which field is compared would settle it.
  4. CHANGELOG entry under [Unreleased].

We're going to fold this into a wider money-safety pass on main (it turned up a few related gaps in settlement and recovery), so I'll carry your fix forward with the tests and credit you as co-author on the commit. Feel free to keep the PR open to follow along; we'll close it with a link once it lands. Thanks again!

crossben added a commit that referenced this pull request Nov 21, 2026
…efunds and payouts

A refund the provider saw and then reported failed keeps its amount until an
operator releases it (admin API + refunds CLI); late refunds and over-refunds
raise alerts; refunds and payouts confirmed at another amount are flagged for
review instead of settled. Money-safety plan M1 (C1, H1), from PR #1.

Co-Authored-By: mooder1 <108179967+mooder1@users.noreply.github.com>
@crossben crossben closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants