Signature Validation
Handling WebHook Requests
const crypto = require('crypto');
const secret = your-secret-key;
// Using ExpressJS
app.post("/your-server/webhook-endpoint", (req, res) => {
//validate event
const hash = crypto.createHmac('sha512', secret).update(JSON.stringify(req.body)).digest('hex');
if (hash == req.headers['x-browpay-signature']) {
// body
const event = req.body;
// you may use the data here
}
// do nothing
res.send(200);
});import hmac
import hashlib
from http.server import BaseHTTPRequestHandler, HTTPServer
class WebhookHandler(BaseHTTPRequestHandler):
def do_POST(self):
secret = b'your-secret-key'
content_length = int(self.headers['Content-Length'])
content = self.rfile.read(content_length)
# validate event
hash_signature = hmac.new(secret, content, hashlib.sha512).hexdigest()
if hash_signature == self.headers.get('x-browpay-signature'):
# process the event
event = content.decode('utf-8')
# use the data here
# do nothing
self.send_response(200)
self.end_headers()
self.wfile.write(b'OK')
if __name__ == '__main__':
server_address = ('', 8000)
httpd = HTTPServer(server_address, WebhookHandler)
httpd.serve_forever()Last updated