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.

reactivaiton code fixed

RajuGangitlaea76d07fb816
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 56fcd1f70a..6920cc327b 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -16,46 +16,49 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
lookup_keys: [`${plan}_${period}`],
});
- const subscription = workspace.stripeId
+ const subscriptions = workspace.stripeId
? await stripe.subscriptions.list({
- customer: workspace.stripeId,
- status: "active",
- })
+ customer: workspace.stripeId,
+ status: "all",
+ })
: null;
// if the user already has a subscription, create billing portal to upgrade
- if (workspace.stripeId && subscription && subscription.data.length > 0) {
- const { url } = await stripe.billingPortal.sessions.create({
- customer: workspace.stripeId,
- return_url: baseUrl,
- flow_data: comparePlans
- ? {
+ 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: subscription.data[0].id,
+ subscription: activeSubscription.id,
},
}
- : {
+ : {
type: "subscription_update_confirm",
subscription_update_confirm: {
- subscription: subscription.data[0].id,
+ subscription: activeSubscription.id,
items: [
{
- id: subscription.data[0].items.data[0].id,
+ id: activeSubscription.items.data[0].id,
quantity: 1,
price: prices.data[0].id,
},
],
},
},
- });
-
- return NextResponse.json({ url });
-
- // if the user does not have a subscription, create a new checkout session
+ });
+ return NextResponse.json({ url });
+ }
} else {
+ // For both new users and users with canceled subscriptions
const stripeSession = await stripe.checkout.sessions.create({
- customer_email: session.user.email,
+ ...(workspace.stripeId ? { customer: workspace.stripeId } : { 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,