From ffb1cb3bad6f06016ef35d5f85f8356d3573698d Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Thu, 16 Jan 2025 14:10:44 -0800 Subject: [PATCH 1/2] Fix customer.created webhook race condition --- .../integration/webhook/customer-created.ts | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.ts index 91c77b381c..59c635bd6b 100644 --- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts +++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts @@ -3,6 +3,7 @@ import { getClickEvent, recordLead } from "@/lib/tinybird"; import { sendWorkspaceWebhook } from "@/lib/webhook/publish"; import { transformLeadEventData } from "@/lib/webhook/transform"; import { prisma } from "@dub/prisma"; +import { Customer } from "@dub/prisma/client"; import { nanoid } from "@dub/utils"; import { waitUntil } from "@vercel/functions"; import type Stripe from "stripe"; @@ -50,27 +51,40 @@ export async function customerCreated(event: Stripe.Event) { }, }); + let customer: Customer; + if (customerFound) { - return `Customer with external ID ${externalId} already exists, skipping...`; + // if customer exists, update it + customer = await prisma.customer.update({ + where: { + id: customerFound.id, + }, + data: { + name: stripeCustomer.name, + email: stripeCustomer.email, + stripeCustomerId: stripeCustomer.id, + projectConnectId: stripeAccountId, + }, + }); + } else { + // else create a new customer + customer = await prisma.customer.create({ + data: { + id: createId({ prefix: "cus_" }), + name: stripeCustomer.name, + email: stripeCustomer.email, + stripeCustomerId: stripeCustomer.id, + projectConnectId: stripeAccountId, + externalId, + projectId: link.projectId, + linkId, + clickId, + clickedAt: new Date(clickData.timestamp + "Z"), + country: clickData.country, + }, + }); } - // Create customer - const customer = await prisma.customer.create({ - data: { - id: createId({ prefix: "cus_" }), - name: stripeCustomer.name, - email: stripeCustomer.email, - stripeCustomerId: stripeCustomer.id, - projectConnectId: stripeAccountId, - externalId, - projectId: link.projectId, - linkId, - clickId, - clickedAt: new Date(clickData.timestamp + "Z"), - country: clickData.country, - }, - }); - const leadData = { ...clickData, event_id: nanoid(16), From af4cb9e5ff4f9cafe8e18e33c872efe32581b7d3 Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Thu, 16 Jan 2025 14:33:19 -0800 Subject: [PATCH 2/2] Update customer-created.ts --- .../app/api/stripe/integration/webhook/customer-created.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.ts index 59c635bd6b..6dd6c17a1b 100644 --- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts +++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts @@ -54,14 +54,13 @@ export async function customerCreated(event: Stripe.Event) { let customer: Customer; if (customerFound) { - // if customer exists, update it + // if customer exists (created via /track/lead) + // update it with the Stripe customer ID (for future reference by invoice.paid) customer = await prisma.customer.update({ where: { id: customerFound.id, }, data: { - name: stripeCustomer.name, - email: stripeCustomer.email, stripeCustomerId: stripeCustomer.id, projectConnectId: stripeAccountId, },