Mastering M-Pesa Integration in Laravel 12: A Guide for Kenyan Businesses
Introduction
In the Kenyan digital economy, if your system doesn't talk to M-Pesa, it’s missing a heartbeat. With over 30 million active users, M-Pesa is no longer just a "feature"—it is the standard for business transactions.
As developers and business owners, moving to Laravel 12 offers us faster performance and better security. But how do we bridge the gap between our modern web apps and Safaricom's Daraja API? At Eelam Innovations, we believe in seamless automation. Here is how you can set up a professional M-Pesa STK Push in the latest Laravel environment.
1. The Setup: Getting Your Credentials
Before touching a single line of code, you need your keys from the Safaricom Daraja Portal. For a production-ready system, you will need:
- Consumer Key & Consumer Secret
- Business Shortcode (Paybill or Till)
- Passkey (for STK Push)
In your Laravel .env file, store these securely:
Bash
MPESA_ENVIRONMENT=sandbox
MPESA_CONSUMER_KEY=your_key_here
MPESA_CONSUMER_SECRET=your_secret_here
MPESA_SHORTCODE=174379
MPESA_PASSKEY=your_passkey_here
MPESA_CALLBACK_URL=https://eelam.co.ke/api/v1/mpesa/callback
2. Implementation: The STK Push Logic
Using Laravel’s Http client makes API calls incredibly clean. Instead of messy cURL scripts, we can create a dedicated MpesaService to handle the logic.
Pro Tip: Always use Environment Variables for your credentials. Never hardcode them into your controllers!
Snippet: Initiating a Payment
PHP
public function initiateStkPush($phone, $amount)
{
$timestamp = now()->format('YmdHis');
$password = base64_encode(config('mpesa.shortcode') . config('mpesa.passkey') . $timestamp);
$response = Http::withToken($this->generateToken())
->post('https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest', [
'BusinessShortCode' => config('mpesa.shortcode'),
'Password' => $password,
'Timestamp' => $timestamp,
'TransactionType' => 'CustomerPayBillOnline',
'Amount' => $amount,
'PartyA' => $phone,
'PartyB' => config('mpesa.shortcode'),
'PhoneNumber' => $phone,
'CallBackURL' => config('mpesa.callback_url'),
'AccountReference' => 'EelamService',
'TransactionDesc' => 'Payment for services',
]);
return $response->json();
}
3. Handling the Callback (The "Silent" Success)
The most common mistake developers make is assuming a payment is successful just because the request was sent. You must handle the callback.
Safaricom will send a JSON payload to your CallBackURL. Your Laravel backend should listen for this, verify the ResultCode (0 is success), and update your database accordingly.
Why Choose Eelam Innovations for Your Integration?
Building a payment system involves more than just an API call. You need:
- Reconciliation: Matching M-Pesa statements with your internal records.
- Error Handling: Managing cases where the user cancels or has insufficient funds.
- Security: Ensuring your callback endpoints are protected.
At Eelam Innovations, we specialize in building robust fintech bridges. Whether you are running an e-commerce platform or a custom ERP, we ensure your payments are secure, fast, and automated.
Final Thoughts
Integrating M-Pesa with Laravel 12 is powerful when done right. It reduces manual work, eliminates "faked" payment screenshots, and improves your cash flow.
Are you ready to automate your business payments? Contact us today and let’s build something amazing together.