feat(billing): refund review, provider refund state, mandate checks, card replacement #175

Sammanfogat
supernaut sammanfogade 7 incheckningar från feat/payment-batch-3 in i main 2026-10-13 10:28:04 +00:00
Ägare

Refunds: log failed refunds until reviewed (#115)

  • refund.reviewed_at (migration 0014, nullable).
  • Every reconcile pass logs [reconcile] <n> failed refund(s) unreviewed, oldest <id> since <time>; needs manual review while a failed refund has no reviewed_at. A refund under an open chargeback is left out.
  • README: the UPDATE refund SET reviewed_at = now() WHERE id = '<refund id>'; step.
  • Tests: the line repeats until reviewed_at is set. A reviewed refund and one under an open chargeback are not counted.

Closes #115

Refunds: provider state, webhook settle, stale refunds (#87)

  • refund.provider_status keeps Mollie's own state (queued, processing, canceled, ...). Migration 0015, nullable.
  • getRefundStatus returns { status, providerStatus }. refund() and the refund event carry providerStatus.
  • A dashboard cancel is logged as canceled at the provider; customer still owed, apart from failed. Both stay failed in status and are reviewed the same way.
  • A refund webhook settles the row with the same update the sweep uses (settleRefund). A failure is flagged once, after commit, whichever of the two sees it first.
  • A refund pending for 24 hours or more makes every reconcile pass log [reconcile] <n> refund(s) pending over 24 h, oldest <id> since <time>, provider status <state>; needs manual review. There is no metrics endpoint, so the alert keys on this log line, as for chargebacks.
  • Tests for each of the three cases.

Closes #87

Checkout: check the mandate (#84)

The card expiry from the mandate was already stored for first payments (0.13.0). What was missing:

  • The adapter reads the mandate status. Only valid and pending are usable; any other word reads as invalid. A failed read leaves it unset, which counts as usable.
  • A paid auto-renew checkout with an invalid mandate is refunded and not activated, like one with no mandate.
  • payment_method.status stores the mandate status (was always valid). A pending method is also revoked when its subscription ends.

Closes #84

Subscriptions: replace the card (#85)

  • New tRPC procedure replaceCard (contract 0.13.0, additive: new input schema, new error reasons not_renewing and subscription_ended).
  • It starts a 1 SEK first payment on the customer's existing Mollie customer. The payment is a card_replacement row (migration 0016), not a charge: no invoice, period or VAT. One may be pending per subscription; one never completed is dropped after an hour.
  • When the check is paid with a usable mandate: the Mollie subscription moves to the new mandate (replaceSubscriptionMandate), a payment_method row is added, the old mandate is revoked after the switch, and the check is refunded through the normal refund row.
  • When it cannot be applied (payment failed, invalid mandate, card country mismatch, subscription ended meanwhile, provider refused the move, start expired): the subscription is unchanged, the new mandate is revoked and the check is refunded. Logged as [card] card replacement ... not applied: <why>.
  • refund.charge_id is nullable, with card_replacement_id beside it and a check that exactly one is set. Existing rows are unchanged.
  • Reconcile treats a decided check payment as settled, so it is not an orphan.
  • Not covered: a subscription Mollie has already cancelled is refused (subscription_ended). Mollie cannot update it. Those customers check out again after the grace, as before.
  • Staging check still to do: replace the card on a past_due subscription and see the next renewal charge the new mandate.
  • Only a provider refusal counts as "not applied". A timeout or 5xx may follow an update Mollie applied, so it rolls back and the replay repeats the same update (idempotent).
  • A check whose provider ref was never saved is matched by the cardReplacementId the payment carries.
  • Before the web "replace card" action (bitborg-web#387) is offered to past_due customers, the staging check must pass: Mollie retries a failed past_due payment on the new mandate.
  • bitborg-web follow-up: a "replace card" action on the account page that calls replaceCard, handles the new reasons, and polls subscriptionStatus after the return.

Closes #85

Reconcile trigger (#14)

The adapter wiring shipped in #73. What this adds: each trigger request logs [reconcile-trigger] kick reason=payment, the line bitborg-web already logs. The silent-event-path alert can then count kicks from the billing container too (bitborg-infra#533, "Alerting gap"). That alert change and the prod check (a test tier_pro grant shows kick reason=payment in the shim log) are not part of this PR.

Refs #14

Not in this PR

  • #53 (email on a failed renewal): billing already reports past_due and graceEndsAt. A suspended or cancelled Mollie subscription, which is what an invalid mandate causes, is already marked past_due by the hourly renewal check. The mail belongs to bitborg-web, whose renewal-reminder run already reads subscriptionStatus per account and dedupes with a claim row. It needs a past-due branch there.

Version 0.14.0.

## Refunds: log failed refunds until reviewed (#115) - `refund.reviewed_at` (migration 0014, nullable). - Every reconcile pass logs `[reconcile] <n> failed refund(s) unreviewed, oldest <id> since <time>; needs manual review` while a failed refund has no `reviewed_at`. A refund under an open chargeback is left out. - README: the `UPDATE refund SET reviewed_at = now() WHERE id = '<refund id>';` step. - Tests: the line repeats until `reviewed_at` is set. A reviewed refund and one under an open chargeback are not counted. Closes #115 ## Refunds: provider state, webhook settle, stale refunds (#87) - `refund.provider_status` keeps Mollie's own state (`queued`, `processing`, `canceled`, ...). Migration 0015, nullable. - `getRefundStatus` returns `{ status, providerStatus }`. `refund()` and the refund event carry `providerStatus`. - A dashboard cancel is logged as `canceled at the provider; customer still owed`, apart from `failed`. Both stay `failed` in `status` and are reviewed the same way. - A refund webhook settles the row with the same update the sweep uses (`settleRefund`). A failure is flagged once, after commit, whichever of the two sees it first. - A refund pending for 24 hours or more makes every reconcile pass log `[reconcile] <n> refund(s) pending over 24 h, oldest <id> since <time>, provider status <state>; needs manual review`. There is no metrics endpoint, so the alert keys on this log line, as for chargebacks. - Tests for each of the three cases. Closes #87 ## Checkout: check the mandate (#84) The card expiry from the mandate was already stored for first payments (0.13.0). What was missing: - The adapter reads the mandate status. Only `valid` and `pending` are usable; any other word reads as `invalid`. A failed read leaves it unset, which counts as usable. - A paid auto-renew checkout with an `invalid` mandate is refunded and not activated, like one with no mandate. - `payment_method.status` stores the mandate status (was always `valid`). A `pending` method is also revoked when its subscription ends. Closes #84 ## Subscriptions: replace the card (#85) - New tRPC procedure `replaceCard` (contract 0.13.0, additive: new input schema, new error reasons `not_renewing` and `subscription_ended`). - It starts a 1 SEK first payment on the customer's existing Mollie customer. The payment is a `card_replacement` row (migration 0016), not a charge: no invoice, period or VAT. One may be pending per subscription; one never completed is dropped after an hour. - When the check is paid with a usable mandate: the Mollie subscription moves to the new mandate (`replaceSubscriptionMandate`), a `payment_method` row is added, the old mandate is revoked after the switch, and the check is refunded through the normal `refund` row. - When it cannot be applied (payment failed, invalid mandate, card country mismatch, subscription ended meanwhile, provider refused the move, start expired): the subscription is unchanged, the new mandate is revoked and the check is refunded. Logged as `[card] card replacement ... not applied: <why>`. - `refund.charge_id` is nullable, with `card_replacement_id` beside it and a check that exactly one is set. Existing rows are unchanged. - Reconcile treats a decided check payment as settled, so it is not an orphan. - Not covered: a subscription Mollie has already cancelled is refused (`subscription_ended`). Mollie cannot update it. Those customers check out again after the grace, as before. - Staging check still to do: replace the card on a `past_due` subscription and see the next renewal charge the new mandate. - Only a provider refusal counts as "not applied". A timeout or 5xx may follow an update Mollie applied, so it rolls back and the replay repeats the same update (idempotent). - A check whose provider ref was never saved is matched by the `cardReplacementId` the payment carries. - Before the web "replace card" action (bitborg-web#387) is offered to `past_due` customers, the staging check must pass: Mollie retries a failed `past_due` payment on the new mandate. - bitborg-web follow-up: a "replace card" action on the account page that calls `replaceCard`, handles the new reasons, and polls `subscriptionStatus` after the return. Closes #85 ## Reconcile trigger (#14) The adapter wiring shipped in #73. What this adds: each trigger request logs `[reconcile-trigger] kick reason=payment`, the line bitborg-web already logs. The silent-event-path alert can then count kicks from the billing container too (bitborg-infra#533, "Alerting gap"). That alert change and the prod check (a test `tier_pro` grant shows `kick reason=payment` in the shim log) are not part of this PR. Refs #14 ## Not in this PR - #53 (email on a failed renewal): billing already reports `past_due` and `graceEndsAt`. A suspended or cancelled Mollie subscription, which is what an invalid mandate causes, is already marked `past_due` by the hourly renewal check. The mail belongs to bitborg-web, whose renewal-reminder run already reads `subscriptionStatus` per account and dedupes with a claim row. It needs a past-due branch there. Version 0.14.0.
supernaut lade till 7 incheckningar 2026-10-13 10:25:01 +00:00
Add refund.reviewed_at and a repeating unreviewed-failed-refund summary in reconcile.
Store refund.provider_status, tell a cancel from a failure in the log, settle a refund row
from its webhook and log refunds pending over 24 h.
The card expiry from the mandate was already stored (#149). A paid first payment whose
mandate is invalid is now refunded, not activated.
Add replaceCard: a small first payment on the existing Mollie customer moves the subscription
to the new mandate and is refunded. Contract 0.13.0, additive.
Lets the silent-event-path alert count kicks from the billing container too.
Only a refusal marks a card replacement not applied. Any other error rolls back so the replay
repeats the update. A check whose provider ref was never saved is claimed by the id it carries.
chore(release): 0.14.0
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m29s
d79367806c
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-13 10:26:41 +00:00
supernaut sammanfogade incheckning bdf108ad5c till main 2026-10-13 10:28:04 +00:00
supernaut tog bort grenen feat/payment-batch-3 2026-10-13 10:28:05 +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!175
Ingen beskrivning angiven.