feat(billing): mirror Stripe discounts into arrangements
Closed
ET
15d ago+916+916+012345678901234567890123456789-58-58-01234567890123456789
Stacked on #29321.
What
- Schedule rewrites keep discounts. Today any change scheduled for period end removes the customer's coupon right away (reproduced in Stripe test mode). Resending the phase's
{ coupon }is not a fix: Stripe redeems the coupon again, so an ended coupon comes back on the next rewrite and again at the phase change (also reproduced). Phases now reference the subscription's live discounts by id, and the next phase keeps only those still running when it starts, since Stripe rejects a phase holding a discount that will have expired. Checked on test clocks: a 1-month coupon is not granted again at the phase change, a forever coupon carries over with the same id, and a product-restricted coupon on a phase with another product is accepted and simply stops applying. - Stripe discounts are mirrored into
DISCOUNTarrangements. Oncustomer.discount.created,updatedanddeleted, the customer's live discounts (subscription and customer level) are read from Stripe inside the workspace lock and matched to arrangements bystripeDiscountId. The arrangement is created, updated or ended in one transaction.- A discount added from the Stripe dashboard becomes an arrangement with no grantor and label
OTHER. - A coupon removed and applied again is a new discount: the old arrangement ends and a new one is recorded.
- The insert is
ON CONFLICT DO NOTHINGon the unique discount id. The admin action can record the same discount at the same time and owns its label and grantor, so whichever write lands first, the row ends up the same. - A revoked arrangement whose discount is still live in Stripe is logged as not mirrored and never recorded again. So are extra live discounts: one is mirrored at a time.
- A recorded discount starts after every existing arrangement, so a retried swap never overlaps. An arrangement that still cannot be recorded throws, so Stripe retries and Sentry sees it.
- A discount added from the Stripe dashboard becomes an arrangement with no grantor and label
- A canceled subscription ends its discount arrangement, since Stripe sends no discount event on cancel (confirmed in test mode). A customer-level discount stays, as Stripe applies it to the next subscription.
normalizePriceRefbecomesgetStripeObjectId, now used for prices, coupons and customers.
Rollout
- The Stripe webhook endpoints must subscribe to the
customer.discount.*events, or nothing is mirrored.
Not in this PR
- Admin actions to apply a discount or end one early, and the "seats only" coupon check. Applying one calls Stripe first, then inserts on
stripeDiscountIdwithON CONFLICT DO UPDATE SET label, comment, grantedByUserId, and reuses the live discount on a retry. - Discounts on a single subscription item are neither kept on schedule rewrites nor mirrored. We only apply discounts to whole subscriptions or customers.
billing.module.tsandbilling.exception.tsconflict with the seats PR; resolve when the second one lands.
Testing
- All 383 billing unit tests pass, including the swap overlap cases from review and the revoked-arrangement case. Lint and format clean; typecheck shows no error in billing files.
- Stripe behaviour above checked with the Stripe CLI and test clocks on the test account. Not run end to end against a running server.