feat: dunning for failed renewals with an 8-day grace #74

Sammanfogat
supernaut sammanfogade 3 incheckningar från feat/53-dunning in i main 2026-10-03 21:01:31 +00:00
Ägare

feat: dunning for failed renewals with an 8-day grace

What

Dunning for failed subscription renewals, built on Mollie's own retries. Renewal periods now line up with Mollie's charge days. Refs #53.

Design

  • Mollie's retries: it retries a failed subscription payment daily, up to 5 times, and cancels the subscription when the retries run out. Some failure reasons cancel it after 3 attempts or at once. Each attempt reaches our webhook as a payment with a subscriptionId. There is no webhook for subscription status changes. Source: docs.mollie.com "Recurring payments". So we build no retry scheduler.
  • Charge days: Mollie charges on the start date's day of the month, and on the last day of a shorter month. A renewed period now ends just before Mollie's next charge day, derived from the first period. Before this, periods chained clamped intervals and drifted up to 5 days ahead of the charge.
  • Failed renewal: the subscription becomes past_due and keeps tier_pro. A failure 3 or more days before the period end is a stale delivery and is ignored.
  • Recovery: a later paid retry makes the subscription active again and extends the period.
  • Grace: 8 days after the period end, for monthly and annual plans. Mollie's first charge falls on the day after the period end. Its 5th retry lands within 7 days. The 8th day lets that webhook arrive.
  • End of grace: the sweep revokes tier_pro and cancels the Mollie subscription. This happens once, and the cancel is a no-op if Mollie already cancelled. A renewal paid after that is refunded, and the customer can check out again.
  • Invoices: periods are printed as Stockholm dates, and a renewal starts on its charge day, so consecutive invoices neither overlap nor leave a gap. Only new invoices change. Issued rows and numbering are untouched.
  • Contract: SubscriptionStatus.graceEndsAt is set while the subscription is past_due and not cancelled. Otherwise it is null. A subscription cancelled while past due ends at the next sweep. @bitborg/payment-contract goes to 0.4.0. The root version is left to the release PR.

How tested

The tests were written first and each new behaviour failed before its fix. Recovery and the post-grace refund pin behaviour that already existed. The tests cover:

  • Monthly and annual plans: past due, still granted 1 ms before the grace ends, then revoked and cancelled exactly once over two sweeps.
  • A paid retry recovers with one invoice. Replayed webhooks change nothing.
  • A stale failure is ignored. A failure on the charge day before the period end's time of day still counts.
  • Invoice text: purchase on 30 Jan reads 2027-01-30 to 2027-02-28, and the 1 Mar renewal reads 2027-03-01 to 2027-03-31. The first invoice row is unchanged after the renewal, and the numbers are consecutive.
  • A stale stored period end never yields a period that ends before its own payment.
  • Monthly periods stay on Mollie's anchored day, including across a February clamp and summer time. Annual periods stay on it across leap years.
  • Cancelling while past due reports no grace.
  • A renewal paid after revocation is refunded, and a new checkout is accepted.

An offline sweep of 75,090 derived periods (start dates every 7 h over 2 years) found 0 that miss Mollie's charge day.

Results with Postgres 17:

  • pnpm test --coverage: 277/277 passed, 16 files.
  • lint, workspace typecheck, build and drizzle-kit check: all pass.
  • No migration.

Follow-ups and open questions

  • Portal task: show the past-due state and graceEndsAt on the account page, and send the reminder email. The portal owns mail. Deploy this service before the portal moves to contract 0.4.0, because the field is required in the output.
  • Portal "renews on": currentPeriodEnd is now the last instant before the charge day, so the portal should show the renewal date as currentPeriodEnd + 1 ms (the charge day), or say "paid until" for currentPeriodEnd.
  • Annual subscriptions started on 29 February: we assume Mollie charges on 28 February in non-leap years. Mollie does not confirm this. If it charges on 1 March instead, the period ends one day early but stays inside the grace, so access is not lost.
  • Terms: they say nothing about failed payments or the 8-day grace. The issue requires them to match, so the owner needs to decide.
  • Card replacement: a failing card cannot be replaced on a live subscription. A past-due customer cannot check out again either (already_subscribed). Mollie's retries do not fix an expired card, so that customer loses access after the grace and then checks out again. A "replace card" flow needs a new first payment and a mandate update. Is it needed before monthly plans launch?
  • The issue asks for retries "on a fixed schedule". That schedule is Mollie's: daily, up to 5, depending on the failure reason. Mollie does not document whether SEK card renewals are retried.
  • Existing subscriptions keep their stored period end until their next renewal. There are no live ones.
# feat: dunning for failed renewals with an 8-day grace ## What Dunning for failed subscription renewals, built on Mollie's own retries. Renewal periods now line up with Mollie's charge days. Refs #53. ## Design - Mollie's retries: it retries a failed subscription payment daily, up to 5 times, and cancels the subscription when the retries run out. Some failure reasons cancel it after 3 attempts or at once. Each attempt reaches our webhook as a payment with a `subscriptionId`. There is no webhook for subscription status changes. Source: docs.mollie.com "Recurring payments". So we build no retry scheduler. - Charge days: Mollie charges on the start date's day of the month, and on the last day of a shorter month. A renewed period now ends just before Mollie's next charge day, derived from the first period. Before this, periods chained clamped intervals and drifted up to 5 days ahead of the charge. - Failed renewal: the subscription becomes `past_due` and keeps `tier_pro`. A failure 3 or more days before the period end is a stale delivery and is ignored. - Recovery: a later paid retry makes the subscription `active` again and extends the period. - Grace: 8 days after the period end, for monthly and annual plans. Mollie's first charge falls on the day after the period end. Its 5th retry lands within 7 days. The 8th day lets that webhook arrive. - End of grace: the sweep revokes `tier_pro` and cancels the Mollie subscription. This happens once, and the cancel is a no-op if Mollie already cancelled. A renewal paid after that is refunded, and the customer can check out again. - Invoices: periods are printed as Stockholm dates, and a renewal starts on its charge day, so consecutive invoices neither overlap nor leave a gap. Only new invoices change. Issued rows and numbering are untouched. - Contract: `SubscriptionStatus.graceEndsAt` is set while the subscription is `past_due` and not cancelled. Otherwise it is null. A subscription cancelled while past due ends at the next sweep. `@bitborg/payment-contract` goes to 0.4.0. The root version is left to the release PR. ## How tested The tests were written first and each new behaviour failed before its fix. Recovery and the post-grace refund pin behaviour that already existed. The tests cover: - Monthly and annual plans: past due, still granted 1 ms before the grace ends, then revoked and cancelled exactly once over two sweeps. - A paid retry recovers with one invoice. Replayed webhooks change nothing. - A stale failure is ignored. A failure on the charge day before the period end's time of day still counts. - Invoice text: purchase on 30 Jan reads 2027-01-30 to 2027-02-28, and the 1 Mar renewal reads 2027-03-01 to 2027-03-31. The first invoice row is unchanged after the renewal, and the numbers are consecutive. - A stale stored period end never yields a period that ends before its own payment. - Monthly periods stay on Mollie's anchored day, including across a February clamp and summer time. Annual periods stay on it across leap years. - Cancelling while past due reports no grace. - A renewal paid after revocation is refunded, and a new checkout is accepted. An offline sweep of 75,090 derived periods (start dates every 7 h over 2 years) found 0 that miss Mollie's charge day. Results with Postgres 17: - `pnpm test --coverage`: 277/277 passed, 16 files. - lint, workspace typecheck, build and `drizzle-kit check`: all pass. - No migration. ## Follow-ups and open questions - Portal task: show the past-due state and `graceEndsAt` on the account page, and send the reminder email. The portal owns mail. Deploy this service before the portal moves to contract 0.4.0, because the field is required in the output. - Portal "renews on": `currentPeriodEnd` is now the last instant before the charge day, so the portal should show the renewal date as `currentPeriodEnd + 1 ms` (the charge day), or say "paid until" for `currentPeriodEnd`. - Annual subscriptions started on 29 February: we assume Mollie charges on 28 February in non-leap years. Mollie does not confirm this. If it charges on 1 March instead, the period ends one day early but stays inside the grace, so access is not lost. - Terms: they say nothing about failed payments or the 8-day grace. The issue requires them to match, so the owner needs to decide. - Card replacement: a failing card cannot be replaced on a live subscription. A past-due customer cannot check out again either (`already_subscribed`). Mollie's retries do not fix an expired card, so that customer loses access after the grace and then checks out again. A "replace card" flow needs a new first payment and a mandate update. Is it needed before monthly plans launch? - The issue asks for retries "on a fixed schedule". That schedule is Mollie's: daily, up to 5, depending on the failure reason. Mollie does not document whether SEK card renewals are retried. - Existing subscriptions keep their stored period end until their next renewal. There are no live ones.
supernaut lade till 3 incheckningar 2026-10-03 00:14:24 +00:00
Mollie retries a failed subscription payment daily, up to 5 times, and
cancels the subscription when the retries run out. So no retry
scheduler is built here: a failed renewal marks the subscription past
due, access continues, a later paid retry makes it active again, and
the sweep revokes tier_pro and cancels the Mollie subscription when the
grace ends.

The grace grows from 7 to 8 days. Mollie charges up to two days after
the period end, so its last retry can land on day 7.

A failed payment that arrives while the period is still paid is a
stale delivery and no longer marks the subscription past due.

The contract reports graceEndsAt while past due, so the portal can warn
the customer. Contract 0.4.0.

Refs #53
Mollie charges on the start date's day of the month and only clamps in
a shorter month. Our renewal periods chained clamped intervals, so
after one February they ended on the 28th while Mollie charged on the
1st. The gap was 3 to 4 days, so a 5th retry landed past the grace and
a paying customer was refunded and lost access. A renewed period now
ends just before Mollie's next charge day, derived from the first
period, so every charge falls on the day after the period end.

The stale-failure guard dropped real failures on the charge day: a
clamped anchor or a DST shift can put the charge before the period
end's time of day. Only a failure 3 or more days before the period end
is ignored now.

A subscription cancelled while past due ends at the next sweep, so its
status reports no grace.

Refs #53
fix: date invoices in swedish days from each renewal's charge day
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m55s
6f8a2346ad
Invoice periods printed UTC dates, and a renewal started at the
previous period's end, so it read the day before the charge and
consecutive invoices overlapped by a day. Dates are now Stockholm days,
and a renewal's period starts on its charge day. Only newly issued
invoices change; issued rows and numbering are untouched.

A renewed period also extends from the payment's own day when the
stored period end is stale, so it can never end before its payment.

The readme notes that some failure reasons stop Mollie's retries
after 3 attempts or at once.

Refs #53
supernaut tvångsskickade feat/53-dunning från 6f8a2346ad
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m55s
till b0d4d887ae
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m58s
2026-10-03 19:32:32 +00:00
Jämför
supernaut sammanfogade incheckning 072a2bab1c till main 2026-10-03 21:01:31 +00:00
supernaut refererade denna ändringsförfrågan från en incheckning 2026-10-03 21:01:31 +00:00
supernaut tog bort grenen feat/53-dunning 2026-10-03 21:01:31 +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!74
Ingen beskrivning angiven.