UNPKG

nextjs-saaskit

Version:
41 lines (35 loc) 1.3 kB
export async function POST(req : Request) { try { const crypto = typeof window === 'undefined' ? require('crypto') : null; if (!crypto) { throw new Error("crypto is required"); } // Catch the event type const clonedReq = req.clone(); const eventType = req.headers.get("X-Event-Name"); const body = await req.json(); // Check signature const secret = process.env.LEMON_SQUEEZY_WEBHOOK_SIGNATURE; if (!secret) { throw new Error("Webhook signature secret is not defined."); } const hmac = crypto.createHmac("sha256", secret); const digest = Buffer.from( hmac.update(await clonedReq.text()).digest("hex"), "utf8" ); const signature = Buffer.from(req.headers.get("X-Signature") || "", "utf8"); if (!crypto.timingSafeEqual(digest, signature)) { throw new Error("Invalid signature."); } console.log(body); if (eventType === "order_created") { const userId = body.meta.custom_data.user_id; const isSuccessful = body.data.attributes.status === "paid"; } return Response.json({ message: "Webhook received" }); } catch (err) { console.error(err); return Response.json({ message: "Something went wrong" }, { status: 500 }); } }