NodeJS Stripe“未找到与负载的预期签名匹配的签名,”

iqxoj9l9  于 2023-05-22  发布在  Node.js
关注(0)|答案(2)|浏览(168)

所以我最近处理了一些条带代码,我决定是时候使用webhook了。我看了看条纹网页上的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章如果是这样的话

enxuqcxy

enxuqcxy1#

现在我知道是什么修改了请求的原始数据。这是我的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);
q8l4jmvw

q8l4jmvw2#

我只是使用webhook id而不是secret。secret将始终以“whsec_******************************”开头
信用:Stripe Error: No signatures found matching the expected signature for payload

相关问题