我正在尝试在我的 NextJS 项目中实现 Stripe webhook。我正在使用 NextJS 14.2.3 应用程序路由器。 这是我在route.ts文件中的代码
export async function POST(req: Request) {
let lineitems;
let event: Stripe.Event;
const body = await req.text();
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string);
const signature = headers().get("stripe-signature") as string;
try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_WEBHOOK_KEY as string
);
return NextResponse.json({ message: "Webhook call is successful" });
} catch (error) {
if (error instanceof Error)
return NextResponse.json({ message: error.message });
}
}
但是在条纹仪表板中,我总是收到以下错误
{
"message": "No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? \n If a webhook request is being forwarded by a third-party tool, ensure that the exact request body, including JSON formatting and new line style, is preserved.\n\nLearn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing\n"
}
我已经尝试了一切,使用“micro”,“raw-body”,仍然无法弄清楚。 它在本地主机中工作正常,但在已部署的 Webhook 端点中工作不正常。
请帮忙!!!