🛂 (billing) Update claimable custom plan options
This commit is contained in:
@@ -21,7 +21,6 @@ export const createCheckoutSession = authenticatedProcedure
|
|||||||
email: z.string(),
|
email: z.string(),
|
||||||
company: z.string(),
|
company: z.string(),
|
||||||
workspaceId: z.string(),
|
workspaceId: z.string(),
|
||||||
prefilledEmail: z.string().optional(),
|
|
||||||
currency: z.enum(['usd', 'eur']),
|
currency: z.enum(['usd', 'eur']),
|
||||||
plan: z.enum([Plan.STARTER, Plan.PRO]),
|
plan: z.enum([Plan.STARTER, Plan.PRO]),
|
||||||
returnUrl: z.string(),
|
returnUrl: z.string(),
|
||||||
|
|||||||
@@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -5,6 +5,7 @@ import { getSubscription } from './getSubscription'
|
|||||||
import { getUsage } from './getUsage'
|
import { getUsage } from './getUsage'
|
||||||
import { listInvoices } from './listInvoices'
|
import { listInvoices } from './listInvoices'
|
||||||
import { updateSubscription } from './updateSubscription'
|
import { updateSubscription } from './updateSubscription'
|
||||||
|
import { createCustomCheckoutSession } from './createCustomCheckoutSession'
|
||||||
|
|
||||||
export const billingRouter = router({
|
export const billingRouter = router({
|
||||||
getBillingPortalUrl,
|
getBillingPortalUrl,
|
||||||
@@ -13,4 +14,5 @@ export const billingRouter = router({
|
|||||||
updateSubscription,
|
updateSubscription,
|
||||||
getSubscription,
|
getSubscription,
|
||||||
getUsage,
|
getUsage,
|
||||||
|
createCustomCheckoutSession,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -15,22 +15,39 @@ import { DashboardHeader } from './DashboardHeader'
|
|||||||
import { FolderContent } from '@/features/folders/components/FolderContent'
|
import { FolderContent } from '@/features/folders/components/FolderContent'
|
||||||
import { TypebotDndProvider } from '@/features/folders/TypebotDndProvider'
|
import { TypebotDndProvider } from '@/features/folders/TypebotDndProvider'
|
||||||
import { ParentModalProvider } from '@/features/graph/providers/ParentModalProvider'
|
import { ParentModalProvider } from '@/features/graph/providers/ParentModalProvider'
|
||||||
|
import { trpc } from '@/lib/trpc'
|
||||||
|
|
||||||
export const DashboardPage = () => {
|
export const DashboardPage = () => {
|
||||||
const scopedT = useScopedI18n('dashboard')
|
const scopedT = useScopedI18n('dashboard')
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
const { query } = useRouter()
|
const router = useRouter()
|
||||||
const { user } = useUser()
|
const { user } = useUser()
|
||||||
const { workspace } = useWorkspace()
|
const { workspace } = useWorkspace()
|
||||||
const [preCheckoutPlan, setPreCheckoutPlan] =
|
const [preCheckoutPlan, setPreCheckoutPlan] =
|
||||||
useState<PreCheckoutModalProps['selectedSubscription']>()
|
useState<PreCheckoutModalProps['selectedSubscription']>()
|
||||||
|
const { mutate: createCustomCheckoutSession } =
|
||||||
|
trpc.billing.createCustomCheckoutSession.useMutation({
|
||||||
|
onSuccess: (data) => {
|
||||||
|
router.push(data.checkoutUrl)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const { subscribePlan, chats, storage, isYearly } = query as {
|
const { subscribePlan, chats, storage, isYearly, claimCustomPlan } =
|
||||||
subscribePlan: Plan | undefined
|
router.query as {
|
||||||
chats: string | undefined
|
subscribePlan: Plan | undefined
|
||||||
storage: string | undefined
|
chats: string | undefined
|
||||||
isYearly: 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') {
|
if (workspace && subscribePlan && user && workspace.plan === 'FREE') {
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
@@ -43,7 +60,7 @@ export const DashboardPage = () => {
|
|||||||
isYearly: isYearly === 'false' ? false : true,
|
isYearly: isYearly === 'false' ? false : true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}, [query, user, workspace])
|
}, [createCustomCheckoutSession, router.query, user, workspace])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack minH="100vh">
|
<Stack minH="100vh">
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -319,6 +319,10 @@ model ClaimableCustomPlan {
|
|||||||
chatsLimit Int
|
chatsLimit Int
|
||||||
storageLimit Int
|
storageLimit Int
|
||||||
seatsLimit Int
|
seatsLimit Int
|
||||||
|
isYearly Boolean @default(false)
|
||||||
|
companyName String?
|
||||||
|
vatType String?
|
||||||
|
vatValue String?
|
||||||
}
|
}
|
||||||
|
|
||||||
model ChatSession {
|
model ChatSession {
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -298,6 +298,10 @@ model ClaimableCustomPlan {
|
|||||||
chatsLimit Int
|
chatsLimit Int
|
||||||
storageLimit Int
|
storageLimit Int
|
||||||
seatsLimit Int
|
seatsLimit Int
|
||||||
|
isYearly Boolean @default(false)
|
||||||
|
companyName String?
|
||||||
|
vatType String?
|
||||||
|
vatValue String?
|
||||||
}
|
}
|
||||||
|
|
||||||
model ChatSession {
|
model ChatSession {
|
||||||
|
|||||||
Reference in New Issue
Block a user