Guides
Handling callbacks
Daraja tells you the outcome of a payment by POSTing to a URL you give it. Those callbacks are not signed, so treat each one as a notification, not as proof.
Answer fast, then confirm
Reply HTTP 200 straight away, then confirm the payment with Daraja before you deliver anything. Anyone who discovers your callback URL can POST a fake "paid" body to it.
import { parseStkCallback } from "@bytegigs/daraja";
app.post("/daraja/callback", async (req, res) => {
res.sendStatus(200); // answer Daraja first
const result = parseStkCallback(req.body);
if (!result) return; // not an STK Push result: ignore it
// result.paid, result.amount, result.receipt, result.meaning ("The customer cancelled the payment")
try {
const confirmed = await daraja.stkQuery({ checkoutRequestId: result.checkoutRequestId });
if (confirmed.state === "paid") await markOrderPaid(result.checkoutRequestId, result.receipt);
} catch (error) {
// A 429 or "still processing": leave the order open and check again later.
// The response is already sent, so nothing may escape here: in Node it would crash the process.
console.warn("payment not confirmed yet:", error.message);
}
});
from daraja import parse_stk_callback
@app.post("/daraja/callback")
def callback():
result = parse_stk_callback(request.get_json(silent=True))
if result:
queue_confirmation(result.checkout_request_id) # confirm with stk_query in a worker
return "", 200
use ByteGigs\Daraja\Callbacks;
http_response_code(200);
$result = Callbacks::stk(file_get_contents('php://input'));
if ($result !== null && $daraja->stkQuery($result->checkoutRequestId)->isPaid()) {
markOrderPaid($result->checkoutRequestId, $result->receipt);
}
What a callback contains
A paid STK Push callback carries the amount, the M-PESA receipt, the phone number and the time. A failed one carries a result code instead. The SDKs turn the code into a plain meaning you can show the customer:
| Code | Meaning |
|---|---|
0 | Paid |
1 | The customer's M-PESA balance was too low |
1032 | The customer cancelled the payment |
1037 | The customer's phone could not be reached, or they did not respond |
2001 | The customer entered the wrong PIN |
1019 | The payment prompt expired |
1001 | The customer already has a payment in progress |
When the callback never comes
Callbacks go missing: a timeout on your server, a deploy at the wrong moment, a network blip. If a payment has had no callback after a minute or two, query it with STK Push Query. waitForStkResult does this for you and also recovers payments whose callback never arrived.
Don't query straight after sending the push. Daraja answers HTTP 429 to a query sent a few seconds after the push. See errors and rate limits.
Other callbacks
- C2B confirmations arrive when a customer pays your paybill or till from the M-PESA menu. Read them with
parseC2bTransaction, and confirm large amounts with transaction status. - Results of B2C, B2B, reversal, transaction status and account balance arrive at your
ResultURL. Read them withparseAsyncResult; see payouts. - Queue timeouts arrive at your
QueueTimeOutURLwhen M-PESA couldn't process a request in time. The outcome is unknown: check with transaction status before retrying a payment.