Skip to content

Engineering · · 2 min read

Integrating M-Pesa properly: what most implementations get wrong

Getting an STK Push to appear on a phone takes an afternoon. Building payments that reconcile perfectly under real-world conditions takes engineering discipline.

By Nidaam Engineering

Safaricom’s Daraja API makes it straightforward to start accepting M-Pesa payments. The happy path, where a customer approves a prompt and a callback arrives a few seconds later, is quick to build. Production is where the edge cases live.

Mistake 1: trusting the callback completely

Callbacks are asynchronous. They can be delayed, delivered more than once, or occasionally not arrive at all. A system that only marks an order paid when a callback arrives will eventually leave paid customers looking unpaid.

The fix is to treat the callback as one signal among several. Record every payment request, and run a background job that queries the status of any request still pending after a set time.

Mistake 2: no idempotency

If the same callback arrives twice, your system must not credit the payment twice. Store the M-Pesa receipt number with a unique constraint and make the payment handler safe to run repeatedly.

-- one row per M-Pesa receipt, whatever happens upstream
create table mpesa_payments (
  receipt_number text primary key,
  checkout_request_id text unique,
  amount numeric(12, 2) not null,
  phone text not null,
  account_reference text,
  received_at timestamptz not null default now()
);

Mistake 3: matching payments loosely

For paybill payments, customers type the account reference themselves, and they make mistakes: extra spaces, wrong case, a digit missing. Normalise references, match exactly where you can, and send everything else to an exceptions queue for a person to resolve rather than guessing.

Mistake 4: no reconciliation

Even with everything above, you need a daily reconciliation between what M-Pesa says you received and what your system recorded. Differences should be rare, visible and investigated, never discovered at an audit.

Mistake 5: secrets in the wrong places

Consumer keys, passkeys and initiator credentials belong in a secrets manager or environment configuration, never in source code or a shared document. Callback endpoints should validate what they receive and log every request.

A checklist for production

  • Every payment request stored before it is sent
  • Idempotent callback handling keyed on the receipt number
  • A status-query job for requests left pending
  • Exact matching, with an exceptions queue for the rest
  • Daily automated reconciliation with alerts on differences
  • Credentials kept out of code, and callback requests logged

None of this is exotic. It is the ordinary discipline that separates a demo from a payment system people can rely on.

Start a project

Tell us what is slowing your business down.

Share a few details about your organisation and the problem you want solved. We will come back with next steps, and a written quotation once we understand the requirements.