Add e2e tests for account
This commit is contained in:
32
apps/builder/pages/api/stripe/customer-portal.ts
Normal file
32
apps/builder/pages/api/stripe/customer-portal.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { User } from 'db'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
import { methodNotAllowed } from 'services/api/utils'
|
||||
import Stripe from 'stripe'
|
||||
|
||||
const createCheckoutSession = async (
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) => {
|
||||
const session = await getSession({ req })
|
||||
if (!session?.user)
|
||||
return res.status(401).json({ message: 'Not authenticated' })
|
||||
const user = session.user as User
|
||||
if (!user.stripeId)
|
||||
return res.status(401).json({ message: 'Not authenticated' })
|
||||
if (req.method === 'GET') {
|
||||
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 session = await stripe.billingPortal.sessions.create({
|
||||
customer: user.stripeId,
|
||||
return_url: `${req.headers.origin}/account`,
|
||||
})
|
||||
res.status(201).redirect(session.url)
|
||||
}
|
||||
return methodNotAllowed(res)
|
||||
}
|
||||
|
||||
export default createCheckoutSession
|
Reference in New Issue
Block a user