feat: revoke tier_pro and stop renewal on a chargeback #75

Sammanfogat
supernaut sammanfogade 5 incheckningar från feat/16-chargeback-revoke in i main 2026-10-04 05:52:34 +00:00
Ägare

What

Handles a Mollie chargeback before live keys.

  • A chargeback on a charge that paid for a live subscription revokes tier_pro (same revoke path as withdrawal and the sweep), cancels the Mollie subscription and sets the subscription to canceled. The provider subscription is not cancelled again when cancel() already stopped it. Only the charge's own subscription is touched. revoke still keeps the tier while another live subscription grants it.
  • New chargeback table (migration 0006), one row per Mollie chargeback id. The UNIQUE ref makes a replay a no-op, even when the provider_event row is missing.
  • Lock order: the charge FOR NO KEY UPDATE, then the subscription FOR UPDATE, both before the refund check. That is the order a first payment uses. NO KEY UPDATE does not conflict with the key share a withdrawal's refund insert takes, so a concurrent withdrawal and chargeback serialise on the subscription and each sees the other's write. Both deadlocks found in review (vs withdrawal, vs first payment) have a test.
  • A first payment with an open chargeback is left unprocessed, so a payment fetched before its chargeback cannot grant. Reconcile then flags it as an orphan.
  • A refund claimed but not yet sent is cancelled (failed) by the chargeback, and issueRefund now sends only pending refunds, so the sweep cannot return the money twice. The chargeback row keeps the ids. Once every chargeback on the subscription is reversed, those still cancelled (failed, never sent) go back to pending and the sweep sends them. If the provider then fails one, the chargeback is flagged again.
  • Reversal (chargeback.reversed): once every chargeback on the subscription is reversed, and one of them ended it, the subscription goes back to active with cancelAtPeriodEnd and tier_pro is granted again, if the paid period still runs and no newer or live subscription exists. The sweep ends it at period end. Otherwise the reversal is flagged for review.
  • Flagged for review, access unchanged: chargeback on refunded money, chargeback on a subscription that already ended, reversal on a first payment never applied (apply or refund by hand; not automatic). An amount not in SEK is flagged too (the subscription still ends).
  • Review lines match the existing Loki alert (\[reconcile\].*needs manual review): one per event, plus a summary each reconcile pass until reviewed_at is set. README documents the operator step.
  • Chargeback on an unknown payment: left unprocessed, as any unmatched event. Reconcile already flags the payment as an orphan.
  • Withdrawal of a charged-back purchase is refused with the new charged_back reason. Contract bumped to 0.5.0 (main's dunning took 0.4.0).
  • Adapter:
    • Chargebacks carry a detail (ref, payment, amount, reversed).
    • They are listed on every callback, because Mollie documents amountChargedBack as present only while non-zero, so a full reversal was invisible.
    • A failed chargeback listing is logged and the payment event still returned.
    • New listChargebacks(since). Reconcile replays each chargeback from the last 14 days that is not recorded locally (a lost webhook on a settled payment). A failed listing is logged and the unreviewed summary still prints.
    • Reconcile also re-fetches the payment of each open chargeback from the last 180 days (newest 100), because reversals usually come weeks later.
  • No invoice is deleted or changed. No credit note is built (payment#63).

Why

Refs #16. A chargeback was stored in provider_event and dropped. The customer kept tier_pro and Mollie kept renewing on a disputed mandate.

How tested

Tests first. Each new one failed before its fix. The withdrawal race test hit a Postgres deadlock before the lock order change.

  • billing (Postgres):
    • Revoke and cancel with the invoice kept.
    • A replay is a no-op, also with the event row deleted.
    • No double cancel after cancel().
    • Unknown payment.
    • A refunded duplicate checkout leaves the live subscription alone and is flagged.
    • An unissued refund is not sent after a chargeback.
    • A chargeback on an ended subscription is flagged.
    • An unreadable amount is flagged.
    • A reversal restores to period end.
    • A partial second chargeback blocks the restore until it is reversed too.
    • A reversal after the period is flagged.
    • A reversal seen first changes nothing.
    • A withdrawal is refused with charged_back.
    • A chargeback during a withdrawal is flagged.
    • A lost chargeback webhook is replayed by reconcile.
    • A reversal older than the listing window is replayed by reconcile.
    • A chargeback racing an in-flight first payment (deadlock before, 3/3).
    • A cancelled withdrawal refund is sent again after the reversal.
    • A failed chargeback listing keeps the unreviewed summary.
    • A reversal on a first payment never applied is flagged.
    • A re-owed refund the provider fails is flagged again.
    • A later reversal does not re-owe a refund that has settled.
    • The review summary repeats until reviewed.
  • mollie adapter:
    • Chargeback detail.
    • Chargebacks listed without amountChargedBack.
    • The payment is still reported when the listing fails.
    • listChargebacks window.
  • pnpm test --coverage against a throwaway Postgres 17: 16 files, 293 tests passed (4 runs). Without TEST_DATABASE_URL: 224 passed, 69 skipped.
  • pnpm lint, pnpm -r --include-workspace-root typecheck, pnpm drizzle-kit check, build: clean.

Merge note

Merged with main after dunning (#74): contract/src/index.ts adds only charged_back on top of main, README "Absent" keeps both sides, contract 0.5.0. 311/311 tests pass against Postgres 17.

Open questions

  • Owner: any chargeback ends the whole subscription, also a partial one or one on an old renewal. Keep that?
  • Owner: after a reversal the renewal stays cancelled (a Mollie subscription cannot be resumed). The customer checks out again after period end. Create a new Mollie subscription instead?
  • Owner: a reversal after the period ended is manual review only. Contact the customer, or nothing?
  • Mollie, before live keys:
    • Confirm in test mode that a chargeback reversal fires the payment webhook.
    • A reversal of a chargeback older than 180 days is seen only through its webhook. The 180 days and the 100-row cap are a guess at the dispute window. Check Mollie's figure.
  • Accountant: how a chargeback is booked (reversal of the sale, VAT correction, Mollie's chargeback fee), and whether it needs a credit note like a refund (payment#63). Same for a chargeback reversal.
  • Accountant: a chargeback on an already refunded payment returns the money twice. Who books the recovery?
## What Handles a Mollie chargeback before live keys. - A chargeback on a charge that paid for a live subscription revokes `tier_pro` (same `revoke` path as withdrawal and the sweep), cancels the Mollie subscription and sets the subscription to `canceled`. The provider subscription is not cancelled again when `cancel()` already stopped it. Only the charge's own subscription is touched. `revoke` still keeps the tier while another live subscription grants it. - New `chargeback` table (migration 0006), one row per Mollie chargeback id. The UNIQUE ref makes a replay a no-op, even when the `provider_event` row is missing. - Lock order: the charge `FOR NO KEY UPDATE`, then the subscription `FOR UPDATE`, both before the refund check. That is the order a first payment uses. NO KEY UPDATE does not conflict with the key share a withdrawal's refund insert takes, so a concurrent withdrawal and chargeback serialise on the subscription and each sees the other's write. Both deadlocks found in review (vs withdrawal, vs first payment) have a test. - A first payment with an open chargeback is left unprocessed, so a payment fetched before its chargeback cannot grant. Reconcile then flags it as an orphan. - A refund claimed but not yet sent is cancelled (`failed`) by the chargeback, and `issueRefund` now sends only pending refunds, so the sweep cannot return the money twice. The chargeback row keeps the ids. Once every chargeback on the subscription is reversed, those still cancelled (failed, never sent) go back to pending and the sweep sends them. If the provider then fails one, the chargeback is flagged again. - Reversal (`chargeback.reversed`): once every chargeback on the subscription is reversed, and one of them ended it, the subscription goes back to `active` with `cancelAtPeriodEnd` and `tier_pro` is granted again, if the paid period still runs and no newer or live subscription exists. The sweep ends it at period end. Otherwise the reversal is flagged for review. - Flagged for review, access unchanged: chargeback on refunded money, chargeback on a subscription that already ended, reversal on a first payment never applied (apply or refund by hand; not automatic). An amount not in SEK is flagged too (the subscription still ends). - Review lines match the existing Loki alert (`\[reconcile\].*needs manual review`): one per event, plus a summary each reconcile pass until `reviewed_at` is set. README documents the operator step. - Chargeback on an unknown payment: left unprocessed, as any unmatched event. Reconcile already flags the payment as an orphan. - Withdrawal of a charged-back purchase is refused with the new `charged_back` reason. Contract bumped to 0.5.0 (main's dunning took 0.4.0). - Adapter: - Chargebacks carry a detail (ref, payment, amount, reversed). - They are listed on every callback, because Mollie documents `amountChargedBack` as present only while non-zero, so a full reversal was invisible. - A failed chargeback listing is logged and the payment event still returned. - New `listChargebacks(since)`. Reconcile replays each chargeback from the last 14 days that is not recorded locally (a lost webhook on a settled payment). A failed listing is logged and the unreviewed summary still prints. - Reconcile also re-fetches the payment of each open chargeback from the last 180 days (newest 100), because reversals usually come weeks later. - No invoice is deleted or changed. No credit note is built (payment#63). ## Why Refs #16. A chargeback was stored in `provider_event` and dropped. The customer kept `tier_pro` and Mollie kept renewing on a disputed mandate. ## How tested Tests first. Each new one failed before its fix. The withdrawal race test hit a Postgres deadlock before the lock order change. - billing (Postgres): - Revoke and cancel with the invoice kept. - A replay is a no-op, also with the event row deleted. - No double cancel after `cancel()`. - Unknown payment. - A refunded duplicate checkout leaves the live subscription alone and is flagged. - An unissued refund is not sent after a chargeback. - A chargeback on an ended subscription is flagged. - An unreadable amount is flagged. - A reversal restores to period end. - A partial second chargeback blocks the restore until it is reversed too. - A reversal after the period is flagged. - A reversal seen first changes nothing. - A withdrawal is refused with `charged_back`. - A chargeback during a withdrawal is flagged. - A lost chargeback webhook is replayed by reconcile. - A reversal older than the listing window is replayed by reconcile. - A chargeback racing an in-flight first payment (deadlock before, 3/3). - A cancelled withdrawal refund is sent again after the reversal. - A failed chargeback listing keeps the unreviewed summary. - A reversal on a first payment never applied is flagged. - A re-owed refund the provider fails is flagged again. - A later reversal does not re-owe a refund that has settled. - The review summary repeats until reviewed. - mollie adapter: - Chargeback detail. - Chargebacks listed without `amountChargedBack`. - The payment is still reported when the listing fails. - `listChargebacks` window. - `pnpm test --coverage` against a throwaway Postgres 17: 16 files, 293 tests passed (4 runs). Without `TEST_DATABASE_URL`: 224 passed, 69 skipped. - `pnpm lint`, `pnpm -r --include-workspace-root typecheck`, `pnpm drizzle-kit check`, build: clean. ## Merge note Merged with main after dunning (#74): `contract/src/index.ts` adds only `charged_back` on top of main, README "Absent" keeps both sides, contract 0.5.0. 311/311 tests pass against Postgres 17. ## Open questions - Owner: any chargeback ends the whole subscription, also a partial one or one on an old renewal. Keep that? - Owner: after a reversal the renewal stays cancelled (a Mollie subscription cannot be resumed). The customer checks out again after period end. Create a new Mollie subscription instead? - Owner: a reversal after the period ended is manual review only. Contact the customer, or nothing? - Mollie, before live keys: - Confirm in test mode that a chargeback reversal fires the payment webhook. - A reversal of a chargeback older than 180 days is seen only through its webhook. The 180 days and the 100-row cap are a guess at the dispute window. Check Mollie's figure. - Accountant: how a chargeback is booked (reversal of the sale, VAT correction, Mollie's chargeback fee), and whether it needs a credit note like a refund (payment#63). Same for a chargeback reversal. - Accountant: a chargeback on an already refunded payment returns the money twice. Who books the recovery?
supernaut lade till 4 incheckningar 2026-10-03 00:14:43 +00:00
A chargeback was recorded in provider_event and then dropped. The customer
kept tier_pro and Mollie kept renewing on a disputed mandate.

A chargeback on a charge that paid for a period now revokes tier_pro, cancels
the Mollie subscription and ends the subscription. Each chargeback is one row
in the new chargeback table, keyed by its Mollie id, so a replay acts once.
A reversal during the paid period restores access to the period end. The
renewal stays cancelled. A chargeback on refunded money, or a reversal that
cannot restore access, logs the line the reconcile alert matches, and each
reconcile pass repeats a summary until reviewed_at is set. No invoice is
touched. A withdrawal of a charged-back purchase is refused, so the money
cannot go back twice.

The adapter now lists chargebacks on every callback: Mollie omits
amountChargedBack once it is zero, so a reversal was never seen.

Refs #16
A reversal restored access while another chargeback on the same
subscription was still open. Access now comes back only once every
chargeback on the subscription is reversed. The column is renamed
holds_subscription and cleared when the subscription is settled, so the
unmerged migration 0006 is regenerated.

Listing a payment's chargebacks failed the whole callback, so a Mollie
outage on that endpoint blocked every payment. The failure is now logged
and the payment event still returned. Reconciliation lists chargebacks
from the last 14 days and replays any not recorded locally, which also
catches a lost chargeback webhook on a settled payment.

A chargeback now cancels a refund claimed but not yet sent, and
issueRefund sends only pending refunds, so the sweep cannot return the
money a second time. The subscription is locked before the refund check,
as withdraw locks it. This removes a deadlock with a concurrent withdrawal
and makes each see the other's write.

A charged-back purchase is refused with the new charged_back reason
(contract 0.4.0). A chargeback on a subscription already ended, or with
an amount not in SEK, is flagged for review.

Refs #16
A chargeback locked the subscription and then took a key share on the
charge, while a first payment held the charge and waited for the
subscription: a deadlock. The chargeback now locks the charge first with
NO KEY UPDATE, as a first payment does, which still leaves a withdrawal's
refund insert free. A first payment with an open chargeback is left
unprocessed, so a payment fetched before its chargeback cannot grant.

A reversal now puts the refunds its chargeback cancelled back to pending,
so the sweep sends them. The ids are kept on the chargeback row, so the
unmerged migration 0006 is regenerated.

Reconciliation logs a failed chargeback listing and still prints the
unreviewed summary. It also re-fetches the payment of each open
chargeback from the last 180 days (up to 100), since reversals usually
come weeks after the 14-day listing window. A reversal seen first with an
unreadable amount is flagged.

Refs #16
fix: flag reversals and re-owed refunds that still need a person
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m50s
660a4b12a7
A first payment refused while its chargeback was open is not applied
when the chargeback is reversed weeks later. The reversal now flags it
for review instead of leaving a paid payment with no access behind it.

A reversal re-owes only refunds still as the chargeback left them
(failed, never sent), so a later reversal cannot reset a refund that has
settled since. The ids stay on the chargeback row, so a re-owed refund
that the provider then fails flags the chargeback again.

Refs #16
supernaut lade till 1 incheckning 2026-10-04 05:50:03 +00:00
Merge origin/main into feat/16-chargeback-revoke
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m57s
1f18649fb2
Contract to 0.5.0: main's dunning already took 0.4.0.
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-04 05:52:10 +00:00
supernaut sammanfogade incheckning 0093fb03c1 till main 2026-10-04 05:52:34 +00:00
supernaut tog bort grenen feat/16-chargeback-revoke 2026-10-04 05:52:34 +00:00
supernaut refererade denna ändringsförfrågan från en incheckning 2026-10-04 06:03:41 +00:00
Logga in för att delta i denna konversation.
Inga granskare
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Förfallodatumet är ogiltigt eller utanför gränserna. Använd formatet "åååå-mm-dd".

Inget förfallodatum satt.

Beroenden

Inga beroenden satta

Referens
bitborg/bitborg-payment!75
Ingen beskrivning angiven.