Developer guide
How to verify crypto payment webhooks (HMAC-SHA256)
Why a payment webhook must be verified before you ship an order, and how to check an HMAC-SHA256 signature with a timestamp in Node.js and PHP.
· 6 min read
Your webhook URL is public. Anyone who finds it can send a request that says “invoice paid”. If your server believes it, you ship an order nobody paid for. That is why every payment webhook should be signed by the gateway and verified by you before anything happens.
How webhook signing works
The gateway and your server share a secret that never travels with the request. For each webhook the gateway computes an HMAC-SHA256 of the body with that secret and sends the result in a header. Your server computes the same HMAC over the body it received. If the two match, the request came from the gateway and the body wasn't changed on the way.
A timestamp is signed along with the body. Without it, an attacker who once captured a real “paid” webhook could send it again later (a replay attack). With it, you simply refuse anything older than a few minutes.
The signature header
AywaPay webhooks carry the signature in X-AywaPay-Signature:
X-AywaPay-Signature: t=1763456700,v1=5f2b…c9a1tis the time the event was signed, in Unix seconds.v1is the hex HMAC-SHA256 of the string<t>.<raw body>, keyed with your store's webhook signing secret (it starts withwhsec_).
The signing secret is not your API key. You find it in the dashboard under your store's Webhooks settings, where you can also rotate it.
The four checks
- Read the raw body exactly as it arrived. Parsing JSON first and re-serialising it changes spacing and key order, and the signature no longer matches.
- Recompute the HMAC of
t + "." + raw bodywith your signing secret. - Compare in constant time (
timingSafeEqual,hash_equals), not with==, so the comparison doesn't leak how many characters matched. - Refuse old timestamps, for example anything more than 5 minutes away from your server's clock.
Node.js (Express)
import crypto from "node:crypto";
import express from "express";
// Keep the raw body: the signature is over the exact bytes.
app.post("/webhooks/aywapay", express.raw({ type: "application/json" }), (req, res) => {
const header = req.get("X-AywaPay-Signature") ?? "";
const { t, v1 } = Object.fromEntries(header.split(",").map((part) => part.split("=")));
const expected = crypto
.createHmac("sha256", process.env.AYWAPAY_WEBHOOK_SECRET) // whsec_…
.update(`${t}.${req.body}`)
.digest("hex");
const isFresh = Math.abs(Date.now() / 1000 - Number(t)) < 300; // refuse replays
const isValid =
v1?.length === expected.length && crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
if (!isFresh || !isValid) return res.status(400).send("bad signature");
const event = JSON.parse(req.body);
// Process each event.id once; the same event can arrive twice.
if (event.type === "invoice.paid" || event.type === "invoice.overpaid") {
// mark order event.data.order_id as paid
}
res.sendStatus(200);
});PHP
<?php
$secret = getenv('AYWAPAY_WEBHOOK_SECRET'); // whsec_…
$raw = file_get_contents('php://input'); // the exact bytes
parse_str(str_replace(',', '&', $_SERVER['HTTP_X_AYWAPAY_SIGNATURE'] ?? ''), $sig);
$expected = hash_hmac('sha256', ($sig['t'] ?? '') . '.' . $raw, $secret);
$fresh = abs(time() - (int) ($sig['t'] ?? 0)) < 300;
if (!$fresh || !hash_equals($expected, $sig['v1'] ?? '')) {
http_response_code(400);
exit('bad signature');
}
$event = json_decode($raw, true);
// Process each $event['id'] once.
http_response_code(200);After the signature checks out
- Process each event once. Webhooks are retried until your server answers 2xx, so the same event id can arrive twice. Store the ids you have handled.
- Answer quickly. Return 200 first and do slow work (emails, fulfilment) in a background job.
- Trust the data, then confirm if you like. For high-value orders you can also read the invoice with
GET /api/v2/invoices/:idbefore shipping. - Watch the clock. If your server's time drifts, fresh webhooks look old. Keep NTP on.
When the signature doesn't match
| Symptom | Usual cause |
|---|---|
| Every webhook fails | The API key was used instead of the whsec_ signing secret, or the secret was rotated. |
| Fails only behind a framework | The body was parsed to JSON before hashing. Hash the raw bytes. |
| Fails only sometimes | Your server clock is off by more than the tolerance. |
| Works locally, fails in production | A proxy or middleware rewrote the body (compression, re-encoding). |
The full event list and payloads are in the API documentation. New to the API? Start with how to integrate a crypto payment API.
Frequently asked questions
- Is checking the source IP enough instead of a signature?
- No. IP addresses change and can sit behind shared infrastructure. A signature proves both who sent the webhook and that the body wasn't changed.
- What should my server answer?
- Any 2xx status once you have accepted the event. Anything else, or a timeout, makes the gateway retry it later.
- Do the shop plugins verify webhooks too?
- Yes. The WooCommerce, WHMCS, PrestaShop and OpenCart plugins check the same signature with the secret you paste into their settings.