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

34 lines
1.1 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
const usdPriceIdTest = 'price_1Jc4TQKexUFvKTWyGvsH4Ff5'
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',
})
const { email } = req.body
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,
line_items: [
{
price: usdPriceIdTest,
quantity: 1,
},
],
})
res.status(201).send({ sessionId: session.id })
2021-12-27 15:59:32 +01:00
}
return methodNotAllowed(res)
}
2022-02-14 10:57:57 +01:00
export default withSentry(handler)