TutorialCodeM-Pesa
M-Pesa Integration Guide for Developers
Tech Team
Integrating Payments in Kenya
M-Pesa is king in Kenya. If you're building an e-commerce site like DevShop, you need to accept mobile money.
The Daraja API
Safaricom's Daraja API provides STK Push capabilities, which is the smoothest experience for users.
Next.js Implementation
We use a server-side route handler to initiate the STK Push. Always ensure you handle the callback URL correctly to update order status.
// Example STK Push payload
const payload = {
BusinessShortCode: process.env.MPESA_PAYBILL,
Password: password,
Timestamp: timestamp,
TransactionType: "CustomerPayBillOnline",
Amount: amount,
PartyA: phoneNumber,
PartyB: process.env.MPESA_PAYBILL,
PhoneNumber: phoneNumber,
CallBackURL: "https://www.devshop.co.ke/api/mpesa/callback",
AccountReference: "DevShop",
TransactionDesc: "Payment for Order"
};
Want to see how we handle payments? Check out our checkout flow!