M-Pesa payment webhooks: events, signatures and retries
JMSURF PAY guide · updated 23 November 2026
JMSURF PAY tells your system about every payment with a webhook: an HTTPS POST to an address you choose, signed with a secret only you and JMSURF PAY know. This page explains the events, the body, the headers, how to check the signature in several languages, and how retries work, so your integration marks orders paid exactly once.
Add a webhook
In the dashboard open Developers, Webhooks and add an HTTPS address. You can add up to 5 per account and choose the events for each one. The secret (whsec_…) is shown when you create it; store it on your server. Private or local addresses are refused, so use a public HTTPS address (a tunnel works while developing).
Events
| Event | When it fires |
|---|---|
payment.completed | An STK push, payment link or button payment was paid. |
payment.failed | The payment failed: wrong PIN, not enough money, no response from the phone. |
payment.cancelled | The customer dismissed the PIN prompt. |
payment.updated | More detail arrived later, for example the M-Pesa receipt for a payment already marked paid. |
c2b.received | Safaricom confirmed a payment made to your paybill or till from the M-Pesa menu (needs paybill confirmations on). |
payout.completed | A payout from your own paybill reached the recipient. payout.failed when it did not. |
sms.credit_empty | An SMS payment alert was skipped because your SMS credit ran out (at most once a day). |
Body and headers
Body
{
"id": "whd_1234",
"event": "payment.completed",
"livemode": true,
"timestamp": "2026-11-23T08:30:12.000Z",
"data": {
"id": 42,
"checkoutRequestId": "ws_CO_...",
"status": "completed",
"amount": "1500.00",
"phoneNumber": "2547XXXXXXXX",
"mpesaReceiptNumber": "TKN1XXXXXX",
"accountReference": "INV1001",
"createdAt": "2026-11-23T08:29:50.000Z",
"source": "api",
"channelId": 7,
"paymentLinkId": null,
"idempotencyKey": "invoice-1001",
"livemode": true
}
}| Header | Value |
|---|---|
X-Webhook-Signature | sha256= + hex HMAC-SHA256 of the raw body, keyed with your webhook secret |
X-Webhook-Id | The delivery id, the same as id in the body and the same on every retry |
X-Webhook-Event | The event name |
X-Webhook-Timestamp | When this attempt was sent (ISO 8601) |
Check the signature
Compute the HMAC over the bytes you received, before any JSON parsing, and compare it with a constant-time function. If they differ, answer 401 and do nothing. Full Node.js, PHP and Python receivers are in the Node.js, PHP and Python guides; here are two more languages.
Go
// Go: net/http handler
func jmsurfpayWebhook(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
mac := hmac.New(sha256.New, []byte(os.Getenv("JMSURFPAY_WEBHOOK_SECRET")))
mac.Write(raw)
expected := "sha256=" + hex.EncodeToString(mac.Sum(nil))
if !hmac.Equal([]byte(expected), []byte(r.Header.Get("X-Webhook-Signature"))) {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.WriteHeader(http.StatusOK)
// parse raw as JSON and handle event, id, livemode, data
}C#
// C# / ASP.NET Core minimal API
app.MapPost("/webhooks/jmsurfpay", async (HttpRequest req) => {
using var reader = new StreamReader(req.Body);
var raw = await reader.ReadToEndAsync();
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
var expected = "sha256=" + Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(raw))).ToLowerInvariant();
var got = req.Headers["X-Webhook-Signature"].ToString();
if (!CryptographicOperations.FixedTimeEquals(Encoding.UTF8.GetBytes(expected), Encoding.UTF8.GetBytes(got)))
return Results.Unauthorized();
return Results.Ok();
});Delivery, retries and duplicates
- A delivery counts as done when your server answers with any 2xx status within 8 seconds.
- Otherwise it is tried again after 1 minute, 5 minutes, 30 minutes and 2 hours: 5 attempts in all. The dashboard lists every delivery with your server's last answer, and you can resend one from there.
- Because of retries, the same event can arrive more than once. Save
idand ignore an id you have already handled. - Answer first and do slow work (emails, stock updates) after, or in a queue.
Checklist before going live
- 1Signature. Every request without a valid signature gets 401.
- 2Livemode. Orders are only fulfilled when
livemodeistrue. - 3Amount. Compare
data.amountwith the order total before you mark it paid. - 4Duplicates. A second delivery with the same
idchanges nothing. - 5Matching. Find the order by
checkoutRequestId(saved when you sent the prompt) or by youraccountReferenceoridempotencyKey.
Not a developer? WooCommerce, Shopify and WHMCS users get all of this built in: see the guides.
Start on the Free plan: 1 payment channel, up to KES 50,000 a month, test keys included. Questions? WhatsApp +254 712 504 593.