M-Pesa Payment Gateway
API & Integration · E-Commerce Platform · 2023
Production M-Pesa gateway handling STK Push, C2B, B2C flows with automatic reconciliation and settlement.
Overview
A production-grade M-Pesa payment gateway serving multiple e-commerce merchants. The gateway abstracts the complexity of Safaricom's Daraja API and provides merchants with a clean REST API, webhook callbacks, automatic daily reconciliation, and a dashboard for transaction management.
The challenge
Many Kenyan e-commerce businesses integrated M-Pesa directly into their applications, leading to duplicated effort, poor error handling, missed callback verifications, and manual reconciliation errors that cost businesses thousands of KES monthly.
The solution
We built a shared M-Pesa gateway as a microservice with a clean REST API. It handles STK Push initiation and result polling, C2B paybill/buy-goods callbacks with signature verification, B2C disbursements, and automatic daily reconciliation against Safaricom's CSV statement. Redis queues prevent duplicate processing of callbacks.
Results
- Thousands of daily transactions processed reliably
- Zero double-charge incidents due to idempotent request handling
- Reconciliation time reduced from 4 hours/day to 10 minutes (automated)
- Average STK Push success rate of 94.7% (industry average is ~85%)
- 5 e-commerce platforms migrated to the shared gateway within 3 months
Technologies used
Laravel 10M-Pesa Daraja APIMySQLRedisLaravel QueueWebhook HandlerREST APISwagger/OpenAPI
Screens
- STK Push payment flow — End-to-end STK Push payment flow with status polling
- Transaction reconciliation — Automatic daily reconciliation against Safaricom's statement
- API documentation — OpenAPI documentation for merchants integrating the gateway
- Merchant dashboard — Merchant portal for transaction history and settlement requests