const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2025-03-31.basil",
});
const allowedEvents: Stripe.Event.Type[] = [
"checkout.session.completed",
.........
"payment_intent.canceled",
];
async function processEvent(event: Stripe.Event) {
// Skip processing if the event isn't one I'm tracking (list of all events below)
if (!allowedEvents.includes(event.type)) return;
console.log("[STRIPE WEBHOOKS] Processing event", event.type);
// All the events I track have a customerId
const { customer: customerId } = event?.data?.object as {
customer: string; // Sadly TypeScript does not know this
};
// This helps make it typesafe and also lets me know if my assumption is wrong
if (typeof customerId !== "string") {
throw new Error(
`[STRIPE HOOK][CANCER] ID isn't string.\nEvent type: ${event.type}`,
);
}
return await syncStripeDataToKV(customerId);
}
export async function POST(req: NextRequest) {
const body = await req.text();
const signature = (await headers()).get("Stripe-Signature");
if (!signature) return NextResponse.json({}, { status: 400 });
........
return NextResponse.json({ received: true });
}