PHP code example of calvinchiulele / mpesa-mz

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

    

calvinchiulele / mpesa-mz example snippets




return [
    /*
    |--------------------------------------------------------------------------
    | Public key for use in M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may specify the public key provided by Vodacom to you
    |
    */
    'public_key' => env('MPESA_PUBLIC_KEY', ''),

    /*
    |--------------------------------------------------------------------------
    | API host of M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may specify the API host provided by Vodacom for API operations
    |
    */
    'api_host' => env('MPESA_API_HOST', 'api.sandbox.vm.co.mz'),

    /*
    |--------------------------------------------------------------------------
    | API Key of M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may specify the API key provided by Vodacom to you
    |
    */
    'api_key' => env('MPESA_API_KEY', ''),

    /*
    |--------------------------------------------------------------------------
    | Origin of M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may specify the API key provided by Vodacom to you
    |
    */
    'origin' => env('MPESA_ORIGIN', ''),

    /*
    |--------------------------------------------------------------------------
    | Service Provider Code of M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may specify the service provider code of M-Pesa provided by Vodacom to you
    |
    */
    'service_provider_code' => env('MPESA_SERVICE_PROVIDER_CODE', ''),

    /*
    |--------------------------------------------------------------------------
    | Initiator Identifier of M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may the initiator identifier provided by Vodacom to you
    |
    */
    'initiator_identifier' => env('MPESA_INITIATOR_IDENTIFIER', ''),

    /*
    |--------------------------------------------------------------------------
    | Security credential of M-Pesa API
    |--------------------------------------------------------------------------
    |
    | Here you may specify the security credential provided by Vodacom to you
    |
    */
    'security_credential' => env('MPESA_SECURITY_CREDENTIAL', '')
];



namespace App\Http\Controllers;

use CalvinChiulele\MPesaMz\Facades\MPesaMz;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    // more code

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // This call is a C2B transaction, which is used as a standard customer-to-business transaction. Funds from the customer’s mobile money wallet will be deducted and be transferred to the mobile money wallet of the business. To authenticate and authorize this transaction, M-Pesa Payments Gateway will initiate a USSD Push message to the customer's phone to gather and verify the mobile money PIN number. This number is not stored and is used only to authorize the transaction.

        // $request->msisdn corresponds to the customer's phone number (e.g: 258840000000)

        // 100 corresponds to the amount of the transaction. Please, do best pratices and not use magic numbers.

        // $reference corresponds to the reference of the transaction for the customer or business making the transaction. This can be a smartcard number for a TV subscription or a reference number of a utility bill.

        // $thirdPartyReference corresponds to the unique reference of the third party system. When there are queries about transactions, this will usually be used to track a transaction.
        $payment = MPesaMz::payment($request->msisdn, 100, $reference, $thirdPartyReference);

        // This call is a refund or reversal, which is used to reverse a successful transaction. // Using the Transaction ID of a previously successful transaction, M-Pesa Payments Gateway will withdraw the funds from the recipient party’s mobile money wallet and revert the funds to the mobile money wallet of the initiating party of the original transaction. 

        // $payment->getTransactionID() corresponds to the transaction ID of the just made sucessfully transaction. It's used to query transactions on the mobile money platform.

        // 100 corresponds to the amount to be reversed.
        $refund = MPesaMz::refund($payment->getTransactionID(), 100);

        // This call is to query a transaction status, which determines the current status of a particular transaction. Using either the Transaction ID or the Conversation ID of a transaction just made, the M-Pesa Payments Gateway will return information about the transaction’s status.

        $query = MPesaMz::query($refund->getTransactionID());

        // more code
    }
}


php artisan vendor:publish
 php artisan config:clear 
 php


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    /**
     * M-Pesa service class
     *
     * @var \CalvinChiulele\MPesaMz\Services\MPesaMz
     */
    protected $mpesaService;

    /**
     * Create a new instance
     *
     * @return void
     */
    public function __construct()
    {
        $this->mpesaService = app('CalvinChiulele\MPesaMz\Services\MPesaMz');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // The documentation on the example above serves for this example.

        $payment = $this->mpesaService->payment($request->msisdn, 100, $reference, $thirdPartyReference);

        $refund = $this->mpesaService->refund($payment->getTransactionID(), 100);

        $query = $this->mpesaService->query($refund->getTransactionID());

        // more code
    }
}