Webhooks
Receive signed events when payments, refunds, payouts and invoices change. Verify Epay-Signature, respond fast, and handle retries idempotently.
Webhooks are how ePay tells your server that something happened — most importantly that a payment succeeded. Fulfil orders from webhooks (or a server-side API check), never from the browser redirect alone.
Set up an endpoint
- In the dashboard open Developers → Webhooks → Add endpoint.
- Enter a public HTTPS URL on your server, e.g.
https://shop.example.lk/webhooks/epay. - Choose the event types to receive, or
*for all. - Copy the signing secret (
whsec_…). It's shown once — store it like a password, e.g.EPAY_WEBHOOK_SECRET.
Sandbox and production have separate endpoints and secrets. Deployed environments only deliver to public HTTPS addresses; private and loopback addresses are refused, so use a tunnel to receive sandbox events locally.
Event types
| Type | data.object | Sent when |
|---|---|---|
payment.succeeded | Payment | The payment was approved. Fulfil the order. |
payment.failed | Payment | An attempt was declined (failure is set). The payer may retry unless it's a legacy one-shot payment (status: "failed"). |
payment.canceled | Payment | Canceled by you or by the payer on the checkout |
payment.expired | Payment | Not paid before expires_at |
refund.created | Refund | A refund was created (may be requires_approval) |
refund.succeeded | Refund | The refund was processed |
refund.failed | Refund | The acquirer rejected the refund |
payout.created | Payout | You requested a payout |
payout.paid | Payout | The payout reached your bank |
payout.failed | Payout | The payout could not be paid to your bank |
invoice.paid | Invoice | An invoice was paid through the checkout |
New event types may be added. Respond 2xx to types you don't handle.
The request
ePay sends a POST with a JSON body — the event envelope — and these headers:
| Header | Example |
|---|---|
Content-Type | application/json |
Epay-Signature | t=1763806601,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd |
Epay-Event-Id | evt_6yR2mK9pQ4dL |
Epay-Event-Type | payment.succeeded |
User-Agent | ePay-Webhooks/1.0 |
{
"id": "evt_6yR2mK9pQ4dL",
"object": "event",
"type": "payment.succeeded",
"created_at": "2026-11-22T10:16:41.000Z",
"livemode": true,
"environment": "production",
"data": {
"object": {
"id": "pay_8Hq2LxV0cT4mZr7N",
"object": "payment",
"status": "succeeded",
"amount": 125000,
"currency": "LKR",
"amount_refunded": 0,
"fee": 3688,
"net": 121312,
"order_id": "1042",
"source": "api",
"payment_method": { "type": "card", "card_brand": "visa", "card_last4": "4242" },
"failure": null,
"metadata": { "cart_id": "c_98431" },
"livemode": true,
"succeeded_at": "2026-11-22T10:16:41.000Z"
}
}
}data.object is the full object at the time of the event (abridged above) — the same shape as the API returns.
Verify the signature
Epay-Signature has a Unix timestamp t and a signature v1:
v1 = hex( HMAC-SHA256( key = your signing secret, message = "<t>.<raw request body>" ) )To verify:
- Read the raw request body exactly as received — before any JSON parsing or re-serialisation.
- Split the header on
,and each part on the first=to gettandv1. - Reject the request if
|now − t| > 300seconds (replay protection). - Compute the HMAC-SHA256 of
t + "." + rawBodywith your full signing secret (including thewhsec_prefix) as the key, hex-encoded. - Compare it with
v1using a constant-time comparison. Reject on mismatch with400.
import crypto from "node:crypto";
import express from "express";
const TOLERANCE_SECONDS = 300;
export function verifyEpaySignature(rawBody: string, header: string | undefined, secret: string): boolean {
if (!header) return false;
const parts = Object.fromEntries(
header.split(",").map((part) => {
const i = part.indexOf("=");
return [part.slice(0, i).trim(), part.slice(i + 1).trim()];
}),
);
const t = Number(parts.t);
if (!Number.isInteger(t) || !parts.v1) return false;
if (Math.abs(Math.floor(Date.now() / 1000) - t) > TOLERANCE_SECONDS) return false;
const expected = crypto.createHmac("sha256", secret).update(`${t}.${rawBody}`).digest();
const received = Buffer.from(parts.v1, "hex");
return received.length === expected.length && crypto.timingSafeEqual(expected, received);
}
const app = express();
// Use the raw body for this route — express.json() would change the bytes.
app.post("/webhooks/epay", express.raw({ type: "application/json" }), async (req, res) => {
const rawBody = req.body.toString("utf8");
if (!verifyEpaySignature(rawBody, req.get("Epay-Signature"), process.env.EPAY_WEBHOOK_SECRET!)) {
return res.status(400).send("invalid signature");
}
const event = JSON.parse(rawBody);
// Idempotency: ignore events you've already processed.
if (await db.webhookEvents.exists(event.id)) return res.sendStatus(200);
await queue.enqueue(event); // do the real work asynchronously
await db.webhookEvents.insert(event.id);
res.sendStatus(200);
});Common verification mistakes
- Parsing and re-serialising the JSON before computing the HMAC (whitespace and key order change). Always use the raw bytes.
- Using
==instead of a constant-time comparison. - Skipping the timestamp check — without it a captured request could be replayed forever.
- Using the sandbox secret in production (or vice versa).
Retries
A delivery succeeds when your endpoint returns any 2xx within 10 seconds. Anything else — a non-2xx status, a timeout, a TLS or connection error — is retried on this schedule:
| Attempt | Delay after previous failure |
|---|---|
| 2 | 1 minute |
| 3 | 5 minutes |
| 4 | 15 minutes |
| 5 | 1 hour |
| 6 | 3 hours |
| 7 | 6 hours |
| 8 | 12 hours |
| 9 | 24 hours |
After the 9th failed attempt (about two days later) the delivery is marked dead. You can see every delivery, its response code and error, and retry it manually in Developers → Webhooks. To recover from a longer outage, list what you missed with GET /v1/events.
Best practices
- Respond fast. Verify, persist or enqueue, return
200. Do slow work (emails, ERP sync) in a background job — the 10-second limit includes your processing time. - Be idempotent. Retries and network races mean you can receive the same event more than once. Store processed
event.ids (also sent asEpay-Event-Id) and skip duplicates; make fulfilment itself safe to run twice. - Don't depend on order. Events can arrive out of order (e.g.
refund.succeededbeforerefund.created). Use the object'sstatusindata.object, or fetch the latest state from the API when order matters. - Check what you're fulfilling. On
payment.succeeded, matchid/order_idto your order and compareamountandcurrencywith what you expected. - Check the environment.
livemodeistrueonly in production; a sandbox event should never fulfil a real order. - Keep the secret secret. If it leaks, add a new endpoint (new secret), switch your handler to it, then delete the old endpoint.
- Legacy integrations that use
notifyUrlreceive the old md5-signed format instead — see Migrating from form-POST.

