Hello, everyone, I have the same problem that was here.
In short, “hmac” from the webhook header and from my function are different, how can I fix it?
According to all the comments I’ve made some changes to fix it, I suppose that my hash function works correctly, I also work with raw data and obviously, I double-checked that my secret key is the same.
Also, I used different “crypto” services, but the result was always the same.
Here is my current simple code that I wrote just to test the “hmac” validation
import express from 'express';
import crypto from 'node:crypto';
import bodyParser from 'body-parser';
const app = express();
const PORT = 9000;
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb' }));
app.post('/shopify/webhooks/customer-data-request', bodyParser.raw({ type: 'application/json' }),async (req, res) => {
try {
const secret = 'SOME_API_SECRET_KEY'
const hmac = req.headers['x-shopify-hmac-sha256'];
const genHash = crypto
.createHmac('sha256', secret)
.update(req.body, 'utf8')
.digest('base64');
if (genHash !== hmac) {
return res.status(401).send('Couldn\'t verify incoming Webhook request!');
}
//...do something
} catch (error) {
console.error(error);
}
});
app.use(bodyParser.json());
app.listen(PORT, (err) => {
if (!err) {
console.log(`App Listen ${PORT}`);
}
});