🚸 (billing) Make sure customer is not created before launching checkout page

This commit is contained in:
Baptiste Arnaud
2023-08-22 10:29:00 +02:00
parent c08e0cdb0a
commit 53dd7ba499
13 changed files with 108 additions and 43 deletions

View File

@@ -133,6 +133,19 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
}
case 'customer.subscription.deleted': {
const subscription = event.data.object as Stripe.Subscription
const { data } = await stripe.subscriptions.list({
customer: subscription.customer as string,
limit: 1,
status: 'active',
})
const existingSubscription = data[0] as
| Stripe.Subscription
| undefined
if (existingSubscription)
return res.send({
message:
'An active subscription still exists. Skipping downgrade.',
})
const workspace = await prisma.workspace.update({
where: {
stripeId: subscription.customer as string,