The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add invoice.paid
Kiran K8a781a7584b1
diff --git a/apps/web/app/api/campaigns/webhooks/stripe/route.ts b/apps/web/app/api/campaigns/webhooks/stripe/route.tsindex 8af0283abc..1466a44b08 100644--- a/apps/web/app/api/campaigns/webhooks/stripe/route.ts+++ b/apps/web/app/api/campaigns/webhooks/stripe/route.ts@@ -15,8 +15,8 @@ const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;const relevantEvents = new Set(["customer.created",- "payment_intent.succeeded","checkout.session.completed",+ "invoice.paid",]);export const POST = async (req: Request) => {@@ -42,12 +42,12 @@ export const POST = async (req: Request) => {case "customer.created":await customerCreated(event);break;- case "payment_intent.succeeded":- await paymentIntentSucceeded(event);- break;case "checkout.session.completed":await checkoutSessionCompleted(event);break;+ case "invoice.paid":+ await invoicePaid(event);+ break;}return new Response("OK", {@@ -55,49 +55,64 @@ export const POST = async (req: Request) => {});};-// Handle event "payment_intent.succeeded"-async function paymentIntentSucceeded(event: Stripe.Event) {- const charge = event.data.object as Stripe.PaymentIntent;+// Handle event "customer.created"+async function customerCreated(event: Stripe.Event) {+ const stripeCustomer = event.data.object as Stripe.Customer;const stripeAccountId = event.account as string;- const stripeCustomerId = charge.customer as string;-- // Find customer- const customer = await prismaEdge.customer.findFirst({- where: {- projectConnectId: stripeAccountId,- stripeCustomerId,- },- });+ const externalId = stripeCustomer.metadata.dubCustomerId || null;+ const clickId = stripeCustomer.metadata.dubClickId || null;- if (!customer) {+ // The client app should always send dubClickId via metadata+ if (!clickId) {return;}- // Find lead- const leadEvent = await getLeadEvent({ customer_id: customer.id });- if (!leadEvent || leadEvent.data.length === 0) {+ // Find click+ const clickEvent = await getClickEvent({ clickId });+ if (!clickEvent || clickEvent.data.length === 0) {return;}- // Record sale- await recordSale({- ...leadEvent.data[0],- event_id: nanoid(16),- payment_processor: "stripe",- amount: charge.amount_received,- currency: charge.currency,- refunded: 0,+ const clickData = clickEventSchemaTB+ .omit({ timestamp: true })+ .parse(clickEvent.data[0]);- // How do we get these?- recurring: 0,- product_id: "",- recurring_interval: "month",- recurring_interval_count: 1,+ // Create customer+ const customerId = nanoid(16);+ const customer = await prismaEdge.customer.create({+ data: {+ id: customerId,+ name: stripeCustomer.name,+ email: stripeCustomer.email,+ stripeCustomerId: stripeCustomer.id,+ projectConnectId: stripeAccountId,+ externalId,+ project: {+ connect: {+ stripeConnectId: stripeAccountId,+ },+ },+ },+ });- metadata: JSON.stringify({- charge,+ await Promise.all([+ // Record customer in TB+ recordCustomer({+ workspace_id: customer.projectId,+ customer_id: customer.id,+ name: customer.name || "",+ email: customer.email || "",+ avatar: customer.avatar || "",}),- });++ // Record lead+ recordLead({+ ...clickData,+ event_id: nanoid(16),+ event_name: "Customer created",+ customer_id: customer.id,+ }),+ ]);}// Handle event "checkout.session.completed"@@ -152,62 +167,50 @@ async function checkoutSessionCompleted(event: Stripe.Event) {});}-// Handle event "customer.created"-async function customerCreated(event: Stripe.Event) {- const stripeCustomer = event.data.object as Stripe.Customer;+// Handle event "invoice.paid"+async function invoicePaid(event: Stripe.Event) {+ const invoice = event.data.object as Stripe.Invoice;const stripeAccountId = event.account as string;- const externalId = stripeCustomer.metadata.dubCustomerId || null;- const clickId = stripeCustomer.metadata.dubClickId || null;+ const stripeCustomerId = invoice.customer as string;- // The client app should always send dubClickId via metadata- if (!clickId) {+ // Find customer+ const customer = await prismaEdge.customer.findFirst({+ where: {+ projectConnectId: stripeAccountId,+ stripeCustomerId,+ },+ });++ if (!customer) {return;}- // Find click- const clickEvent = await getClickEvent({ clickId });- if (!clickEvent || clickEvent.data.length === 0) {+ // Find lead+ const leadEvent = await getLeadEvent({ customer_id: customer.id });+ if (!leadEvent || leadEvent.data.length === 0) {return;}- const clickData = clickEventSchemaTB- .omit({ timestamp: true })- .parse(clickEvent.data[0]);+ // Find the product from line items+ const stripeProductId = invoice.lines.data[0]?.plan?.product as string;- // Create customer- const customerId = nanoid(16);- const customer = await prismaEdge.customer.create({- data: {- id: customerId,- name: stripeCustomer.name,- email: stripeCustomer.email,- stripeCustomerId: stripeCustomer.id,- projectConnectId: stripeAccountId,- externalId,- project: {- connect: {- stripeConnectId: stripeAccountId,- },- },- },- });+ // Record sale+ await recordSale({+ ...leadEvent.data[0],+ event_id: nanoid(16),+ payment_processor: "stripe",+ amount: invoice.amount_paid,+ currency: invoice.currency,+ refunded: 0,- await Promise.all([- // Record customer in TB- recordCustomer({- workspace_id: customer.projectId,- customer_id: customer.id,- name: customer.name || "",- email: customer.email || "",- avatar: customer.avatar || "",- }),+ // How do we get these?+ recurring: 0,+ product_id: stripeProductId,+ recurring_interval: "month",+ recurring_interval_count: 1,- // Record lead- recordLead({- ...clickData,- event_id: nanoid(16),- event_name: "Customer created",- customer_id: customer.id,+ metadata: JSON.stringify({+ invoice,}),- ]);+ });}