2.04 Refunds and Disputes: Exit Evidence
Evidence · Step: 2.04 Refunds and Disputes
| Step | 2.04 Refunds and Disputes |
| Date | 2026-11-14 |
| Database | PostgreSQL 18 (postgres:18-alpine, compose.yaml) in Docker Desktop on Windows 11 |
| Result | Passed; the dispute policy is written down and awaits review |
Deliverables
Section titled “Deliverables”| Deliverable | Where |
|---|---|
| The refund rule | crates/core/src/refund.rs (RefundLimits); EntryDraft::refunds; entry kind refund; new LedgerError variants |
| Refunds in the reference ledger | MemLedger::post (refunds), MemLedger::refunded, guards in reverse and post_adjustment; INV-15 in check_invariants |
| Refunds on PostgreSQL | crates/engine/src/post.rs: check_refund, lock_tallies, record_refund; guards in reverse_once |
| Tally table | migrations/0006_refunds.{up,down}.sql: refund_tallies, refunds_entry_id, the refund kind, the guard trigger and the deferred journal_entries_refund_rules (MZ007) |
| Refund and dispute templates | crates/ops/src/refunds.rs: merchant_refund.v1, dispute_open.v1, dispute_resolve_payer.v1, dispute_resolve_merchant.v1; Template::REFUNDS; errors 2021–2022 |
| Chart | 2060 Disputed merchant funds (one dsp-… sub-account per dispute), in the ageing report |
| Decision record | ADR-0015: refund vs reversal, the per-account cap, disputes as sub-accounts |
| Dispute policy | refunds-and-disputes.md (draft for review) |
| Worked entries | E11 and E12 name their template and refunded payment; new E21–E24; trial balance after E24 |
| Tests | crates/core/src/refund.rs (unit), crates/core/tests/refunds.rs (property), crates/engine/tests/refunds.rs (PostgreSQL), crates/ops/tests/refunds.rs |
| Documentation | invariants.md, INV-15, db-schema.md, Refunds, spec.md 4.9, threat TM-22 |
The work arrived in seven pull requests: the refund rule and the reference ledger (#56), the tallies migration (#57), the engine (#58), the chart and the dispute worked entries (#59), the templates (#60), the reference docs (#61), and this evidence.
Implementation tasks
Section titled “Implementation tasks”| Task | Done |
|---|---|
The refund template references original_entry_id; the engine locks the original’s refund tally and enforces sum(refunds) ≤ original |
Yes, and per account rather than one total. A refund names its original in EntryDraft::refunds. The engine locks the original’s tally rows, one per account, checks the refund, posts it and updates the tallies in one transaction. The limits come from the original’s own lines, so the client cannot state them. The cap covers the payer, the merchant and the fee: money goes back only to whoever paid, and never more than the original moved on any account |
refund_tallies table, updated in the same transaction |
Yes: migration 0006 |
dispute_open.v1 (a hold on the merchant), dispute_resolve_payer.v1, dispute_resolve_merchant.v1 |
Yes. The “hold” is a per-dispute 2060:dsp-… sub-account, not a ledger hold, because a hold expires by itself (ADR-0015). A decision for the payer is a refund of the disputed payment |
| Refund vs reversal (ADR) | Yes: ADR-0015. A reversed entry cannot be refunded, a refunded entry cannot be reversed, and a refund is never reversed |
| Fee policy on refund, configured per template | Yes: merchant_refund.v1 takes fee_refund, the part of the refund that comes out of fee revenue. The client decides it for each refund, and the cap stops it exceeding the payment’s fee |
Name change: the roadmap calls the refund template refund.v1. It is
merchant_refund.v1, because “refund” is now a generic engine concept
(an entry kind), and the acceptance check from step 2.01 is that no template
name appears in the engine.
Testing requirements
Section titled “Testing requirements”| Requirement | Test | Result |
|---|---|---|
| Partial refunds up to the cap succeed; one more santim is rejected | partial_refunds_up_to_the_cap_and_not_one_santim_more (PostgreSQL and the reference ledger, compared) |
Pass: three partial refunds reach exactly 100.00; one more santim, from the merchant or from the fee, is RefundExceedsOriginal; the tallies end at their limits |
| 50 concurrent partial refunds on one payment: the total never exceeds the cap | fifty_concurrent_partial_refunds_never_exceed_the_cap (8 threads, 20 connections) |
Pass: 50 refunds of 3.00 against 100.00. Exactly 33 post, 17 are refused, and the tally is 99.00 |
| Refund after reversal: rejected | refunds_and_reversals_exclude_each_other |
Pass: RefundOfReversedEntry. Also refused: reversing a refunded payment, reversing a refund, refunding a refund or a reversal, and an adjustment that is also a refund |
| Dispute when the merchant’s balance is insufficient: defined behaviour | a_dispute_larger_than_the_merchants_balance_is_refused |
Pass: refused (1012), nothing changes. The policy: no partial hold and no suspense; the case stays open, or the platform funds the payer with an approved adjustment |
Also tested:
- Money goes back only to whoever paid. A refund to someone who never
paid, or one that moves an account the wrong way, is
RefundLineNotAllowed(unit, PostgreSQL, ops). - The database refuses on its own. A tally that shrinks gives
MZ007, a tally above its limit23514, and a delete is refused. An over-refund written straight to the journal, around the engine, is refused at commit withMZ007. - Idempotency. A repeated refund returns the original and counts once.
The same key for a refund of a different entry is
IdempotencyKeyReused: the refunded entry is part of the fingerprint. - Disputes. The disputed money is held until one side wins. Each dispute is decided wholly and once. A decision for the payer counts against the payment’s refund limit, so the payer cannot recover the same money by refund and by dispute. A dispute of a reversed payment cannot be decided for the payer. The platform keeps its fee when the payer wins.
- Worked entries. E11 and E12 (refunds of E08 and E07) and E21–E24 (two disputes) are booked by their templates in the full story. Each equals its table, and the story ends at the trial balance after E24 (32 tables, 10,773.20 posted on each side).
Acceptance criteria
Section titled “Acceptance criteria”The over-refund property test passes 10k runs.
refunds_never_give_back_more_than_was_paid (crates/core/tests/refunds.rs,
ProptestConfig::with_cases(10_000)) takes a random payment (price up to
10,000 ETB, any fee up to 10%) and up to 11 random attempts: refunds of any
split (many over the limit, many small enough to reach it exactly), refunds
to someone who never paid, and reversals. For each attempt, the test
predicts the outcome from its own model, and the reference ledger must
agree. At the end:
- what went back never exceeds the payment, on the payer, the merchant or fee revenue;
- what the payer got back equals what the merchant and fee revenue gave;
- refunds and a reversal never both succeed;
check_invariants, including the INV-15 check, holds.
All 10,000 cases pass, in about 8 seconds in a debug build.
Exit gate
Section titled “Exit gate”The dispute policy is written down and reviewed. It is written down in refunds-and-disputes.md: the refund rule, the fee on a refund, the dispute lifecycle, six rules, the behaviour when the merchant’s balance is insufficient, and a list of decisions for the reviewer. The review is pending. Like the accounting review of step 1.01, it needs a person: the product owner, and someone with accounting knowledge. The step is marked done with the review pending, and the policy’s status line records the reviewer and date once it is reviewed.
Not done in this step
Section titled “Not done in this step”- Whether open disputes block a merchant’s settlement payout: step 3.07.
- A partial dispute decision: not in this version; a split is two disputes.
- Verification (step 1.13) does not yet compare the tallies with the journal; the deferred trigger does, at every commit.