PHPackages                             knox/mpesa-rest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. knox/mpesa-rest

ActiveLibrary[API Development](/categories/api)

knox/mpesa-rest
===============

Laravel Package implementation of the MPESA REST API

v1.6.1(7mo ago)01.8k5PHPCI failing

Since Dec 8Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/knox2/mpesa-rest)[ Packagist](https://packagist.org/packages/knox/mpesa-rest)[ RSS](/packages/knox-mpesa-rest/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (26)Used By (0)

MPESA REST API for Laravel
==========================

[](#mpesa-rest-api-for-laravel)

Laravel Package for the MPESA REST API

Installation
------------

[](#installation)

### Add this package using Composer

[](#add-this-package-using-composer)

From the command line inside your project directory, simply type:

`composer require knox/mpesa-rest`

### Update your config

[](#update-your-config)

**NB: from laravel 5.5 due to autodiscovery the below can be skipped**

Add the service provider to the providers array in config/app.php:

`Knox\MPESA\MpesaServiceProvider::class`

Add the facade to the aliases array in config/app.php:

`'MPESA' => Knox\MPESA\Facades\MPESA::class`

### Publish the package configuration

[](#publish-the-package-configuration)

Publish the configuration file and migrations by running the provided console command:

`php artisan vendor:publish --provider="Knox\MPESA\MpesaServiceProvider"`

Setup
-----

[](#setup)

### Environmental Variables

[](#environmental-variables)

MPESA\_ENV=`'live' for production and 'test' for sandbox`
MPESA\_VERSION=`'v1' or 'v2'`
MPESA\_CONSUMER\_KEY=`consumer key`
MPESA\_CONSUMER\_SECRET=`consumer secret`
MPESA\_IDENTIFIER=shortcode `shortcode or till`
MPESA\_SHORT\_CODE=`shortcode`
MPESA\_PASSKEY=`passkey`

MPESA\_TILL\_NUMBER=`'till_number' in case of a till number add it here. Make sure to republish the config`

MPESA\_INITIATOR\_NAME=`initiator username`
MPESA\_INITIATOR\_PASSWORD=`initiator password`

MPESA\_B2C\_TIMEOUT\_URL=`url in your site`
MPESA\_B2C\_RESULT\_URL=`url in your site`
MPESA\_B2B\_TIMEOUT\_URL=`url in your site`
MPESA\_B2B\_RESULT\_URL=`url in your site`

MPESA\_STK\_CALLBACK\_URL=`url in your site`
MPESA\_C2B\_VALIDATION\_URL=`url in your site`
MPESA\_C2B\_CONFIRMATION\_URL=`url in your site`

MPESA\_ACCOUNT\_BALANCE\_TIMEOUT\_URL=`url in your site`
MPESA\_ACCOUNT\_BALANCE\_CONFIRMATION\_URL=`url in your site`

MPESA\_REVERSAL\_TIMEOUT\_URL=`url in your site`
MPESA\_REVERSAL\_CONFIRMATION\_URL=`url in your site`

MPESA\_TRANSACTION\_STATUS\_TIMEOUT\_URL=`url in your site`
MPESA\_TRANSACTION\_STATUS\_CONFIRMATION\_URL=`url in your site`

MPESA\_IDENTITY\_CALLBACK\_URL=`url in your site`

Usage
-----

[](#usage)

At the top of your controller include the facade
`use MPESA;`

### Registration of C2B Urls

[](#registration-of-c2b-urls)

**If you haven't registered the mpesa url callbacks then use the below**

```
use MPESA;
use Illuminate\Http\Request;

class PaymentsController extends Controller
{
    public function registerURL(){
        $mpesa = MPESA::registerC2bUrl();
    }
}
```

#### Posible Operations

[](#posible-operations)

```

mpesa = MPESA::b2c('254700123456',10,'PromotionPayment', 'No Remarks');

mpesa = MPESA::b2b('600000',100,'BusinessPayBill',4,4, 'No Remarks',123456);

mpesa = MPESA::b2b('600000',100,'BusinessBuyGoods',4,4, 'No Remarks');

mpesa = MPESA::registerC2bUrl();

mpesa = MPESA::c2bSimulate('254700123456',100,'CustomerPayBillOnline','123456');
mpesa = MPESA::c2bSimulate('254700123456',1000,'CustomerBuyGoodsOnline');

mpesa = MPESA::getAccountBalance();

mpesa = MPESA::doReversal('ND893KKHX1', 100, 602984, 4);

mpesa = MPESA::stkPush('254700123456', 1000, 'Account 123');

mpesa = MPESA::stkQuery('ws_CO_14092017184227664');

mpesa = MPESA::getTransactionStatus('ND893KKHX1', null,4);

```

#### Example response handler

[](#example-response-handler)

```
public function c2bConfirmation(Request $request)
{
    $response = json_decode($request->getContent(), true);
    $mpesa_transaction_id = $response['TransID'];
    $date_time = Carbon::parse($response['TransTime']);
    $amount = $response['TransAmount'];
    $account = strtoupper(preg_replace('/\s+/', '', $response['BillRefNumber']));
    $merchant_transaction_id = $response['ThirdPartyTransID'];
    $phone = $response['MSISDN'];
    $payer = preg_replace('!\s+!', ' ', ucwords(strtolower($response['FirstName'] . ' ' . $response['MiddleName'] . ' ' . $response['LastName'])));
}

```

#### All Done

[](#all-done)

Feel free to report any issues

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance62

Regular maintenance activity

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~120 days

Recently: every ~247 days

Total

25

Last Release

238d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16401?v=4)[Mickey](/maintainers/knox)[@knox](https://github.com/knox)

---

Top Contributors

[![knox2](https://avatars.githubusercontent.com/u/2101866?v=4)](https://github.com/knox2 "knox2 (35 commits)")

---

Tags

laravelmpesa

### Embed Badge

![Health badge](/badges/knox-mpesa-rest/health.svg)

```
[![Health](https://phpackages.com/badges/knox-mpesa-rest/health.svg)](https://phpackages.com/packages/knox-mpesa-rest)
```

###  Alternatives

[statamic/cms

The Statamic CMS Core Package

4.8k3.6M985](/packages/statamic-cms)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3741.3M46](/packages/tencentcloud-tencentcloud-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k656.1k38](/packages/neuron-core-neuron-ai)[avalara/avataxclient

Client library for Avalara's AvaTax suite of business tax calculation and processing services. Uses the REST v2 API.

528.5M7](/packages/avalara-avataxclient)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

273.0k](/packages/eslazarev-wildberries-sdk)[files.com/files-php-sdk

Files.com PHP SDK

2481.1k](/packages/filescom-files-php-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
