1. Go to this page and download the library: Download kemboielvis/mpesa-sdk-php 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/ */
kemboielvis / mpesa-sdk-php example snippets
Kemboielvis\MpesaSdkPhp\Mpesa;
// Method 1: Using setCredentials() with parameters
$mpesa = (new Mpesa())->setCredentials(
'YOUR_CONSUMER_KEY',
'YOUR_CONSUMER_SECRET',
'sandbox' // or 'live' for production
);
// Method 2: Using fluent setters
$mpesa = (new Mpesa())
->setBusinessCode('YOUR_BUSINESS_CODE')
->setPassKey('YOUR_PASS_KEY')
->setCredentials(
'YOUR_CONSUMER_KEY',
'YOUR_CONSUMER_SECRET',
'sandbox'
);
$response = $mpesa->stk()
->setTransactionType('CustomerPayBillOnline') // or 'CustomerBuyGoodsOnline'
->setAmount(100) // Amount in KES
->setPhoneNumber('254712345678') // Customer phone number
->setCallbackUrl('https://yourdomain.com/callback')
->setAccountReference('INV-12345')
->setTransactionDesc('Payment for invoice')
->push()
->getResponse();
print_r($response);