Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Improve `checkout.session.completed` webhook to match customer by email (#3163)

Steven Tey38aca29d722e
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index 2d19b76e94..e4be287a6e 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -27,7 +27,7 @@ import {
transformSaleEventData,
} from "@/lib/webhook/transform";
import { prisma } from "@dub/prisma";
-import { Customer, WorkflowTrigger } from "@dub/prisma/client";
+import { Customer, Project, WorkflowTrigger } from "@dub/prisma/client";
import { COUNTRIES_TO_CONTINENTS, nanoid, pick } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
@@ -60,6 +60,22 @@ export async function checkoutSessionCompleted(
let leadEvent: LeadEventTB | undefined;
let linkId: string | undefined;
+ const workspace = await prisma.project.findUnique({
+ where: {
+ stripeConnectId: stripeAccountId,
+ },
+ select: {
+ id: true,
+ stripeConnectId: true,
+ defaultProgramId: true,
+ webhookEnabled: true,
+ },
+ });
+
+ if (!workspace) {
+ return `Workspace with stripeConnectId ${stripeAccountId} not found, skipping...`;
+ }
+
/*
for stripe checkout links:
- if client_reference_id is a dub_id, we find the click event
@@ -75,31 +91,21 @@ export async function checkoutSessionCompleted(
return `Click event with dub_id ${dubClickId} not found, skipping...`;
}
- const workspace = await prisma.project.findUnique({
- where: {
- stripeConnectId: stripeAccountId,
- },
- select: {
- id: true,
- },
- });
-
- if (!workspace) {
- return `Workspace with stripeConnectId ${stripeAccountId} not found, skipping...`;
- }
-
existingCustomer = await prisma.customer.findFirst({
where: {
projectId: workspace.id,
- // check for existing customer with the same externalId (via clickId or email)
- // TODO: should we support checks for email and stripeCustomerId too?
+ // check for existing customer with the same externalId (via clickId or email) or email or stripeCustomerId
OR: [
{
externalId: clickEvent.click_id,
},
- {
- externalId: stripeCustomerEmail,
- },
+ ...(stripeCustomerEmail
+ ? [
+ {
+ externalId: stripeCustomerEmail,
+ },
+ ]
+ : []),
],
},
});
@@ -189,6 +195,7 @@ export async function checkoutSessionCompleted(
const promoCodeResponse = await attributeViaPromoCode({
promotionCodeId,
stripeAccountId,
+ workspace,
mode,
charge,
});
@@ -202,9 +209,22 @@ export async function checkoutSessionCompleted(
}
}
} else {
- existingCustomer = await prisma.customer.findUnique({
+ // find customer by stripeCustomerId or email
+ existingCustomer = await prisma.customer.findFirst({
where: {
- stripeCustomerId,
+ OR: [
+ {
+ stripeCustomerId,
+ },
+ ...(stripeCustomerEmail
+ ? [
+ {
+ projectId: workspace.id,
+ email: stripeCustomerEmail,
+ },
+ ]
+ : []),
+ ],
},
});
@@ -232,6 +252,7 @@ export async function checkoutSessionCompleted(
const promoCodeResponse = await attributeViaPromoCode({
promotionCodeId,
stripeAccountId,
+ workspace,
mode,
charge,
});
@@ -350,7 +371,7 @@ export async function checkoutSessionCompleted(
linkId,
});
- const [_sale, linkUpdated, workspace] = await Promise.all([
+ const [_sale, linkUpdated] = await Promise.all([
recordSale(saleData),
// update link stats
@@ -495,11 +516,16 @@ export async function checkoutSessionCompleted(
async function attributeViaPromoCode({
promotionCodeId,
stripeAccountId,
+ workspace,
mode,
charge,
}: {
promotionCodeId: string;
stripeAccountId: string;
+ workspace: Pick<
+ Project,
+ "id" | "defaultProgramId" | "stripeConnectId" | "webhookEnabled"
+ >;
mode: StripeMode;
charge: Stripe.Checkout.Session;
}) {
@@ -517,26 +543,6 @@ async function attributeViaPromoCode({
return null;
}
- // Find the workspace
- const workspace = await prisma.project.findUnique({
- where: {
- stripeConnectId: stripeAccountId,
- },
- select: {
- id: true,
- stripeConnectId: true,
- defaultProgramId: true,
- webhookEnabled: true,
- },
- });
-
- if (!workspace) {
- console.log(
- `Workspace with stripeConnectId ${stripeAccountId} not found, skipping...`,
- );
- return null;
- }
-
if (!workspace.defaultProgramId) {
console.log(
`Workspace with stripeConnectId ${stripeAccountId} has no default program, skipping...`,