lipa na mpesa

How To Use MPesa PHP Package Version 3

The popular package SmoDav M-Pesa has been updated to use the current API provided by Safaricom. Being a major upgrade, this version will not be 100% backward compatible with version 2. So what are we waiting for, let's get started!

So far We've implemented the C2B section of the API by providing Register URL, Identity and STK Push. Just as the previous versions, this package works with both Laravel and Vanilla PHP or any other framework. When using vanilla PHP, we have a helper script that will copy the required files to ease the configuration process. You can either run it manually or leverage composer to do it for you as a post-update command:


"scripts": { "post-update-cmd": [ "SmoDav\\Mpesa\\Support\\Installer::install" ] }

To get started, lets pull in the latest version of the package via composer.

composer require smodav/mpesa

Laravel 5.5+ offers great package auto-discovery and the package will automatically register it's service providers and facades once you install it. For Laravel 5.4 and below, include the service provider and its alias within your config/app.php.


'providers' => [ SmoDav\Mpesa\Laravel\ServiceProvider::class, ], 'aliases' => [ 'STK' => SmoDav\Mpesa\Laravel\Facades\STK::class, 'Registrar' => SmoDav\Mpesa\Laravel\Facades\Registrar::class, 'Identity' => SmoDav\Mpesa\Laravel\Facades\Identity::class, ],

We will need to change the configurations and set our own consumer keys and secrets thus publish the config file if using Laravel. For the native guys, the post-update script did that for you!

php artisan vendor:publish

There will now be a new mpesa.php file in your config directory that is at the root of your project. All the configuration options are present in the file. Also remember to set the production endpoint when going live. E.g.


'production_endpoint' => ' https://production.safaricom.co.ke/ ',

Note for other frameworks

To implement this package, a configuration repository is needed, thus any other framework will need to create its own implementation of the ConfigurationStore and CacheStore interface.

Usage

For Vanilla PHP you will need to initialize the core engine before any requests below.


use GuzzleHttp\Client; use SmoDav\Mpesa\Engine\Core; use SmoDav\Mpesa\Native\NativeCache; use SmoDav\Mpesa\Native\NativeConfig; require "vendor/autoload.php"; $config = new NativeConfig(); $cache = new NativeCache($config); $engine = new Core(new Client, $config, $cache);

URL Registration

submit(shortCode = null, confirmationURL = null, validationURL = null, onTimeout = 'Completed|Cancelled')

Register callback URLs

Vanilla

use SmoDav\Mpesa\C2B\Registrar; $registrar = new Registrar($engine); // fluent implementation $response = $registrar->register(600000) ->onConfirmation(' https://payments.smodavproductions.com/checkout.php ') ->onValidation(' https://payments.smodavproductions.com/checkout.php ') ->submit(); // one function $response = $registrar->submit(600000, ' https://payments.smodavproductions.com/checkout.php ', ' https://payments.smodavproductions.com/checkout.php ');
Laravel

// fluent implementation $response = \Registrar::register(600000) ->onConfirmation(' https://payments.smodavproductions.com/checkout.php ') ->onValidation(' https://payments.smodavproductions.com/checkout.php ') ->submit(); // one function $response = \Registrar::submit(600000, ' https://payments.smodavproductions.com/checkout.php ', ' https://payments.smodavproductions.com/checkout.php ');

STK PUSH

push(amount = null, number = null, reference = null, description = null)

Initiate an C2B STK Push request.

Vanilla

use SmoDav\Mpesa\C2B\STK; $stk = new STK($engine); // fluent implementation $response = $stk->request(10) ->from(254722000000) ->usingReference('f4u239fweu', 'Test Payment') ->push(); // one function $response = $stk->push(10, 254722000000, 'f4u239fweu', 'Test Payment');
Laravel

// fluent implementation $response = \STK::request(10) ->from(254722000000) ->usingReference('f4u239fweu', 'Test Payment') ->push(); // one function $response = \STK::push(10, 254722000000, 'f4u239fweu', 'Test Payment');

Identity

validate(number)

Validate the phone number and get details about it.

Vanilla

use SmoDav\Mpesa\C2B\Identity; $identity = new Identity($engine); $response = $identity->validate(254722000000);
Laravel

$response = \Identity::validate(254722000000);

License

The M-Pesa Package is open-sourced software licensed under the MIT license.

Related Articles