Blog · E-commerce Kenya
How M-Pesa STK Push Works on a Kenyan Online Store
STK push is the prompt asking a customer for their M-Pesa PIN. This guide explains what happens between the request and the confirmation, what you need before any of it works, and the five places it usually goes wrong.
How M-Pesa STK Push Works on a Kenyan Online Store
STK push is the prompt that appears on a customer's phone asking them to enter their M-Pesa PIN to pay you. Your website asks Safaricom to send that prompt, the customer approves it, and Safaricom tells your website whether the money arrived. Done properly the order is confirmed automatically. Done badly you are still reconciling payments by hand at the end of the day.
This guide explains what happens between those two points, what you need before any of it works, and the five places it usually goes wrong. It is written for the person who owns the shop, not the person who writes the code, though the questions at the end are worth putting to whoever builds it.
What STK push actually is
STK stands for SIM Toolkit. The prompt is not a text message and it is not an app notification. It is a menu pushed to the SIM card itself, which is why it appears on a basic handset as readily as on a smartphone, and why it cannot be screenshotted on many phones.
Your customer never leaves your website. They type their phone number at checkout, the prompt arrives seconds later, they enter their PIN, and the page updates to confirm the order. Compare that with the older pattern: the customer opens the M-Pesa menu, selects Lipa na M-Pesa, types your paybill number, types your account number, types the amount, and then you match that payment to an order yourself. Every one of those steps is a chance to mistype something or give up.
The difference between those two flows is covered in more detail in our comparison of STK push, paybill and till. The short version is that STK push removes the typing and removes the manual matching.
The three parties involved
Safaricom holds the money and runs the M-Pesa system. Nothing happens without them.
The Daraja API is Safaricom's developer interface. Daraja is how your website speaks to M-Pesa. It is free to use as an interface, though the transaction charges on your paybill or till still apply.
Your website sits in the middle. It sends the request, waits, and records the result.
A common misunderstanding is that your website holds the money at some point. It does not. The funds move from the customer's M-Pesa account to your paybill or till account directly. Your website only learns whether that happened.
What happens, step by step
1. The customer enters a phone number and presses pay. 2. Your website sends a request to Daraja containing the amount, the phone number, your shortcode and a reference for the order. 3. Safaricom validates that request and sends the PIN prompt to the customer's SIM. 4. The customer enters their PIN, or cancels, or ignores it until it expires. 5. Safaricom sends the result to a callback URL on your website. 6. Your website matches that result to the order and marks it paid or failed.
Steps two and five are separate conversations. This matters more than anything else in this article. The request going out and the result coming back are not one continuous connection. Your website asks, then hangs up, then waits to be called back. If your site is not reachable when Safaricom calls back, the customer has paid and your shop does not know.
What you need before any of it works
A paybill or a till number. You apply through Safaricom for a business shortcode. A personal number will not do.
A Daraja account and an app. Registered on Safaricom's developer portal, which gives you a consumer key and secret. These are credentials. They belong in your hosting environment, never in your website's code and never in a WhatsApp message.
A passkey for the shortcode. Issued by Safaricom and specific to your business.
A public callback URL served over HTTPS. Safaricom will not send results to an insecure address, and it cannot reach a site running only on a laptop.
Go-live approval. Daraja has a sandbox for testing with fake money and a production environment for real money. Moving between them requires Safaricom to approve your application. Build time should assume this takes days, not minutes.
If you are scoping a project, this list is also a useful test of whether a quote is realistic. A proposal that includes M-Pesa checkout but says nothing about go-live has not thought about the calendar. We cover what a complete quote looks like in our guide to what each line item should include.
Where it breaks, and what to do
The customer ignores the prompt
The prompt expires. Safaricom sends a result saying so. Your website should show the customer a clear message and offer to send it again, rather than leaving a spinner turning forever.
The most common mistake here is a checkout that waits indefinitely. Decide how long you will wait, show a countdown, and give the customer a way out that does not involve closing the tab.
The phone number is in the wrong format
Daraja expects a number in the format 2547XXXXXXXX. Customers type 07, or 7, or add spaces, or paste a number with a plus sign. Your checkout should accept all of those and convert them quietly rather than rejecting the customer.
This is a small piece of work that prevents a large number of failed checkouts, and it is worth asking about directly.
The callback never arrives
Safaricom tried, your server was down or the URL was wrong, and the result went nowhere. The customer has paid. Your shop shows the order as unpaid.
The fix is a second mechanism: your website asks Safaricom about the status of a transaction it has not heard back on. Any serious implementation has this. Ask whether yours does, because without it every missed callback becomes a support conversation and an angry customer.
The customer pays twice
They did not see the first prompt, pressed pay again, and approved both. Without protection you now owe a refund and your stock count is wrong.
The defence is a unique reference per attempt and a check on your side before a second request is sent.
The amount does not match
Rounding, a currency conversion, or a discount applied after the request was sent. The payment arrives for a different figure than the order total. Your system should flag that for a human rather than quietly accepting it.
Reconciliation, which is the real prize
The point of STK push is not that it is modern. It is that the payment and the order are joined at the moment of payment, because the reference you sent in step two comes back in step five.
With a paybill alone, you get a statement of payments and a list of orders, and somebody has to marry them up. On a slow day that is irritating. In December it is a part-time job, and it is where mistakes about who has paid actually happen.
If you are already selling with a paybill and reconciling by hand, that daily cost is the number to put against the build. Our page on M-Pesa payment integration sets out what the work involves.
Sandbox and production are different worlds
Everything works in the sandbox. The sandbox is forgiving, the test credentials are public, and the money is imaginary.
Production is stricter. Rate limits apply, your callback URL is checked, and real customers behave in ways no test script does. Budget for a short period after go-live where you watch real transactions closely and fix what the sandbox never showed you.
Ask for a staging environment that points at the sandbox and a production environment that points at the real thing, kept separate. A single environment that gets switched over is how test transactions end up in your real accounts.
What it costs
Two separate costs, often confused.
Safaricom's transaction charges apply to your paybill or till account on the tariff you agreed with them. Daraja does not add a fee on top for the API itself.
The integration work is part of building the store. Our e-commerce builds start from KES 40,000 and take three to four weeks, with the full breakdown on the e-commerce cost page. M-Pesa checkout is part of that rather than an extra product.
Be cautious with any plugin sold as instant M-Pesa integration for a small one-off fee. The code is the easy part. Go-live approval, callback reliability, duplicate protection and reconciliation are the work, and a plugin cannot do your Safaricom paperwork for you.
Questions worth asking your developer
Any developer who has done this before will answer all seven without hesitation. Hesitation on more than one of them is a reason to ask more questions before you commit.
- What happens if the callback does not arrive? Is there a status query, and how often does it run?
- How do you stop the same customer paying twice?
- Which phone number formats does checkout accept?
- Where are the Daraja credentials stored, and who can see them?
- Is there a staging environment on the sandbox, separate from production?
- What does the customer see when the prompt times out?
- How does a failed payment appear in the admin, and can we retry it from there?
When you do not need STK push
If you take a handful of orders a week, mostly from repeat customers, and you are comfortable confirming payments yourself, a paybill with a clear reference may be enough for now. Integration is work, and work you do not need yet is cost you do not need yet.
The point to revisit it is when reconciliation starts eating real time, when you are losing orders because customers abandon the manual payment steps, or when you want delivery and stock to update themselves when a payment lands.
What it looks like from your side
Once it is working, a day in the admin should be unremarkable, and that is the point.
An order arrives with a status. Paid, failed, or pending. Pending should be rare and should resolve itself within minutes through the status query described above. If your orders list fills with pending entries that never move, the callback handling is not finished, whatever anyone has told you.
Each paid order should carry the M-Pesa receipt number. That is the string beginning with letters that the customer also receives by text. It is the single most useful field you have, because when a customer says they paid and you cannot find it, that number settles the question in seconds. Make sure it is stored, make sure it is searchable, and make sure whoever answers your phone knows where to look for it.
Failed orders are worth keeping rather than deleting. A pattern of failures at a particular hour, or from a particular network, or always at a particular amount, tells you something. Deleted failures tell you nothing.
Refunds and cancellations
This is the part that gets skipped in planning and then arrives on a Friday afternoon.
A refund through M-Pesa is a separate operation from the payment. It is not an undo. Depending on how your shortcode is set up, it may be a transaction you perform from your Safaricom portal rather than something your website can trigger, and the money does not always travel instantly.
Decide the process before you need it. Who authorises a refund, how it is recorded against the order, and what the customer is told about timing. A customer waiting on a refund with no information is a customer writing a review.
Partial refunds, for a single item out of a larger order, need particular thought because your stock and your accounts both have to agree with what actually happened.
None of this is difficult. It is simply easier to decide calmly in advance than under pressure with somebody on the phone.
Where to go next
If you are planning a store, start with e-commerce development for what a build covers, or the e-commerce insights hub for the surrounding decisions on delivery, compliance and platform choice.
If you already sell online and the payment side is the part that hurts, tell us what you are using now and we will tell you whether integration is worth it in your case, including when it is not.
Sources and further reading
Talk to us about e-commerce kenya
Tell us what you need help with in e-commerce kenya. We can help you choose the right next step for your business.
Rather just ask?
Send your brief and get a fixed scope and price back within a working day.
