tumainimosha/laravel-mpesa-push
Composer 安装命令:
composer require tumainimosha/laravel-mpesa-push
包简介
Laravel package for TZ Mpesa USSD Push integration
README 文档
README
Install
Install via composer
composer require tumainimosha/laravel-mpesa-push
Publish Configuration File
Publish config file to customize the default package config.
php artisan vendor:publish --provider="Tumainimosha\MpesaPush\MpesaPushServiceProvider" --tag="config"
Publish Migrations
php artisan vendor:publish --provider="Tumainimosha\MpesaPush\MpesaPushServiceProvider" --tag="migrations" php artisan migrate
Configuration
Authentication
Configure your api username and password in .env file as follows
TZ_MPESA_PUSH_SSL_CERT_PASSWORD=secret TZ_MPESA_PUSH_USERNAME=123123 TZ_MPESA_PUSH_PASSWORD=VeryStrongPasswd TZ_MPESA_PUSH_BUSINESS_NAME=FooCompany TZ_MPESA_PUSH_BUSINESS_NUMBER=123123
Other configuration can be found in the config file published by this package. The options are well commented 😊
Usage
use Tumainimosha\MpesaPush\MpesaPush; ... public function testPush() { // Resolve service object $push = MpesaPush::instance(); $customerMsisdn = '<substitute valid mpesa-tz number>'; $amount = 250; $txnId = str_random('20'); $responseCode = $push->postRequest($customerMsisdn, $amount, $txnId); // Check for response code // Valid response codes // '0' - Success (note: response code is string '0' not numeric 0) // 'Duplicate' - Duplicate transaction ID // Others - fail }
Handling callback
Out of the box, this package stores transactions in table mpesa_push_transactions, and updates their status on receiving callback.
However, you may need to do further actions on your app after receiving callback, by listening to event MpesaCallbackReceived::class fired at callback.
You need to implement your own event listener to listen for this event and do any additional steps after receiving callback.
The event has public attribute $transaction which contains the transaction parameters including status
// EventServiceProvider.php protected $listen = [ ... \Tumainimosha\MpesaPush\Events\MpesaCallbackReceived::class => [ \App\Listeners\MpesaCallbackReceivedHandler::class, ], ]; // MpesaCallbackReceivedHandler.php public function handle(MpesaCallbackReceived $event) { $transaction = $event->transaction; // do your custom logic here }
Customize config values at runtime
The service offers fluent setters to change config values at runtime if your use case requires.
Such a use case could be when you have multiple accounts on the same project, and you fetch your config values from DB.
// $account here could be a Model fetched from db with account attributes $pushService = MpesaPush::instance(); $pushService->setUsername($account->username) ->setPassword($account->password) ->setBusinessName($account->business_name) ->setBusinessNumber($account->business_number) ->setCommand($account->command); $pushService->postRequest($customerMsisdn, $amount, $txnId);
Testing
Run the tests with:
vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details.
TODO
- Login
- Push Transaction request
- Callback processing
- More documentation and examples
- Unit testing
** Help needed with improving documentation and unit testing. Pull Requests are welcome.
Security
If you discover any security-related issues, please email princeton.mosha@gmail.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
tumainimosha/laravel-mpesa-push 适用场景与选型建议
tumainimosha/laravel-mpesa-push 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 709 次下载、GitHub Stars 达 10, 最近一次更新时间为 2019 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tumainimosha/laravel-mpesa-push 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tumainimosha/laravel-mpesa-push 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 709
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-12