feat(editor): ✨ Team workspaces
This commit is contained in:
@ -10,8 +10,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
||||
apiVersion: '2020-08-27',
|
||||
})
|
||||
const { email, currency } =
|
||||
const { email, currency, plan, workspaceId } =
|
||||
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
||||
|
||||
console.log(plan, workspaceId)
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
success_url: `${req.headers.origin}/typebots?stripe=success`,
|
||||
cancel_url: `${req.headers.origin}/typebots?stripe=cancel`,
|
||||
@ -19,12 +21,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
allow_promotion_codes: true,
|
||||
customer_email: email,
|
||||
mode: 'subscription',
|
||||
metadata: { workspaceId, plan },
|
||||
line_items: [
|
||||
{
|
||||
price:
|
||||
currency === 'eur'
|
||||
? process.env.STRIPE_PRICE_EUR_ID
|
||||
: process.env.STRIPE_PRICE_USD_ID,
|
||||
price: getPrice(plan, currency),
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
@ -34,4 +34,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
return methodNotAllowed(res)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
export default withSentry(handler)
|
||||
|
@ -1,25 +1,36 @@
|
||||
import { User } from 'db'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
import { methodNotAllowed } from 'utils'
|
||||
import {
|
||||
badRequest,
|
||||
forbidden,
|
||||
methodNotAllowed,
|
||||
notAuthenticated,
|
||||
} from 'utils'
|
||||
import Stripe from 'stripe'
|
||||
import { withSentry } from '@sentry/nextjs'
|
||||
import { getAuthenticatedUser } from 'services/api/utils'
|
||||
import prisma from 'libs/prisma'
|
||||
import { WorkspaceRole } from 'db'
|
||||
|
||||
const handler = 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' })
|
||||
const user = await getAuthenticatedUser(req)
|
||||
if (!user) return notAuthenticated(res)
|
||||
if (req.method === 'GET') {
|
||||
const workspaceId = req.query.workspaceId as string | undefined
|
||||
if (!workspaceId) return badRequest(res)
|
||||
if (!process.env.STRIPE_SECRET_KEY)
|
||||
throw Error('STRIPE_SECRET_KEY var is missing')
|
||||
const workspace = await prisma.workspace.findFirst({
|
||||
where: {
|
||||
id: workspaceId,
|
||||
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
|
||||
},
|
||||
})
|
||||
if (!workspace?.stripeId) return forbidden(res)
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
||||
apiVersion: '2020-08-27',
|
||||
})
|
||||
const session = await stripe.billingPortal.sessions.create({
|
||||
customer: user.stripeId,
|
||||
customer: workspace.stripeId,
|
||||
return_url: req.headers.referer,
|
||||
})
|
||||
res.redirect(session.url)
|
||||
|
@ -40,36 +40,44 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
switch (event.type) {
|
||||
case 'checkout.session.completed': {
|
||||
const session = event.data.object as Stripe.Checkout.Session
|
||||
const { customer_email } = session
|
||||
if (!customer_email)
|
||||
return res.status(500).send(`customer_email not found`)
|
||||
await prisma.user.update({
|
||||
where: { email: customer_email },
|
||||
data: { plan: Plan.PRO, stripeId: session.customer as string },
|
||||
const { metadata } = session
|
||||
if (!metadata?.workspaceId || !metadata?.plan)
|
||||
return res.status(500).send({ message: `customer_email not found` })
|
||||
await prisma.workspace.update({
|
||||
where: { id: metadata.workspaceId },
|
||||
data: {
|
||||
plan: metadata.plan === 'team' ? Plan.TEAM : Plan.PRO,
|
||||
stripeId: session.customer as string,
|
||||
},
|
||||
})
|
||||
return res.status(200).send({ message: 'user upgraded in DB' })
|
||||
return res.status(200).send({ message: 'workspace upgraded in DB' })
|
||||
}
|
||||
case 'customer.subscription.deleted': {
|
||||
const subscription = event.data.object as Stripe.Subscription
|
||||
await prisma.user.update({
|
||||
const { metadata } = subscription
|
||||
if (!metadata.workspaceId)
|
||||
return res.status(500).send(`workspaceId not found`)
|
||||
await prisma.workspace.update({
|
||||
where: {
|
||||
stripeId: subscription.customer as string,
|
||||
id: metadata.workspaceId,
|
||||
},
|
||||
data: {
|
||||
plan: Plan.FREE,
|
||||
},
|
||||
})
|
||||
return res.status(200).send({ message: 'user downgraded in DB' })
|
||||
return res.send({ message: 'workspace downgraded in DB' })
|
||||
}
|
||||
default: {
|
||||
return res.status(304).send({ message: 'event not handled' })
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
if (err instanceof Error) {
|
||||
console.error(err)
|
||||
return res.status(400).send(`Webhook Error: ${err.message}`)
|
||||
}
|
||||
return res.status(500).send(`Error occured: ${err}`)
|
||||
}
|
||||
}
|
||||
return methodNotAllowed(res)
|
||||
|
Reference in New Issue
Block a user