Subscriptions
Learn how to set up recurring subscriptions.
Subscriptions are the most common billing type for SaaS products. The starter kit offers first-class support for recurring payments, including monthly and yearly intervals.
With minimal configuration, you can charge users on a recurring basis, track subscription status, and conditionally gate access to features depending on the active plan.
Billing configuration
Let's define a monthly and yearly subscription for a Pro plan in packages/billing/src/config.ts:
export const config = createBillingConfig({
products: [
{
id: 'pro',
name: 'Pro',
description: 'Upgrade to Pro for advanced features',
label: 'Upgrade now',
features: ['Advanced analytics', 'Priority support'],
plans: [
{
id: 'plan-pro-month',
displayIntervals: [PriceInterval.Month],
prices: [
{
id: 'price-pro-month-id',
// [\!code highlight:1]
type: PriceType.Recurring,
model: PriceModel.Flat,
// [\!code highlight:1]
interval: PriceInterval.Month,
cost: 29,
currency: 'USD'
}
]
},
{
id: 'plan-pro-year',
displayIntervals: [PriceInterval.Year],
prices: [
{
id: 'price-pro-year-id',
// [\!code highlight:1]
type: PriceType.Recurring,
model: PriceModel.Flat,
// [\!code highlight:1]
interval: PriceInterval.Year,
cost: 290, // 20% discount
currency: 'USD'
}
]
}
]
}
]
});Subscription handling
When a subscription gets created, the starter kit will:
- Create a record in the
Subscriptiontable. - Create corresponding entries in the
SubscriptionItemtable.
When a subscription get's updated or canceled, the starter kit will:
- Update the record in the
Subscriptiontable. - Update the corresponding entries in the
Subscriptiontable.
You can use the purchases helper to check what for active purchases.
Deleting an organization
When an organization get's deleted, the starter kit will:
- Cancel any active subscription.
- Update the
Subscriptionentry (it won't get deleted on purpose). - Update the
SubscriptionItementry (it won't get deleted on purpose).
This means that a customer will forfeit the remaining time of their subscription. If you want to be nice, you can implement soft-delete for organizations and hard-delete only when subscriptions are truly over but the engineering effort might be too big in the beginning.