Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Merge branch 'main' into billing-upgrade-noop-400

Steven Tey6448f3f47de9
diff --cc apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index 2e3f10b984,9b18d768ec..168b086ed5
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@@ -83,53 -78,30 +78,58 @@@ export const POST = withWorkspace
return NextResponse.json({ url: successUrl.toString() });
}
+ // Active subscriptions: use the billing portal's plan-change confirmation flow.
+ const currentItem = existingSubscription.items.data[0];
+ const targetPriceId = prices.data[0].id;
+ const alreadyOnTargetPlan =
+ currentItem.price.id === targetPriceId &&
+ (currentItem.quantity ?? 1) === 1;
+
+ if (alreadyOnTargetPlan) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "You're already on this plan. Refresh the page or choose a different plan.",
+ });
+ }
+
+ const flags = await getFeatureFlags({ workspaceId: workspace.id });
+
- // Active subscriptions: use the billing portal's plan-change confirmation flow.
- const { url } = await stripe.billingPortal.sessions.create({
- customer: workspace.stripeId,
- ...(flags.noProrationUpgrade
- ? { configuration: "bpc_1TyLVFAlJJEpqkPVbJdDVr4m" }
- : {}),
- return_url: baseUrl,
- flow_data: {
- type: "subscription_update_confirm",
- subscription_update_confirm: {
- subscription: existingSubscription.id,
- items: [
- {
- id: existingSubscription.items.data[0].id,
- quantity: 1,
- price: prices.data[0].id,
- },
- ],
+ try {
+ const { url } = await stripe.billingPortal.sessions.create({
+ customer: workspace.stripeId,
++ ...(flags.noProrationUpgrade
++ ? { configuration: "bpc_1TyLVFAlJJEpqkPVbJdDVr4m" }
++ : {}),
+ return_url: baseUrl,
+ flow_data: {
+ type: "subscription_update_confirm",
+ subscription_update_confirm: {
+ subscription: existingSubscription.id,
+ items: [
+ {
+ id: currentItem.id,
+ quantity: 1,
+ price: targetPriceId,
+ },
+ ],
+ },
},
- },
- });
- return NextResponse.json({ url });
+ });
+ return NextResponse.json({ url });
+ } catch (error) {
+ if (
+ error instanceof Error &&
+ error.message.includes("no changes to confirm")
+ ) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "You're already on this plan. Refresh the page or choose a different plan.",
+ });
+ }
+ throw error;
+ }
} else {
const customer = await getDubCustomer(session.user.id);