Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion deploy/prometheus/alerts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions docs/runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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()))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Loading