joemuigai / laravel-mpesa
A Laravel Mpesa integration package
Fund package maintenance!
Joemuigai
Installs: 229
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/joemuigai/laravel-mpesa
Requires
- php: ^8.4
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
_ _ __ __ ____
| | | | | \/ | | _ \
| | __ _ _ __ __ ___ _____| | | \ / |_____| |_) | ___ ___ __ _
| | / _` | '__/ _` \ \ / / _ \ | | |\/| |_____| __/ / _ \/ __|/ _` |
| |___| (_| | | | (_| |\ V / __/ | | | | | | | | __/\__ \ (_| |
|______\__,_|_| \__,_| \_/ \___|_| |_| |_| |_| \___||___/\__,_|
by Joemuigai
A comprehensive, production-ready Laravel package for integrating with Safaricom's M-Pesa Daraja API. Built for both single-merchant applications and multi-tenant SaaS platforms.
Table of Contents
- Features
- Requirements
- Installation
- Configuration
- Usage
- Multi-Tenant Usage
- Production Checklist
- Testing
- Contributing
- License
Features
โจ Complete API Coverage - 11 M-Pesa APIs supported
๐ข Multi-Tenant Ready - Database-driven account management
๐ช Production Optimized - HTTP retries, caching, and failsafe mechanisms
๐ฏ Flexible - Paybill & Till Number (Buy Goods) support
๐ง Developer Friendly - Interactive installation, IDE autocomplete
๐งช Well Tested - Comprehensive test suite
๐ Type Safe - Full PHPDoc annotations
๐ View Full Features Guide for detailed capabilities.
Requirements
- PHP 8.4+
- Laravel 11.0+ or 12.0+
Installation
Install via Composer:
composer require joemuigai/laravel-mpesa
Run the interactive installation command to set up configuration and migrations:
php artisan laravel-mpesa:install
The wizard will guide you through:
- Scenario Selection (Single vs Multi-tenant)
- API Selection (STK, C2B, B2C, etc.)
- Environment Setup
Configuration
Environment Variables
After installation, add your credentials to .env:
# Core Credentials MPESA_CONSUMER_KEY=your_consumer_key MPESA_CONSUMER_SECRET=your_consumer_secret MPESA_ENVIRONMENT=sandbox # or production # STK Push MPESA_STK_SHORTCODE=174379 MPESA_STK_PASSKEY=your_passkey MPESA_STK_CALLBACK_URL=https://yourdomain.com/mpesa/callback
See config/mpesa.php for all available options.
Usage
For detailed code examples, payloads, and callback handling, please refer to the USAGE.md file.
Quick Example: STK Push
use Joemuigai\LaravelMpesa\Facades\LaravelMpesa; try { $response = LaravelMpesa::stkPush( amount: 100, phoneNumber: '254712345678', reference: 'INV-001', description: 'Payment' ); } catch (\Exception $e) { // Handle error }
Supported APIs
- STK Push (Lipa Na M-Pesa Online)
- STK Push Query
- C2B (Customer to Business)
- B2C (Business to Customer)
- B2B (Business to Business)
- Transaction Status
- Account Balance
- Reversal
- Dynamic QR Code
- Pull Transaction
Multi-Tenant Usage
For SaaS platforms, you can switch accounts dynamically:
// Switch by Tenant ID LaravelMpesa::forAccount('tenant-123')->stkPush(100, '254712345678'); // Switch by Model $account = MpesaAccount::find(1); LaravelMpesa::withAccount($account)->stkPush(100, '254712345678');
See the Multi-Tenant Guide for database setup and more examples.
Production Checklist
Before going live, ensure you have:
- Set
MPESA_ENVIRONMENT=productionin.env - Updated
MPESA_CONSUMER_KEYandMPESA_CONSUMER_SECRETwith production credentials - Enabled SSL verification (
MPESA_HTTP_VERIFY=true) - Configured HTTPS callback URLs (M-Pesa requires valid SSL)
- Set up logging to monitor
laravel-mpesachannel or default logs - Implemented
try-catchblocks around all API calls - Configured queue workers if processing callbacks asynchronously
Testing
Run the package test suite:
composer test
Contributing
Contributions are welcome! Please see CONTRIBUTING for details.
Security
If you discover a security vulnerability, please email joemuigai004@gmail.com.
Credits
License
The MIT License (MIT). Please see License File for more information.
Support
- Issues: GitHub Issues
- Email: joemuigai004@gmail.com
Made with โค๏ธ in Kenya ๐ฐ๐ช