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
- The customer taps Pay in your app and confirms their phone number.
- Your server asks Safaricom's API to send a payment prompt to that number.
- The customer enters their PIN on the prompt.
- Safaricom tells your server whether the payment succeeded.
- 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 →