ToroyTech
← All insights
Payments7 min readToroyTech

Integrating M-Pesa with Daraja: a practical walkthrough

The production details the sandbox tutorials skip.

Why M-Pesa integration looks easy and isn't

The Daraja API gets you a sandbox token quickly. Production is where the real work starts: handling duplicate callbacks, matching payments to orders, and recovering from timeouts.

STK push, end to end

  1. Authenticate and cache the OAuth token until it expires.
  2. Trigger an STK push with a unique account reference per order.
  3. Handle the asynchronous callback — and assume it may arrive twice.

Idempotency is non-negotiable

Store a unique key per transaction and make your callback handler safe to run multiple times. Daraja can and will retry.

Reconciliation

Match transactions to orders automatically, and put anything ambiguous into an exception queue a human can resolve. This is the difference between a demo and a system you can trust.

ShareXLinkedIn

Get new insights by email

Occasional, practical notes on civic tech, security, and building dependable software. No spam, unsubscribe anytime.