coinbase-commerce-node icon indicating copy to clipboard operation
coinbase-commerce-node copied to clipboard

Error occured. Invalid payload provided. No JSON object could be decoded.

Open MrlolDev opened this issue 3 years ago • 2 comments

I have this error when I send a test.

app.post('/pay/crypto/charge', rate, async (req, res) => {
  const webhookSecret = 'my-secret';
  var event;

	console.log(req.headers);

	try {
		event = Webhook.verifyEventBody(
			req.rawBody,
			req.headers['x-cc-webhook-signature'],
			webhookSecret
		);
    console.log(event);
	} catch (error) {
		console.log('Error occured', error.message);

		return res.status(400).send('Webhook Error:' + error.message);
	}
})

MrlolDev avatar Sep 14 '21 19:09 MrlolDev

Hello @MrlolDev If you are using express, probably the issue comes from bodyParser or express.json() middleware. You could add this to solve the issue with the rawBody.

app.use(
  express.json({
    verify: (req, res, buf) => {
      req.rawBody = buf;
    },
  })
);

You can find a more detailed explanation here: https://flaviocopes.com/express-get-raw-body/

And this is my repo using this package: https://github.com/FaztWeb/nodejs-coibase-commerce

fazt avatar Dec 02 '21 04:12 fazt

I had same issue and I am able to fix it after using JSON.stringify(req.body) instead of req.rawBody. Something like this

app.post("/webhook", async (req, res) => { try { const event = Webhook.verifyEventBody( JSON.stringify(req.body), req.headers['x-cc-webhook-signature'], process.env.COINBASE_WEBHOOK_SECRET ); console.log(event); } catch (error) { res.status(400).json({ error }); } })

muhammadsaqib95 avatar Oct 06 '23 19:10 muhammadsaqib95