Callback Signature Verification
The X-StagePay-Signature HMAC-SHA256 signature specification, verification examples in Node.js, PHP, Python, Go, and Java, and fixed test vectors.
Every callback carries a signature created with the app's webhookSecret. Verifying the signature confirms that the request was sent by StagePay, that the body has not been tampered with, and that it is not a replay of an old request.
Specification
signed_payload = X-StagePay-Timestamp + "." + rawBody
signature = "v1=" + hex( HMAC-SHA256( key = webhookSecret, message = signed_payload ) )| Item | Value |
|---|---|
| Algorithm | HMAC-SHA256; the result is 64 lowercase hexadecimal characters |
| Key | The app's full webhookSecret string (including the whsec_ prefix), as UTF-8 bytes |
| Message | Timestamp string + . + the received body bytes exactly as received |
| Header format | X-StagePay-Signature: v1=<hex> |
| Time tolerance | |now − X-StagePay-Timestamp| ≤ 300 seconds |
Verification steps:
- Check that
X-StagePay-Timestampis an integer and within 300 seconds of the current time. - Compute the expected signature from the raw body before parsing.
- Compare it with
X-StagePay-Signatureusing a constant-time comparison (timing-safe compare). - If it passes, check for duplicates using
X-StagePay-Delivery, then process it.
The most common mistake: computing the signature from parsed JSON
If you parse the JSON and serialize it again, whitespace, key order, Unicode escapes (\uXXXX), and number formatting can change, and the signature will not match. Capture the raw bytes before your framework's body parser runs (Express express.raw(), PHP php://input, Flask request.get_data(), Go io.ReadAll(r.Body), Spring @RequestBody byte[]).
Examples
Each example's verification function can take now as an argument, so you can unit test it with the test vectors below.
import crypto from 'node:crypto';
/**
* @param {Buffer|string} rawBody Raw received body
* @param {string} timestamp X-StagePay-Timestamp
* @param {string} signature X-StagePay-Signature
* @param {string} secret webhookSecret (whsec_...)
*/
export function verifyStagePaySignature(rawBody, timestamp, signature, secret, toleranceSec = 300, now = Math.floor(Date.now() / 1000)) {
if (!/^\d+$/.test(timestamp ?? '')) return false;
if (Math.abs(now - Number(timestamp)) > toleranceSec) return false;
const body = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody, 'utf8');
const expected =
'v1=' + crypto.createHmac('sha256', secret).update(`${timestamp}.`).update(body).digest('hex');
const a = Buffer.from(expected);
const b = Buffer.from(signature ?? '');
return a.length === b.length && crypto.timingSafeEqual(a, b);
}import express from 'express';
import { verifyStagePaySignature } from './stagepay-signature.mjs';
const app = express();
app.post('/stagepay/callback', express.raw({ type: '*/*' }), (req, res) => {
const ok = verifyStagePaySignature(
req.body,
req.get('X-StagePay-Timestamp'),
req.get('X-StagePay-Signature'),
process.env.STAGEPAY_WEBHOOK_SECRET,
);
if (!ok) return res.status(401).end();
const event = JSON.parse(req.body.toString('utf8'));
// ... idempotency check, then grant
res.sendStatus(200);
});
app.listen(3000);Test vectors
Use the fixed values below to check that your implementation is correct. All five language examples produce the same result.
| Item | Value |
|---|---|
webhookSecret | whsec_3f8a1c9e5b7d2f4a6c8e0b1d3f5a7c9e1b3d5f7a9c0e2b4d6f8a0c2e4b6d8f0a |
X-StagePay-Timestamp | 1767225600 |
| Expected signature | v1=3246f40b61ee5eba6e703fc1c8e763e19576a057391094c6300cd0cd26ebde7e |
Body (raw body, no trailing newline, 206 bytes):
{"event":"payment.succeeded","tid":"KCP-20260101090000-a1b2c3","appId":"6650f0c2a1b2c3d4e5f60718","amount":12000,"amountMinor":12000,"currency":"KRW","status":"success","paidAt":"2026-01-01T00:00:00+00:00"}# Node.js
node -e "const c=require('crypto');const b='{\"event\":\"payment.succeeded\",\"tid\":\"KCP-20260101090000-a1b2c3\",\"appId\":\"6650f0c2a1b2c3d4e5f60718\",\"amount\":12000,\"amountMinor\":12000,\"currency\":\"KRW\",\"status\":\"success\",\"paidAt\":\"2026-01-01T00:00:00+00:00\"}';console.log('v1='+c.createHmac('sha256','whsec_3f8a1c9e5b7d2f4a6c8e0b1d3f5a7c9e1b3d5f7a9c0e2b4d6f8a0c2e4b6d8f0a').update('1767225600.'+b).digest('hex'))"
# OpenSSL
printf '%s' '1767225600.{"event":"payment.succeeded","tid":"KCP-20260101090000-a1b2c3","appId":"6650f0c2a1b2c3d4e5f60718","amount":12000,"amountMinor":12000,"currency":"KRW","status":"success","paidAt":"2026-01-01T00:00:00+00:00"}' \
| openssl dgst -sha256 -hmac 'whsec_3f8a1c9e5b7d2f4a6c8e0b1d3f5a7c9e1b3d5f7a9c0e2b4d6f8a0c2e4b6d8f0a'
# → SHA2-256(stdin)= 3246f40b61ee5eba6e703fc1c8e763e19576a057391094c6300cd0cd26ebde7ePassing now = 1767225600 to the verification function should return true; changing one character of the signature or passing now = 1767225901 (301 seconds later) should return false.
Rotating the secret
- Issue a new secret in the console under Apps → Webhook secret.
- Deploy your merchant server so that, during the rotation period, it verifies with both the old and the new secret (passing if either matches).
- Activate the new secret in the console. Subsequent callbacks are signed with the new secret.
- After all callbacks awaiting retry have been processed (up to about 48 hours), remove the old secret from your server.
If you suspect the secret has leaked, rotate it immediately. The price override signature (priceSig) uses the same secret, so it is affected as well.
Webhook/Callback Events and Payloads
The event types, HTTP headers, and complete body fields of the payment completion and refund callbacks StagePay sends to the merchant server.
Retries and Idempotency
How callback failures are determined, the retry schedule (1 minute to 24 hours, up to 8 times), manual resends, and preventing duplicate grants with X-StagePay-Delivery.