Blog / Mobile apps

Adding M-Pesa Payments to Your Mobile App

How M-Pesa works inside a mobile app: the STK push prompt, confirming payments on your server, and the details that stop missed or double payments.

Titus Kaleli · · 2 min read

If your app sells anything in Kenya, customers will expect to pay with M-Pesa. The smoothest way is the prompt that pops up on the customer's phone asking for their M-Pesa PIN, usually called an STK push. Here is how it fits into an app.

How the flow works

  1. The customer taps Pay in your app and confirms their phone number.
  2. Your server asks Safaricom's API to send a payment prompt to that number.
  3. The customer enters their PIN on the prompt.
  4. Safaricom tells your server whether the payment succeeded.
  5. Your server marks the order as paid and the app shows a confirmation.

Why the server matters

The app itself should never decide that a payment went through. Only your server, after hearing back from Safaricom, should mark an order as paid. This protects you from fake confirmations and keeps your order list in line with your M-Pesa statement.

Details that prevent headaches

  • Handle slow confirmations. Sometimes the reply from Safaricom is delayed. Show "waiting for payment" and check again, rather than failing the order.
  • Stop double payments. Disable the Pay button while a prompt is open, and tie each prompt to one order.
  • Let customers retry. Wrong PIN, cancelled prompt or low balance should lead to a clear "try again" button.
  • Keep the receipt code. Store the M-Pesa transaction code against the order so staff can look it up.
  • Reconcile. A daily report comparing paid orders with M-Pesa records catches anything that slipped.

Paybill or Till?

Both can be used. A Paybill lets customers enter an account number, which suits invoices and balances. A Till suits straightforward purchases. Your Safaricom setup decides which API credentials you use.

Same setup as your website

If your website already takes M-Pesa, the app can usually use the same server and the same payment records. Our guide to M-Pesa on your website covers that side, and what drives app costs explains how integrations affect the quote.

Want help with this?

SparkWorld builds Android and iOS apps in Kotlin, Flutter or NativePHP Mobile, with M-Pesa payments, push notifications and offline mode, connects them to your website, POS or business system, and publishes them on Google Play and the App Store. Chat with us on WhatsApp (0110 757 810), read more about mobile app development, or book a free discovery call.

Want this working in your business?

Book a discovery call →

More from the blog

Mobile apps · · 2 min read

How Much Does It Cost to Build an App in Kenya?

App quotes in Kenya can differ wildly for what sounds like the same idea. Here is what actually drives the price, and how to keep your first version affordable.

Mobile apps · · 2 min read

App or Website First? A Guide for Kenyan Businesses

Should you build a mobile app or a website first? For most Kenyan businesses the answer depends on how often customers come back. Here is how to decide.

You're offline. Some things won't load until you reconnect.
Chat on WhatsApp

SparkWorld

+254 110 757 810

Chat with us on WhatsApp

Before we open WhatsApp, leave your name and number.

Why we ask

  • On a computer, WhatsApp only opens if WhatsApp Web is linked to your phone, and you may not be logged in.
  • So we don't lose you: if the chat doesn't start, we'll message you first.
  • We only use your details to reply to you.
Skip and open WhatsApp now