PHPackages                             manuelgeek/mpesab2c - 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. manuelgeek/mpesab2c

ActiveLibrary[API Development](/categories/api)

manuelgeek/mpesab2c
===================

Safaricom's Mpesa Daraja API B2C implementation

v1.0(7y ago)12283MITPHPPHP &gt;=5.6.0

Since Apr 29Pushed 6y ago3 watchersCompare

[ Source](https://github.com/manuelgeek/mpesab2c)[ Packagist](https://packagist.org/packages/manuelgeek/mpesab2c)[ RSS](/packages/manuelgeek-mpesab2c/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel MpesaB2C
================

[](#laravel-mpesab2c)

[![Latest Stable Version](https://camo.githubusercontent.com/1a13f2d568566a29020d2714aeacd201f3a1231f162df7481c81791d3db2f2ff/68747470733a2f2f706f7365722e707567782e6f72672f6d616e75656c6765656b2f6d706573616232632f762f737461626c65)](https://packagist.org/packages/manuelgeek/mpesab2c)[![Total Downloads](https://camo.githubusercontent.com/03e9d60f3bb265816a0f8a1d86b75375b244039c445c118debe3fcfe61ce7466/68747470733a2f2f706f7365722e707567782e6f72672f6d616e75656c6765656b2f6d706573616232632f646f776e6c6f616473)](https://packagist.org/packages/manuelgeek/mpesab2c)[![Latest Unstable Version](https://camo.githubusercontent.com/b21fe48586eb53a946c9fb8d33f854d3aecb9c1608a9d1dae6d243e0078af203/68747470733a2f2f706f7365722e707567782e6f72672f6d616e75656c6765656b2f6d706573616232632f762f756e737461626c65)](https://packagist.org/packages/manuelgeek/mpesab2c)[![License](https://camo.githubusercontent.com/bac3371c4c0934783e5dcca576cf22324a5ea8147c0e52f7bde6ce70e007f109/68747470733a2f2f706f7365722e707567782e6f72672f6d616e75656c6765656b2f6d706573616232632f6c6963656e7365)](https://packagist.org/packages/manuelgeek/mpesab2c)

Safaricom's Mpesa Daraja API B2C implementation

Getting Started
---------------

[](#getting-started)

Read on the Daraja B2C documentation to get to understand more on this implementation.

### Installing

[](#installing)

```
composer require manuelgeek/mpesab2c

```

Laravel 5 and above
-------------------

[](#laravel-5-and-above)

This package supports the auto-discovery feature of Laravel 5.5 and above, So skip these Setup instructions if you're using Laravel 5.5 and above.

In `app/config/app.php` add the following :

1- The ServiceProvider to the providers array :

```
Manuelgeek\MpesaB2C\MpesaServiceProvider::class,

```

2- The class alias to the aliases array :

```
'B2C' => Manuelgeek\MpesaB2C\Facades\B2C::class,

```

3- Publish the config file

```
php artisan vendor:publish --provider="Manuelgeek\MpesaB2C\MpesaServiceProvider"

```

There will now be a new mpesa\_b2c.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 QueueTimeOutURL and ResultURL endpoints w. E.g.

```
"QueueTimeOutURL" => "http:example.com/mpesa/receive",

"ResultURL" => "http://example.com/mpesa/receive"

```

Usage
-----

[](#usage)

### Mpesa B2C Laravel

[](#mpesa-b2c-laravel)

```
    $Amount ='300';
    $CommandID = 'BusinessPayment';
    $PartyB = '254708374149';
    $Remarks = 'Payed well';

    //Using the Facade
    $response = \B2C::sendMpesaMoney($Amount,$CommandID,$PartyB, $Remarks);

    //or the function
    $response = sendMpesaMoney($Amount,$CommandID,$PartyB, $Remarks);
```

### Without Laravel

[](#without-laravel)

Example

```

```

Sample Responses
----------------

[](#sample-responses)

### Successful Request

[](#successful-request)

Once sent, you shall expect a success acknowledgement response from the API informing you that your request was accepted. The response format is as below:

```
{
  "ConversationID": "AG_20180326_00005ca7f7c21d608166",
  "OriginatorConversationID": "12363-1328499-6",
  "ResponseCode": "0",
  "ResponseDescription": "Accept the service request successfully."
}
```

Note the value of ResponseCode. Any value other than 0 (zero) means the request was unsuccessful, and the error is defined in the ResponseDescription element. So you need to fix that first. A value of 0 means the request was accepted by the API.

After M-Pesa completes processing the transaction, it sends back the callback via the ResultURL you specified in the initial request. A callback from M-Pesa can either be a success callback or a failure callback. A sample of a successful transaction callback is as shown below:

```
{
	"Result":
	{
		"ResultType":0,
		"ResultCode":0,
		"ResultDesc":"The service request has been accepted successfully.",
		"OriginatorConversationID":"14593-80515-2",
		"ConversationID":"AG_20170821_000049448b24712383de",
		"TransactionID":"LHL41AHJ6G",
		"ResultParameters":
		{
			"ResultParameter":
			[
				{
					"Key":"TransactionAmount",
					"Value":100
				},
				{
					"Key":"TransactionReceipt",
					"Value":"LHL41AHJ6G"
				},
				{
					"Key":"B2CRecipientIsRegisteredCustomer",
					"Value":"Y"
				},
				{
					"Key":"B2CChargesPaidAccountAvailableFunds",
					"Value":0.00
				},
				{
					"Key":"ReceiverPartyPublicName",
					"Value":"254708374149 - John Doe"
				},
				{
					"Key":"TransactionCompletedDateTime",
					"Value":"21.08.2017 12:01:59"
				},
				{
					"Key":"B2CUtilityAccountAvailableFunds",
					"Value":98834.00
				},
				{
					"Key":"B2CWorkingAccountAvailableFunds",
					"Value":100000.00
				}
			]
		},
		"ReferenceData":
		{
			"ReferenceItem":
			{
				"Key":"QueueTimeoutURL",
				"Value":"https:\/\/internalsandbox.safaricom.co.ke\/mpesa\/b2cresults\/v1\/submit"
			}
		}
	}
}
```

A sample method to consume the callback response would be as below, just get the data and dump it to a `transactions` table

```
    public function saveResponse()
        {
            $postData =  file_get_contents('php://input');

            $request = json_decode($postData,true);

            if ($request['Result']['ResultCode'] == 0) {

            	DB::table('mpesa_transactions')
                ->insert([
                    'ResultType' => $request['Result']['ResultType'],
                    'ResultCode' => $request['Result']['ResultCode'],
                    "ResultDesc" => $request['Result']['ResultDesc'],
                    "OriginatorConversationID" => $request['Result']['OriginatorConversationID'],
                    "ConversationID" => $request['Result']['ConversationID'],
                    "TransactionID" => $request['Result']['TransactionID'],
                    "TransactionReceipt" => $request['Result']['ResultParameters']['ResultParameter'][0]['Value'] ,
                    "TransactionAmount" => $request['Result']['ResultParameters']['ResultParameter'][1]['Value'] ,
                    "B2CWorkingAccountAvailableFunds" => $request['Result']['ResultParameters']['ResultParameter'][2]['Value'] ,
                    "B2CUtilityAccountAvailableFunds" => $request['Result']['ResultParameters']['ResultParameter'][3]['Value'] ,
                    "TransactionCompletedDateTime" => $request['Result']['ResultParameters']['ResultParameter'][4]['Value'] ,
                    "ReceiverPartyPublicName" => $request['Result']['ResultParameters']['ResultParameter'][5]['Value'] ,
                    "B2CChargesPaidAccountAvailableFunds" => $request['Result']['ResultParameters']['ResultParameter'][6]['Value'] ,
                    "B2CRecipientIsRegisteredCustomer" => $request['Result']['ResultParameters']['ResultParameter'][7]['Value'] ,
                    "QueueTimeoutURL" => $request['Result']['ReferenceData']['ReferenceItem']['Value'] ,
                    'created_at'=>\Carbon\Carbon::now(),
                    'updated_at'=>\Carbon\Carbon::now(),
                ]);
            }

            return "success";
        }
```

Contributing
------------

[](#contributing)

Authors
-------

[](#authors)

- **Magak Emmanuel** - [Manuelgeek](https://github.com/manuelgeek)

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

Acknowledgments
---------------

[](#acknowledgments)

[![](https://camo.githubusercontent.com/d1e7cd46bd17e9501d3d80b5f5910e12a55d421b639f36f960565526daea07d1/68747470733a2f2f617070736c61622e636f2e6b652f6173736574732f696d672f6c6f676f2e706e67)](https://appslab.co.ke)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

2570d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8266f8b6edf450366c1fd0dcd4f0b7c02bd136fd6a979031c1871f46c035b6a0?d=identicon)[manuelgeek](/maintainers/manuelgeek)

---

Top Contributors

[![manuelgeek](https://avatars.githubusercontent.com/u/23236306?v=4)](https://github.com/manuelgeek "manuelgeek (19 commits)")

---

Tags

apib2cdarajalaravellaravel5mpesasafaricomapilaraveltransactionsmpesasafaricom

### Embed Badge

![Health badge](/badges/manuelgeek-mpesab2c/health.svg)

```
[![Health](https://phpackages.com/badges/manuelgeek-mpesab2c/health.svg)](https://phpackages.com/packages/manuelgeek-mpesab2c)
```

###  Alternatives

[smodav/mpesa

M-Pesa API implementation

16363.7k1](/packages/smodav-mpesa)[kabangi/mpesa

M-Pesa API implementation

453.0k2](/packages/kabangi-mpesa)[rakibdevs/openweather-laravel-api

Laravel package to connect https://openweathermap.org/ to get customized weather data for any location on the globe immediately

7648.2k](/packages/rakibdevs-openweather-laravel-api)

PHPackages © 2026

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