fix(billing): end the first period before the first provider charge #165

Sammanfogat
supernaut sammanfogade 2 incheckningar från fix/first-period-end in i main 2026-10-12 18:47:30 +00:00
Ägare

What

The first period of an auto-renewing subscription now ends 1 ms before Amsterdam midnight of the first provider charge day. The Mollie startDate is unchanged.

Why

The first period ended one interval after payment (addInterval), while Mollie charges on the next Amsterdam day. Seen on staging: paid 2026-10-12 18:19 UTC, period end 2026-11-12 18:19 UTC, Mollie start 2026-11-13. The portal showed "renews on 12 November". Renewals already end 1 ms before the charge day. Only the first period broke that rule.

How

  • In applyFirstPayment, when renewal = "auto", a payment method exists and no provider subscription exists yet, periodEnd = amsterdamMidnight(renewalStartDate(addInterval(periodStart, interval))) - 1 ms.
  • startDate still uses renewalStartDate of the unaligned end, so the Mollie schedule does not move. renewalStartDate(periodEnd) returns the same day.
  • One-time purchases (renewal = "none") and purchases that reuse an existing provider subscription keep the plain interval. An extension of a running period with an existing subscription must not re-anchor it.
  • Readers of the period end (charge.periodEnd, invoice period, reminders, nextCharge.on, renewal anchor) already derive the charge day with renewalStartDate. They return the same day as before.

Side effect: the first period is up to one day longer. Withdrawal refunds prorate over the period, so the refund can differ by a minor unit or so. The existing withdrawal test now accepts 13500 or 13501.

How tested

New table test in src/billing.test.ts: monthly and annual, a late-evening Stockholm checkout, a checkout before and a period end after the late-October DST change, and a 31 January checkout clamped to 28 February. Each asserts the period end on the subscription and the charge row, renewalStartDate(periodEnd), and the startDate sent to the provider. It failed before the fix.

pnpm test (473 passed with a Postgres), pnpm typecheck and pnpm lint pass.

Closes #163

Version 0.11.3.

## What The first period of an auto-renewing subscription now ends 1 ms before Amsterdam midnight of the first provider charge day. The Mollie `startDate` is unchanged. ## Why The first period ended one interval after payment (`addInterval`), while Mollie charges on the next Amsterdam day. Seen on staging: paid 2026-10-12 18:19 UTC, period end 2026-11-12 18:19 UTC, Mollie start 2026-11-13. The portal showed "renews on 12 November". Renewals already end 1 ms before the charge day. Only the first period broke that rule. ## How - In `applyFirstPayment`, when `renewal = "auto"`, a payment method exists and no provider subscription exists yet, `periodEnd = amsterdamMidnight(renewalStartDate(addInterval(periodStart, interval))) - 1 ms`. - `startDate` still uses `renewalStartDate` of the unaligned end, so the Mollie schedule does not move. `renewalStartDate(periodEnd)` returns the same day. - One-time purchases (`renewal = "none"`) and purchases that reuse an existing provider subscription keep the plain interval. An extension of a running period with an existing subscription must not re-anchor it. - Readers of the period end (`charge.periodEnd`, invoice period, reminders, `nextCharge.on`, renewal anchor) already derive the charge day with `renewalStartDate`. They return the same day as before. Side effect: the first period is up to one day longer. Withdrawal refunds prorate over the period, so the refund can differ by a minor unit or so. The existing withdrawal test now accepts 13500 or 13501. ## How tested New table test in `src/billing.test.ts`: monthly and annual, a late-evening Stockholm checkout, a checkout before and a period end after the late-October DST change, and a 31 January checkout clamped to 28 February. Each asserts the period end on the subscription and the charge row, `renewalStartDate(periodEnd)`, and the `startDate` sent to the provider. It failed before the fix. `pnpm test` (473 passed with a Postgres), `pnpm typecheck` and `pnpm lint` pass. Closes #163 Version 0.11.3.
supernaut lade till 2 incheckningar 2026-10-12 18:33:18 +00:00
The first period ended one interval after payment, a day before the provider's first charge. It now ends 1 ms before Amsterdam midnight of the charge day, like renewals. The subscription startDate is unchanged.
chore(release): 0.11.3
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m19s
5b053791c2
supernaut tvångsskickade fix/first-period-end från 5b053791c2
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m19s
till 5960dd738e
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m4s
2026-10-12 18:44:50 +00:00
Jämför
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-12 18:44:57 +00:00
supernaut sammanfogade incheckning d8e56bc75c till main 2026-10-12 18:47:30 +00:00
supernaut tog bort grenen fix/first-period-end 2026-10-12 18:47:30 +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!165
Ingen beskrivning angiven.