manuelgeek/mpesab2c
Composer 安装命令:
composer require manuelgeek/mpesab2c
包简介
Safaricom's Mpesa Daraja API B2C implementation
README 文档
README
Safaricom's Mpesa Daraja API B2C implementation
Getting Started
Read on the Daraja B2C documentation to get to understand more on this implementation.
Installing
composer require manuelgeek/mpesab2c
Laravel 5 and above
This package supports the auto-discovery feature of Laravel 5.5 and above, So skip these Setup instructions if you're using Laravel 5.5 and above.
In app/config/app.php add the following :
1- The ServiceProvider to the providers array :
Manuelgeek\MpesaB2C\MpesaServiceProvider::class,
2- The class alias to the aliases array :
'B2C' => Manuelgeek\MpesaB2C\Facades\B2C::class,
3- Publish the config file
php artisan vendor:publish --provider="Manuelgeek\MpesaB2C\MpesaServiceProvider"
There will now be a new mpesa_b2c.php file in your config directory that is at the root of your project. All the configuration options are present in the file. Also remember to set the QueueTimeOutURL and ResultURL endpoints w. E.g.
"QueueTimeOutURL" => "http:example.com/mpesa/receive",
"ResultURL" => "http://example.com/mpesa/receive"
Usage
Mpesa B2C Laravel
$Amount ='300'; $CommandID = 'BusinessPayment'; $PartyB = '254708374149'; $Remarks = 'Payed well'; //Using the Facade $response = \B2C::sendMpesaMoney($Amount,$CommandID,$PartyB, $Remarks); //or the function $response = sendMpesaMoney($Amount,$CommandID,$PartyB, $Remarks);
Without Laravel
Example
<?php require_once "vendor/autoload.php"; $Amount ='300'; $CommandID = 'BusinessPayment'; $PartyB = '254708374149'; $Remarks = 'Payed well'; $mpesa = new \Manuelgeek\MpesaB2C\B2C(); $response = $mpesa->sendMpesaMoney($Amount,$CommandID,$PartyB, $Remarks); ?>
Sample Responses
Successful Request
Once sent, you shall expect a success acknowledgement response from the API informing you that your request was accepted. The response format is as below:
{
"ConversationID": "AG_20180326_00005ca7f7c21d608166",
"OriginatorConversationID": "12363-1328499-6",
"ResponseCode": "0",
"ResponseDescription": "Accept the service request successfully."
}
Note the value of ResponseCode. Any value other than 0 (zero) means the request was unsuccessful, and the error is defined in the ResponseDescription element. So you need to fix that first. A value of 0 means the request was accepted by the API.
After M-Pesa completes processing the transaction, it sends back the callback via the ResultURL you specified in the initial request. A callback from M-Pesa can either be a success callback or a failure callback. A sample of a successful transaction callback is as shown below:
{
"Result":
{
"ResultType":0,
"ResultCode":0,
"ResultDesc":"The service request has been accepted successfully.",
"OriginatorConversationID":"14593-80515-2",
"ConversationID":"AG_20170821_000049448b24712383de",
"TransactionID":"LHL41AHJ6G",
"ResultParameters":
{
"ResultParameter":
[
{
"Key":"TransactionAmount",
"Value":100
},
{
"Key":"TransactionReceipt",
"Value":"LHL41AHJ6G"
},
{
"Key":"B2CRecipientIsRegisteredCustomer",
"Value":"Y"
},
{
"Key":"B2CChargesPaidAccountAvailableFunds",
"Value":0.00
},
{
"Key":"ReceiverPartyPublicName",
"Value":"254708374149 - John Doe"
},
{
"Key":"TransactionCompletedDateTime",
"Value":"21.08.2017 12:01:59"
},
{
"Key":"B2CUtilityAccountAvailableFunds",
"Value":98834.00
},
{
"Key":"B2CWorkingAccountAvailableFunds",
"Value":100000.00
}
]
},
"ReferenceData":
{
"ReferenceItem":
{
"Key":"QueueTimeoutURL",
"Value":"https:\/\/internalsandbox.safaricom.co.ke\/mpesa\/b2cresults\/v1\/submit"
}
}
}
}
A sample method to consume the callback response would be as below, just get the data and dump it to a transactions table
public function saveResponse() { $postData = file_get_contents('php://input'); $request = json_decode($postData,true); if ($request['Result']['ResultCode'] == 0) { DB::table('mpesa_transactions') ->insert([ 'ResultType' => $request['Result']['ResultType'], 'ResultCode' => $request['Result']['ResultCode'], "ResultDesc" => $request['Result']['ResultDesc'], "OriginatorConversationID" => $request['Result']['OriginatorConversationID'], "ConversationID" => $request['Result']['ConversationID'], "TransactionID" => $request['Result']['TransactionID'], "TransactionReceipt" => $request['Result']['ResultParameters']['ResultParameter'][0]['Value'] , "TransactionAmount" => $request['Result']['ResultParameters']['ResultParameter'][1]['Value'] , "B2CWorkingAccountAvailableFunds" => $request['Result']['ResultParameters']['ResultParameter'][2]['Value'] , "B2CUtilityAccountAvailableFunds" => $request['Result']['ResultParameters']['ResultParameter'][3]['Value'] , "TransactionCompletedDateTime" => $request['Result']['ResultParameters']['ResultParameter'][4]['Value'] , "ReceiverPartyPublicName" => $request['Result']['ResultParameters']['ResultParameter'][5]['Value'] , "B2CChargesPaidAccountAvailableFunds" => $request['Result']['ResultParameters']['ResultParameter'][6]['Value'] , "B2CRecipientIsRegisteredCustomer" => $request['Result']['ResultParameters']['ResultParameter'][7]['Value'] , "QueueTimeoutURL" => $request['Result']['ReferenceData']['ReferenceItem']['Value'] , 'created_at'=>\Carbon\Carbon::now(), 'updated_at'=>\Carbon\Carbon::now(), ]); } return "success"; }
Contributing
https://github.com/manuelgeek/mpesab2c/pulls
Authors
- Magak Emmanuel - Manuelgeek
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Acknowledgments
manuelgeek/mpesab2c 适用场景与选型建议
manuelgeek/mpesab2c 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 11, 最近一次更新时间为 2019 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「transactions」 「mpesa」 「safaricom」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 manuelgeek/mpesab2c 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 manuelgeek/mpesab2c 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 manuelgeek/mpesab2c 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
A Laravel package to easily integrate Xendit payment gateway. It supports credit and debit cards, and e-wallet payments and custom invoices, queued notifications, webhook listeners and more.
Advanced Redis cache-driver with in-memory store, transactions and preloading
Effortlessly wrap your controller actions with database transactions.
Bundle of annotations to make it easier and faster to develop Rest APIs with Symfony.
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
统计信息
- 总下载量: 28
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-29