ADR-132: Returned credit goes back to the grants it came from
Date: 2026-10-30 Status: Accepted
Summary
Section titled “Summary”When a void, or a credit note on an unpaid invoice, hands back credit the invoice drew from the customer’s balance, that credit goes back to the grants it was drawn from. It keeps their expiry and their kind (promotional, commit or paid). A share whose grant is already past its expiry expires at once, by the expiry sweep’s rule. A share of a commit whose funding invoice is voided was retired again; that rule is gone, since a commit’s funding invoice is paid when the credit is granted and can’t be voided (ADR-078’s amendment of 2026-11-24). A credit note on a paid invoice follows one more rule: it never gives the customer less than it says. Its drawn share goes back to its grants only while they stay spendable, and the rest is new credit. Before this, every return was a new grant with no expiry and no kind, so expiring or promotional credit came back as never-expiring paid credit. On a void this is what Stripe does. On a credit note Stripe restores nothing; Hardcount restores, a deliberate difference stated below.
Context
Section titled “Context”The bug. Three writers hand drawn credit back to the balance, and each
wrote a fresh grant row with no expires_ and no grant_:
- Void (
credit., run in the void’s transaction): what the invoice still has applied.Service. ReverseForInvoiceTx - Credit note on an unpaid invoice (
creditnote.): the applied credit the note relieves beyond what is still due (ADR-061 amendment 2026-10-12).issueUnpaidTx - Credit note on a paid invoice (
creditnote., the credit channel): the whole credit amount, even the part the invoice had paid with credit.Issue
A customer with credit due to expire next week, or with free promotional
credit, got it back as never-expiring paid-class credit. ADR-071 recorded
this as an accepted residual, ADR-061 left “what kind of credit comes back”
open with a trigger, and ADR-078 listed it as a commit limitation. All three
waited on per-block attribution. That data exists: since 2026-07-05 every
drain writes metadata. on its
usage row.
What the peers do (verified 2026-10-30):
- Stripe, Billing credits, “Void invoices and issue credit notes”: “Voiding an invoice with credits applied reinstates the applied balance to the credit grant. If the credit grant is past the expiry date, the reinstated credits expire immediately.” And: “Issuing a Credit Note doesn’t refund credit grants applied to an invoice. To restore those billing credits, you must create a new credit grant.”
- Lago, Void invoices: “Only prepaid credits used to pay the invoice will be refunded to the customer’s balance (as they have been prepaid).” The credit goes back to the prepaid wallet it came from.
- Orb, Void invoice: “if the invoice used $10 of customer balance, that amount will be added back to the customer balance upon voiding.” Its docs do not say how a void treats credit drawn from prepaid credit blocks.
Decision
Section titled “Decision”- One primitive.
PostgresStore.writes every return, on the caller’s transaction. Its callers are the void reversal and the credit-note grant (ReturnCreditTx GrantForCreditNoteTx, both branches). - Restore to the source blocks, last drawn first. The return reads the
invoice’s usage rows newest first (by id, which sorts by creation time)
and each row’s
drained_in reverse. It skips what earlier returns on the invoice already restored (theirblocks metadata.). It lowers each source block’srestored_ blocks consumed_by its share.cents - One row per source. The whole amount is still one grant row, with the
source id the dedup indexes key on (
source_, 0106;invoice_ reversal_ id source_, 0093). The row is written withcredit_ note_ id consumed_= the restored part and metadatacents restored_andblocks restored_. So it adds the whole amount to the balance, but only the unattributed rest to drainable capacity.cents InvoiceCreditsTxand the void’s “still applied” sum are unchanged. - Past expiry: the sweep’s rule. At the return’s instant on the
customer’s clock (a test clock’s frozen time for a pinned customer), a
grant with
expires_is expired. Its restored share expires at once through the sweep’s own write (at < now expireGrantRemainderTx): consumed back up, anExpired grant <id>entry stamped at the return’s instant. As in the sweep, a grant an owed draw still needs (domain.) is not expired. Its share stays live for that draw, and the sweep retires it later. The invoice the credit is returned for never counts as that draw (CreditDrawOwedSQL CreditDrawOwedExceptSQL): it is the draw being undone, even while it is still queued for collection. As in ADR-071 §2, retirement wins: a grant the sweep already wrote off is expired again even if a draw is now owed. A grant past expiry that the sweep has not written off yet is written off first, its remainder stamped atexpires_, exactly as the sweep would.at - Commit re-retire. A share restored to a commit grant whose funding
invoice is voided is retired again through
retireCommitSliceTx(reasoncommit_). This comes before the expiry test. It reads the funding invoice’s status, so it also covers a void that found the grant fully drawn and retired nothing. (Removed by ADR-078’s amendment of 2026-11-24: no commit grant’s funding invoice can be voided.)void_ retire - Credit note on a paid invoice: never less than it says. The
customer’s balance rises by exactly the note’s credit amount, the figure
the note’s PDF, hosted page and audit row state.
- The note is split pro rata to how the invoice was paid. Its drawn share
is
amount × attributed draws / invoice total, in integer cents, cumulative over the invoice’s earlier paid-branch notes:floor((earlier + amount) × drawn / total) − floor(earlier × drawn / total). The rounding telescopes, so the notes never return more drawn credit than their share of it (paidDrawnShare). - The drawn share goes back to its blocks, last drawn first, only where
it stays spendable: not past expiry (
spendableAt; the voided-commit test went with item 5, ADR-078’s amendment of 2026-11-24). A share that would expire is new credit instead. Nothing on this branch expires at once. - The rest of the note is new credit with no expiry, as before.
- Each such row carries metadata
paid_andinvoice: true drawn_, which the next note’s cumulative share reads.share_ cents
- The note is split pro rata to how the invoice was paid. Its drawn share
is
- Legacy rows. A usage row from before 2026-07-05 has no
drained_. Its part goes back as new credit with no expiry, with a WARN naming the invoice. The amount handed back that way is (amount still applied) − (attributed and not yet restored). On a void or an unpaid note, every cent of the return is drawn credit. Such a return larger than the attributed outstanding plus the legacy rows is refused as corrupt, not minted.blocks - Locks. The return’s immediate caller holds the invoice row (the
void, the credit note’s issue, finalize’s held relief). The return then
takes the customer advisory lock, then the customer’s ledger rows
FOR UPDATE, then reads. Every restore is a CAS on theconsumed_it read (cents credit_when it does not apply). The engine’s cancel and plan-swap relief takes more first:block_ changed VoidUnchangedTxandLockInvoicesForDraftsTxlock several invoice rows before any return. No path takes ledger rows and then an invoice row, so no cycle was found. - Readers.
- Balance crossings (
credit., ADR-078) are emitted once, for the return’s net change: a return whose share expires at once sends nothing when the balance ends where it started.balance_ * ListGrantSummariesreports a return row for its unattributed rest only, and leaves out a row with none. The restored part shows on the blocks it went back to: as remaining, or as expired or retired.- The balance totals (
GET /, the credit list, thev1/ credits/ balance credit.webhooks) take the restored part off both* total_andgranted total_. They then match the grant list, andused total_still holds.granted = total_ used + total_ expired + total_ retired + total_ deducted + balance - The ledger list reports a return row’s
consumed_without its restored part, so the field stays “how much has been used or expired”.cents - The void audit row adds
expired_(insideon_ return_ cents credits_), when not zero.reversed_ cents commit_went with item 5 (ADR-078’s amendment of 2026-11-24).retired_ on_ return_ cents
- Balance crossings (
Why this design
Section titled “Why this design”Credit keeps the terms it was given. A return undoes a draw; it does not create credit. Restoring the blocks puts the customer exactly where they would be had the invoice never drawn, which is Stripe’s void rule. A share of an expired grant would have expired, so it does.
Credit notes restore too, unlike Stripe. Taking the credit away, as Stripe does on a credit note, would be a new money rule against the customer. Minting it all fresh is the bug. On an unpaid invoice the note relieves applied credit, so restoring it gives back exactly the credit the customer had, on its terms.
A paid-invoice note never gives less than it says. The note’s documents
state an amount added to the balance. If that credit went back to a grant
that is expired, or to a commit whose funding invoice is voided, it would
vanish at once, and the customer would get less than the note says. A review
found both cases: a note on an invoice paid partly with promotional credit
that expired yesterday, and the engine’s cancel relief on a paid prebill
(reliefTx → CreateAdjustmentDraftTx). The pro-rata split puts back only
what the invoice’s own way of payment implies, and only where it can be
spent.
One row, one primitive. The dedup indexes and the void’s sum read one row per source; keeping that shape means none of them changes. The expiry and commit writes reuse the existing single writers (ADR-071, ADR-080 §5), so there is still one expiry write and one commit-retire core.
Alternatives considered
Section titled “Alternatives considered”- A. Proportional restore over blocks on a partial return. Spread a partial note over every block the invoice drew. Rejected: it needs a rounding rule per block, and last drawn first is what the drain’s order implies (the last block drawn is the first the invoice would not have needed). The paid branch’s pro-rata split is between drawn credit and cash, not between blocks.
- A2. Drawn credit first on a paid invoice. Place the note’s credit on drawn credit before cash. Rejected after review: a note on an invoice paid with expired credit restored it to the expired grant, and the customer got less than the note said.
- B. Hand back an expired share as live credit. Rejected: it gives the customer credit they would not have had, and differs from Stripe’s void.
- C. Several rows per return, one per source block. Rejected: the 0093 and 0106 dedup indexes allow one row per source, and a second row per source breaks them.
- D. Follow Stripe on credit notes and restore nothing. Rejected: that takes credit away from the customer, a new rule this fix should not add.
Consequences
Section titled “Consequences”Positive
Section titled “Positive”- A void or a credit note no longer turns expiring or promotional credit into never-expiring paid credit.
- The ledger stays balanced: balance equals block capacity after every
return, and no written-off grant gets headroom back (the
balance_andexceeds_ block_ capacity expired_doctor checks).grant_ headroom_ reopened
Risks / open items
Section titled “Risks / open items”- ADR-080’s ratio-integrity line changes. It said no writer shrinks
consumed_on a commit grant. A return now does, by at most what an invoice drew from it. Consumed never falls belowcents cn_(relief retires only remaining credit), so relief cash stays bounded by what was paid.retired_ cents - Rounding and legacy draws on the paid branch. The drawn share counts only attributed draws, so on an invoice with draws from before 2026-07-05 more of the note is new credit than the invoice’s payment implies. With legacy draws, refunds plus new credit can also exceed the cash paid; that was true before ADR-132 too.
- A refused return blocks its writer. A void or credit note whose attribution does not add up fails loudly instead of minting credit; an operator has to look at the ledger. No such ledger is known.
- Restore order within one second. Usage-row ids are xids, ordered by creation second; two rows on one invoice from two processes in the same second restore in id order.
- A grant past expiry can have two expiry entries (the sweep’s remainder, then the returned share). Every reader sums them.
- Not in scope. The credit-note audit row is unchanged: the ledger rows show what expired or was retired. The dashboard needs no change: a return row’s restored part no longer reads as used.
References
Section titled “References”- ADR-061: credit-note issue, and its open item “what kind of credit comes back”.
- ADR-071: expiry retires the block, and its accepted residual.
- ADR-078: prepaid commits, and its listed limitation.
- ADR-080: commit relief, and its ratio-integrity line.
- ADR-129: credit carries its own currency.
- Migrations 0093 (credit-note dedup index), 0106 (reversal dedup index). No new migration.
internal/(credit/ return. go ReturnCreditTx,planRestore,paidDrawnShare,spendableAt,classifyRestore),internal/(credit/ postgres. go expireGrantRemainderTx,retireCommitSliceTx,ListGrantSummaries,balanceGrantedUsedColumns).
Stuck, or found a bug? Ask in Discussions · Open an issue