Sub topic 14
Refunds, voids, and chargebacks
This chapter assumes you know the authorization, capture, and settlement steps, the payment state machine, and the double-entry ledger from earlier chapters. It covers the three ways a payment can be undone.
Three ways to undo a payment
The right tool depends on how far the payment has progressed.
| Operation | When it applies | Who starts it | Money moved? |
|---|---|---|---|
| Void | After authorization, before capture | Merchant | No. The hold on the customer's funds is released. |
| Refund | After capture | Merchant | Yes. Money flows back to the customer. |
| Chargeback | After settlement, usually weeks later | Cardholder, through the issuer | Yes. The issuer pulls the money back. |
A void is the cheapest option. Nothing has settled, so no ledger entries exist to reverse, and the payment moves from authorized to voided. If the void request times out, retry it with the same idempotency key.
Refunds
A refund is a new record that points at the original payment. It is not an edit to that payment. One payment can have many partial refunds, so the service must enforce one invariant: the total refunded never exceeds the amount captured.
def validate_refund(captured_minor: int, refunded_minor: int, requested_minor: int) -> int:
"""Return the new refunded total, or raise if the refund is not allowed."""
if requested_minor <= 0:
raise ValueError("refund amount must be positive")
if refunded_minor + requested_minor > captured_minor:
raise ValueError("refund exceeds captured amount")
return refunded_minor + requested_minor
print(validate_refund(1999, 500, 700)) # 1200This check is a read-then-write, so two concurrent refund requests can both pass it. Run it inside a database transaction that locks the payment row (SELECT ... FOR UPDATE). Give each refund its own idempotency key so a retried request cannot refund twice.
In the ledger, a refund appends new entries that reverse the original ones: debit merchant_payable and credit the customer-side account. Never update or delete past entries. The history must stay auditable.
Chargebacks
A chargeback is a dispute. The cardholder tells the issuer that a charge is wrong, for example fraud or goods never received. The issuer reverses the money through the card network, and the merchant is debited, often with a dispute fee. The merchant can contest by submitting evidence before a deadline.
Model a dispute as its own object linked to the payment, with its own states. Do not add these states to the payment itself.
The ledger follows the dispute. When it opens, debit the merchant and hold the funds. If the merchant wins, reverse that entry. If the merchant loses, the debit stands.
Common mistakes
- Refunding a payment that has a dispute open. The merchant can lose the money twice.
- Missing the evidence deadline. The deadline is set by the card network, so store it and alert on it.
- Treating a refund as a new charge with a negative amount. This breaks reconciliation, because the PSP reports it as a refund.
Exercise
A payment captured 5000 minor units and already has 3000 refunded. A request arrives to refund 2500. What should happen, and which database technique keeps two simultaneous requests from both succeeding?
Show answer
Reject it, because 3000 + 2500 = 5500 exceeds the 5000 captured. Lock the payment row inside a transaction (SELECT ... FOR UPDATE) so the check and the write happen as one step.