From e8f08f4083dbd943167ff044a0f96d8be9e1d53d Mon Sep 17 00:00:00 2001 From: Mythie Date: Fri, 20 Oct 2023 12:14:39 +1100 Subject: [PATCH] fix: update webhook handler --- packages/ee/server-only/stripe/webhook/handler.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/ee/server-only/stripe/webhook/handler.ts b/packages/ee/server-only/stripe/webhook/handler.ts index c063ee2d9..e1a6716e2 100644 --- a/packages/ee/server-only/stripe/webhook/handler.ts +++ b/packages/ee/server-only/stripe/webhook/handler.ts @@ -55,8 +55,19 @@ export const stripeWebhookHandler = async ( const customerId = typeof session.customer === 'string' ? session.customer : session.customer?.id; + // Attempt to get the user ID from the client reference id. let userId = Number(session.client_reference_id); + // If the user ID is not found, attempt to get it from the Stripe customer metadata. + if (!userId && customerId) { + const customer = await stripe.customers.retrieve(customerId); + + if (!customer.deleted) { + userId = Number(customer.metadata.userId); + } + } + + // Finally, attempt to get the user ID from the subscription within the database. if (!userId && customerId) { const result = await prisma.subscription.findFirst({ select: {