PHP code example of nevar / mpesa

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

    

nevar / mpesa example snippets




return [
    'providers' => [
        /*
         * Package Service Providers...
         */

        MPESA\Providers\AuthServiceProvider::class,
        MPESA\Providers\ConfigurationServiceProvider::class,
        MPESA\Providers\OnlinePaymentServiceProvider::class,
    ]
];


MPESA_DEVELOPEMENT_MODE=true
MPESA_USERNAME=m&yu$ern@ame
MPESA_PASSWORD=password
MPESA_PAYBILLNO=0000
MPESA_STK_PASSKEY=000c2daf998f92aa5a925350031f2471f873bff7877879b45cc364f2cb9a9907ef1245
MPESA_CALLBACK_URL= http://5c3f4b1d.ngrok.io/api/mpesa/callback




namespace App\Http\Controllers;

use Illuminate\Http\Request;

use MPESA\Services\OnlinePaymentService;

class PaymentController extends Controller
{
    public function requestPayment(Request $request,OnlinePaymentService $stk)
    {
        // phone number
        $user_phone_number = "254712345678"; # insert user phone number
        $amount = 50; # insert amount payable

        // this is optional if you want to overide the IPN on your .env
        $stk->callBackURL = "http://8bbea5f4.ngrok.io/api/mpesa/response-callback";
        
        // request for payment
        $stk->initiateSTKCheckout($amount,$user_phone_number);

    }
}