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.

Update route.ts

Steven Tey10b065ad4de6
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index e262df1fa2..e1a0b05063 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -4,7 +4,7 @@ import { APP_DOMAIN } from "@dub/utils";
import { NextResponse } from "next/server";
export const POST = withWorkspace(async ({ req, workspace, session }) => {
- let { plan, period, baseUrl, comparePlans, onboarding } = await req.json();
+ let { plan, period, baseUrl, onboarding } = await req.json();
if (!plan || !period) {
return new Response("Invalid plan or period", { status: 400 });
@@ -16,60 +16,50 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
lookup_keys: [`${plan}_${period}`],
});
- const subscriptions = workspace.stripeId
- ? await stripe.subscriptions.list({
- customer: workspace.stripeId,
- status: "all",
- })
+ const activeSubscription = workspace.stripeId
+ ? await stripe.subscriptions
+ .list({
+ customer: workspace.stripeId,
+ status: "active",
+ })
+ .then((res) => res.data[0])
: null;
- // if the user already has a subscription, create billing portal to upgrade
- if (workspace.stripeId && subscriptions && subscriptions?.data.some(sub => sub.status === "active")) {
-
- const activeSubscription = subscriptions.data.find(sub => sub.status === "active");
-
- if (activeSubscription) {
- const { url } = await stripe.billingPortal.sessions.create({
- customer: workspace.stripeId,
- return_url: baseUrl,
- flow_data: comparePlans
- ? {
- type: "subscription_update",
- subscription_update: {
- subscription: activeSubscription.id,
- },
- }
- : {
- type: "subscription_update_confirm",
- subscription_update_confirm: {
- subscription: activeSubscription.id,
- items: [
- {
- id: activeSubscription.items.data[0].id,
- quantity: 1,
- price: prices.data[0].id,
- },
- ],
+ // if the user has an active subscription, create billing portal to upgrade
+ if (workspace.stripeId && activeSubscription) {
+ const { url } = await stripe.billingPortal.sessions.create({
+ customer: workspace.stripeId,
+ return_url: baseUrl,
+ flow_data: {
+ type: "subscription_update_confirm",
+ subscription_update_confirm: {
+ subscription: activeSubscription.id,
+ items: [
+ {
+ id: activeSubscription.items.data[0].id,
+ quantity: 1,
+ price: prices.data[0].id,
},
- },
- });
- return NextResponse.json({ url });
- }
+ ],
+ },
+ },
+ });
+ return NextResponse.json({ url });
} else {
// For both new users and users with canceled subscriptions
const stripeSession = await stripe.checkout.sessions.create({
...(workspace.stripeId
? {
- customer: workspace.stripeId,
- customer_update: {
- name: 'auto',
- address: 'auto',
- },
- }
+ customer: workspace.stripeId,
+ // need to pass this or Stripe will throw an error: https://git.new/kX4fi6B
+ customer_update: {
+ name: "auto",
+ address: "auto",
+ },
+ }
: {
- customer_email: session.user.email
- }
- ),
+ customer_email: session.user.email,
+ }),
billing_address_collection: "required",
success_url: `${APP_DOMAIN}/${workspace.slug}?${onboarding ? "onboarded" : "upgraded"}=true&plan=${plan}&period=${period}`,
cancel_url: baseUrl,