The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
handle the events from Stripe
Kiran K41f55f3bc04e
diff --git a/apps/web/app/api/campaigns/webhooks/stripe/route.ts b/apps/web/app/api/campaigns/webhooks/stripe/route.tsindex 49dd82555d..06b3eb5d54 100644--- a/apps/web/app/api/campaigns/webhooks/stripe/route.ts+++ b/apps/web/app/api/campaigns/webhooks/stripe/route.ts@@ -5,8 +5,11 @@ import type Stripe from "stripe";const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;-// "customer.created"-const SUPPORTED_STRIPE_EVENTS = ["charge.succeeded"];+const relevantEvents = new Set([+ "charge.succeeded",+ "payment_intent.succeeded",+ "checkout.session.completed",+]);export const POST = async (req: Request) => {const buf = await req.text();@@ -22,15 +25,17 @@ export const POST = async (req: Request) => {const event = stripe.webhooks.constructEvent(buf, sig, webhookSecret);// Ignore unsupported events- if (!SUPPORTED_STRIPE_EVENTS.includes(event.type)) {+ if (!relevantEvents.has(event.type)) {return new Response("OK", {status: 200,});}switch (event.type) {- case "charge.succeeded":- await handleChargeSucceeded(event);+ // case "charge.succeeded":+ // case "payment_intent.succeeded":+ case "checkout.session.completed":+ await checkoutSessionCompleted(event);break;}@@ -46,51 +51,36 @@ export const POST = async (req: Request) => {}};-// Handle the "charge.succeeded" event-async function handleChargeSucceeded(event: Stripe.Event) {- const charge = event.data.object as Stripe.Charge;-- console.log("event", charge);-- if (!charge.customer) {- return;- }-- // Find customer details using charge.customer- const customer = (await stripe.customers.retrieve(- charge.customer as string,- )) as Stripe.Customer;-- const customerId = customer.metadata?.customerId || null;+async function checkoutSessionCompleted(event: Stripe.Event) {+ const charge = event.data.object as Stripe.Checkout.Session;+ const customerId = charge.metadata?.dubCustomerId || null;if (!customerId) {+ console.error("No `dubCustomerId` found in metadata", charge);return;}- console.log("customer", customer);-- // Check customerId exists in TB-- // TODO: Update clickIdconst leadEvent = await getLeadEvent({ customer_id: customerId });if (!leadEvent || leadEvent.data.length === 0) {+ console.error("No lead event found for `dubCustomerId`", customerId);return;}await recordSale({...leadEvent.data[0],+ timestamp: new Date(Date.now()).toISOString(),event_id: nanoid(16),payment_processor: "stripe",product_id: "",- amount: charge.amount,- currency: charge.currency,+ amount: charge.amount_total || 0,+ currency: charge.currency || "usd",recurring: true, // TODO: Update thisrecurring_interval: "month", // TODO: Update thisrecurring_interval_count: 1, // TODO: Update thisrefunded: false,metadata: {charge,- } as any, // TODO: Fix this type+ },});}diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.tsindex 4b6ea047a8..d29c4f94a5 100644--- a/apps/web/lib/zod/schemas/sales.ts+++ b/apps/web/lib/zod/schemas/sales.ts@@ -29,6 +29,8 @@ export const saleEventSchemaTB = clickEventSchemaTB.omit({ url: true }).and(recurring_interval: z.enum(["month", "year"]).default("month"),recurring_interval_count: z.number().int().positive().default(1),refunded: z.boolean().default(false),- metadata: z.string().default(""),+ metadata: z+ .record(z.unknown())+ .transform((val) => (val ? JSON.stringify(val) : "")),}),);diff --git a/packages/stripe-app/stripe-app.json b/packages/stripe-app/stripe-app.jsonindex eda9d44c28..109da28dc2 100644--- a/packages/stripe-app/stripe-app.json+++ b/packages/stripe-app/stripe-app.json@@ -1,6 +1,6 @@{"id": "com.example.dub",- "version": "0.0.8",+ "version": "0.0.10","name": "Dub","icon": "","permissions": [@@ -19,6 +19,10 @@{"permission": "charge_read","purpose": "Allows Dub to read charge information."+ },+ {+ "permission": "checkout_session_read",+ "purpose": "Allows Dub to read checkout session information."}],"ui_extension": {@@ -29,7 +33,7 @@}},"allowed_redirect_uris": [- "https://4eaf-103-181-40-87.ngrok-free.app/api/stripe-app/callback"+ "https://1402-103-181-40-112.ngrok-free.app/api/campaigns/callback/stripe"],"stripe_api_access_type": "oauth","distribution_type": "public"