#dodo54

1 messages · Page 1 of 1 (latest)

pale topazBOT
lofty elbow
#

It's mostly when your web app try to modify the raw data sent from Stripe. It's a common error tho

placid tiger
#

Here is the code in server.js:

app.use('/webhook', bodyParser.raw({type: "*/*"}))
app.use(bodyParser.json());

And my webhook URL:

router.post(
  "/webhook",
  bodyParser.raw({ type: 'application/json' }),(req, res) => {

    const payload = req.body;

    const sig = req.headers["stripe-signature"];
    let event;
    try {
      event = stripe.webhooks.constructEvent(payload, sig, endpointSecret); //Here I am recieveing error
      res.status(200).end();
    } catch (e) {
      console.log(e)
        return res.status(400).send(`Webhook Error: ${e.message}`);
    }
    if (event.type === 'invoice.paid') {
        const session = event.data.object;
        sendEmailToMe(session.customer_email, session.customer_name);
        // Fulfill the purchase...
        fulfillOrder(session);
      }
  }
);
placid tiger
#

I copied this answer but not working

lofty elbow
#

bodyParse looks suspicious to me. Let's read through this issue and try out solutions there

placid tiger
#

Ok I am trying this:

app.use(bodyParser.json({
  // Because Stripe needs the raw body, we compute it but only when hitting the Stripe callback URL.
  verify: function(req,res,buf) {
      var url = req.originalUrl;
      if (url.startsWith('/webhook')) {
          req.rawBody = buf.toString()
      }
  }}));

And what should I change in this code:

router.post(
  "/webhook",
  bodyParser.raw({ type: 'application/json' }),(req, res) => {

    const payload = req.body;

    const sig = req.headers["stripe-signature"];
    let event;
    try {
      event = stripe.webhooks.constructEvent(payload, sig, endpointSecret); //Here I am recieveing error
      res.status(200).end();
    } catch (e) {
      console.log(e)
        return res.status(400).send(`Webhook Error: ${e.message}`);
    }
    if (event.type === 'invoice.paid') {
        const session = event.data.object;
        sendEmailToMe(session.customer_email, session.customer_name);
        // Fulfill the purchase...
        fulfillOrder(session);
      }
  }
);
lofty elbow
#

Um probably that's it and you can try sending webhook event again?

placid tiger
#

I try to resend it

#

But it gives the same error

#

Don't know what is happening

lofty elbow
#

Okie could you try other suggestions on the thread as well?