PHPackages                             drh/mpesa - 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. drh/mpesa

ActiveLibrary

drh/mpesa
=========

Mpesa Payments Library

v4.2.0(1mo ago)32.6k↓36.4%[1 PRs](https://github.com/DrH97/mpesa/pulls)MITPHPPHP ^8.2CI passing

Since Aug 9Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/DrH97/mpesa)[ Packagist](https://packagist.org/packages/drh/mpesa)[ RSS](/packages/drh-mpesa/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (27)Versions (52)Used By (0)

Mpesa Payments API
==================

[](#mpesa-payments-api)

[![GitHub TestCI Workflow](https://github.com/DrH97/mpesa/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/DrH97/mpesa/actions/workflows/test.yml)[![Github StyleCI Workflow](https://github.com/DrH97/mpesa/actions/workflows/styleci.yml/badge.svg?branch=main)](https://github.com/DrH97/mpesa/actions/workflows/styleci.yml)[![codecov](https://camo.githubusercontent.com/acfea0666ce089793cbbcdbc7999c58b914d6bf3aa10e79f855a6a31e5f5d26a/68747470733a2f2f636f6465636f762e696f2f67682f44724839372f6d706573612f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d36623064306261312d633263362d343037372d386333612d316635363765656138386130)](https://codecov.io/gh/DrH97/mpesa)

[![Latest Stable Version](https://camo.githubusercontent.com/8a5656b4c5e03c00d3848ccb7bd42726cf5d8788dda07903d663d57fcc56a323/687474703a2f2f706f7365722e707567782e6f72672f6472682f6d706573612f76)](https://packagist.org/packages/drh/mpesa)[![Total Downloads](https://camo.githubusercontent.com/8fbaad5b0624d8d3e2b110a6ea851ce9e34308d1c5bf59166c1df6ae36d8dbfe/687474703a2f2f706f7365722e707567782e6f72672f6472682f6d706573612f646f776e6c6f616473)](https://packagist.org/packages/drh/mpesa)[![Latest Unstable Version](https://camo.githubusercontent.com/46d8742d3501d9fbac37470efcee5aca3552cfc0d43c73baabd2ea6dad604588/687474703a2f2f706f7365722e707567782e6f72672f6472682f6d706573612f762f756e737461626c65)](https://packagist.org/packages/drh/mpesa)[![License](https://camo.githubusercontent.com/1920a2704843bc17c140ee33eb5fa641cf855755a88dec061e004b38c9c3bdc1/687474703a2f2f706f7365722e707567782e6f72672f6472682f6d706573612f6c6963656e7365)](https://packagist.org/packages/drh/mpesa)[![PHP Version Require](https://camo.githubusercontent.com/bfa1be288d992261fb61806631676a30d12673b4dc1f37413d617dcce53fe115/687474703a2f2f706f7365722e707567782e6f72672f6472682f6d706573612f726571756972652f706870)](https://packagist.org/packages/drh/mpesa)

---

A Laravel package for integrating with Safaricom's M-Pesa payment APIs. Supports STK Push, C2B, B2C, B2B payments, transaction status queries, and identity verification.

Inspired by Mobile Money Library by Agweria:

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 8.x - 13.x

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

[](#installation)

```
composer require drh/mpesa
```

The package auto-discovers its service provider and facades. Publish the configuration file:

```
php artisan vendor:publish --provider="DrH\Mpesa\MpesaServiceProvider"
```

Run migrations to create the payment tracking tables:

```
php artisan migrate
```

Configuration
-------------

[](#configuration)

Add these environment variables to your `.env` file:

```
# General
MPESA_SANDBOX=true

# C2B / STK Push
MPESA_KEY=your-consumer-key
MPESA_SECRET=your-consumer-secret
MPESA_C2B_SHORTCODE=174379
MPESA_C2B_PASS_KEY=your-passkey
MPESA_C2B_TRANSACTION_TYPE=CustomerPayBillOnline

# B2C (Business to Customer)
MPESA_B2C_KEY=your-b2c-key
MPESA_B2C_SECRET=your-b2c-secret
MPESA_B2C_SHORTCODE=603021
MPESA_B2C_INITIATOR=apiop37

# B2B (Business to Business)
MPESA_B2B_KEY=your-b2b-key
MPESA_B2B_SECRET=your-b2b-secret
MPESA_B2B_SHORTCODE=600584
MPESA_B2B_INITIATOR=apiop37
```

See the published `config/drh.mpesa.php` for all available options including callback URLs, retry settings, multi-tenancy, and logging.

Usage
-----

[](#usage)

### STK Push (Lipa Na M-Pesa Online)

[](#stk-push-lipa-na-m-pesa-online)

```
use DrH\Mpesa\Facades\STK;

// Simple push
$result = STK::push(amount: 100, phone: '254712345678', ref: 'Order001', description: 'Payment for order');

// Fluent API
$result = STK::amount(100)
    ->from('254712345678')
    ->usingReference('Order001', 'Payment for order')
    ->push();

// Check STK transaction status
$status = STK::status($checkoutRequestId);
```

### C2B (Customer to Business)

[](#c2b-customer-to-business)

Register your validation and confirmation URLs with M-Pesa:

```
use DrH\Mpesa\Facades\Registrar;

Registrar::shortcode(174379)
    ->onConfirmation('https://example.com/payments/callbacks/c2b-confirmation')
    ->onValidation('https://example.com/payments/callbacks/c2b-validation')
    ->submit();
```

### B2C (Business to Customer)

[](#b2c-business-to-customer)

```
use DrH\Mpesa\Facades\B2C;

// Send payment
$result = B2C::send(number: '254712345678', amount: 500, remarks: 'Salary payment');

// Fluent API
$result = B2C::to('254712345678')
    ->amount(500)
    ->withRemarks('Salary payment')
    ->send();

// Check balance
$balance = B2C::balance();

// Check transaction status
$status = B2C::status($transactionId);
```

### B2B (Business to Business)

[](#b2b-business-to-business)

```
use DrH\Mpesa\Facades\B2B;

// Send payment
$result = B2B::pay(
    type: 'BusinessPayBill',
    shortcode: '600000',
    amount: 1000,
    reference: 'INV001',
    phone: '254712345678'
);

// Check transaction status
$status = B2B::status($request);
```

### Identity Verification

[](#identity-verification)

```
use DrH\Mpesa\Facades\Identity;

$result = Identity::validate('254712345678');
```

Events
------

[](#events)

The package dispatches events for payment lifecycle hooks. Listen for these in your `EventServiceProvider` or via `Event::listen()`:

EventDescription`StkPushRequestedEvent`STK push request initiated`StkPushPaymentSuccessEvent`STK push payment completed successfully`StkPushPaymentFailedEvent`STK push payment failed`C2bConfirmationEvent`C2B payment confirmed`B2cPaymentSuccessEvent`B2C payment completed successfully`B2cPaymentFailedEvent`B2C payment failed`B2bPaymentSuccessEvent`B2B payment completed successfully`B2bPaymentFailedEvent`B2B payment failed`TransactionStatusSuccessEvent`Transaction status query succeeded`TransactionStatusFailedEvent`Transaction status query failed`QueueTimeoutEvent`Queue timeout occurredAll events are in the `DrH\Mpesa\Events` namespace.

Artisan Commands
----------------

[](#artisan-commands)

```
# Register C2B validation and confirmation URLs
php artisan mpesa:register_c2b_urls

# Check status of pending STK transactions
php artisan mpesa:query_stk_status

# Check status of pending B2C transactions
php artisan mpesa:b2c_transaction_status

# Check status of pending B2B transactions
php artisan mpesa:b2b_transaction_status
```

Callback Routes
---------------

[](#callback-routes)

The package automatically registers callback routes under `/payments/callbacks/`:

- `POST /payments/callbacks/stk-callback` - STK push callback
- `POST /payments/callbacks/c2b-validation` - C2B validation
- `POST /payments/callbacks/c2b-confirmation` - C2B confirmation
- `POST /payments/callbacks/result/` - B2C/B2B result
- `POST /payments/callbacks/timeout/` - B2C/B2B timeout

A `pesa.cors` middleware alias is available for CORS handling on callback routes.

Testing
-------

[](#testing)

```
composer run-script test
```

License
-------

[](#license)

MIT

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance88

Actively maintained with recent releases

Popularity25

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 93% 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 ~36 days

Recently: every ~175 days

Total

47

Last Release

58d ago

Major Versions

v1.1.1 → v2.0.02022-02-12

v1.x-dev → v2.0.22022-07-09

v2.6.2 → v3.0.02023-07-25

v3.3.0 → V4.0.02024-04-19

PHP version history (4 changes)v1.0.0PHP &gt;=7.4

v2.0.0PHP ^8.0

v2.4.0PHP ^8.1

v4.1.1PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/c2e18beb4ba5e22e1336e73e5c17fd4f3e193d978ffd21b33d925b4549c972d2?d=identicon)[Dr H](/maintainers/Dr%20H)

---

Top Contributors

[![DrH97](https://avatars.githubusercontent.com/u/22273907?v=4)](https://github.com/DrH97 "DrH97 (106 commits)")[![Nabcellent](https://avatars.githubusercontent.com/u/68481507?v=4)](https://github.com/Nabcellent "Nabcellent (8 commits)")

---

Tags

librarympesa

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[laravel-zero/framework

The Laravel Zero Framework.

3371.4M369](/packages/laravel-zero-framework)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)

PHPackages © 2026

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