The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
return 400 when billing upgrade has no stripe subscription changes
Pedro Ladeirafdef602e0c49
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.tsindex 8a118c2d8f..2e3f10b984 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts@@ -84,24 +84,52 @@ export const POST = withWorkspace(}// Active subscriptions: use the billing portal's plan-change confirmation flow.- const { url } = await stripe.billingPortal.sessions.create({- customer: workspace.stripeId,- 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,- },- ],+ 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.",+ });+ }++ try {+ const { url } = await stripe.billingPortal.sessions.create({+ customer: workspace.stripeId,+ 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);