This is a PHP package for the Safaricom's M-Pesa API. The API allows a merchant to initiate C2B online checkout (paybill via web) transactions. The merchant submits authentication details, transaction details, callback url and callback method.
After request submission, the merchant receives instant feedback with validity status of their requests. The C2B API handles customer validation and authentication via USSD push. The customer then confirms the transaction. If the validation of the customer fails or the customer declines the transaction, the API makes a callback to merchant. Otherwise the transaction is processed and its status is made through a callback.
Pull in the package through Composer.
When using vanilla PHP, modify your composer.json
file to include:
"scripts": {
"post-update-cmd": [
"SmoDav\\Mpesa\\Support\\Installer::install"
]
},
This script will copy the default configuration file to a config folder in the root directory of your project. Now proceed to require the package.
Run composer require smodav/mpesa
to get the latest stable version of the package.
v3 of this API uses the new M-Pesa API thus it might break some parts of the code in the previous versions. Checkout the 2.0 Branch for the older version.
When using Laravel 5, 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,
],
Publish the package specific config using:
php artisan vendor:publish
This will publish the M-Pesa configuration file into the config
directory as
mpesa.php
. This file contains all the configurations required to use the package.
When going live edit the config and set the production_endpoint
e.g.
'production_endpoint' => 'https://production.safaricom.co.ke/'
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);
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('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');
// 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');
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.