The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor Stripe customer webhook handlers for better error handling and consistency
Kiran K9b26d2e07e47
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.tsindex 3a1d768d6a..e7487f9b1f 100644--- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts@@ -8,33 +8,35 @@ export async function customerCreated(event: Stripe.Event) {const stripeAccountId = event.account as string;const externalId = stripeCustomer.metadata?.dubCustomerId;- if (externalId) {- // Check the customer is not already created- // Find customer using projectConnectId and externalId (the customer's ID in the client app)- const customerFound = await prisma.customer.findUnique({+ if (!externalId) {+ return "External ID not found in Stripe customer metadata, skipping...";+ }++ // Check the customer is not already created+ // Find customer using projectConnectId and externalId (the customer's ID in the client app)+ const customer = await prisma.customer.findUnique({+ where: {+ projectConnectId_externalId: {+ projectConnectId: stripeAccountId,+ externalId,+ },+ },+ });++ if (customer) {+ // if customer exists (created via /track/lead)+ // update it with the Stripe customer ID (for future reference by invoice.paid)+ await prisma.customer.update({where: {- projectConnectId_externalId: {- projectConnectId: stripeAccountId,- externalId,- },+ id: customer.id,+ },+ data: {+ stripeCustomerId: stripeCustomer.id,+ projectConnectId: stripeAccountId,},});- if (customerFound) {- // if customer exists (created via /track/lead)- // update it with the Stripe customer ID (for future reference by invoice.paid)- await prisma.customer.update({- where: {- id: customerFound.id,- },- data: {- stripeCustomerId: stripeCustomer.id,- projectConnectId: stripeAccountId,- },- });-- return `Dub customer with ID ${customerFound.id} updated with Stripe customer ID ${stripeCustomer.id}`;- }+ return `Dub customer with ID ${customer.id} updated with Stripe customer ID ${stripeCustomer.id}`;}// otherwise create a new customerdiff --git a/apps/web/app/api/stripe/integration/webhook/customer-updated.ts b/apps/web/app/api/stripe/integration/webhook/customer-updated.tsindex d86f14480f..3f9ca4d7e4 100644--- a/apps/web/app/api/stripe/integration/webhook/customer-updated.ts+++ b/apps/web/app/api/stripe/integration/webhook/customer-updated.ts@@ -5,29 +5,35 @@ import { createNewCustomer } from "./utils";// Handle event "customer.updated"export async function customerUpdated(event: Stripe.Event) {const stripeCustomer = event.data.object as Stripe.Customer;- const stripeCustomerId = stripeCustomer.id;+ const stripeAccountId = event.account as string;const externalId = stripeCustomer.metadata?.dubCustomerId;- // Check if an existing Stripe customer exists- const customerFound = await prisma.customer.findUnique({+ if (!externalId) {+ return "External ID not found in Stripe customer metadata, skipping...";+ }++ const customer = await prisma.customer.findUnique({where: {- stripeCustomerId,+ projectConnectId_externalId: {+ projectConnectId: stripeAccountId,+ externalId,+ },},});- if (customerFound) {+ if (customer) {await prisma.customer.update({where: {- id: customerFound.id,+ id: customer.id,},data: {+ externalId,name: stripeCustomer.name,email: stripeCustomer.email,- ...(externalId && { externalId }),},});- return `Dub customer with ID ${customerFound.id} updated.`;+ return `Dub customer with ID ${customer.id} updated.`;}// otherwise create a new customer