drh/mpesa
Composer 安装命令:
composer require drh/mpesa
包简介
Mpesa Payments Library
README 文档
README
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: https://mobile-money.agweria.com
Requirements
- PHP 8.2+
- Laravel 8.x - 13.x
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
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
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)
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)
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)
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
use DrH\Mpesa\Facades\Identity; $result = Identity::validate('254712345678');
Events
The package dispatches events for payment lifecycle hooks. Listen for these in your EventServiceProvider or via Event::listen():
| Event | Description |
|---|---|
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 occurred |
All events are in the DrH\Mpesa\Events namespace.
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
The package automatically registers callback routes under /payments/callbacks/:
POST /payments/callbacks/stk-callback- STK push callbackPOST /payments/callbacks/c2b-validation- C2B validationPOST /payments/callbacks/c2b-confirmation- C2B confirmationPOST /payments/callbacks/result/- B2C/B2B resultPOST /payments/callbacks/timeout/- B2C/B2B timeout
A pesa.cors middleware alias is available for CORS handling on callback routes.
Testing
composer run-script test
License
MIT
drh/mpesa 适用场景与选型建议
drh/mpesa 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.27k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「mpesa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 drh/mpesa 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drh/mpesa 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 drh/mpesa 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
CakePHP Bitly Plugin
统计信息
- 总下载量: 3.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-06