mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
fix: create custom pricing table
This commit is contained in:
@ -8,10 +8,9 @@ import { getPortalSession } from '@documenso/ee/server-only/stripe/get-portal-se
|
||||
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-session';
|
||||
import { Stripe, stripe } from '@documenso/lib/server-only/stripe';
|
||||
import { getSubscriptionByUserId } from '@documenso/lib/server-only/subscription/get-subscription-by-user-id';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export const createBillingPortal = async () => {
|
||||
const user = await getRequiredServerComponentSession();
|
||||
const { user } = await getRequiredServerComponentSession();
|
||||
|
||||
const existingSubscription = await getSubscriptionByUserId({ userId: user.id });
|
||||
|
||||
@ -42,39 +41,6 @@ export const createBillingPortal = async () => {
|
||||
});
|
||||
}
|
||||
|
||||
const stripeCustomerSubscriptions = stripeCustomer.subscriptions?.data ?? [];
|
||||
|
||||
// Create a free subscription for user if it does not exist.
|
||||
if (!existingSubscription && stripeCustomerSubscriptions.length === 0) {
|
||||
const newSubscription = await stripe.subscriptions.create({
|
||||
customer: stripeCustomer.id,
|
||||
items: [
|
||||
{
|
||||
plan: process.env.NEXT_PUBLIC_STRIPE_FREE_PLAN_ID,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await prisma.subscription.upsert({
|
||||
where: {
|
||||
userId: user.id,
|
||||
customerId: stripeCustomer.id,
|
||||
},
|
||||
create: {
|
||||
userId: user.id,
|
||||
customerId: stripeCustomer.id,
|
||||
planId: process.env.NEXT_PUBLIC_STRIPE_FREE_PLAN_ID,
|
||||
periodEnd: new Date(newSubscription.current_period_end * 1000),
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
update: {
|
||||
planId: process.env.NEXT_PUBLIC_STRIPE_FREE_PLAN_ID,
|
||||
periodEnd: new Date(newSubscription.current_period_end * 1000),
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return getPortalSession({
|
||||
customerId: stripeCustomer.id,
|
||||
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
||||
|
||||
Reference in New Issue
Block a user