Mangopay / mangopay2-nodejs-sdk

Node.js SDK for MANGOPAY
https://www.npmjs.com/package/mangopay2-nodejs-sdk
MIT License
51 stars 38 forks source link

How to verify the event from webhook? #411

Open letruonglamit opened 4 months ago

letruonglamit commented 4 months ago

I think we need to have something for verification the event like this

app.post('/webhook', (req, res) => {
    const event = req.body;

    // Verify the event (implement verification logic if necessary)
    if (!verifyMangopaySignature(req)) {
        return res.status(400).send({ status: 'error', message: 'Invalid signature' });
    }

 })

 function verifyMangopaySignature(req) {
    const signature = req.headers['x-request-signature'];
    const body = JSON.stringify(req.body);
    const secretKey = 'your_mangopay_secret_key'; // Replace with your actual secret key

    const hash = crypto
        .createHmac('sha256', secretKey)
        .update(body)
        .digest('hex');

    return signature === hash;
}