2
0

(billing) Automatic usage-based billing (#924)

BREAKING CHANGE: Stripe environment variables simplified. Check out the
new configs to adapt your existing system.

Closes #906





<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
### Summary by CodeRabbit

**New Features:**
- Introduced a usage-based billing system, providing more flexibility
and options for users.
- Integrated with Stripe for a smoother and more secure payment process.
- Enhanced the user interface with improvements to the billing,
workspace, and pricing pages for a more intuitive experience.

**Improvements:**
- Simplified the billing logic, removing additional chats and yearly
billing for a more streamlined user experience.
- Updated email notifications to keep users informed about their usage
and limits.
- Improved pricing and currency formatting for better clarity and
understanding.

**Testing:**
- Updated tests and specifications to ensure the reliability of new
features and improvements.

**Note:** These changes aim to provide a more flexible and user-friendly
billing system, with clearer pricing and improved notifications. Users
should find the new system more intuitive and easier to navigate.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Baptiste Arnaud
2023-10-17 08:03:30 +02:00
committed by GitHub
parent a8c2deb258
commit 797751b418
55 changed files with 1589 additions and 1497 deletions

View File

@ -4,25 +4,26 @@ import {
PrismaClient,
WorkspaceRole,
} from '@typebot.io/prisma'
import { isDefined } from '@typebot.io/lib'
import { getChatsLimit } from '@typebot.io/lib/pricing'
import { isDefined, isEmpty } from '@typebot.io/lib'
import { getChatsLimit } from '@typebot.io/lib/billing/getChatsLimit'
import { getUsage } from '@typebot.io/lib/api/getUsage'
import { promptAndSetEnvironment } from './utils'
import { Workspace } from '@typebot.io/schemas'
import { sendAlmostReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/AlmostReachedChatsLimitEmail'
import { sendReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/ReachedChatsLimitEmail'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import Stripe from 'stripe'
import { createId } from '@paralleldrive/cuid2'
const prisma = new PrismaClient()
const LIMIT_EMAIL_TRIGGER_PERCENT = 0.8
const LIMIT_EMAIL_TRIGGER_PERCENT = 0.75
type WorkspaceForDigest = Pick<
Workspace,
| 'id'
| 'plan'
| 'name'
| 'customChatsLimit'
| 'additionalChatsIndex'
| 'isQuarantined'
| 'chatsLimitFirstEmailSentAt'
| 'chatsLimitSecondEmailSentAt'
@ -32,12 +33,40 @@ type WorkspaceForDigest = Pick<
})[]
}
export const sendTotalResultsDigest = async () => {
type ResultWithWorkspace = {
userId: string
workspace: {
id: string
typebots: {
id: string
}[]
members: {
user: {
id: string
email: string | null
}
role: WorkspaceRole
}[]
additionalStorageIndex: number
customChatsLimit: number | null
customStorageLimit: number | null
plan: Plan
isQuarantined: boolean
stripeId: string | null
}
typebotId: string
totalResultsYesterday: number
isFirstOfKind: true | undefined
}
export const checkAndReportChatsUsage = async () => {
await promptAndSetEnvironment('production')
console.log('Get collected results from the last hour...')
const hourAgo = new Date(Date.now() - 1000 * 60 * 60)
const zeroedMinutesHour = new Date()
zeroedMinutesHour.setUTCMinutes(0, 0, 0)
const hourAgo = new Date(zeroedMinutesHour.getTime() - 1000 * 60 * 60)
const results = await prisma.result.groupBy({
by: ['typebotId'],
@ -47,6 +76,7 @@ export const sendTotalResultsDigest = async () => {
where: {
hasStarted: true,
createdAt: {
lt: zeroedMinutesHour,
gte: hourAgo,
},
},
@ -69,11 +99,11 @@ export const sendTotalResultsDigest = async () => {
},
select: {
id: true,
name: true,
typebots: { select: { id: true } },
members: {
select: { user: { select: { id: true, email: true } }, role: true },
},
additionalChatsIndex: true,
additionalStorageIndex: true,
customChatsLimit: true,
customStorageLimit: true,
@ -81,6 +111,7 @@ export const sendTotalResultsDigest = async () => {
isQuarantined: true,
chatsLimitFirstEmailSentAt: true,
chatsLimitSecondEmailSentAt: true,
stripeId: true,
},
})
@ -110,9 +141,27 @@ export const sendTotalResultsDigest = async () => {
.map((result) => result.workspace)
)
console.log(`Send ${events.length} auto quarantine events...`)
await reportUsageToStripe(resultsWithWorkspaces)
await sendTelemetryEvents(events)
const newResultsCollectedEvents = resultsWithWorkspaces.map(
(result) =>
({
name: 'New results collected',
userId: result.userId,
workspaceId: result.workspace.id,
typebotId: result.typebotId,
data: {
total: result.totalResultsYesterday,
isFirstOfKind: result.isFirstOfKind,
},
} satisfies TelemetryEvent)
)
console.log(
`Send ${newResultsCollectedEvents.length} new results events and ${events.length} auto quarantine events...`
)
await sendTelemetryEvents(events.concat(newResultsCollectedEvents))
}
const sendAlertIfLimitReached = async (
@ -144,7 +193,7 @@ const sendAlertIfLimitReached = async (
to,
usagePercent: Math.round((totalChatsUsed / chatsLimit) * 100),
chatsLimit,
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
workspaceName: workspace.name,
})
await prisma.workspace.updateMany({
where: { id: workspace.id },
@ -155,32 +204,7 @@ const sendAlertIfLimitReached = async (
}
}
if (
chatsLimit > 0 &&
totalChatsUsed >= chatsLimit &&
!workspace.chatsLimitSecondEmailSentAt
) {
const to = workspace.members
.filter((member) => member.role === WorkspaceRole.ADMIN)
.map((member) => member.user.email)
.filter(isDefined)
try {
console.log(`Send reached chats limit email to ${to.join(', ')}...`)
await sendReachedChatsLimitEmail({
to,
chatsLimit,
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
})
await prisma.workspace.updateMany({
where: { id: workspace.id },
data: { chatsLimitSecondEmailSentAt: new Date() },
})
} catch (err) {
console.error(err)
}
}
if (totalChatsUsed > chatsLimit * 3 && workspace.plan === Plan.FREE) {
if (totalChatsUsed > chatsLimit * 1.5 && workspace.plan === Plan.FREE) {
console.log(`Automatically quarantine workspace ${workspace.id}...`)
await prisma.workspace.updateMany({
where: { id: workspace.id },
@ -207,4 +231,67 @@ const sendAlertIfLimitReached = async (
return events
}
sendTotalResultsDigest().then()
const reportUsageToStripe = async (
resultsWithWorkspaces: (Pick<ResultWithWorkspace, 'totalResultsYesterday'> & {
workspace: Pick<
ResultWithWorkspace['workspace'],
'id' | 'plan' | 'stripeId'
>
})[]
) => {
if (isEmpty(process.env.STRIPE_SECRET_KEY))
throw new Error('Missing STRIPE_SECRET_KEY env variable')
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2022-11-15',
})
for (const result of resultsWithWorkspaces.filter(
(result) =>
result.workspace.plan === 'STARTER' || result.workspace.plan === 'PRO'
)) {
if (!result.workspace.stripeId)
throw new Error(
`Found paid workspace without a stripeId: ${result.workspace.stripeId}`
)
const subscriptions = await stripe.subscriptions.list({
customer: result.workspace.stripeId,
})
const currentSubscription = subscriptions.data
.filter((sub) => ['past_due', 'active'].includes(sub.status))
.sort((a, b) => a.created - b.created)
.shift()
if (!currentSubscription)
throw new Error(
`Found paid workspace without a subscription: ${result.workspace.stripeId}`
)
const subscriptionItem = currentSubscription.items.data.find(
(item) =>
item.price.id === process.env.STRIPE_STARTER_CHATS_PRICE_ID ||
item.price.id === process.env.STRIPE_PRO_CHATS_PRICE_ID
)
if (!subscriptionItem)
throw new Error(
`Could not find subscription item for workspace ${result.workspace.id}`
)
const idempotencyKey = createId()
await stripe.subscriptionItems.createUsageRecord(
subscriptionItem.id,
{
quantity: result.totalResultsYesterday,
timestamp: 'now',
},
{
idempotencyKey,
}
)
}
}
checkAndReportChatsUsage().then()

View File

@ -180,17 +180,12 @@ const resetBillingProps = async () => {
{
chatsLimitFirstEmailSentAt: { not: null },
},
{
storageLimitFirstEmailSentAt: { not: null },
},
],
},
data: {
isQuarantined: false,
chatsLimitFirstEmailSentAt: null,
storageLimitFirstEmailSentAt: null,
chatsLimitSecondEmailSentAt: null,
storageLimitSecondEmailSentAt: null,
},
})
console.log(`Resetted ${count} workspaces.`)

View File

@ -0,0 +1,50 @@
import Stripe from 'stripe'
import { promptAndSetEnvironment } from './utils'
import {
proChatTiers,
starterChatTiers,
} from '@typebot.io/lib/billing/constants'
const chatsProductId = 'prod_MVXtq5sATQzIcM'
const createChatsPrices = async () => {
await promptAndSetEnvironment()
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: '2022-11-15',
})
await stripe.prices.create({
currency: 'usd',
billing_scheme: 'tiered',
recurring: { interval: 'month', usage_type: 'metered' },
tiers: starterChatTiers,
tiers_mode: 'volume',
tax_behavior: 'exclusive',
product: chatsProductId,
currency_options: {
eur: {
tax_behavior: 'exclusive',
tiers: starterChatTiers,
},
},
})
await stripe.prices.create({
currency: 'usd',
billing_scheme: 'tiered',
recurring: { interval: 'month', usage_type: 'metered' },
tiers: proChatTiers,
tiers_mode: 'volume',
tax_behavior: 'exclusive',
product: chatsProductId,
currency_options: {
eur: {
tax_behavior: 'exclusive',
tiers: proChatTiers,
},
},
})
}
createChatsPrices()

View File

@ -39,7 +39,6 @@ const inspectUser = async () => {
user: { email: { not: response.email } },
},
},
additionalChatsIndex: true,
additionalStorageIndex: true,
typebots: {
orderBy: {
@ -82,10 +81,6 @@ const inspectUser = async () => {
console.log(' - Name:', workspace.workspace.name)
console.log(' Plan:', workspace.workspace.plan)
console.log(' Members:', workspace.workspace.members.length + 1)
console.log(
' Additional chats:',
workspace.workspace.additionalChatsIndex
)
console.log(
' Additional storage:',
workspace.workspace.additionalStorageIndex

View File

@ -0,0 +1,278 @@
import { PrismaClient } from '@typebot.io/prisma'
import { promptAndSetEnvironment } from './utils'
import { Stripe } from 'stripe'
import { createId } from '@paralleldrive/cuid2'
import { writeFileSync } from 'fs'
const migrateSubscriptionsToUsageBased = async () => {
await promptAndSetEnvironment()
const prisma = new PrismaClient()
if (
!process.env.STRIPE_STARTER_CHATS_PRICE_ID ||
!process.env.STRIPE_PRO_CHATS_PRICE_ID ||
!process.env.STRIPE_SECRET_KEY ||
!process.env.STRIPE_STARTER_PRICE_ID ||
!process.env.STRIPE_PRO_PRICE_ID
)
throw new Error('Missing some env variables')
const {
starterChatsPriceId,
proChatsPriceId,
secretKey,
starterPriceId,
proPriceId,
starterYearlyPriceId,
proYearlyPriceId,
} = {
starterChatsPriceId: process.env.STRIPE_STARTER_CHATS_PRICE_ID,
proChatsPriceId: process.env.STRIPE_PRO_CHATS_PRICE_ID,
secretKey: process.env.STRIPE_SECRET_KEY,
starterPriceId: process.env.STRIPE_STARTER_PRICE_ID,
proPriceId: process.env.STRIPE_PRO_PRICE_ID,
starterYearlyPriceId: process.env.STRIPE_STARTER_YEARLY_PRICE_ID,
proYearlyPriceId: process.env.STRIPE_PRO_YEARLY_PRICE_ID,
}
const workspacesWithPaidPlan = await prisma.workspace.findMany({
where: {
plan: {
in: ['PRO', 'STARTER'],
},
isSuspended: false,
},
select: {
plan: true,
name: true,
id: true,
stripeId: true,
isQuarantined: true,
members: {
select: {
user: {
select: { email: true },
},
},
},
},
})
writeFileSync(
'./workspacesWithPaidPlan.json',
JSON.stringify(workspacesWithPaidPlan, null, 2)
)
const stripe = new Stripe(secretKey, {
apiVersion: '2022-11-15',
})
const todayMidnight = new Date()
todayMidnight.setUTCHours(0, 0, 0, 0)
const failedWorkspaces = []
const workspacesWithoutSubscription = []
const workspacesWithoutStripeId = []
let i = 0
for (const workspace of workspacesWithPaidPlan) {
i += 1
console.log(
`(${i} / ${workspacesWithPaidPlan.length})`,
'Migrating workspace:',
workspace.id,
workspace.name,
workspace.stripeId,
JSON.stringify(workspace.members.map((member) => member.user.email))
)
if (!workspace.stripeId) {
console.log('No stripe ID, skipping...')
workspacesWithoutStripeId.push(workspace)
writeFileSync(
'./workspacesWithoutStripeId.json',
JSON.stringify(workspacesWithoutStripeId, null, 2)
)
continue
}
const subscriptions = await stripe.subscriptions.list({
customer: workspace.stripeId,
})
const currentSubscription = subscriptions.data
.filter((sub) => ['past_due', 'active'].includes(sub.status))
.sort((a, b) => a.created - b.created)
.shift()
if (!currentSubscription) {
console.log('No current subscription in workspace:', workspace.id)
workspacesWithoutSubscription.push(workspace)
writeFileSync(
'./workspacesWithoutSubscription.json',
JSON.stringify(workspacesWithoutSubscription)
)
continue
}
if (
currentSubscription.items.data.find(
(item) =>
item.price.id === starterChatsPriceId ||
item.price.id === proChatsPriceId
)
) {
console.log(
'Already migrated to usage based billing for workspace. Skipping...'
)
continue
}
if (
!currentSubscription.items.data.find(
(item) =>
item.price.id === starterPriceId ||
item.price.id === proPriceId ||
item.price.id === starterYearlyPriceId ||
item.price.id === proYearlyPriceId
)
) {
console.log(
'Could not find STARTER or PRO plan in items for workspace:',
workspace.id
)
failedWorkspaces.push(workspace)
writeFileSync(
'./failedWorkspaces.json',
JSON.stringify(failedWorkspaces, null, 2)
)
continue
}
const newSubscription = await stripe.subscriptions.update(
currentSubscription.id,
{
items: [
...currentSubscription.items.data.flatMap<Stripe.SubscriptionUpdateParams.Item>(
(item) => {
if (
item.price.id === starterPriceId ||
item.price.id === proPriceId
)
return {
id: item.id,
price: item.price.id,
quantity: item.quantity,
}
if (
item.price.id === starterYearlyPriceId ||
item.price.id === proYearlyPriceId
)
return [
{
id: item.id,
price: item.price.id,
quantity: item.quantity,
deleted: true,
},
{
price:
workspace.plan === 'STARTER'
? starterPriceId
: proPriceId,
quantity: 1,
},
]
return {
id: item.id,
price: item.price.id,
quantity: item.quantity,
deleted: true,
}
}
),
{
price:
workspace.plan === 'STARTER'
? starterChatsPriceId
: proChatsPriceId,
},
],
}
)
const totalResults = await prisma.result.count({
where: {
typebot: { workspaceId: workspace.id },
hasStarted: true,
createdAt: {
gte: new Date(newSubscription.current_period_start * 1000),
lt: todayMidnight,
},
},
})
if (workspace.plan === 'STARTER' && totalResults >= 4000) {
console.log(
'Workspace has more than 4000 chats, automatically upgrading to PRO plan'
)
const currentPlanItemId = newSubscription?.items.data.find((item) =>
[starterPriceId, proPriceId].includes(item.price.id)
)?.id
if (!currentPlanItemId)
throw new Error(`Could not find current plan item ID for workspace`)
await stripe.subscriptions.update(newSubscription.id, {
items: [
{
id: currentPlanItemId,
price: proPriceId,
quantity: 1,
},
],
})
await prisma.workspace.update({
where: { id: workspace.id },
data: {
plan: 'PRO',
},
})
}
const subscriptionItem = newSubscription.items.data.find(
(item) =>
item.price.id === starterChatsPriceId ||
item.price.id === proChatsPriceId
)
if (!subscriptionItem)
throw new Error(
`Could not find subscription item for workspace ${workspace.id}`
)
const idempotencyKey = createId()
console.log('Reporting total results:', totalResults)
await stripe.subscriptionItems.createUsageRecord(
subscriptionItem.id,
{
quantity: totalResults,
timestamp: 'now',
},
{
idempotencyKey,
}
)
if (workspace.isQuarantined) {
await prisma.workspace.update({
where: { id: workspace.id },
data: {
isQuarantined: false,
},
})
}
}
}
migrateSubscriptionsToUsageBased()

View File

@ -13,9 +13,11 @@
"db:bulkUpdate": "tsx bulkUpdate.ts",
"db:fixTypebots": "tsx fixTypebots.ts",
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts",
"sendAlertEmails": "tsx sendAlertEmails.ts",
"checkAndReportChatsUsage": "tsx checkAndReportChatsUsage.ts",
"inspectUser": "tsx inspectUser.ts",
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts"
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts",
"createChatsPrices": "tsx createChatsPrices.ts",
"migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts"
},
"devDependencies": {
"@typebot.io/emails": "workspace:*",
@ -31,5 +33,8 @@
"tsx": "3.12.7",
"typescript": "5.1.6",
"zod": "3.21.4"
},
"dependencies": {
"@paralleldrive/cuid2": "2.2.1"
}
}

View File

@ -1,200 +0,0 @@
import {
MemberInWorkspace,
PrismaClient,
WorkspaceRole,
} from '@typebot.io/prisma'
import { isDefined } from '@typebot.io/lib'
import { getChatsLimit } from '@typebot.io/lib/pricing'
import { promptAndSetEnvironment } from './utils'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import { Workspace } from '@typebot.io/schemas'
const prisma = new PrismaClient()
type WorkspaceForDigest = Pick<
Workspace,
| 'id'
| 'plan'
| 'customChatsLimit'
| 'customStorageLimit'
| 'additionalChatsIndex'
| 'additionalStorageIndex'
| 'isQuarantined'
> & {
members: (Pick<MemberInWorkspace, 'role'> & {
user: { id: string; email: string | null }
})[]
}
export const sendTotalResultsDigest = async () => {
await promptAndSetEnvironment('production')
console.log("Generating total results yesterday's digest...")
const todayMidnight = new Date()
todayMidnight.setUTCHours(0, 0, 0, 0)
const yesterday = new Date(todayMidnight)
yesterday.setDate(yesterday.getDate() - 1)
const results = await prisma.result.groupBy({
by: ['typebotId'],
_count: {
_all: true,
},
where: {
hasStarted: true,
createdAt: {
gte: yesterday,
lt: todayMidnight,
},
},
})
console.log(
`Found ${results.reduce(
(total, result) => total + result._count._all,
0
)} results collected yesterday.`
)
const workspaces = await prisma.workspace.findMany({
where: {
typebots: {
some: {
id: { in: results.map((result) => result.typebotId) },
},
},
},
select: {
id: true,
typebots: { select: { id: true } },
members: {
select: { user: { select: { id: true, email: true } }, role: true },
},
additionalChatsIndex: true,
additionalStorageIndex: true,
customChatsLimit: true,
customStorageLimit: true,
plan: true,
isQuarantined: true,
},
})
const resultsWithWorkspaces = results
.flatMap((result) => {
const workspace = workspaces.find((workspace) =>
workspace.typebots.some((typebot) => typebot.id === result.typebotId)
)
if (!workspace) return
return workspace.members
.filter((member) => member.role !== WorkspaceRole.GUEST)
.map((member, memberIndex) => ({
userId: member.user.id,
workspace: workspace,
typebotId: result.typebotId,
totalResultsYesterday: result._count._all,
isFirstOfKind: memberIndex === 0 ? (true as const) : undefined,
}))
})
.filter(isDefined)
console.log('Computing workspaces limits...')
const workspaceLimitReachedEvents = await sendAlertIfLimitReached(
resultsWithWorkspaces
.filter((result) => result.isFirstOfKind)
.map((result) => result.workspace)
)
const newResultsCollectedEvents = resultsWithWorkspaces.map(
(result) =>
({
name: 'New results collected',
userId: result.userId,
workspaceId: result.workspace.id,
typebotId: result.typebotId,
data: {
total: result.totalResultsYesterday,
isFirstOfKind: result.isFirstOfKind,
},
} satisfies TelemetryEvent)
)
await sendTelemetryEvents(
workspaceLimitReachedEvents.concat(newResultsCollectedEvents)
)
console.log(
`Sent ${workspaceLimitReachedEvents.length} workspace limit reached events.`
)
console.log(
`Sent ${newResultsCollectedEvents.length} new results collected events.`
)
}
const sendAlertIfLimitReached = async (
workspaces: WorkspaceForDigest[]
): Promise<TelemetryEvent[]> => {
const events: TelemetryEvent[] = []
const taggedWorkspaces: string[] = []
for (const workspace of workspaces) {
if (taggedWorkspaces.includes(workspace.id) || workspace.isQuarantined)
continue
taggedWorkspaces.push(workspace.id)
const { totalChatsUsed } = await getUsage(workspace.id)
const chatsLimit = getChatsLimit(workspace)
if (chatsLimit > 0 && totalChatsUsed >= chatsLimit) {
events.push(
...workspace.members
.filter((member) => member.role === WorkspaceRole.ADMIN)
.map(
(member) =>
({
name: 'Workspace limit reached',
userId: member.user.id,
workspaceId: workspace.id,
data: {
totalChatsUsed,
chatsLimit,
},
} satisfies TelemetryEvent)
)
)
continue
}
}
return events
}
const getUsage = async (workspaceId: string) => {
const now = new Date()
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1)
const typebots = await prisma.typebot.findMany({
where: {
workspace: {
id: workspaceId,
},
},
select: { id: true },
})
const [totalChatsUsed] = await Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
},
},
}),
])
return {
totalChatsUsed,
}
}
sendTotalResultsDigest().then()