Accepting M-Pesa payments on your website is not optional in Kenya. It is the primary way Kenyans pay online. But integrating M-Pesa correctly requires more than copying code from a tutorial. This guide covers the full integration process — from business registration to production go-live — and the mistakes that cost businesses money.
What you need before you start
Before writing any code, you need: a registered business (certificate of incorporation or business name registration), a KRA PIN, a business bank account, a Safaricom Daraja developer account, and a Paybill or Buy Goods till number. You cannot use a personal M-Pesa number for business website payments. If you do not have a business registration, start there — it takes 1-3 weeks and costs KES 1,000-5,000.
Understanding the Daraja API
Safaricom Daraja is the API platform for M-Pesa. It provides three main payment flows for websites. STK Push (Lipa Na M-Pesa Online) sends a payment prompt to the customer phone — they enter their PIN and the payment completes. C2B (Customer to Business) lets customers send money to your Paybill or till number manually. B2C (Business to Customer) lets you send money to customers (refunds, payouts). For e-commerce, STK Push is the primary flow. C2B is the fallback. B2C is for refunds.
Step 1: Create a Daraja app
Go to developer.safaricom.co.ke and create an account. Create a new app and select the APIs you need (Lipa Na M-Pesa Online, C2B, B2C). You will get a Consumer Key and Consumer Secret. These authenticate your API calls. Keep them secret — never commit them to your codebase. Use environment variables.
Step 2: Sandbox testing
Safaricom provides a sandbox environment for testing. Use it. Test every scenario: successful payment, failed payment (wrong PIN), timeout (customer does not respond), and duplicate request. The sandbox uses test phone numbers and test amounts. Do not go to production until sandbox tests pass for all scenarios. Skipping sandbox testing is the most common cause of production failures.
Step 3: STK Push implementation
The STK Push flow works like this: your website sends a request to Daraja with the customer phone number, amount, and account reference. Daraja sends an STK Push prompt to the customer phone. The customer enters their M-Pesa PIN. Daraja processes the payment and sends a callback to your server with the result. Your server matches the callback to the order and updates the order status. The entire flow takes 10-30 seconds.
Step 4: Callback handling
Callbacks are the most critical part. Daraja sends a POST request to your callback URL with the payment result. Your endpoint must: validate the callback (check the transaction ID), match it to the correct order using the account reference, update the order status, and return a 200 OK response. If your endpoint does not return 200, Daraja will retry. If it still fails, the payment is lost. Use idempotency keys to prevent duplicate processing.
Step 5: Reconciliation
Even with perfect callback handling, some payments will be missed. Network failures, server downtime, and callback timeouts happen. Build a reconciliation process: compare your order records against M-Pesa transaction statements daily. Flag any mismatches. For high-volume merchants, automate this with a dashboard that shows unmatched payments and orders. We covered the reconciliation pattern in detail in what three M-Pesa integrations taught us.
Common pitfalls
Using personal M-Pesa numbers for business (breaks reconciliation and looks unprofessional). Skipping sandbox testing (leads to production failures). Not handling callbacks (payments are lost). Not using idempotency (customers charged twice). Hardcoding credentials (security risk). Not testing on real phones (emulators do not replicate Safaricom network conditions). And the biggest: assuming STK Push always works. It fails about 5-10% of the time due to network issues, phone problems, or insufficient balance. Build retry logic and clear error messages.
Step 6: Production go-live
Once sandbox tests pass, apply for production access through your Daraja account. Safaricom reviews your application and approves production credentials. This takes 1-2 weeks. Test in production with small amounts (KES 10-50) before going live with real customers. Monitor the first 48 hours closely. Check every callback, every order update, and every reconciliation mismatch.
Where to go next
If you need to choose between Paybill and Buy Goods, our guide to Paybill vs till number in Kenya compares them. If you want to understand M-Pesa integration challenges, what three M-Pesa integrations taught us covers the real-world lessons. And if you are building an e-commerce site, how M-Pesa is changing e-commerce in Kenya explains the business context.
Build what comes next
Want to explore what these ideas could mean for your business? Start a conversation with our team.