savannabits / daraja
Safaricom Mpesa API (Safaricom Daraja) for PHP Laravel。与 Safaricom 的 MPESA API 简单集成,称为 { DARAJA },并允许您在良好的环境中发出请求
Requires
- php: ^8.0
- ext-curl: *
- illuminate/support: >=8.0
Requires (Dev)
- orchestra/testbench: >=6
- phpunit/phpunit: >=9
This package is auto-updated.
Last update: 2024-08-28 09:19:46 UTC
README
savannabits/daraja 包是一个 Laravel 包,它简化了与 Safaricom MPESA 集成 RESTFul API(称为 Safaricom Daraja)的通信。
动机
为什么选择 Daraja,而不是其他包呢?我已经查看了一些可以完成相同任务的包,但我所寻找的是一个可以在代码中动态生成令牌和回调注册的包。这意味着我可以在数据库中存储我的凭证(消费者密钥和消费者密钥),并动态地检索它们,使用它们来注册我的回调。使用此解决方案,您甚至可以拥有多个简码网关(许多应用程序使用不同的 paybill 可以使用此网关与 Safaricom 通信,并根据需要注册动态确认 URL)。
安装
您可以通过 composer 安装此包
composer require savannabits/daraja
用法
用法非常简单。如果您使用 C2B api,第一步是注册 URL。大部分工作已经在幕后为您完成。
回调 URL 注册
use Savannabits\Daraja\Daraja; $shortcode = "600737";//Your Paybill or till number here $confirmationURL = 'your dynamic validation url here'; $validationURL = 'your dynamic validation url here'; // Optional. Leave null if you don't want validation $environment = "sandbox"; // or "live" $responseType = "Cancelled"; //Default Response type in case the validation URL is unreachable or is undefined. Either Cancelled or Completed as per the safaricom documentation $response = Daraja::getInstance() ->setCredentials("CONSUMER KEY","CONSUMER SECRET",$environment) ->registerCallbacks($shortcode, $confirmationURL, $validationURL,$responseType);
就是这样。您甚至可以将此代码放入 Console Command 类中,以便从终端运行类似于 php artisan mpesa:register-callbacks
的命令。
C2B 模拟
如果您在 Dev 上并想模拟 C2B 交易(不使用任何实际资金),您可以使用沙箱凭证,并假设您已注册了上面的回调,运行以下命令:
use Savannabits\Daraja\Daraja; /** * Here is the c2b Function documentation to help you understand the params: * Use this function to initiate a C2B transaction * @param $ShortCode | 6 digit M-Pesa Till Number or PayBill Number * @param $CommandID | Unique command for each transaction type. either "CustomerPayBillOnline" or "CustomerBuyGoodsOnline" * @param $Amount | The amount been transacted. * @param $Msisdn | MSISDN (phone number) sending the transaction, start with country code without the plus(+) sign. * @param $BillRefNumber | Bill Reference Number (Optional). * @return mixed|string */ $shortcode = "YOUR SHORTCODE"; $commandID = "CustomerPayBillOnline"; $amount = 100; $msisdn = "07xxxxxxxx"; // See safaricom daraja documentation and check your credentials for the specific number given for testing. $billRefNumber = "THE PAYBILL ACCOUNT NO."; // e.g "MAMA MBOGA 212" $response = Daraja::getInstance() ->setCredentials("YOUR CONSUMER KEY","YOUR CONSUMER SECRET","sandbox") ->c2b($shortcode, $commandID, $amount, $msisdn, $billRefNumber);
STK 模拟(Lipa na MPESA 在线)
这里是方法文档
/** * Use this function to initiate an STKPush Simulation * @param $BusinessShortCode | The organization shortcode used to receive the transaction. * @param $LipaNaMpesaPasskey | The password for encrypting the request. This is generated by base64 encoding BusinessShortcode, Passkey and Timestamp. * @param $TransactionType | The transaction type to be used for this request. Only CustomerPayBillOnline is supported. * @param $Amount | The amount to be transacted. * @param $PartyA | The MSISDN sending the funds. * @param $PartyB | The organization shortcode receiving the funds * @param $PhoneNumber | The MSISDN sending the funds. * @param $CallBackURL | The url to where responses from M-Pesa will be sent to. * @param $AccountReference | Used with M-Pesa PayBills. * @param $TransactionDesc | A description of the transaction. * @param $Remark | Remarks * @return mixed|string */ \Savannabits\Daraja\Daraja::getInstance() ->setCredentials("CONSUMER KEY","CONSUMER SECRET","sandbox") ->STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remark);
// Example $json = \Savannabits\Daraja\Daraja::getInstance() ->setCredentials("CONSUMER KEY","CONSUMER SECRET","sandbox") ->STKPushSimulation($app->short_code,$app->lnm_passkey,$app->transaction_type, $amount, $phone_number, $short_code, $phone_number, $confirm_callback,$account_ref, $transaction_desc, $comments);
验证和确认回调示例,完成交易
use Savannabits\Daraja\Daraja; class MyController extends \App\Http\Controllers\Controller { public function c2bValidationCallback(Request $request) { // Perform your validations here and set the status $status = true; // Or false based on whether you want to accept or reject the transaction. Daraja::getInstance()->finishTransaction($status); } public function c2bConfirmationCallback(Request $request) { //Get Response data $response = Daraja::getInstance()->getDataFromCallback(); // $response = $request->all(); //Alternatively... // Do what you want with the data // ... // Finish Transaction Daraja::getInstance()->finishTransaction(true); } public function stkConfirmationCallback(Request $request) { $mpesa = new Daraja(); // Get data from safaricom response. $data = $mpesa->getDataFromCallback(); \Log::info($data); // Handle the data //... //Finish Transaction by sending acknowledgment to safaricom $mpesa->finishTransaction(true); } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现任何安全相关的问题,请通过电子邮件 maosa.sam@gmail.com 联系我们,而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。
Laravel 包模板
此包使用Laravel 包模板生成。