Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
Hi team,
I cannot able to validate the web hook response from the shopify. and i am using the following code to validate the signature.
Below code is on app.js
app.use(bodyParser.json({ type:'application/json', limit: '50mb', verify: function(req, res, buf, encoding) { if (req.url.startsWith('/webhook')){ req.rawbody = buf; } } }) ); app.use("/webhook", webhookRouter);
Below on webhook.router.js
router.post('/orders/create', verifyWebhook, async (req, res) => { console.log('🎉 We got an order') res.sendStatus(200) });
Below for the verification function
function verifyWebhook(req, res, next) { let hmac; let data; try { hmac = req.get("X-Shopify-Hmac-SHA256"); data = req.rawbody; } catch (e) { console.log(`Webhook request failed from: ${req.get("X-Shopify-Shop-Domain")}`); res.sendStatus(200); } if (verifyHmac(JSON.stringify(data), hmac)) { // Problem Starting from Here req.topic = req.get("X-Shopify-Topic"); req.shop = req.get("X-Shopify-Shop-Domain"); return next(); } return res.sendStatus(200); }
Verify signature function
function verifyHmac(data, hmac) { if (!hmac) { return false; } else if (!data || typeof data.data !== "object") { // I am Getting Error HERE console.log('Error in data', data); return false; } const sharedSecret = config.shopify_shared_secret; const calculatedSignature = crypto .createHmac("sha256", sharedSecret) .update(Buffer.from(data), "utf8") .digest("base64"); console.log('calculatedsecret', calculatedSignature); return calculatedSignature === hmac; };
and the body I am getting it as undefined. Please sugget me how to resolve this problem
My store name is "marclab-consulting.myshopify.com"
Do let me know any if queries any!!
Solved! Go to the solution
This is an accepted solution.
Can you verify you're definitely getting to the line in your app.js:
req.rawbody = buf;
Hi @torontotom ,
I am getting the response as below for the "req.rawbody"
body <Buffer 7b 22 69 64 22 3a 38 32 30 39 38 32 39 31 31 39 34 36 31 35 34 35 30 38 2c 22 65 6d 61 69 6c 22 3a 22 6a 6f 6e 40 64 6f 65 2e 63 61 22 2c 22 63 6c 6f ... 6451 more bytes>
This is an accepted solution.