From 88d41646241a5aea2f33bfb6a49707dbbffcaa85 Mon Sep 17 00:00:00 2001 From: mooder1 <> Date: Tue, 17 Nov 2026 20:47:05 +0000 Subject: [PATCH 1/2] fix(refund): prevent over-refund via late provider success 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 --- .../dev/yoonpay/server/lifecycle/Alerts.java | 4 ++++ .../server/refund/RefundRepository.java | 7 ++++-- .../server/refund/RefundTransitions.java | 24 ++++++++++++++++--- .../yoonpay/server/settlement/Settlement.java | 22 +++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/yoon-server/src/main/java/dev/yoonpay/server/lifecycle/Alerts.java b/yoon-server/src/main/java/dev/yoonpay/server/lifecycle/Alerts.java index c86383e..4c95ef9 100644 --- a/yoon-server/src/main/java/dev/yoonpay/server/lifecycle/Alerts.java +++ b/yoon-server/src/main/java/dev/yoonpay/server/lifecycle/Alerts.java @@ -23,6 +23,10 @@ public enum Type { payout_needs_review, /** A provider reported a status its adapter does not map. */ unmapped_provider_status, + /** A refund that was FAILED turned out to be refunded; total may exceed the payment. */ + late_refund, + /** The total refunded amount exceeds the payment amount after a late success. */ + refund_exceeds_payment, /** An outbound event exhausted its delivery attempts. */ event_dead, /** A payment arrived for an invoice that had been voided: the money is applied, a human should look. */ diff --git a/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundRepository.java b/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundRepository.java index d48c4c2..cac1009 100644 --- a/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundRepository.java +++ b/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundRepository.java @@ -29,9 +29,12 @@ INSERT INTO refunds (id, application_id, payment_id, status, amount, currency, r .update(); } - /** Refunds that are, or may become, money returned: everything but FAILED. */ + /** Refunds that are, or may become, money returned. FAILED refunds with a provider reference + * are included because the provider may still confirm them as a late success. */ public long committedAmount(String paymentId) { - return jdbc.sql("SELECT coalesce(sum(amount), 0) FROM refunds WHERE payment_id = :p AND status <> 'FAILED'") + return jdbc.sql(""" + SELECT coalesce(sum(amount), 0) FROM refunds + WHERE payment_id = :p AND (status <> 'FAILED' OR provider_reference IS NOT NULL)""") .param("p", paymentId).query(Long.class).single(); } diff --git a/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundTransitions.java b/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundTransitions.java index 19dab4a..75fc8c9 100644 --- a/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundTransitions.java +++ b/yoon-server/src/main/java/dev/yoonpay/server/refund/RefundTransitions.java @@ -7,9 +7,11 @@ import dev.yoonpay.core.money.Money; import dev.yoonpay.server.auth.AppPrincipal; import dev.yoonpay.server.ledger.LedgerRepository; +import dev.yoonpay.server.lifecycle.Alerts; import dev.yoonpay.server.lifecycle.StatusEvents; import dev.yoonpay.server.lifecycle.StatusEvents.Cause; import dev.yoonpay.server.outbox.Outbox; +import dev.yoonpay.server.payment.PaymentRepository; import org.springframework.stereotype.Component; import org.springframework.transaction.support.TransactionTemplate; @@ -22,20 +24,24 @@ public class RefundTransitions { private final RefundRepository refunds; + private final PaymentRepository payments; private final StatusEvents events; private final LedgerRepository ledger; private final Outbox outbox; + private final Alerts alerts; private final TransactionTemplate tx; private final dev.yoonpay.server.lifecycle.StatusMetrics metrics; - public RefundTransitions(RefundRepository refunds, StatusEvents events, LedgerRepository ledger, Outbox outbox, - TransactionTemplate tx, - dev.yoonpay.server.lifecycle.StatusMetrics metrics) { + public RefundTransitions(RefundRepository refunds, PaymentRepository payments, StatusEvents events, + LedgerRepository ledger, Outbox outbox, Alerts alerts, TransactionTemplate tx, + dev.yoonpay.server.lifecycle.StatusMetrics metrics) { this.metrics = metrics; this.refunds = refunds; + this.payments = payments; this.events = events; this.ledger = ledger; this.outbox = outbox; + this.alerts = alerts; this.tx = tx; } @@ -55,6 +61,18 @@ public Decision apply(AppPrincipal app, String id, RefundStatus to, Cause cause, switch (to) { case REFUNDED -> { + if (from == RefundStatus.FAILED) { + alerts.raise(Alerts.Type.late_refund, id, + "was FAILED, provider confirms refunded"); + } + long totalRefunded = refunds.committedAmount(updated.paymentId()); + long paymentAmount = payments.find(app.id(), updated.paymentId()) + .orElseThrow().amount(); + if (totalRefunded > paymentAmount) { + alerts.raise(Alerts.Type.refund_exceeds_payment, id, + "total refunded " + totalRefunded + " exceeds payment " + + paymentAmount + " " + updated.currency()); + } ledger.post(app.id(), LedgerPosting.transfer("refund " + id + " of " + updated.paymentId(), Accounts.customerFunds(updated.provider()), Accounts.providerBalance(updated.provider()), new Money(updated.amount(), Currency.getInstance(updated.currency())))); diff --git a/yoon-server/src/main/java/dev/yoonpay/server/settlement/Settlement.java b/yoon-server/src/main/java/dev/yoonpay/server/settlement/Settlement.java index d37f66d..ea2d1c1 100644 --- a/yoon-server/src/main/java/dev/yoonpay/server/settlement/Settlement.java +++ b/yoon-server/src/main/java/dev/yoonpay/server/settlement/Settlement.java @@ -155,6 +155,17 @@ public Outcome settleRefund(AppPrincipal app, RefundRecord r, Cause cause) { refunds.countStatusCheck(r.id()); return Outcome.NO_ANSWER; } + + Money expected = new Money(r.amount(), Currency.getInstance(r.currency())); + if (result.confirmedAmount() != null && !result.confirmedAmount().equals(expected)) { + alerts.raise(Alerts.Type.amount_mismatch, r.id(), + "refund: provider confirms " + result.confirmedAmount() + ", expected " + expected); + events.record(app.id(), "refund", r.id(), RefundStatus.valueOf(r.status()), result.status(), + Decision.IGNORE, cause, result.rawStatus(), + "amount mismatch: provider confirms " + result.confirmedAmount() + ", expected " + expected); + return Outcome.AMOUNT_MISMATCH; + } + if (!result.status().isFinal()) { return Outcome.STILL_OPEN; } @@ -186,6 +197,17 @@ public Outcome settlePayout(AppPrincipal app, PayoutRecord po, Cause cause) { payouts.countStatusCheck(po.id()); return Outcome.NO_ANSWER; } + + Money expected = new Money(po.amount(), Currency.getInstance(po.currency())); + if (result.confirmedAmount() != null && !result.confirmedAmount().equals(expected)) { + alerts.raise(Alerts.Type.amount_mismatch, po.id(), + "payout: provider confirms " + result.confirmedAmount() + ", expected " + expected); + events.record(app.id(), "payout", po.id(), PayoutStatus.valueOf(po.status()), result.status(), + Decision.IGNORE, cause, result.rawStatus(), + "amount mismatch: provider confirms " + result.confirmedAmount() + ", expected " + expected); + return Outcome.AMOUNT_MISMATCH; + } + if (!result.status().isFinal()) { return Outcome.STILL_OPEN; } From e7f6db00ae624219859ce1f2555a3ac1755d0b2d Mon Sep 17 00:00:00 2001 From: mooder1 <> Date: Tue, 17 Nov 2026 20:56:07 +0000 Subject: [PATCH 2/2] ops: alert rules and runbook for new refund guards - 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 --- deploy/prometheus/alerts.yml | 8 +++++++- docs/runbook.md | 13 +++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/deploy/prometheus/alerts.yml b/deploy/prometheus/alerts.yml index 2e156e4..78dada8 100644 --- a/deploy/prometheus/alerts.yml +++ b/deploy/prometheus/alerts.yml @@ -24,11 +24,17 @@ groups: summary: "{{ $value }} payout(s) have an unknown outcome and need a human (runbook: resolve a payout)." - alert: YoonLateSuccess - expr: increase(yoon_alerts_total{type=~"late_success|late_payout"}[1h]) > 0 + expr: increase(yoon_alerts_total{type=~"late_success|late_payout|late_refund"}[1h]) > 0 labels: {severity: warning} annotations: summary: Money arrived or left after Yoon had given up on it. Check the affected order with the application. + - alert: YoonRefundExceedsPayment + expr: increase(yoon_alerts_total{type="refund_exceeds_payment"}[15m]) > 0 + labels: {severity: critical} + annotations: + summary: The total refunded exceeds the payment amount. A late provider success caused an over-refund. + - alert: YoonDeadEvents expr: yoon_outbox_dead > 0 for: 10m diff --git a/docs/runbook.md b/docs/runbook.md index 4a1bb8a..689a5c7 100644 --- a/docs/runbook.md +++ b/docs/runbook.md @@ -181,8 +181,17 @@ application's settings if the outage lasts. ## Amount mismatch Alert `YoonAmountMismatch`: a provider confirmed a different amount. Yoon did **not** settle the -payment. Find it in the logs (`ALERT amount_mismatch resource=pay_…`) and its history -(`GET /v1/payments/{id}/events`), then settle the difference with the customer outside Yoon. +payment, refund or payout. Find it in the logs (`ALERT amount_mismatch resource=pay_…` or +`resource=ref_…` or `resource=po_…`) and its history (`GET /v1/{resource}/{id}/events`), then +settle the difference with the customer outside Yoon. + +## Refund exceeds payment + +Alert `YoonRefundExceedsPayment`: a provider confirmed a refund Yoon had marked failed (late +success). The total refunded now exceeds the payment amount. The ledger posting was recorded +(the money left). Find the refund in the logs (`ALERT refund_exceeds_payment resource=ref_…`) +and its payment (`GET /v1/payments/{id}/refunds`). Contact the provider to recover the excess +or settle it with the application. ## Restore a backup