Verify webhook signatures
Every webhook carries a NOGORPAY-SIGNATURE header. Check it to make sure the request came from NogorPay and wasn’t changed.
How the signature works
Section titled “How the signature works”- Algorithm: HMAC-SHA256, hex encoded, prefixed with
sha256=. - Message: the raw request body, exactly as received.
- Secret: the API key that was used to create the charge.
NOGORPAY-SIGNATURE: sha256=<hex(HMAC_SHA256(api_key, raw_body))>Examples
Section titled “Examples”// Expressconst crypto = require('crypto');const express = require('express');const app = express();
function isValidSignature(rawBody, header, secret) { const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex'); const a = Buffer.from(header || ''); const b = Buffer.from(expected); return a.length === b.length && crypto.timingSafeEqual(a, b);}
app.post( '/api/nogorpay/webhook', express.raw({ type: 'application/json' }), (req, res) => { const signature = req.get('NOGORPAY-SIGNATURE'); if (!isValidSignature(req.body, signature, process.env.NOGORPAY_API_KEY)) { return res.status(401).send('Invalid signature'); }
const event = JSON.parse(req.body.toString('utf8')); if (event.event === 'charge.completed') { // Check event.amount against your order, then mark event.invoice_id as paid. } res.sendStatus(200); },);<?php$rawBody = file_get_contents('php://input');$signature = $_SERVER['HTTP_NOGORPAY_SIGNATURE'] ?? '';$secret = getenv('NOGORPAY_API_KEY');
$expected = 'sha256=' . hash_hmac('sha256', $rawBody, $secret);
if (!hash_equals($expected, $signature)) { http_response_code(401); exit('Invalid signature');}
$event = json_decode($rawBody, true);if ($event['event'] === 'charge.completed') { // Check $event['amount'] against your order, then mark $event['invoice_id'] as paid.}http_response_code(200);In Laravel, use $request->getContent() for the raw body and $request->header('NOGORPAY-SIGNATURE') for the header. Exclude the webhook route from CSRF protection.
# Flaskimport hashlibimport hmacimport os
from flask import Flask, abort, request
app = Flask(__name__)
@app.post("/api/nogorpay/webhook")def nogorpay_webhook(): raw_body = request.get_data() signature = request.headers.get("NOGORPAY-SIGNATURE", "") secret = os.environ["NOGORPAY_API_KEY"].encode()
expected = "sha256=" + hmac.new(secret, raw_body, hashlib.sha256).hexdigest() if not hmac.compare_digest(expected, signature): abort(401)
event = request.get_json() if event["event"] == "charge.completed": pass # Check event["amount"], then mark event["invoice_id"] as paid. return "", 200In Django, read request.body and request.headers["NOGORPAY-SIGNATURE"], and mark the view @csrf_exempt.
Troubleshooting
Section titled “Troubleshooting”| Problem | Likely cause |
|---|---|
| Signature never matches | Your framework parsed the body before you hashed it. Use the raw body. |
| Works for new charges, fails for old ones | You rotated API keys. Old charges are signed with the key that created them. |
| Compare throws an error in Node.js | timingSafeEqual needs equal-length buffers. Check the length first, as in the example. |
