The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Support Stripe payment links
Steven Tey48ffc09f378c
diff --git a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.tsindex a6d5f00a37..273d909d71 100644--- a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts+++ b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts@@ -1,9 +1,21 @@import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";import { createSaleData } from "@/lib/api/sales/create-sale-data";-import { getLeadEvent, recordSale } from "@/lib/tinybird";+import { createId } from "@/lib/api/utils";+import {+ getClickEvent,+ getLeadEvent,+ recordLead,+ recordSale,+} from "@/lib/tinybird";import { redis } from "@/lib/upstash";import { sendWorkspaceWebhook } from "@/lib/webhook/publish";-import { transformSaleEventData } from "@/lib/webhook/transform";+import {+ transformLeadEventData,+ transformSaleEventData,+} from "@/lib/webhook/transform";+import z from "@/lib/zod";+import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";+import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";import { prisma } from "@dub/prisma";import { Customer } from "@dub/prisma/client";import { nanoid } from "@dub/utils";@@ -14,62 +26,133 @@ import type Stripe from "stripe";export async function checkoutSessionCompleted(event: Stripe.Event) {const charge = event.data.object as Stripe.Checkout.Session;const dubCustomerId = charge.metadata?.dubCustomerId;+ const clientReferenceId = charge.client_reference_id;const stripeAccountId = event.account as string;const stripeCustomerId = charge.customer as string;+ const stripeCustomerName = charge.customer_details?.name;+ const stripeCustomerEmail = charge.customer_details?.email;const invoiceId = charge.invoice as string;- if (!dubCustomerId) {- return "Customer ID not found in Stripe checkout session metadata, skipping...";- }-let customer: Customer;- try {- // Update customer with stripe customerId if exists- customer = await prisma.customer.update({- where: {- projectConnectId_externalId: {- projectConnectId: stripeAccountId,- externalId: dubCustomerId,+ let clickEvent: z.infer<typeof clickEventSchemaTB> | null = null;+ let leadEvent: z.infer<typeof leadEventSchemaTB>;+ let linkId: string;++ /*+ for regular stripe checkout setup:+ - if dubCustomerId is found, we update the customer with the stripe customerId+ - we then find the lead event using the customer's unique ID on Dub+ - the lead event will then be passed to the remaining logic to record a sale+ */+ if (dubCustomerId) {+ try {+ // Update customer with stripe customerId if exists+ customer = await prisma.customer.update({+ where: {+ projectConnectId_externalId: {+ projectConnectId: stripeAccountId,+ externalId: dubCustomerId,+ },+ },+ data: {+ stripeCustomerId,},+ });+ } catch (error) {+ // Skip if customer not found+ console.log(error);+ return `Customer with dubCustomerId ${dubCustomerId} not found, skipping...`;+ }++ if (invoiceId) {+ // Skip if invoice id is already processed+ const ok = await redis.set(`dub_sale_events:invoiceId:${invoiceId}`, 1, {+ ex: 60 * 60 * 24 * 7,+ nx: true,+ });++ if (!ok) {+ console.info(+ "[Stripe Webhook] Skipping already processed invoice.",+ invoiceId,+ );+ return `Invoice with ID ${invoiceId} already processed, skipping...`;+ }+ }++ // Find lead+ leadEvent = await getLeadEvent({ customerId: customer.id }).then(+ (res) => res.data[0],+ );++ linkId = leadEvent.link_id;++ /*+ for stripe checkout links:+ - if client_reference_id is a dub_id, we find the click event+ - the click event will be used to create a lead event + customer+ - the lead event will then be passed to the remaining logic to record a sale+ */+ } else if (clientReferenceId?.startsWith("dub_id_")) {+ const dubClickId = clientReferenceId.split("dub_id_")[1];++ clickEvent = await getClickEvent({ clickId: dubClickId }).then(+ (res) => res.data[0],+ );++ if (!clickEvent) {+ 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...`;+ }++ customer = await prisma.customer.create({data: {+ id: createId({ prefix: "cus_" }),+ name: stripeCustomerName,+ email: stripeCustomerEmail,+ externalId: stripeCustomerEmail, // using Stripe customer email as externalId+ projectId: workspace.id,+ projectConnectId: stripeAccountId,stripeCustomerId,+ clickId: clickEvent.click_id,+ linkId: clickEvent.link_id,+ country: clickEvent.country,+ clickedAt: new Date(clickEvent.timestamp + "Z"),},});- } catch (error) {- // Skip if customer not found- console.log(error);- return `Customer with dubCustomerId ${dubCustomerId} not found, skipping...`;- }- if (invoiceId) {- // Skip if invoice id is already processed- const ok = await redis.set(`dub_sale_events:invoiceId:${invoiceId}`, 1, {- ex: 60 * 60 * 24 * 7,- nx: true,- });+ leadEvent = {+ ...clickEvent,+ event_id: nanoid(16),+ event_name: "Checkout session completed",+ customer_id: customer.id,+ metadata: "",+ };- if (!ok) {- console.info(- "[Stripe Webhook] Skipping already processed invoice.",- invoiceId,- );- return `Invoice with ID ${invoiceId} already processed, skipping...`;- }+ await recordLead(leadEvent);+ linkId = clickEvent.link_id;+ } else {+ return `Customer ID not found in Stripe checkout session metadata and client_reference_id is not a dub_id, skipping...`;}if (charge.amount_total === 0) {- return `Checkout session completed for customer with external ID ${dubCustomerId} and invoice ID ${invoiceId} but amount is 0, skipping...`;- }-- // Find lead- const leadEvent = await getLeadEvent({ customerId: customer.id });- if (!leadEvent || leadEvent.data.length === 0) {- return `Lead event with customer ID ${customer.id} not found, skipping...`;+ return `Checkout session completed for Stripe customer ${stripeCustomerId} with invoice ID ${invoiceId} but amount is 0, skipping...`;}const saleData = {- ...leadEvent.data[0],+ ...leadEvent,event_id: nanoid(16),event_name: "Subscription creation",payment_processor: "stripe",@@ -81,35 +164,35 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {}),};- // Find link- const linkId = leadEvent.data[0].link_id;const link = await prisma.link.findUnique({where: {id: linkId,},});- if (!link) {- return `Link with ID ${linkId} not found, skipping...`;- }-const [_sale, _link, workspace] = await Promise.all([recordSale(saleData),// update link sales count- prisma.link.update({- where: {- id: linkId,- },- data: {- sales: {- increment: 1,+ link &&+ prisma.link.update({+ where: {+ id: link.id,},- saleAmount: {- increment: charge.amount_total!,+ data: {+ ...(clickEvent && {+ leads: {+ increment: 1,+ },+ }),+ sales: {+ increment: 1,+ },+ saleAmount: {+ increment: charge.amount_total!,+ },},- },- }),+ }),// update workspace sales usageprisma.project.update({@@ -118,7 +201,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {},data: {usage: {- increment: 1,+ increment: clickEvent ? 2 : 1,},salesUsage: {increment: charge.amount_total!,@@ -128,7 +211,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {]);// for program links- if (link.programId) {+ if (link?.programId) {const { program, partnerId, commissionAmount } =await prisma.programEnrollment.findUniqueOrThrow({where: {@@ -160,7 +243,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {paymentProcessor: saleData.payment_processor,},metadata: {- ...leadEvent.data[0],+ ...leadEvent,stripeMetadata: charge,},});@@ -184,22 +267,42 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {);}- // send workspace webhookwaitUntil(- sendWorkspaceWebhook({- trigger: "sale.created",- workspace,- data: transformSaleEventData({- ...saleData,- link,- customerId: customer.id,- customerExternalId: customer.externalId,- customerName: customer.name,- customerEmail: customer.email,- customerAvatar: customer.avatar,- customerCreatedAt: customer.createdAt,- }),- }),+ (async () => {+ if (clickEvent) {+ await sendWorkspaceWebhook({+ trigger: "lead.created",+ workspace,+ data: transformLeadEventData({+ ...clickEvent,+ link,+ eventName: "Checkout session completed",+ customerId: customer.id,+ customerExternalId: customer.externalId,+ customerName: customer.name,+ customerEmail: customer.email,+ customerAvatar: customer.avatar,+ customerCreatedAt: customer.createdAt,+ }),+ });+ }++ // send workspace webhook+ await sendWorkspaceWebhook({+ trigger: "sale.created",+ workspace,+ data: transformSaleEventData({+ ...saleData,+ link,+ customerId: customer.id,+ customerExternalId: customer.externalId,+ customerName: customer.name,+ customerEmail: customer.email,+ customerAvatar: customer.avatar,+ customerCreatedAt: customer.createdAt,+ }),+ });+ })(),);return `Checkout session completed for customer with external ID ${dubCustomerId} and invoice ID ${invoiceId}`;