|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { stripe, syncStripeData } from "@/lib/stripe"; |
| 3 | +import { db } from "@/lib/db"; |
| 4 | +import { profiles, purchases } from "@/lib/db/schema"; |
| 5 | +import { eq } from "drizzle-orm"; |
| 6 | +import Stripe from "stripe"; |
| 7 | + |
| 8 | +const allowedEvents: Stripe.Event.Type[] = [ |
| 9 | + "checkout.session.completed", |
| 10 | + "customer.subscription.created", |
| 11 | + "customer.subscription.updated", |
| 12 | + "customer.subscription.deleted", |
| 13 | + "customer.subscription.paused", |
| 14 | + "customer.subscription.resumed", |
| 15 | + "invoice.paid", |
| 16 | + "invoice.payment_failed", |
| 17 | + "invoice.payment_succeeded", |
| 18 | +]; |
| 19 | + |
| 20 | +export async function POST(request: NextRequest) { |
| 21 | + const body = await request.text(); |
| 22 | + const signature = request.headers.get("stripe-signature"); |
| 23 | + |
| 24 | + if (!signature) { |
| 25 | + return NextResponse.json({ error: "No signature" }, { status: 400 }); |
| 26 | + } |
| 27 | + |
| 28 | + let event: Stripe.Event; |
| 29 | + try { |
| 30 | + event = stripe.webhooks.constructEvent( |
| 31 | + body, |
| 32 | + signature, |
| 33 | + process.env.STRIPE_WEBHOOK_SECRET!, |
| 34 | + ); |
| 35 | + } catch (err) { |
| 36 | + console.error("Webhook signature verification failed:", err); |
| 37 | + return NextResponse.json({ error: "Invalid signature" }, { status: 400 }); |
| 38 | + } |
| 39 | + |
| 40 | + if (!allowedEvents.includes(event.type)) { |
| 41 | + return NextResponse.json({ received: true }); |
| 42 | + } |
| 43 | + |
| 44 | + if (event.type === "checkout.session.completed") { |
| 45 | + const session = event.data.object as Stripe.Checkout.Session; |
| 46 | + |
| 47 | + if (session.mode === "payment" && session.customer) { |
| 48 | + const customerId = session.customer as string; |
| 49 | + |
| 50 | + const profile = await db.query.profiles.findFirst({ |
| 51 | + where: eq(profiles.stripeCustomerId, customerId), |
| 52 | + }); |
| 53 | + |
| 54 | + if (profile) { |
| 55 | + const lineItems = await stripe.checkout.sessions.listLineItems( |
| 56 | + session.id, |
| 57 | + { limit: 1 }, |
| 58 | + ); |
| 59 | + const item = lineItems.data[0]; |
| 60 | + if (item) { |
| 61 | + await db.insert(purchases).values({ |
| 62 | + userId: profile.id, |
| 63 | + stripePriceId: item.price?.id ?? "", |
| 64 | + stripeSessionId: session.id, |
| 65 | + productName: item.description ?? "Product", |
| 66 | + amount: session.amount_total ?? 0, |
| 67 | + currency: session.currency ?? "cad", |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return NextResponse.json({ received: true }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const { customer: customerId } = event.data.object as { |
| 77 | + customer: string; |
| 78 | + }; |
| 79 | + |
| 80 | + if (typeof customerId !== "string") { |
| 81 | + console.error( |
| 82 | + `[STRIPE WEBHOOK] No customer ID on event type: ${event.type}`, |
| 83 | + ); |
| 84 | + return NextResponse.json({ received: true }); |
| 85 | + } |
| 86 | + |
| 87 | + await syncStripeData(customerId); |
| 88 | + |
| 89 | + return NextResponse.json({ received: true }); |
| 90 | +} |
0 commit comments