Shopify provides examples in Ruby and PHP to accomplish this. In my node/express app I try:
var data = querystring.stringify(req.body);
var calculatedSha256 = crypto.createHmac("SHA256", APP_SECRET).update(new Buffer(data, 'utf8')).digest('base64');
and also
var data = req.body;
var calculatedSha256 = crypto.createHmac("SHA256", APP_SECRET).update(new Buffer(data, 'utf8')).digest('base64');
but none of them provides an identical string to the one Shopify sends as a signature.
A bit old, but figured I'd post my solution:
var express = require('express')
, bodyParser = require('body-parser')
, crypto = require('crypto');
var app = express();
app.use(bodyParser.json({ verify: function(req, res, buf, encoding) {
req.headers['x-generated-signature'] = crypto.createHmac('sha256', 'SHARED_SECRET')
.update(buf)
.digest('base64');
} }));
app.post('/webhook', function(req, res) {
if (req.headers['x-generated-signature'] != req.headers['x-shopify-hmac-sha256']) {
return res.status(401).send('Invalid Signature');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With