Skip to content

Commit

Permalink
🛂 (billing) Past due status only for unpaid invoices with additional …
Browse files Browse the repository at this point in the history
…usage
  • Loading branch information
baptisteArno committed Dec 5, 2023
1 parent f64784f commit b0d7039
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion apps/builder/src/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RequestHandler } from 'next/dist/server/next'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import { Settings } from '@typebot.io/schemas'
import { env } from '@typebot.io/env'
import { prices } from '@typebot.io/lib/billing/constants'

if (!env.STRIPE_SECRET_KEY || !env.STRIPE_WEBHOOK_SECRET)
throw new Error('STRIPE_SECRET_KEY or STRIPE_WEBHOOK_SECRET missing')
Expand Down Expand Up @@ -176,7 +177,11 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
customer: invoice.customer as string,
status: 'open',
})
if (outstandingInvoices.data.length > 0)
const outstandingInvoicesWithAdditionalUsageCosts =
outstandingInvoices.data.filter(
(invoice) => invoice.amount_due > prices['PRO'] * 100
)
if (outstandingInvoicesWithAdditionalUsageCosts.length > 0)
return res.send({
message: 'Workspace has outstanding invoices, skipping.',
})
Expand Down Expand Up @@ -221,6 +226,14 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
message:
'An active subscription still exists. Skipping downgrade.',
})
const outstandingInvoices = await stripe.invoices.list({
customer: subscription.customer as string,
status: 'open',
})
const outstandingInvoicesWithAdditionalUsageCosts =
outstandingInvoices.data.filter(
(invoice) => invoice.amount_due > prices['PRO'] * 100
)
const workspace = await prisma.workspace.update({
where: {
stripeId: subscription.customer as string,
Expand All @@ -230,6 +243,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
customChatsLimit: null,
customStorageLimit: null,
customSeatsLimit: null,
isPastDue: outstandingInvoicesWithAdditionalUsageCosts.length > 0,
},
include: {
members: {
Expand Down

1 comment on commit b0d7039

@vercel
Copy link

@vercel vercel bot commented on b0d7039 Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

Please sign in to comment.