2
0

🛂 (billing) Update claimable custom plan options

This commit is contained in:
Baptiste Arnaud
2023-04-28 10:55:15 +02:00
parent 5845e1cb8c
commit 458d715648
8 changed files with 211 additions and 69 deletions

View File

@ -21,7 +21,6 @@ export const createCheckoutSession = authenticatedProcedure
email: z.string(),
company: z.string(),
workspaceId: z.string(),
prefilledEmail: z.string().optional(),
currency: z.enum(['usd', 'eur']),
plan: z.enum([Plan.STARTER, Plan.PRO]),
returnUrl: z.string(),

View File

@ -0,0 +1,172 @@
import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { Plan, WorkspaceRole } from '@typebot.io/prisma'
import Stripe from 'stripe'
import { z } from 'zod'
export const createCustomCheckoutSession = authenticatedProcedure
.meta({
openapi: {
method: 'POST',
path: '/billing/subscription/custom-checkout',
protect: true,
summary:
'Create custom checkout session to make a workspace pay for a custom plan',
tags: ['Billing'],
},
})
.input(
z.object({
email: z.string(),
workspaceId: z.string(),
returnUrl: z.string(),
})
)
.output(
z.object({
checkoutUrl: z.string(),
})
)
.mutation(
async ({ input: { email, workspaceId, returnUrl }, ctx: { user } }) => {
if (!process.env.STRIPE_SECRET_KEY)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Stripe environment variables are missing',
})
const workspace = await prisma.workspace.findFirst({
where: {
id: workspaceId,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
include: {
claimableCustomPlan: true,
},
})
if (
!workspace?.claimableCustomPlan ||
workspace.claimableCustomPlan.claimedAt
)
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Custom plan not found',
})
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2022-11-15',
})
const vat =
workspace.claimableCustomPlan.vatValue &&
workspace.claimableCustomPlan.vatType
? ({
type: workspace.claimableCustomPlan.vatType,
value: workspace.claimableCustomPlan.vatValue,
} as Stripe.CustomerCreateParams.TaxIdDatum)
: undefined
const customer = await stripe.customers.create({
email,
name: workspace.claimableCustomPlan.companyName ?? workspace.name,
metadata: { workspaceId },
tax_id_data: vat ? [vat] : undefined,
})
const session = await stripe.checkout.sessions.create({
success_url: `${returnUrl}?stripe=${Plan.CUSTOM}&success=true`,
cancel_url: `${returnUrl}?stripe=cancel`,
allow_promotion_codes: true,
customer: customer.id,
customer_update: {
address: 'auto',
name: 'never',
},
mode: 'subscription',
metadata: {
claimableCustomPlanId: workspace.claimableCustomPlan.id,
userId: user.id,
},
currency: workspace.claimableCustomPlan.currency,
billing_address_collection: 'required',
automatic_tax: { enabled: true },
line_items: [
{
price_data: {
currency: workspace.claimableCustomPlan.currency,
tax_behavior: 'exclusive',
recurring: {
interval: workspace.claimableCustomPlan.isYearly
? 'year'
: 'month',
},
product_data: {
name: workspace.claimableCustomPlan.name,
description:
workspace.claimableCustomPlan.description ?? undefined,
},
unit_amount: workspace.claimableCustomPlan.price * 100,
},
quantity: 1,
},
{
price_data: {
currency: workspace.claimableCustomPlan.currency,
tax_behavior: 'exclusive',
recurring: {
interval: workspace.claimableCustomPlan.isYearly
? 'year'
: 'month',
},
product_data: {
name: 'Included chats per month',
},
unit_amount: 0,
},
quantity: workspace.claimableCustomPlan.chatsLimit,
},
{
price_data: {
currency: workspace.claimableCustomPlan.currency,
tax_behavior: 'exclusive',
recurring: {
interval: workspace.claimableCustomPlan.isYearly
? 'year'
: 'month',
},
product_data: {
name: 'Included storage per month',
},
unit_amount: 0,
},
quantity: workspace.claimableCustomPlan.storageLimit,
},
{
price_data: {
currency: workspace.claimableCustomPlan.currency,
tax_behavior: 'exclusive',
recurring: {
interval: workspace.claimableCustomPlan.isYearly
? 'year'
: 'month',
},
product_data: {
name: 'Included seats',
},
unit_amount: 0,
},
quantity: workspace.claimableCustomPlan.seatsLimit,
},
],
})
if (!session.url)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Stripe checkout session creation failed',
})
return {
checkoutUrl: session.url,
}
}
)

View File

@ -5,6 +5,7 @@ import { getSubscription } from './getSubscription'
import { getUsage } from './getUsage'
import { listInvoices } from './listInvoices'
import { updateSubscription } from './updateSubscription'
import { createCustomCheckoutSession } from './createCustomCheckoutSession'
export const billingRouter = router({
getBillingPortalUrl,
@ -13,4 +14,5 @@ export const billingRouter = router({
updateSubscription,
getSubscription,
getUsage,
createCustomCheckoutSession,
})

View File

@ -15,22 +15,39 @@ import { DashboardHeader } from './DashboardHeader'
import { FolderContent } from '@/features/folders/components/FolderContent'
import { TypebotDndProvider } from '@/features/folders/TypebotDndProvider'
import { ParentModalProvider } from '@/features/graph/providers/ParentModalProvider'
import { trpc } from '@/lib/trpc'
export const DashboardPage = () => {
const scopedT = useScopedI18n('dashboard')
const [isLoading, setIsLoading] = useState(false)
const { query } = useRouter()
const router = useRouter()
const { user } = useUser()
const { workspace } = useWorkspace()
const [preCheckoutPlan, setPreCheckoutPlan] =
useState<PreCheckoutModalProps['selectedSubscription']>()
const { mutate: createCustomCheckoutSession } =
trpc.billing.createCustomCheckoutSession.useMutation({
onSuccess: (data) => {
router.push(data.checkoutUrl)
},
})
useEffect(() => {
const { subscribePlan, chats, storage, isYearly } = query as {
subscribePlan: Plan | undefined
chats: string | undefined
storage: string | undefined
isYearly: string | undefined
const { subscribePlan, chats, storage, isYearly, claimCustomPlan } =
router.query as {
subscribePlan: Plan | undefined
chats: string | undefined
storage: string | undefined
isYearly: string | undefined
claimCustomPlan: string | undefined
}
if (claimCustomPlan && user?.email && workspace) {
setIsLoading(true)
createCustomCheckoutSession({
email: user.email,
workspaceId: workspace.id,
returnUrl: `${window.location.origin}/typebots`,
})
}
if (workspace && subscribePlan && user && workspace.plan === 'FREE') {
setIsLoading(true)
@ -43,7 +60,7 @@ export const DashboardPage = () => {
isYearly: isYearly === 'false' ? false : true,
})
}
}, [query, user, workspace])
}, [createCustomCheckoutSession, router.query, user, workspace])
return (
<Stack minH="100vh">

View File

@ -1,61 +0,0 @@
import { Plan } from '@typebot.io/prisma'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
import Stripe from 'stripe'
import { methodNotAllowed, notAuthenticated } from '@typebot.io/lib/api'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req, res)
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-11-15',
})
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,
userId,
},
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 handler

View File

@ -319,6 +319,10 @@ model ClaimableCustomPlan {
chatsLimit Int
storageLimit Int
seatsLimit Int
isYearly Boolean @default(false)
companyName String?
vatType String?
vatValue String?
}
model ChatSession {

View File

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "ClaimableCustomPlan" ADD COLUMN "companyName" TEXT,
ADD COLUMN "isYearly" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "vatType" TEXT,
ADD COLUMN "vatValue" TEXT;

View File

@ -298,6 +298,10 @@ model ClaimableCustomPlan {
chatsLimit Int
storageLimit Int
seatsLimit Int
isYearly Boolean @default(false)
companyName String?
vatType String?
vatValue String?
}
model ChatSession {