mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
feat: add free tier Stripe subscription
This commit is contained in:
@ -0,0 +1,80 @@
|
||||
'use server';
|
||||
|
||||
import {
|
||||
getStripeCustomerByEmail,
|
||||
getStripeCustomerById,
|
||||
} from '@documenso/ee/server-only/stripe/get-customer';
|
||||
import { getPortalSession } from '@documenso/ee/server-only/stripe/get-portal-session';
|
||||
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 existingSubscription = await getSubscriptionByUserId({ userId: user.id });
|
||||
|
||||
let stripeCustomer: Stripe.Customer | null = null;
|
||||
|
||||
// Find the Stripe customer for the current user subscription.
|
||||
if (existingSubscription) {
|
||||
stripeCustomer = await getStripeCustomerById(existingSubscription.customerId);
|
||||
|
||||
if (!stripeCustomer) {
|
||||
throw new Error('Missing Stripe customer for subscription');
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to check if a Stripe customer already exists for the current user.
|
||||
stripeCustomer = await getStripeCustomerByEmail(user.email);
|
||||
|
||||
// Create a Stripe customer if it does not exist for the current user.
|
||||
if (!stripeCustomer) {
|
||||
stripeCustomer = await stripe.customers.create({
|
||||
name: user.name ?? undefined,
|
||||
email: user.email,
|
||||
metadata: {
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const stripeCustomerSubsriptions = stripeCustomer.subscriptions?.data ?? [];
|
||||
|
||||
// Create a free subscription for user if it does not exist.
|
||||
if (!existingSubscription && stripeCustomerSubsriptions.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_APP_URL}/settings/billing`,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user