PHP code example of mxgel / mpesa

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

    

mxgel / mpesa example snippets


    /**
     * @var Auth
     */
    private $auth;

    /**
     * @return \Mxgel\MPesa\Auth\Auth
     */
    public function getAuth(): Auth
    {
        if (!$this->auth) {
            return $this->auth = new Mxgel\MPesa\Auth\Auth([
                'key'    => config('key'),
                'secret' => config('secret'),
            ]);
        }

        return $this->auth;
    }
    

$request = LNMO::make($amount, $phoneNumber, $shortCode, 'Wallet top up');
$request->setPassKey(config('LNMO_passkey'))
    ->setBusinessShortCode(config('LNMO_short_code'))
    ->setCallBackURL($callback);


$resp = $request->execute($this->getAuth());
Log::info('STk data', $request->toArray());

//NOTE: For a real application, save this details in a data store

// Assuming u have an Express model, u can have this.

Log::debug('LNMO Data', $this->request->all());
$resp = new LNMOCallbackResponse($this->request->get('Body'));
Express::whereMerchantRequestId($resp->getMerchantRequestID())
    ->whereCheckoutRequestId($resp->getCheckoutRequestID())
    ->whereConfirmed(false)                 // Ensure it's yet to be updated
    ->update([
        'receipt_number' => $resp->getMpesaReceiptNumber(),
        'confirmed'      => $resp->completed(),
    ]);

$request = new LNMQ([
    'businessShortCode' => $business_short_code,
    'checkoutRequestID' => $checkout_request_id,
    'passKey'           => config('LNMO_passkey'),
]);

// Remember our auth object? Yes, we need it.
$resp = $request->execute($this->getAuth());
Log::info("Execute status with data: ", $request->toArray());
Log::info("Executed status check with response", $resp->toArray());
if ($resp->completed()) {
    // Your code here...
}