Created
July 10, 2023 19:00
-
-
Save SimRunBot/6aff22c5add1631fed38edd302bbbe7d to your computer and use it in GitHub Desktop.
Stripe Webhook Event Handler | Signature Verification with request body as string | Next.js 13.4 appDir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file path: app/api/stripe/webhook/route.ts | |
import { prisma } from "@/lib/prisma"; | |
import { NextResponse } from "next/server"; | |
import Stripe from "stripe"; | |
export async function POST(request: Request) { | |
const body = await request.text(); | |
const sig = request.headers.get("Stripe-Signature"); | |
if (!sig) { | |
console.log("No signature"); | |
return NextResponse.json({ error: "No signature" }); | |
} | |
const stripe = new Stripe(process.env.STRIPE_API_KEY!, { | |
apiVersion: "2022-11-15", | |
typescript: true, | |
}); | |
let event: Stripe.Event; | |
try { | |
event = stripe.webhooks.constructEvent( | |
body, | |
sig, | |
process.env.STRIPE_WEBHOOK_SECRET! | |
); | |
} catch (err: any) { | |
console.log(`Webhook signature verification failed.`, err.message); | |
return NextResponse.json({ error: err }); | |
} | |
console.log("received ", event.type); | |
// Handle the event | |
switch (event.type) { | |
case "payment_intent.succeeded": | |
const paymentIntent = event.data.object as Stripe.Checkout.Session; | |
const userId = Number(paymentIntent?.metadata?.userId) || 1; | |
console.log("userId ", userId); | |
await prisma.user.update({ | |
where: { | |
id: userId, | |
}, | |
data: { | |
tokens: { | |
increment: 1, | |
}, | |
paid: true, | |
}, | |
}); | |
break; | |
case "payment_method.attached": | |
const paymentMethod = event.data.object; | |
break; | |
// ... handle other event types | |
default: | |
console.log(`Unhandled event type ${event.type}`); | |
} | |
// Return a response to acknowledge receipt of the event | |
return NextResponse.json({ received: true }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment