PHP code example of kemboielvis / mpesa-sdk-php

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);

$status = $mpesa->stk()
    ->query('CHECKOUT_REQUEST_ID')
    ->getResponse();

$response = $mpesa->customerToBusiness()
    ->setResponseType('Completed')
    ->setConfirmationUrl('https://yourdomain.com/confirmation')
    ->setValidationUrl('https://yourdomain.com/validation')
    ->registerUrl()
    ->getResponse();

$response = $mpesa->customerToBusiness()
    ->setCommandId('CustomerPayBillOnline') // or 'CustomerBuyGoodsOnline'
    ->setAmount(100)
    ->setPhoneNumber('254712345678')
    ->setBillRefNumber('INV-123') // For paybill only
    ->simulate()
    ->getResponse();

$response = $mpesa->businessToCustomer()
    ->setInitiatorName('YOUR_INITIATOR_NAME')
    ->setCommandId('SalaryPayment') // or 'BusinessPayment', 'PromotionPayment'
    ->setAmount(1000)
    ->setPhoneNumber('254712345678')
    ->setRemarks('Salary payment')
    ->setOccasion('May 2023 salary')
    ->paymentRequest(
        'YOUR_INITIATOR_NAME',
        'YOUR_INITIATOR_PASSWORD',
        'SalaryPayment',
        1000,
        'YOUR_BUSINESS_CODE',
        '254712345678',
        'Salary payment',
        'https://yourdomain.com/timeout',
        'https://yourdomain.com/result',
        'May 2023 salary'
    );

print_r($response);

$response = $mpesa->reversal()
    ->setInitiator('YOUR_INITIATOR_NAME')
    ->setTransactionId('YOUR_TRANSACTION_ID')
    ->setReceiverIdentifierType('11') // 1=MSISDN, 2=Till, 4=Shortcode
    ->setRemarks('Refund')
    ->setOccasion('Customer refund')
    ->reverse(
        'YOUR_INITIATOR_NAME',
        'YOUR_INITIATOR_PASSWORD',
        'Refund',
        'YOUR_BUSINESS_CODE',
        'YOUR_TRANSACTION_ID',
        '11', // Identifier type
        'https://yourdomain.com/timeout',
        'https://yourdomain.com/result',
        'Customer refund'
    );

print_r($response);

try {
    $response = $mpesa->stk()->push()->getResponse();
} catch (\Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
bash
composer