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

EventWhen it fires
payment.completedAn STK push, payment link or button payment was paid.
payment.failedThe payment failed: wrong PIN, not enough money, no response from the phone.
payment.cancelledThe customer dismissed the PIN prompt.
payment.updatedMore detail arrived later, for example the M-Pesa receipt for a payment already marked paid.
c2b.receivedSafaricom confirmed a payment made to your paybill or till from the M-Pesa menu (needs paybill confirmations on).
payout.completedA payout from your own paybill reached the recipient. payout.failed when it did not.
sms.credit_emptyAn 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
  }
}
HeaderValue
X-Webhook-Signaturesha256= + hex HMAC-SHA256 of the raw body, keyed with your webhook secret
X-Webhook-IdThe delivery id, the same as id in the body and the same on every retry
X-Webhook-EventThe event name
X-Webhook-TimestampWhen 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 id and 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

  1. 1Signature. Every request without a valid signature gets 401.
  2. 2Livemode. Orders are only fulfilled when livemode is true.
  3. 3Amount. Compare data.amount with the order total before you mark it paid.
  4. 4Duplicates. A second delivery with the same id changes nothing.
  5. 5Matching. Find the order by checkoutRequestId (saved when you sent the prompt) or by your accountReference or idempotencyKey.

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.

Read next

© 2026 JMSURF PAY. All rights reserved. · ODPC reg. no. 194-9812-26EBPowered by JMSURF TECHNOLOGIES LTD