条纹 "No signatures found matching the expected signature for payload."

Stripe "No signatures found matching the expected signature for payload."

所以我最近处理了一些条纹代码,我决定是时候使用 webhook 了。 stripe网页上的webhooks页面看了下,复制了源码也没用

client.post('/webhook', express.raw({ type: 'application/json' }), (request, response) => {
        const sig = request.headers['stripe-signature'];
        let event;
        try {
            event = exports.stripe.webhooks.constructEvent(request.body, sig, code);
        }
        catch (err) {
            response.status(400).send(`Webhook Error: ${err.message}`);
            return;
        }
        // Handle the event
        switch (event.type) {
            case 'payment_intent.succeeded':
                const paymentIntent = event.data.object;
                console.log(paymentIntent);
                break;
            // ... handle other event types
            default:
                console.log(`Unhandled event type ${event.type}`);
        }
        // Return a 200 response to acknowledge receipt of the event
        response.send();
    });

我已经多次检查我的代码是否相同,但确实如此。有人能告诉我怎么了吗?如果是这样的话,我也在监听端口 3001。

所以现在我知道是什么修改了请求的原始数据。这是我的 api 路由使用的 express.json() 中间件。将它们移到 webhook 下面后,它就像一个魅力!

client.post('/webhook', express.raw({ type: 'application/json' }), (request, response) => {
        const sig = request.headers['stripe-signature'];

        let event: Stripe.Event;

        try {
            event = stripe.webhooks.constructEvent(request.body, sig, code);
        } catch (err) {
            response.status(400).send(`Webhook Error: ${err.message}`);
            return;
        }

        // Handle the event
        switch (event.type) {
            case 'payment_intent.succeeded':
                const paymentIntent = event.data.object;
                console.log(paymentIntent);

                break;
            // ... handle other event types
            default:
                console.log(`Unhandled event type ${event.type}`);
        }

        // Return a 200 response to acknowledge receipt of the event
        response.send();
    });
    client.use(express.json());

    client.use("/api/", router);