✨ (billing) Implement custom plan
This commit is contained in:
committed by
Baptiste Arnaud
parent
3f7dc79918
commit
385853ca3c
61
apps/builder/pages/api/stripe/custom-plan-checkout.ts
Normal file
61
apps/builder/pages/api/stripe/custom-plan-checkout.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import { withSentry } from '@sentry/nextjs'
|
||||
import { Plan } from 'db'
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getAuthenticatedUser } from 'services/api/utils'
|
||||
import Stripe from 'stripe'
|
||||
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const user = await getAuthenticatedUser(req)
|
||||
if (!user) return notAuthenticated(res)
|
||||
if (req.method === 'GET') {
|
||||
const session = await createCheckoutSession(user.id)
|
||||
if (!session?.url) return res.redirect('/typebots')
|
||||
return res.redirect(session.url)
|
||||
}
|
||||
|
||||
return methodNotAllowed(res)
|
||||
}
|
||||
|
||||
const createCheckoutSession = async (userId: string) => {
|
||||
if (!process.env.STRIPE_SECRET_KEY)
|
||||
throw Error('STRIPE_SECRET_KEY var is missing')
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
||||
apiVersion: '2022-08-01',
|
||||
})
|
||||
|
||||
const claimableCustomPlan = await prisma.claimableCustomPlan.findFirst({
|
||||
where: { workspace: { members: { some: { userId } } } },
|
||||
})
|
||||
|
||||
if (!claimableCustomPlan) return null
|
||||
|
||||
return stripe.checkout.sessions.create({
|
||||
success_url: `${process.env.NEXTAUTH_URL}/typebots?stripe=${Plan.CUSTOM}&success=true`,
|
||||
cancel_url: `${process.env.NEXTAUTH_URL}/typebots?stripe=cancel`,
|
||||
mode: 'subscription',
|
||||
metadata: {
|
||||
claimableCustomPlanId: claimableCustomPlan.id,
|
||||
},
|
||||
currency: claimableCustomPlan.currency,
|
||||
automatic_tax: { enabled: true },
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: claimableCustomPlan.currency,
|
||||
tax_behavior: 'exclusive',
|
||||
recurring: { interval: 'month' },
|
||||
product_data: {
|
||||
name: claimableCustomPlan.name,
|
||||
description: claimableCustomPlan.description ?? undefined,
|
||||
},
|
||||
unit_amount: claimableCustomPlan.price * 100,
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export default withSentry(handler)
|
Reference in New Issue
Block a user