PHP code example of johnes-ke / mpesa-php-api

1. Go to this page and download the library: Download johnes-ke/mpesa-php-api 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/ */

    

johnes-ke / mpesa-php-api example snippets

bash
php artisan vendor:publish --tag=mpesa-php-api-config
bash
php artisan vendor:publish --tag=mpesa-php-api-migrations
 php



    use JohnesKe\MpesaPhpApi\MpesaPhpApi;

    $mpesa = new MpesaPhpApi(
            config('mpesa-php-api.mpesa_environment'),
            config('mpesa-php-api.consumer_key'),
            config('mpesa-php-api.consumer_secret')
        );

    $accessToken = $mpesa->getToken();

    $amount = '1';
    $phoneNumber = '2547********';
    $callBackUrl = 'https:://example.com';
    $accountReference = 'Test001';
    $transactionRef = 'test';

   // Stk Push Example
    $result = $mpesa->stkPushRequest(
        $accessToken,
        config('mpesa-php-api.c2b_stk_short_code'),
        config('mpesa-php-api.lipa_na_mpesa_pass_key'),
        $amount,
        $phoneNumber,
        $callBackUrl,
        $accountReference,
        $transactionRef,
    );

    $ResponseCode        = $result->ResponseCode;
    $ResponseDescription = $result->ResponseDescription;
    $merchantRequestID   = $result->MerchantRequestID;
    $checkoutRequestID   = $result->CheckoutRequestID;
    $CustomerMessage     = $result->CustomerMessage;
    
    //Response   
    echo "<br/>Response Code --> ".$ResponseCode;
    echo "<br/>Response Desc --> ".$ResponseDescription;
    echo "<br/>Merchant Request ID --> ".$merchantRequestID;
    echo "<br/>Checkout Request ID--> ".$checkoutRequestID;
    echo "<br/>Customer Message --> ".$CustomerMessage;