BlogPayments
How to accept M‑Pesa on your website.
What you need from Safaricom, how the checkout actually works, and the mistakes that lose orders and money.
By Elvis Chege7 minute read
The two ways to do it
Manual. You show your Paybill or Till number on the site, the customer pays from their phone, and someone checks the M-Pesa message and confirms the order by hand. It costs nothing to set up, and it is fine for a handful of orders. It breaks down as soon as orders grow: payments get missed, customers wait, and mistakes creep in.
Integrated. The customer types their phone number at checkout, an M-Pesa prompt appears on their phone, they enter their PIN, and the site confirms the order automatically. This uses Safaricom's developer platform, Daraja, and the prompt is called STK Push (Lipa na M-Pesa Online, or M-Pesa Express). For most online shops, this is what you want.
What you need
- A Paybill or Till number registered to your business. This is where the money lands. Getting one is a separate application with Safaricom, so start early.
- A Daraja account on Safaricom's developer portal, where the integration is built and tested in a sandbox first.
- A website with a server-side part that holds your keys and receives Safaricom's confirmations. This can't be done safely in the browser alone.
- Go-live approval. Once it works in the sandbox, you apply to go live with your shortcode and callback addresses, and Safaricom issues production credentials. Guides report this taking anywhere from a few days to a few weeks (example guide), so check the current checklist on Safaricom's Daraja portal.
How a checkout works
- The customer checks out and enters their M-Pesa number.
- Your server asks Safaricom to send a payment prompt for that amount to that phone.
- The customer enters their PIN on their phone.
- Safaricom calls your server back with the result: paid, cancelled, insufficient funds, or timed out.
- Your site confirms the order only when that callback says it is paid, and records the M-Pesa receipt number against the order.
Mistakes that cost money
- Marking an order as paid because the customer clicked "I've paid". Anyone can then get goods for free. Only the server-side callback should count.
- Ignoring failed and late callbacks. Prompts get cancelled and phones go off mid-payment. Handle every outcome, and if a callback never arrives, ask Safaricom for the payment status so orders don't sit stuck.
- Charging twice. A customer who taps "pay" twice should be charged once, so each order needs one payment request at a time.
- Putting your Daraja keys in the website's code, where anyone can read them. They belong on the server.
- Skipping the daily check. At the end of each day your orders and your M-Pesa statement should agree, and if they don't, you want to find out the same day.
- Letting a middleman hold your money. Some setups route payments through someone else's account first. Make sure the money goes straight into your own Paybill or Till.
Build it, or have it built
A developer comfortable with Daraja can build a solid integration, and plugins exist for some shop platforms. Whoever builds it, ask them how they handle the mistakes above. If they can't answer clearly, keep looking.
At Riua we build online shops where customers pay with M-Pesa and the money goes straight into your own till. We never hold your money. We also look after the shop every month: hosting, security, backups and updates. Read more about online shops with M-Pesa checkout, or see what a website costs in Kenya.