Webhooks
Royal Pay sends a POST to the pay-in's webhookUrl each time its status changes. Credit your user when you receive approved, after checking the signature.
When webhooks are sent #
status | Sent when |
|---|---|
pending | The payer submitted their payment proof (screenshot or UTR). Verification has started. Again if they submit a different UTR. |
approved | The payment is verified. Credit the user. |
rejected | The payment was not accepted. remarks says why. |
cancelled | The payer cancelled, left the checkout, or it timed out, before submitting a payment. remarks says which. |
No webhook is sent for progress (the pay-in was just created).
Request #
POST /royalpay/webhook HTTP/1.1
Content-Type: application/json
User-Agent: RoyalPay-Webhooks/1.0
X-Event-Id: EV-3c9d1f0a7b2e4d58
X-Timestamp: 1796040981
X-Signature: 6f1e0b0c4a1d0c9f7b0d3c2e5a8b1f4d6c9e2a5b8d1f4c7a0e3b6d9c2f5a8e1b{
"transactionId": "RP-7f3a9c21d84e5b02",
"status": "approved",
"amount": 500,
"remarks": "Payment successful",
"updatedAt": "2026-11-30T10:16:42.118Z",
"orderId": "ORDER-10045",
"userId": "USER-881",
"utrNumber": "531512345678"
}| Header | Description |
|---|---|
X-Event-Id | Unique ID of this event (EV-…). Retries of the same event keep the same ID. |
X-Timestamp | When this attempt was signed, in Unix seconds. |
X-Signature | HMAC-SHA256 of the event, hex-encoded. See below. |
The body fields are the same as Pay-in status.
Verify the signature #
Compute HMAC-SHA256 with your webhook signing key (merchant portal → View keys) over:
X-Event-Id + "." + X-Timestamp + "." + raw request bodyCompare it with X-Signature in constant time, and reject the request if the timestamp is more than 5 minutes old. Use the raw body bytes as received: parsing and re-encoding the JSON changes them.
import crypto from "node:crypto";
import express from "express";
const app = express();
// Keep the raw body: the signature is over the exact bytes.
app.post("/royalpay/webhook", express.raw({ type: "application/json" }), (req, res) => {
const id = req.get("X-Event-Id"), ts = req.get("X-Timestamp"), sig = req.get("X-Signature") || "";
const expected = crypto
.createHmac("sha256", process.env.ROYALPAY_SIGNING_KEY)
.update(`${id}.${ts}.`)
.update(req.body)
.digest("hex");
const fresh = Math.abs(Date.now() / 1000 - Number(ts)) < 300;
if (!fresh || sig.length !== expected.length ||
!crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected))) {
return res.status(401).end();
}
const event = JSON.parse(req.body);
// Idempotent: credit once per transactionId, only on "approved".
handle(event).then(() => res.status(200).end());
});$raw = file_get_contents("php://input");
$id = $_SERVER["HTTP_X_EVENT_ID"] ?? "";
$ts = $_SERVER["HTTP_X_TIMESTAMP"] ?? "";
$sig = $_SERVER["HTTP_X_SIGNATURE"] ?? "";
$expected = hash_hmac("sha256", $id . "." . $ts . "." . $raw, getenv("ROYALPAY_SIGNING_KEY"));
if (abs(time() - (int)$ts) > 300 || !hash_equals($expected, $sig)) {
http_response_code(401);
exit;
}
$event = json_decode($raw, true);
// Credit once per transactionId when $event["status"] === "approved".
http_response_code(200);import hashlib, hmac, os, time
from flask import Flask, request, abort
app = Flask(__name__)
@app.post("/royalpay/webhook")
def royalpay_webhook():
raw = request.get_data() # the exact bytes received
event_id = request.headers.get("X-Event-Id", "")
ts = request.headers.get("X-Timestamp", "0")
expected = hmac.new(os.environ["ROYALPAY_SIGNING_KEY"].encode(),
f"{event_id}.{ts}.".encode() + raw, hashlib.sha256).hexdigest()
if abs(time.time() - int(ts)) > 300 or not hmac.compare_digest(expected, request.headers.get("X-Signature", "")):
abort(401)
event = request.get_json()
# Credit once per transactionId when event["status"] == "approved".
return "", 200Responding and retries #
- Answer with any 2xx within 8 seconds. Do slow work (crediting, emails) after answering, or in a queue.
- Anything else (another status, a timeout, a connection or TLS error) is retried with the same
X-Event-Id, 10 s, 30 s, 1 min, 5 min, 15 min, 30 min, 1 h, 3 h and 6 h after each failed attempt: 10 tries over about 12 hours. - Redirects are not followed. Give the final HTTPS address.
- A
webhookUrlwhose host resolves to a private, loopback or link-local address is never called, and isn't retried.
Handling them correctly #
- Credit once. You can receive the same event more than once, for example a retry after your answer was lost. Key your crediting on
transactionIdand credit only the firstapproved. - Check the details. Before crediting, check that
orderId,userIdandamountmatch the deposit you created. - Order isn't guaranteed. Act on the status, not the arrival order:
approvedis final and wins over a latepending. - Missed one? Ask Pay-in status. It always has the current answer.