On this page
  1. TL;DR
  2. Match the flow to how the customer pays
  3. Safaricom’s charges sit apart from the build
  4. Seven faults that break live integrations
  5. What a sound integration can show you
  6. If you are starting from nothing

TL;DR

  • Use a paybill when customers pay against an account number. Use a till when the amount is set at checkout or the counter.
  • Store the M-PESA receipt number under a unique constraint, so a repeated callback can never credit an order twice.
  • If no callback arrives within about 60 seconds, do not leave the order unpaid. Query the M-PESA Express Query API with the CheckoutRequestID.
  • Match your ledger to the paybill or till statement every day, and clear each mismatch before it compounds.

An M-PESA integration in Kenya usually fails the same way. The first STK push works in the demo. Months later a callback arrives twice, or never, and the ledger stops matching the paybill statement.

Two early decisions prevent most of it. Pick the Daraja flow that matches how your customers pay. Then build the integration so a late, missing or repeated callback cannot change what it records.

Match the flow to how the customer pays

Safaricom’s Daraja API offers a long list of endpoints. Most businesses need one or two of them, built well.

Flow Who starts the payment Use it for
M-PESA Express (STK push) Your system, while the customer is on your screen Online checkout, fee payments, deposits, app top-ups
C2B The customer, from their own phone School fees by admission number, rent, utility bills
B2C Your business, sending money out Refunds, seller payouts, agent commissions, casual labour
Buy Goods (till) The customer, at the counter Retail and restaurant sales where the till sets the amount

M-PESA Express is the flow older tutorials call Lipa Na M-PESA Online. The name changed, and the endpoint stayed the same.

M-PESA Express

Your system sends Daraja the customer’s phone number and the amount. A prompt appears on their phone, they enter their PIN, and Daraja posts the result to your callback URL. You need a paybill or till, Daraja credentials and a callback URL the internet can reach.

It gives the smoothest checkout and is the easiest flow to reconcile. Use it wherever the customer is already in your app or site.

C2B

The customer pays your paybill or till from their own phone. Safaricom then calls the validation and confirmation URLs you registered. You need a registered shortcode, those two URLs, and a way to map each account reference back to a customer.

That mapping is most of the work. A parent who types “Form 2 Wanjiru” instead of an admission number lands on somebody’s review list.

B2C

B2C is money leaving your account, so it needs tighter controls than anything inbound. You need an organisation shortcode with enough float, B2C API access and clear rules on who can release a payment.

Check the float with the Account Balance API before each send. Without that check, a payout can fail halfway through a run.

Paybill or till

A paybill suits fee-style payments that carry a reference, such as school fees, rent or a water bill. A till suits sales where the amount is decided at checkout or at the counter. Choosing wrong usually means a migration within a year.

Safaricom’s charges sit apart from the build

Safaricom charges the merchant on incoming payments and charges per transaction on B2C sends. Sweeping funds from a paybill or till to a bank carries a tiered fee too.

Rates depend on your tier and change over time, and higher-volume merchants can negotiate. Ask your Safaricom account manager for current rates before you model your margins.

Seven faults that break live integrations

Every integration we have been asked to rescue failed in at least one of these ways.

1. The callback is treated as the final word

Callbacks fail, arrive late or go missing, more often at peak hours. If “paid” depends only on the callback, a customer can be charged while your system shows the order unpaid.

If no callback arrives within about 60 seconds, query the M-PESA Express Query API with the CheckoutRequestID for the result. Then reconcile against the statement every day. Our post on M-PESA webhooks and polling sets out a schedule for those queries.

2. The handler counts a repeat as a new payment

M-PESA can deliver the same callback more than once. Without a check, the second copy double-credits the order, marks a fee paid twice or sends a refund twice. Nobody notices until a customer does.

Store the MpesaReceiptNumber under a unique constraint and treat any repeat as a no-op. Of the seven fixes, do this one first.

3. Phone numbers arrive in four shapes

The same customer can reach you as 0712345678, 254712345678, +254712345678 or 712345678. Stored as they come, these break lookups during reconciliation. An STK push can even reach the wrong number.

Convert every number to one format, such as 254XXXXXXXXX, at every entry point. That means checkout, admin screens, imports and callbacks.

4. The callback URL does too much before replying

If your callback URL is slow to return HTTP 200, Safaricom retries, times out or stops trusting it. The usual cause is a handler that sends notifications, builds receipts and updates reports before it answers.

Reply within a second with HTTP 200 and the ResultCode: 0 body. Put the rest of the work on a queue and process it afterwards.

5. The sandbox was skipped

Teams that skip the Daraja sandbox end up debugging with real customer money. Run every state there first. Test a successful payment, a cancelled prompt, a timeout, insufficient balance, a wrong PIN and a network failure.

Then send a small live amount to a test number you control. Open the flow to customers only after that passes.

6. Nobody compares the ledger with the statement

The paybill or till statement from the M-PESA Business portal is the record that counts. If your ledger drifts from it and nobody looks, the gap grows every day.

A daily job should compare the two, alert on any mismatch and produce an exception list. A person then clears that list, not the whole statement.

7. Nobody is watching

When the integration breaks at 2am, you want to know before the first customer calls. Track the success rate by hour, callback latency at p95, payments deducted but not recorded, and rate-limit warnings. Run a health check every day.

What a sound integration can show you

Ask whoever runs your integration to show each of these on the live system:

  • A repeated callback recorded once, keyed on the receipt number.
  • Every transaction logged with receipt, amount, phone, time and each change of state.
  • Yesterday’s statement matched to the ledger, with the exceptions listed.
  • An order closed by a status query after its callback never came.
  • A dashboard of success rate, latency and failure patterns.
  • Tests for each failure state, run before every release.

When we repair an integration that lost track of payments, these six are what we fix it around. Patching the one symptom that surfaced first leaves the other five waiting.

If you are starting from nothing

  1. Choose paybill or till from how your customers pay, not from what a competitor uses.
  2. Use M-PESA Express wherever the customer is on your screen.
  3. Build reconciliation into the first release. Bolting it on later means reworking payments that are already live.
  4. Test cancellations, timeouts and double callbacks. At any real volume they happen every week.
  5. Keep Daraja credentials out of source control and out of team chats. Leaked credentials are a common way merchant accounts get compromised.

Before writing any code, trace one real payment from the customer’s phone to your books. Note every point where it could be lost or counted twice. That list is your scope.

Our payments service builds Daraja integrations with idempotent callbacks, daily reconciliation and an audit-ready ledger. It can also repair a live one in place. Book a free scoping call to walk through yours.

Need it built properly? A free 30-minute call with an engineer, then one fixed price in writing.

Book a free call

Need it built properly? A free 30-minute call with an engineer, then one fixed price in writing.

Book a free call

Keep reading

M-PESA Webhooks vs Polling: Use Both, Trust the Statement

M-PESA Webhooks vs Polling: Use Both, Trust the Statement

WhatsApp Business API in Kenya: When to Move and How

WhatsApp Business API in Kenya: When to Move and How

Selling Power Directly to Businesses in Kenya: The Billing You Will Need

Selling Power Directly to Businesses in Kenya: The Billing You Will Need