2
0
Files
bot/apps/builder/pages/api/stripe/checkout.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-12-27 15:59:32 +01:00
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils'
2021-12-27 15:59:32 +01:00
import Stripe from 'stripe'
2022-02-14 10:57:57 +01:00
import { withSentry } from '@sentry/nextjs'
2021-12-27 15:59:32 +01:00
2022-02-14 10:57:57 +01:00
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
2021-12-27 15:59:32 +01:00
if (req.method === 'POST') {
if (!process.env.STRIPE_SECRET_KEY)
throw Error('STRIPE_SECRET_KEY var is missing')
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27',
})
2022-05-13 15:22:44 -07:00
const { email, currency, plan, workspaceId } =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
2022-05-13 15:22:44 -07:00
console.log(plan, workspaceId)
2021-12-27 15:59:32 +01:00
const session = await stripe.checkout.sessions.create({
success_url: `${req.headers.origin}/typebots?stripe=success`,
cancel_url: `${req.headers.origin}/typebots?stripe=cancel`,
automatic_tax: { enabled: true },
allow_promotion_codes: true,
customer_email: email,
mode: 'subscription',
2022-05-13 15:22:44 -07:00
metadata: { workspaceId, plan },
2021-12-27 15:59:32 +01:00
line_items: [
{
2022-05-13 15:22:44 -07:00
price: getPrice(plan, currency),
2021-12-27 15:59:32 +01:00
quantity: 1,
},
],
})
return res.status(201).send({ sessionId: session.id })
2021-12-27 15:59:32 +01:00
}
return methodNotAllowed(res)
}
2022-05-13 15:22:44 -07:00
const getPrice = (plan: 'pro' | 'team', currency: 'eur' | 'usd') => {
if (plan === 'team')
return currency === 'eur'
? process.env.STRIPE_PRICE_TEAM_EUR_ID
: process.env.STRIPE_PRICE_TEAM_USD_ID
return currency === 'eur'
? process.env.STRIPE_PRICE_EUR_ID
: process.env.STRIPE_PRICE_USD_ID
}
2022-02-14 10:57:57 +01:00
export default withSentry(handler)