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' => ' ',

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.


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


use SmoDav\Mpesa\C2B\Registrar; $registrar = new Registrar($engine); // fluent implementation $response = $registrar->register(600000) ->onConfirmation(' ') ->onValidation(' ') ->submit(); // one function $response = $registrar->submit(600000, ' ', ' ');

// fluent implementation $response = \Registrar::register(600000) ->onConfirmation(' ') ->onValidation(' ') ->submit(); // one function $response = \Registrar::submit(600000, ' ', ' ');


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

Initiate an C2B STK Push request.


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');

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



Validate the phone number and get details about it.


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

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


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

Related Articles