The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'conversion' of https://github.com/dubinc/dub into conversion
Kiran Kab49c8536756
diff --cc apps/web/app/api/campaigns/webhooks/stripe/route.tsindex ab3cb019d0,99b6a2faf5..6286ea823b--- a/apps/web/app/api/campaigns/webhooks/stripe/route.ts+++ b/apps/web/app/api/campaigns/webhooks/stripe/route.ts@@@ -1,13 -1,6 +1,13 @@@- import { prisma } from "@/lib/prisma";+ import { prismaEdge } from "@/lib/prisma/edge";import { stripe } from "@/lib/stripe";-import { getLeadEvent, recordSale } from "@/lib/tinybird";+import {+ getClickEvent,+ getLeadEvent,+ recordCustomer,+ recordLead,+ recordSale,+} from "@/lib/tinybird";+import { clickEventSchemaTB } from "@/lib/zod/schemas";import { nanoid } from "@dub/utils";import type Stripe from "stripe";@@@ -55,72 -48,13 +55,72 @@@ 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;+ const stripeAccountId = event.account as string;+ const stripeCustomerId = charge.customer as string;++ // Find customer- const customer = await prisma.customer.findFirst({++ const customer = await prismaEdge.customer.findFirst({+ where: {+ projectConnectId: stripeAccountId,+ stripeCustomerId,+ },+ });++ if (!customer) {+ return;+ }++ // Find lead+ const leadEvent = await getLeadEvent({ customer_id: customer.id });+ if (!leadEvent || leadEvent.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,++ // How do we get these?+ recurring: 0,+ product_id: "",+ recurring_interval: "month",+ recurring_interval_count: 1,++ metadata: JSON.stringify({+ charge,+ }),+ });+}+// Handle event "checkout.session.completed"async function checkoutSessionCompleted(event: Stripe.Event) {const charge = event.data.object as Stripe.Checkout.Session;- const customerId = charge.metadata?.dubCustomerId || null;+ const externalId = charge.metadata?.dubCustomerId || null;- if (!customerId) {- console.error("No `dubCustomerId` found in metadata", charge);+ if (!externalId) {+ return;+ }++ // Find customer- const customer = await prisma.customer.findFirst({++ const customer = await prismaEdge.customer.findFirst({+ where: {+ externalId,+ projectConnectId: event.account,+ },+ });++ // TODO:+ // Should we create a customer if not found?++ if (!customer) {return;}@@@ -165,22 -94,11 +165,22 @@@ async function customerCreated(event: Sreturn;}- const customer = await prismaEdge.customer.findUnique({- where: {- projectConnectId_externalId: {- projectConnectId,- externalId,+ const clickData = clickEventSchemaTB+ .omit({ timestamp: true })+ .parse(clickEvent.data[0]);++ // Create customer- const customer = await prisma.customer.create({++ const customer = await prismaEdge.customer.create({+ data: {+ name: stripeCustomer.name,+ email: stripeCustomer.email,+ stripeCustomerId: stripeCustomer.id,+ projectConnectId: stripeAccountId,+ externalId,+ project: {+ connect: {+ stripeConnectId: stripeAccountId,+ },},},});