πͺWebhook Integration

Last updated
const crypto = require('crypto');
function verifyFamewallSignature(payload, receivedSignature) {
const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET_KEY);
const calculatedSignature = hmac.update(payload, 'utf-8').digest('base64');
const isValid = crypto.timingSafeEqual(
Buffer.from(calculatedSignature, 'base64'),
Buffer.from(receivedSignature, 'base64')
);
return isValid;
}
app.post('/webhook', function(req,res,next){
// Verify the payload
const isPayloadValid = verifyFamewallSignature(JSON.stringify(req.body), req.headers['x-webhook-signature']);
if (isPayloadValid) {
console.log('Payload is valid. Process the webhook.');
//verification valid
} else {
console.log('Payload is not valid. Discard the webhook.');
//verification invalid
}
} )