PHPackages                             nyanumba-codes/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. [API Development](/categories/api)
4. /
5. nyanumba-codes/mpesa

ActiveLibrary[API Development](/categories/api)

nyanumba-codes/mpesa
====================

MPESA Integration into Laravel and other php frameworks

1.2.0(7mo ago)0289↓100%MITPHPPHP &gt;=8.0

Since Jan 20Pushed 7mo agoCompare

[ Source](https://github.com/NyanumbaCodes/M-Pesa)[ Packagist](https://packagist.org/packages/nyanumba-codes/mpesa)[ RSS](/packages/nyanumba-codes-mpesa/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

M-Pesa Integration for Laravel
==============================

[](#m-pesa-integration-for-laravel)

A robust PHP library for integrating Safaricom's M-Pesa API with Laravel applications. This package simplifies payments, QR code generation, and transaction management.

Features
--------

[](#features)

- Dynamic QR Code Generation
- STK Push (Customer Payment)
- C2B Simulations
- B2C Transactions
- Transaction Status Checks
- Account Balance Inquiry
- Reversals
- Secure Authentication

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

[](#installation)

Install via Composer:

```
composer require nyanumba-codes/mpesa
```

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

[](#configuration)

First Run the Installation:

```
php artisan mpesa:install {environment}
```

The Environment value can either be left blank, or written `sandbox` for The Sandbox Certificate file to be downloaded into the public folder or `production` for the Production Certificate to be downloaded.

```
# For Sandbox

php artisan mpesa:install

# or

php artisan mpesa:install sandbox
```

```
# For Production

php artisan mpesa:install production
```

Ensure the Safaricom public certificate (cert.cer) is stored securely under `public/mpesa`.

After this you may proceed to update your .env file. This one is rather longer this time round because all the MPESA APIs have been covered by this application. I have even separated all the Callbacks, Timeouts and Result URL to allow for development maleability.

```
# Environment (sandbox or production)
MPESA_ENV=sandbox

# Consumer credentials
MPESA_CONSUMER_KEY=your_consumer_key_here
MPESA_CONSUMER_SECRET=your_consumer_secret_here

# Security credential
MPESA_USERNAME=your_username_here
MPESA_SECURITY_CREDENTIAL=your_security_credential_here

# Shortcode and passkey
MPESA_SHORTCODE=your_shortcode_here
MPESA_PASSKEY=your_passkey_here

# C2B (Customer to Business) URLs
MPESA_C2B_CONFIRMATION=https://yourdomain.com/api/c2b/confirmation
MPESA_C2B_VALIDATION=https://yourdomain.com/api/c2b/validation

# Transaction status callback URLs
MPESA_TRANSACTION_RESULT=https://yourdomain.com/api/transaction/result
MPESA_TRANSACTION_TIMEOUT=https://yourdomain.com/api/transaction/timeout

# B2C (Business to Customer) URLs
MPESA_B2C_RESULT=https://yourdomain.com/api/b2c/result
MPESA_B2C_TIMEOUT=https://yourdomain.com/api/b2c/timeout

# B2C Top-Up URLs
MPESA_B2C_TOPUP_RESULT=https://yourdomain.com/api/b2c/topup/result
MPESA_B2C_TOPUP_TIMEOUT=https://yourdomain.com/api/b2c/topup/timeout

# Account balance inquiry URLs
MPESA_BALANCE_RESULT=https://yourdomain.com/api/balance/result
MPESA_BALANCE_TIMEOUT=https://yourdomain.com/api/balance/timeout

# Reversal request URLs
MPESA_REVERSAL_RESULT=https://yourdomain.com/api/reversal/result
MPESA_REVERSAL_TIMEOUT=https://yourdomain.com/api/reversal/timeout

# Tax inquiry URLs
MPESA_TAX_RESULT=https://yourdomain.com/api/tax/result
MPESA_TAX_TIMEOUT=https://yourdomain.com/api/tax/timeout

# Ratiba callback URL (if applicable)
MPESA_RATIBA_CALLBACK=https://yourdomain.com/api/ratiba/callback
```

Usage
-----

[](#usage)

Initialize the class where you wish to use it:

```
use NyanumbaCodes\Mpesa\Mpesa;

$mpesa = new Mpesa();
```

### 1. Generate Dynamic QR Code

[](#1-generate-dynamic-qr-code)

```
$response = $mpesa->dynamicQr('MerchantName', 'Ref123');
```

### 2. Process STK Push (MPESA Express Simulate)

[](#2-process-stk-push-mpesa-express-simulate)

```
$response = $mpesa->stkPush(100, '254700000000', 'AccountRef', 'TransactionDesc');
```

### 3. Process STK Push (MPESA Express Simulate)

[](#3-process-stk-push-mpesa-express-simulate)

```
$response = $mpesa->transactionStatus('TransactionID', 'originatorConversationID');
```

### 4. Perform C2B Simulation

[](#4-perform-c2b-simulation)

```
$response = $mpesa->c2bSimulate(100, '254700000000', 'Ref123');
```

### 5. Handle Reversals

[](#5-handle-reversals)

```
$response = $mpesa->reversal('TransactionID', 100);
```

License
-------

[](#license)

This package is open-source and licensed under the MIT License.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance66

Regular maintenance activity

Popularity13

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

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 ~237 days

Total

2

Last Release

236d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cf3c126127c5cfe30641a8258a7b74ca30187888781ccce742c7f8b351d695d5?d=identicon)[SteveNyanumba](/maintainers/SteveNyanumba)

### Embed Badge

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

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

###  Alternatives

[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[saloonphp/laravel-plugin

The official Laravel plugin for Saloon

765.7M124](/packages/saloonphp-laravel-plugin)[mll-lab/laravel-graphiql

Easily integrate GraphiQL into your Laravel project

683.2M9](/packages/mll-lab-laravel-graphiql)[neuron-core/neuron-laravel

Official Neuron AI Laravel SDK.

10710.0k](/packages/neuron-core-neuron-laravel)[grantholle/powerschool-api

A Laravel package to make interacting with PowerSchool less painful.

1715.6k1](/packages/grantholle-powerschool-api)[litalico-engineering/eg-r2

Easy request validation and route generation from open API specifications (for Laravel)

1112.5k](/packages/litalico-engineering-eg-r2)

PHPackages © 2026

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