NextJS 14.2.3 中的 Stripe 签名验证失败

问题描述 投票:0回答:1

我正在尝试在我的 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 端点中工作不正常。

请帮忙!!!

next.js stripe-payments webhooks app-router
1个回答
0
投票

看起来您还没有禁用默认的正文解析行为。将

api.bodyParser
设置为 false,然后重试。您可以找到有关此的更多详细信息doc

© www.soinside.com 2019 - 2024. All rights reserved.