The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
dispatchWebhook when sales are recorded
Kiran K4ecc76233633
diff --git a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.tsindex 5e817c8f3c..e76cc9d504 100644--- a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts+++ b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts@@ -1,7 +1,9 @@import { prisma } from "@/lib/prisma";import { getLeadEvent, recordSale } from "@/lib/tinybird";import { redis } from "@/lib/upstash";+import { dispatchWebhook } from "@/lib/webhook/publish";import { nanoid } from "@dub/utils";+import { waitUntil } from "@vercel/functions";import type Stripe from "stripe";// Handle event "checkout.session.completed"@@ -58,19 +60,22 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {return `Lead event with customer ID ${customer.id} not found, skipping...`;}- await Promise.all([- recordSale({- ...leadEvent.data[0],- event_id: nanoid(16),- event_name: "Subscription creation",- payment_processor: "stripe",- amount: charge.amount_total!,- currency: charge.currency!,- invoice_id: invoiceId || "",- metadata: JSON.stringify({- charge,- }),+ const saleData = {+ ...leadEvent.data[0],+ event_id: nanoid(16),+ event_name: "Subscription creation",+ payment_processor: "stripe",+ amount: charge.amount_total!,+ currency: charge.currency!,+ invoice_id: invoiceId || "",+ metadata: JSON.stringify({+ charge,}),+ };++ await Promise.all([+ recordSale(saleData),+// update link sales countprisma.link.update({where: {@@ -84,5 +89,27 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {}),]);+ waitUntil(+ (async () => {+ const workspace = await prisma.project.findUniqueOrThrow({+ where: {+ id: customer.projectId,+ },+ select: {+ id: true,+ webhookEnabled: true,+ },+ });++ dispatchWebhook("sale.created", {+ workspace,+ data: {+ ...customer,+ ...saleData,+ },+ });+ })(),+ );+return `Checkout session completed for customer with external ID ${dubCustomerId} and invoice ID ${invoiceId}`;}diff --git a/apps/web/app/api/stripe/connect/webhook/customer-created.ts b/apps/web/app/api/stripe/connect/webhook/customer-created.tsindex e525ccf04c..4ac79631b5 100644--- a/apps/web/app/api/stripe/connect/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/connect/webhook/customer-created.ts@@ -1,6 +1,6 @@import { prisma } from "@/lib/prisma";import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";-import { dispatchLinkWebhook } from "@/lib/webhook/publish-edge";+import { dispatchWebhook } from "@/lib/webhook/publish-edge";import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import { nanoid } from "@dub/utils";import { waitUntil } from "@vercel/functions";@@ -59,6 +59,13 @@ export async function customerCreated(event: Stripe.Event) {.omit({ timestamp: true }).parse(clickEvent.data[0]);+ const leadData = {+ ...clickData,+ event_id: nanoid(16),+ event_name: "New customer",+ customer_id: customer.id,+ };+await Promise.all([// Record customerrecordCustomer({@@ -70,12 +77,7 @@ export async function customerCreated(event: Stripe.Event) {}),// Record lead- recordLead({- ...clickData,- event_id: nanoid(16),- event_name: "New customer",- customer_id: customer.id,- }),+ recordLead(leadData),// update link leads countprisma.link.update({@@ -90,5 +92,27 @@ export async function customerCreated(event: Stripe.Event) {}),]);+ waitUntil(+ (async () => {+ const workspace = await prisma.project.findUniqueOrThrow({+ where: {+ id: customer.projectId,+ },+ select: {+ id: true,+ webhookEnabled: true,+ },+ });++ dispatchWebhook("lead.created", {+ workspace,+ data: {+ ...customer,+ ...leadData,+ },+ });+ })(),+ );+return `Customer created: ${customer.id}`;}diff --git a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts b/apps/web/app/api/stripe/connect/webhook/invoice-paid.tsindex 6e3cf0afa7..0789173581 100644--- a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts+++ b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts@@ -1,7 +1,9 @@import { prisma } from "@/lib/prisma";import { getLeadEvent, recordSale } from "@/lib/tinybird";import { redis } from "@/lib/upstash";+import { dispatchWebhook } from "@/lib/webhook/publish";import { nanoid } from "@dub/utils";+import { waitUntil } from "@vercel/functions";import type Stripe from "stripe";// Handle event "invoice.paid"@@ -43,19 +45,22 @@ export async function invoicePaid(event: Stripe.Event) {return `Lead event with customer ID ${customer.id} not found, skipping...`;}- await Promise.all([- recordSale({- ...leadEvent.data[0],- event_id: nanoid(16),- event_name: "Subscription update",- payment_processor: "stripe",- amount: invoice.amount_paid,- currency: invoice.currency,- invoice_id: invoiceId,- metadata: JSON.stringify({- invoice,- }),+ const saleData = {+ ...leadEvent.data[0],+ event_id: nanoid(16),+ event_name: "Subscription update",+ payment_processor: "stripe",+ amount: invoice.amount_paid,+ currency: invoice.currency,+ invoice_id: invoiceId,+ metadata: JSON.stringify({+ invoice,}),+ };++ await Promise.all([+ recordSale(saleData),+// update link sales countprisma.link.update({where: {@@ -69,5 +74,27 @@ export async function invoicePaid(event: Stripe.Event) {}),]);+ waitUntil(+ (async () => {+ const workspace = await prisma.project.findUniqueOrThrow({+ where: {+ id: customer.projectId,+ },+ select: {+ id: true,+ webhookEnabled: true,+ },+ });++ dispatchWebhook("sale.created", {+ workspace,+ data: {+ ...customer,+ ...saleData,+ },+ });+ })(),+ );+return `Sale recorded for customer ID ${customer.id} and invoice ID ${invoiceId}`;}