PHP code example of breviam / mpesa-sdk
1. Go to this page and download the library: Download breviam/mpesa-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
breviam / mpesa-sdk example snippets
use Breviam\MpesaSdk\Facades\Mpesa;
// Initiate STK Push
$response = Mpesa::stkPush(
phone: '254712345678',
amount: 100,
reference: 'ORDER123',
description: 'Payment for order #123'
);
// Query STK Push status
$status = Mpesa::stkQuery($response['CheckoutRequestID']);
// Register C2B URLs (usually done once)
$response = Mpesa::c2b()->registerUrls(
confirmationUrl: 'https://yourdomain.com/mpesa/webhooks/c2b/confirmation',
validationUrl: 'https://yourdomain.com/mpesa/webhooks/c2b/validation'
);
// Simulate C2B payment (sandbox only)
$response = Mpesa::c2b()->simulate(
phone: '254712345678',
amount: 100,
reference: 'BILL123'
);
$response = Mpesa::sendMoney(
phone: '254712345678',
amount: 1000,
commandId: 'BusinessPayment',
remarks: 'Salary payment'
);
$response = Mpesa::checkBalance('Balance inquiry');
$response = Mpesa::checkTransactionStatus(
transactionId: 'ABC123XYZ',
partyA: '254712345678',
remarks: 'Transaction status check'
);
use Breviam\MpesaSdk\Contracts\StkInterface;
use Breviam\MpesaSdk\Contracts\AuthInterface;
class PaymentController extends Controller
{
public function __construct(
private StkInterface $stkService,
private AuthInterface $authService
) {}
public function initiatePayment()
{
$response = $this->stkService->push(
'254712345678',
100,
'ORDER123',
'Payment description'
);
return response()->json($response);
}
}
use Illuminate\Support\Facades\Event;
// In your EventServiceProvider
Event::listen('mpesa.stk.callback', function ($data) {
// Handle STK Push callback
Log::info('STK Push callback received', $data);
});
Event::listen('mpesa.c2b.confirmation', function ($data) {
// Handle C2B confirmation
$payment = Payment::where('reference', $data['BillRefNumber'])->first();
$payment->update(['status' => 'confirmed']);
});
return [
'env' => 'sandbox', // or 'production'
'consumer_key' => env('MPESA_CONSUMER_KEY'),
'consumer_secret' => env('MPESA_CONSUMER_SECRET'),
'shortcode' => env('MPESA_SHORTCODE'),
'passkey' => env('MPESA_PASSKEY'),
'initiator' => env('MPESA_INITIATOR'),
'security_credential' => env('MPESA_SECURITY_CREDENTIAL'),
'callback_url' => env('MPESA_CALLBACK_URL'),
'cache' => [
'prefix' => 'mpesa_',
'ttl' => 3300, // 55 minutes
],
'timeout' => 30,
'logging' => [
'enabled' => true,
'channel' => 'daily',
],
];
use Breviam\MpesaSdk\Exceptions\MpesaException;
use Breviam\MpesaSdk\Exceptions\AuthenticationException;
try {
$response = Mpesa::stkPush('254712345678', 100, 'REF123', 'Description');
} catch (AuthenticationException $e) {
// Handle authentication errors
Log::error('M-Pesa authentication failed: ' . $e->getMessage());
} catch (MpesaException $e) {
// Handle general M-Pesa API errors
Log::error('M-Pesa API error: ' . $e->getMessage());
$context = $e->getContext(); // Get additional context
}
bash
php artisan vendor:publish --tag=mpesa-config
bash
php artisan mpesa:token
bash
php artisan mpesa:token --clear
bash
php artisan mpesa:simulate-payment 254712345678 100 --reference=TEST123