PHPackages                             savannabits/daraja - 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. [Payment Processing](/categories/payments)
4. /
5. savannabits/daraja

ActiveLibrary[Payment Processing](/categories/payments)

savannabits/daraja
==================

Safaricom Mpesa API (Safaricom Daraja) for PHP Laravel . Simple integration with safaricom's MPESA API dubbed { DARAJA } and allow you to make requests in the nice

v1.0.0(3y ago)122395MITPHPPHP ^8.0

Since May 15Pushed 3y ago3 watchersCompare

[ Source](https://github.com/savannabits/daraja)[ Packagist](https://packagist.org/packages/savannabits/daraja)[ Docs](https://github.com/savannabits/daraja)[ RSS](/packages/savannabits-daraja/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (8)Dependencies (3)Versions (10)Used By (0)

Description
===========

[](#description)

[![Latest Version on Packagist](https://camo.githubusercontent.com/cf02cedd328c1e7a43761ac7db2d73224e62aa13c25f928f17e46be34dda3cbb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736176616e6e61626974732f646172616a612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/savannabits/daraja)[![Build Status](https://camo.githubusercontent.com/7d6da26b2d4b7825717e7016a5c56daa5482391fcb05fab50a496cb731d4b1ce/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736176616e6e61626974732f646172616a612f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/savannabits/daraja)[![Quality Score](https://camo.githubusercontent.com/425a8ccbcf222c906f1327eebc3a2574d7fbc422e7e88a8e47d732622443ff5b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f736176616e6e61626974732f646172616a612e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/savannabits/daraja)[![Total Downloads](https://camo.githubusercontent.com/4ecc13eb72be3b6dd6d1c57db89b7886f6a1a0afaaa6d1ba4bff4ba6e9ceb7db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736176616e6e61626974732f646172616a612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/savannabits/daraja)

The savannabits/daraja package is a Laravel package that makes it easier to talk to the Safaricom MPESA integration RESTFul API dubbed Safaricom Daraja.

### Motivation

[](#motivation)

So why Daraja and not other packages out there? I have looked at a number of packages that can accomplish the same, but the solution I was looking for was a package that can allow me do token generation and Callback registration on the fly from my code. That means I can store my credentials (Consumer Key and consumer secret) even in a database and retrieve them dynamically, using them to register my callbacks. With this solution, you can even have a multiple shortcode gateway (Many apps each using a different paybill can use this gateway to talk to safaricom and register dynamic confirmation urls according to their needs).

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

[](#installation)

You can install the package via composer:

```
composer require savannabits/daraja
```

Usage
-----

[](#usage)

Usage is very simple. If you are using C2B apis, the first step is to register the urls. Most of the work has been done for you under the hood.

### Callback URLs Registration

[](#callback-urls-registration)

```
 use Savannabits\Daraja\Daraja;

 $shortcode = "600737";//Your Paybill or till number here
 $confirmationURL = 'your dynamic validation url here';
 $validationURL = 'your dynamic validation url here'; // Optional. Leave null if you don't want validation
 $environment = "sandbox"; // or "live"
 $responseType = "Cancelled"; //Default Response type in case the validation URL is unreachable or is undefined. Either Cancelled or Completed as per the safaricom documentation
 $response = Daraja::getInstance()
      ->setCredentials("CONSUMER KEY","CONSUMER SECRET",$environment)
      ->registerCallbacks($shortcode, $confirmationURL, $validationURL,$responseType);
```

That's it. You can even put this code inside a Console Command class so that you run something like `php artisna mpesa:register-callbacks` from your terminal.

### C2B simulation

[](#c2b-simulation)

If you are on Dev and would like to simulate a C2B transaction (without using any actual money) you can use the sandbox credentials and assuming you have registered the callbacks above, run the following:

```
    use Savannabits\Daraja\Daraja;
    /**
     * Here is the c2b Function documentation to help you understand the params:
     * Use this function to initiate a C2B transaction
     * @param $ShortCode | 6 digit M-Pesa Till Number or PayBill Number
     * @param $CommandID | Unique command for each transaction type. either "CustomerPayBillOnline" or "CustomerBuyGoodsOnline"
     * @param $Amount | The amount been transacted.
     * @param $Msisdn | MSISDN (phone number) sending the transaction, start with country code without the plus(+) sign.
     * @param $BillRefNumber | 	Bill Reference Number (Optional).
     * @return mixed|string
     */
    $shortcode = "YOUR SHORTCODE";
    $commandID = "CustomerPayBillOnline";
    $amount = 100;
    $msisdn = "07xxxxxxxx"; // See safaricom daraja documentation and check your credentials for the specific number given for testing.
    $billRefNumber = "THE PAYBILL ACCOUNT NO."; // e.g "MAMA MBOGA 212"
    $response = Daraja::getInstance()
                    ->setCredentials("YOUR CONSUMER KEY","YOUR CONSUMER SECRET","sandbox")
                    ->c2b($shortcode, $commandID, $amount, $msisdn, $billRefNumber);
```

### STK Simulation (Lipa na MPESA Online)

[](#stk-simulation-lipa-na-mpesa-online)

Here is the method documentation

```
/**
 * Use this function to initiate an STKPush Simulation
 * @param $BusinessShortCode | The organization shortcode used to receive the transaction.
 * @param $LipaNaMpesaPasskey | The password for encrypting the request. This is generated by base64 encoding BusinessShortcode, Passkey and Timestamp.
 * @param $TransactionType | The transaction type to be used for this request. Only CustomerPayBillOnline is supported.
 * @param $Amount | The amount to be transacted.
 * @param $PartyA | The MSISDN sending the funds.
 * @param $PartyB | The organization shortcode receiving the funds
 * @param $PhoneNumber | The MSISDN sending the funds.
 * @param $CallBackURL | The url to where responses from M-Pesa will be sent to.
 * @param $AccountReference | Used with M-Pesa PayBills.
 * @param $TransactionDesc | A description of the transaction.
 * @param $Remark | Remarks
 * @return mixed|string
 */
\Savannabits\Daraja\Daraja::getInstance()
            ->setCredentials("CONSUMER KEY","CONSUMER SECRET","sandbox")
            ->STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remark);
```

```
    // Example
   $json = \Savannabits\Daraja\Daraja::getInstance()
            ->setCredentials("CONSUMER KEY","CONSUMER SECRET","sandbox")
            ->STKPushSimulation($app->short_code,$app->lnm_passkey,$app->transaction_type, $amount, $phone_number, $short_code, $phone_number, $confirm_callback,$account_ref, $transaction_desc, $comments);
```

### Validation and Confirmation Callback example, Finishing a Transaction

[](#validation-and-confirmation-callback-example-finishing-a-transaction)

```
use Savannabits\Daraja\Daraja;
class MyController extends \App\Http\Controllers\Controller {
    public function c2bValidationCallback(Request $request) {
        // Perform your validations here and set the status
        $status = true; // Or false based on whether you want to accept or reject the transaction.
        Daraja::getInstance()->finishTransaction($status);
    }
    public function c2bConfirmationCallback(Request $request) {
        //Get Response data
        $response = Daraja::getInstance()->getDataFromCallback();
        // $response = $request->all(); //Alternatively...
        // Do what you want with the data
        // ...
        // Finish Transaction
        Daraja::getInstance()->finishTransaction(true);
    }
    public function stkConfirmationCallback(Request $request) {
       $mpesa = new Daraja();
        // Get data from safaricom response.
       $data = $mpesa->getDataFromCallback();
       \Log::info($data);
        // Handle the data
        //...
        //Finish Transaction by sending acknowledgment to safaricom
        $mpesa->finishTransaction(true);

    }
}
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Sam Maosa](https://github.com/savannabits)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 82.4% 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 ~132 days

Recently: every ~217 days

Total

8

Last Release

1268d ago

Major Versions

v0.2.0 → v1.0.02022-11-28

PHP version history (3 changes)v0.0.1PHP ^7.2

v0.2.0PHP ^7.3|^8.0

v1.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5610289?v=4)[Sam Maosa](/maintainers/coolsam726)[@coolsam726](https://github.com/coolsam726)

---

Top Contributors

[![smaosa](https://avatars.githubusercontent.com/u/275793777?v=4)](https://github.com/smaosa "smaosa (14 commits)")[![coolsam726](https://avatars.githubusercontent.com/u/5610289?v=4)](https://github.com/coolsam726 "coolsam726 (3 commits)")

---

Tags

b2bc2bdaraja-apidaraja-mpesalaravellaravel-packagempesampesa-paymentsmpesa-sdksafaricomsafaricom-apisavannabitsstkpushmpesalaravel mpesasafaricomdarajasavannabitsdaraja-apidaraja mpesasafaricom mpesalaravel safaricomlaravel darajasafaricom apilaravel mpesa apiphp mpesaphp darajasafaricom php mpesa

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/savannabits-daraja/health.svg)

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

###  Alternatives

[smodav/mpesa

M-Pesa API implementation

16363.7k1](/packages/smodav-mpesa)[knox/pesapal

A laravel package that integrates into the pesapal api

29106.3k](/packages/knox-pesapal)[gathuku/laravelmpesa

A simple mpesa package for laravel framework

5118.1k](/packages/gathuku-laravelmpesa)[iankumu/mpesa

A package that helps integrate the Mpesa APIs to your Laravel Project

5014.7k](/packages/iankumu-mpesa)[itsmurumba/laravel-mpesa

Laravel Package for Mpesa Daraja API

191.6k](/packages/itsmurumba-laravel-mpesa)[karson/mpesa-php-sdk

172.2k](/packages/karson-mpesa-php-sdk)

PHPackages © 2026

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