From d1cc9180c893db8d230af9acf52f8a3beaed6e54 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Tue, 11 Oct 2022 13:54:28 +0200 Subject: [PATCH] :bug: (stripe) add back subscription delete webhook handler --- apps/builder/pages/api/stripe/webhook.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/builder/pages/api/stripe/webhook.ts b/apps/builder/pages/api/stripe/webhook.ts index 4e20167200..a4fac578f8 100644 --- a/apps/builder/pages/api/stripe/webhook.ts +++ b/apps/builder/pages/api/stripe/webhook.ts @@ -5,6 +5,7 @@ import Cors from 'micro-cors' import { buffer } from 'micro' import prisma from 'libs/prisma' import { withSentry } from '@sentry/nextjs' +import { Plan } from 'db' if (!process.env.STRIPE_SECRET_KEY || !process.env.STRIPE_WEBHOOK_SECRET) throw new Error('STRIPE_SECRET_KEY or STRIPE_WEBHOOK_SECRET missing') @@ -66,6 +67,24 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => { }) return res.status(200).send({ message: 'workspace upgraded in DB' }) } + case 'customer.subscription.deleted': { + const subscription = event.data.object as Stripe.Subscription + await prisma.workspace.update({ + where: { + stripeId: subscription.customer as string, + }, + data: { + plan: Plan.FREE, + additionalChatsIndex: 0, + additionalStorageIndex: 0, + chatsLimitFirstEmailSentAt: null, + chatsLimitSecondEmailSentAt: null, + storageLimitFirstEmailSentAt: null, + storageLimitSecondEmailSentAt: null, + }, + }) + return res.send({ message: 'workspace downgraded in DB' }) + } default: { return res.status(304).send({ message: 'event not handled' }) }