How To Use M-Pesa PHP Package Version 2

19th Oct 2017

When the packages installs, the installer will be called and this will copy the default configuration file to the root directory of your project. You can now pull in the package via composer:

composer require smodav/mpesa

Once pulled, the package is usable out of the box. All you will be required to do is edit the configuration file but before that, if you are using Larave Framework, you will need to register the service providers and facades on you app.php

'providers' => [
    SmoDav\Mpesa\MpesaServiceProvider::class,
],
'aliases' => [
    'Mpesa' => SmoDav\Mpesa\Mpesa::class,
]

The installer script above will publish the file for the vanilla php users, for laravel you will need to execute:

php artisan vendor:publish

Modifying the configuration.

A new file will be published; mpesa.php; under config directory of your Laravel application. You can now edit the configuration file with your credentials as below:

demo: boolean Possible values: true | false Default: true This is a flag to set the API in demo mode and use the demo timestamp and password. When demo is set to true, set the paybill number to 898998. endpoint: string Default: "https://safaricom.co.ke/mpesa_online/lnmo_checkout_server.php?wsdl" This is the default Safaricom API endpoint to be queried for transactional requests. callback_url: string This is a fully qualified http endpoint that will be be queried by Safaricom's API on completion or failure of the transaction. It should point to the notification endpoint for your application where you can finalize the checkout process. callback_method: HTTP Request Method Possible values: GET | POST Default: POST This is the request method to be used when communicating with the Callback endpoint paybill_number: int Default: 898998 (Safaricom Demo Paybill) This is a registered Paybill Number that will be used as the Merchant ID on every transaction. This is also the account to be debited on every successful transaction. passkey: string This is the secret SAG Passkey generated by Safaricom on registration of the Merchant's Paybill Number. transaction_id_handler: class Default: '\SmoDav\Mpesa\Generator' Provide a fully qualified class name of the Class that will be used to generate the transaction number. This class must implement the Transactable Interface. On every transaction, this class will be queried to provide a transaction number to be used. The default works fine.

Using the package.

The simplest part is using the package. There are a few transactions you can carry out with this package as listed below:

Initiate STK Push

The STK Push interface allows the merchant to initiate a C2B transaction on behalf of the customer. The merchant will need to generate the transaction details, then MPesa SAG will receive the details and verify them before initiating a new transaction. When the request is successful, a customer will receive a USSD prompt on their mobile and be requested to enter their service pin. To initiate this transaction you can use any of the below methods depending on the platform.

When using Laravel framework, we provide a helper and also a facade. Just request an amount from a mobile number using a given reference and transact.

public function checkout()
{
  $repsonse = mpesa()
      ->request(100)
      ->from(254722000000)
      ->usingReferenceId('Software')
      ->transact();
  // OR
  $repsonse = mpesa(100)
      ->from(254722000000)
      ->usingReferenceId('Software')
      ->transact();
  // OR
  $repsonse = mpesa(100, 254722000000)
      ->usingReferenceId('Software')
      ->transact();
  // OR
  $repsonse = mpesa(100, 254722000000, 'Software')
      ->transact();
}

In case you are not using Laravel framework, you can use the package natively by making an instance of the native Mpesa class:

use SmoDav\Mpesa\Native\Mpesa;

public function checkout()
{
    $mpesa = new Mpesa;
    $repsonse = $mpesa->request(100)
        ->from(254722000000)
        ->usingReferenceId(115445)
        ->transact();
}

If you have more than one PayBill number that is authorized to be used, you can take advantage of the setPaybill method by calling it before you call the transact method. This applies to the facade, helper function and also the native implementation. I have omitted the Laravel implementation for brevity, I will show you the native implementation:

public function checkout()
{
    $mpesa = new Mpesa;
    $repsonse = $mpesa->request(100)
        ->from(254722000000)
        ->usingReferenceId(115445)
        ->setPayBill(898998, 'the SAG passkey')
        ->transact();
}

Validate a transaction

Once a transaction is complete, you might need to verify its status. You will be required to have the transaction number that you received when the transaction was completed.

use SmoDav\Mpesa\Native\Mpesa;

$mpesa = new Mpesa;
$status = $mpesa->validate('h3E3N1zv7lIhSlX1i');
// Laravel
$status = mpesa()->validate('h3E3N1zv7lIhSlX1i');
// OR
$status = Mpesa::validate('h3E3N1zv7lIhSlX1i');

The value of status will be an object with the status field and the other transaction details that were received from Safaricom. Hope this will aid you in making use of Safaricom's API. I am currently working on a new version of this package that will leverage all the features of the brand new MPesa API.

© 2020 SmoDav Productions