Guides
Webhooks
Get a signed POST when a payment is marked paid.
Set a webhook URL in the dashboard. We POST payment.success when a payment is marked paid, including when you simulate a test payment.
Events
| Event | When |
|---|---|
payment.success | A payment was marked paid. Verify amountKobo on your server, then fulfil. Test simulations set simulated to true. |
The body is { id, event, createdAt, data }. data includes reference, sessionId, status, amountKobo, currency, paidAt, channel, customer, metadata, simulated, environment, and paymentLinkId (null when the payment did not come from a link).
Check the signature
Each request carries X-CSwitch-Signature: t=<unix seconds>,v1=<hex>. v1 is HMAC-SHA256 of timestamp + "." + rawBody, using the webhook signing secret (whsec_…), not your API key. X-CSwitch-Event is the event name. X-CSwitch-Delivery is the id to ignore when the same delivery is retried. Read the raw body before parsing JSON, compare in constant time, and reject the event when t is more than 300 seconds from your clock.
import { verifyWebhook } from "@cswitch/node";
export async function POST(request) {
const raw = await request.text();
const check = verifyWebhook({
rawBody: raw,
header: request.headers.get("x-cswitch-signature"),
secret: process.env.CSWITCH_WEBHOOK_SECRET,
});
if (!check.valid) return new Response("invalid signature", { status: 401 });
const event = JSON.parse(raw);
// Ignore a repeated X-CSwitch-Delivery, then verify amountKobo before you fulfil.
return new Response("ok");
}import hashlib, hmac, os, time
def is_from_cswitch(raw_body: bytes, header: str) -> bool:
parts = {}
for piece in (header or "").split(","):
key, _, value = piece.partition("=")
parts[key.strip()] = value.strip()
timestamp, signature = parts.get("t", ""), parts.get("v1", "")
if not timestamp.isdigit() or len(signature) != 64:
return False
if abs(int(time.time()) - int(timestamp)) > 300:
return False
message = timestamp.encode() + b"." + raw_body
expected = hmac.new(os.environ["CSWITCH_WEBHOOK_SECRET"].encode(), message, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, signature)<?php
$raw = file_get_contents("php://input");
$parts = [];
foreach (explode(",", $_SERVER["HTTP_X_CSWITCH_SIGNATURE"] ?? "") as $piece) {
$eq = strpos($piece, "=");
if ($eq === false) continue;
$parts[trim(substr($piece, 0, $eq))] = trim(substr($piece, $eq + 1));
}
$t = $parts["t"] ?? "";
$v1 = $parts["v1"] ?? "";
if (!ctype_digit($t) || !preg_match("/^[0-9a-f]{64}$/", $v1) || abs(time() - (int) $t) > 300) {
http_response_code(401);
exit;
}
$expected = hash_hmac("sha256", $t . "." . $raw, getenv("CSWITCH_WEBHOOK_SECRET"));
if (!hash_equals($expected, $v1)) {
http_response_code(401);
exit;
}Retries
Any 2xx counts as delivered. Otherwise the same delivery is retried after 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours and 24 hours, then marked failed. Each retry keeps X-CSwitch-Delivery and is signed again, so make your handler safe to run twice for the same delivery.
Live webhook URLs must be https and public. A test webhook can be local only when the gateway is explicitly configured to allow it.