Stripe Error: No signatures found matching the expected signature for payload

前端 未结 5 987
眼角桃花
眼角桃花 2021-01-03 19:02

I have a stripe webhook that call a Firebase function. In this function I need to verify that this request comes from Stripe servers. Here is the code :

cons         


        
相关标签:
5条回答
  • 2021-01-03 19:28

    I was able to obtain data from one webhook but not from a second one: the problem was that the secret key I used was the same as the one used for the first webhook, but I found out that every webhook has a different key, that's way I got that same message.

    0 讨论(0)
  • 2021-01-03 19:30

    This happened to me when sending a test webhook from the Stripe dashboard after I had renamed a firebase cloud function. All my other functions were working fine. Solved by re-setting in the terminal firebase functions:config:set stripe.webhook_signature="Your webhook signing secret" (if you're using that) and redeploying the functions firebase deploy --only functions

    On a second occasion I solved the problem by rolling the stripe signature in the stripe dashboard.

    0 讨论(0)
  • 2021-01-03 19:33

    Cloud Functions automatically parses body content of known types. If you're getting JSON, then it's already parsed and available to you in req.body. You shouldn't need to add other body parsing middleware.

    If you need to process the raw data, you should use req.rawBody, but I don't think you'll need to do that here.

    0 讨论(0)
  • 2021-01-03 19:37

    Here is code which is working for me:

    app.use(bodyParser.json({
      verify: function (req, res, buf) {
        var url = req.originalUrl;
        if (url.startsWith('/stripe')) {
           req.rawBody = buf.toString();
        }
      }
    }));
    

    And then pass the req.rawBody for verification

    stripe.checkWebHook(req.rawBody, signature);
    

    Reference: https://github.com/stripe/stripe-node/issues/341

    0 讨论(0)
  • 2021-01-03 19:45

    Here is what is working for me:

    add this line:

    app.use('/api/subs/stripe-webhook', bodyParser.raw({type: "*/*"}))
    

    just before this line:

    app.use(bodyParser.json());
    

    (it doesn't affect all your operation, just this: '/api/subs/stripe-webhook')

    Then:

    const endpointSecret = 'whsec_........'
    
    const stripeWebhook = async (req, res) => {
        const sig = req.headers['stripe-signature'];
    
        let eventSecure = {}
        try {
            eventSecure = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
            //console.log('eventSecure :', eventSecure);
        }
        catch (err) {
            console.log('err.message :', err.message);
            res.status(400).send(`Webhook Secure Error: ${err.message}`)
            return
        }
        res.status(200).send({ received: true });
    }
    
    0 讨论(0)
提交回复
热议问题