SaaS: Plan Upgrade and Downgrade
Plan change is one of trickiest billing operations. Stripe handles proportional calculations, but business logic (what happens to data on downgrade) is on developer side.
Upgrade: Immediate Update
export async function upgradeSubscription(
tenantId: string,
newPriceId: string
): Promise<void> {
const subscription = await db.subscription.findUniqueOrThrow({
where: { tenantId }
});
// Stripe auto-recalculates
// Example: 20 days left of 30, upgrade from $29 to $99
// Charge = (99 - 29) * 20/30 = $46.67 immediately
const updatedSub = await stripe.subscriptions.update(
subscription.stripeSubscriptionId!,
{
items: [{
id: (await stripe.subscriptions.retrieve(
subscription.stripeSubscriptionId!
)).items.data[0].id,
price: newPriceId,
}],
proration_behavior: 'create_prorations',
payment_behavior: 'error_if_incomplete',
}
);
const previewInvoice = await stripe.invoices.retrieveUpcoming({
customer: subscription.stripeCustomerId,
subscription: subscription.stripeSubscriptionId!,
subscription_items: [{
id: updatedSub.items.data[0].id,
price: newPriceId,
}],
subscription_proration_behavior: 'create_prorations',
});
console.log('Charge now:', previewInvoice.amount_due / 100);
}
Preview Amount for UI
export async function POST(request: Request) {
const { newPriceId } = await request.json();
const tenant = await getCurrentTenant();
const subscription = await db.subscription.findUnique({
where: { tenantId: tenant!.id }
});
const preview = await stripe.invoices.retrieveUpcoming({
customer: subscription!.stripeCustomerId,
subscription: subscription!.stripeSubscriptionId!,
subscription_items: [{
id: (await stripe.subscriptions.retrieve(
subscription!.stripeSubscriptionId!
)).items.data[0].id,
price: newPriceId,
}],
});
return Response.json({
amountDue: preview.amount_due / 100,
currency: preview.currency,
periodEnd: new Date(preview.period_end * 1000),
});
}
Downgrade: At Period End
export async function scheduleDowngrade(
tenantId: string,
newPriceId: string
): Promise<void> {
const subscription = await db.subscription.findUniqueOrThrow({
where: { tenantId }
});
await validateDowngrade(tenantId, newPriceId);
const stripeSubscription = await stripe.subscriptions.retrieve(
subscription.stripeSubscriptionId!
);
await stripe.subscriptions.update(subscription.stripeSubscriptionId!, {
items: [{
id: stripeSubscription.items.data[0].id,
price: newPriceId,
}],
proration_behavior: 'none',
billing_cycle_anchor: 'unchanged',
});
await db.subscription.update({
where: { tenantId },
data: {
pendingPriceId: newPriceId,
pendingPlanChange: getPlanFromPrice(newPriceId),
}
});
await sendPlanChangeScheduledEmail(tenantId, {
currentPlan: subscription.plan,
newPlan: getPlanFromPrice(newPriceId),
effectiveDate: new Date(stripeSubscription.current_period_end * 1000),
});
}
Downgrade Validation
export async function validateDowngrade(
tenantId: string,
newPriceId: string
): Promise<void> {
const newPlan = getPlanFromPrice(newPriceId);
const limits = PLAN_LIMITS[newPlan];
const [projectCount, memberCount, storageGb] = await Promise.all([
db.project.count({ where: { tenantId } }),
db.tenantUser.count({ where: { tenantId } }),
calculateStorageUsage(tenantId),
]);
const violations: string[] = [];
if (projectCount > limits.projects) {
violations.push(
`You have ${projectCount} projects. Limit for ${newPlan}: ${limits.projects}. ` +
`Delete ${projectCount - limits.projects} projects.`
);
}
if (memberCount > limits.members) {
violations.push(
`You have ${memberCount} members. Limit for ${newPlan}: ${limits.members}.`
);
}
if (storageGb > limits.storageGb) {
violations.push(
`Used ${storageGb.toFixed(1)} GB. Limit for ${newPlan}: ${limits.storageGb} GB.`
);
}
if (violations.length > 0) {
throw new PlanDowngradeError(violations);
}
}
UI: Plan Change Page
export function PlanChangeModal({
currentPlan,
targetPlan,
previewAmount,
isUpgrade,
onConfirm,
}: PlanChangeModalProps) {
return (
<Dialog>
<DialogHeader>
<DialogTitle>
{isUpgrade ? 'Upgrade' : 'Change'} plan: {currentPlan} → {targetPlan}
</DialogTitle>
</DialogHeader>
{isUpgrade ? (
<div>
<p>${previewAmount} will be charged from your card immediately.</p>
<p>This is proportional payment for the remaining period.</p>
</div>
) : (
<div>
<p>Current plan active until end of billing period.</p>
<p>After that switch to <strong>{targetPlan}</strong>.</p>
{targetPlan === 'FREE' && (
<Alert>Check limits: {targetPlan} plan supports up to 3 projects.</Alert>
)}
</div>
)}
<DialogFooter>
<Button variant="outline" onClick={onClose}>Cancel</Button>
<Button onClick={onConfirm}>
{isUpgrade ? 'Upgrade and Pay' : 'Confirm Plan Change'}
</Button>
</DialogFooter>
</Dialog>
);
}
Upgrade/downgrade implementation with Stripe proreration, validation and UI — 2–3 working days.







