Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Implement trial handling in subscription update and preview

Bill Yang323ffa9614b3
diff --git a/client/src/components/subscription/PaidPlain/PlanChangePreviewDialog.tsx b/client/src/components/subscription/PaidPlain/PlanChangePreviewDialog.tsx
index 321ebab6d..bdc1c5455 100644
--- a/client/src/components/subscription/PaidPlain/PlanChangePreviewDialog.tsx
+++ b/client/src/components/subscription/PaidPlain/PlanChangePreviewDialog.tsx
@@ -3,6 +3,7 @@ import { Button } from "@/components/ui/button";
import { Loader2 } from "lucide-react";
interface PreviewData {
+ isTrialing?: boolean;
currentPlan: {
priceId: string;
amount: number;
@@ -63,33 +64,45 @@ export function PlanChangePreviewDialog({
</div>
</div>
- {/* Proration Details */}
- <div className="pt-1">
- {previewData.proration.charge > 0 && (
- <div className="flex justify-between text-sm mb-2">
- <span className="text-neutral-500 dark:text-neutral-400">New plan charge (prorated)</span>
- <span className="text-neutral-900 dark:text-neutral-100">${previewData.proration.charge.toFixed(2)}</span>
- </div>
- )}
- {previewData.proration.credit > 0 && (
- <div className="flex justify-between text-sm mb-2">
- <span className="text-neutral-500 dark:text-neutral-400">Unused time credit</span>
- <span className="text-emerald-400">-${previewData.proration.credit.toFixed(2)}</span>
- </div>
- )}
-
- <div className="flex justify-between text-sm font-medium pt-2">
- <span className="text-neutral-900 dark:text-neutral-100">Due now</span>
- <span className={previewData.proration.immediatePayment > 0 ? "text-neutral-900 dark:text-neutral-100" : "text-emerald-400"}>
- ${previewData.proration.immediatePayment.toFixed(2)}
- </span>
+ {previewData.isTrialing ? (
+ <div className="text-sm text-neutral-500 dark:text-neutral-400 pt-1">
+ Your trial will continue with the new plan. You won't be charged until your trial ends
+ {previewData.proration.nextBillingDate && (
+ <> on {new Date(previewData.proration.nextBillingDate).toLocaleDateString()}</>
+ )}
+ .
</div>
- </div>
+ ) : (
+ <>
+ {/* Proration Details */}
+ <div className="pt-1">
+ {previewData.proration.charge > 0 && (
+ <div className="flex justify-between text-sm mb-2">
+ <span className="text-neutral-500 dark:text-neutral-400">New plan charge (prorated)</span>
+ <span className="text-neutral-900 dark:text-neutral-100">${previewData.proration.charge.toFixed(2)}</span>
+ </div>
+ )}
+ {previewData.proration.credit > 0 && (
+ <div className="flex justify-between text-sm mb-2">
+ <span className="text-neutral-500 dark:text-neutral-400">Unused time credit</span>
+ <span className="text-emerald-400">-${previewData.proration.credit.toFixed(2)}</span>
+ </div>
+ )}
- {previewData.proration.nextBillingDate && (
- <div className="text-xs text-neutral-500 dark:text-neutral-400">
- Your next billing date: {new Date(previewData.proration.nextBillingDate).toLocaleDateString()}
- </div>
+ <div className="flex justify-between text-sm font-medium pt-2">
+ <span className="text-neutral-900 dark:text-neutral-100">Due now</span>
+ <span className={previewData.proration.immediatePayment > 0 ? "text-neutral-900 dark:text-neutral-100" : "text-emerald-400"}>
+ ${previewData.proration.immediatePayment.toFixed(2)}
+ </span>
+ </div>
+ </div>
+
+ {previewData.proration.nextBillingDate && (
+ <div className="text-xs text-neutral-500 dark:text-neutral-400">
+ Your next billing date: {new Date(previewData.proration.nextBillingDate).toLocaleDateString()}
+ </div>
+ )}
+ </>
)}
{/* Action Buttons */}
diff --git a/server/src/api/stripe/previewSubscriptionUpdate.ts b/server/src/api/stripe/previewSubscriptionUpdate.ts
index 709ecf702..b70369a27 100644
--- a/server/src/api/stripe/previewSubscriptionUpdate.ts
+++ b/server/src/api/stripe/previewSubscriptionUpdate.ts
@@ -78,7 +78,35 @@ export async function previewSubscriptionUpdate(
stripe!.prices.retrieve(newPriceId),
]);
- // 5. Create a preview of the upcoming invoice with proration
+ const isTrialing = subscription.status === "trialing";
+
+ // 5. For trialing subscriptions, no proration is needed — just return plan details
+ if (isTrialing) {
+ return reply.send({
+ success: true,
+ preview: {
+ isTrialing: true,
+ currentPlan: {
+ priceId: currentItem.price.id,
+ amount: currentPrice.unit_amount || 0,
+ interval: currentPrice.recurring?.interval || "month",
+ },
+ newPlan: {
+ priceId: newPriceId,
+ amount: newPrice.unit_amount || 0,
+ interval: newPrice.recurring?.interval || "month",
+ },
+ proration: {
+ credit: 0,
+ charge: 0,
+ immediatePayment: 0,
+ nextBillingDate: currentPeriodEnd ? new Date(currentPeriodEnd * 1000).toISOString() : null,
+ },
+ },
+ });
+ }
+
+ // 6. Create a preview of the upcoming invoice with proration
const upcomingInvoice = await (stripe as Stripe).invoices.createPreview({
customer: org.stripeCustomerId,
subscription: subscription.id,
@@ -93,7 +121,7 @@ export async function previewSubscriptionUpdate(
},
});
- // 6. Calculate proration details
+ // 7. Calculate proration details
const prorationItems = upcomingInvoice.lines.data.filter(item => {
// Proration flag is nested in parent.subscription_item_details
return item.parent?.subscription_item_details?.proration === true;
@@ -112,10 +140,11 @@ export async function previewSubscriptionUpdate(
const immediateCharge = upcomingInvoice.amount_due;
- // 7. Return preview information
+ // 8. Return preview information
return reply.send({
success: true,
preview: {
+ isTrialing: false,
currentPlan: {
priceId: currentItem.price.id,
amount: currentPrice.unit_amount || 0,
diff --git a/server/src/api/stripe/updateSubscription.ts b/server/src/api/stripe/updateSubscription.ts
index 126f80d3f..5fcf694ab 100644
--- a/server/src/api/stripe/updateSubscription.ts
+++ b/server/src/api/stripe/updateSubscription.ts
@@ -79,7 +79,7 @@ export async function updateSubscription(
}
// 5. Update the subscription with the new price
- // Using always_invoice to charge immediately for the proration
+ const isTrialing = subscription.status === "trialing";
const updatedSubscription = await (stripe as Stripe).subscriptions.update(subscription.id, {
items: [
{
@@ -87,7 +87,9 @@ export async function updateSubscription(
price: newPriceId,
},
],
- proration_behavior: "always_invoice", // Immediately invoice the proration amount
+ // For trialing subscriptions, swap the plan without charging — preserve the trial
+ proration_behavior: isTrialing ? "none" : "always_invoice",
+ ...(isTrialing && subscription.trial_end && { trial_end: subscription.trial_end }),
});
// Get the updated subscription details