calvinchiulele / mpesa-mz
非官方的 Laravel 包,用于集成 Vodacom Mozambique M-Pesa
Requires
- php: ^7.3
- abdulmueid/mpesa: ^1.0
- illuminate/support: 5.8.* || 6.* || 7.* || 8.*
Requires (Dev)
- phpunit/phpunit: ^9.4
README
此包是 abdulmueid/mpesa-php-api 的包装器,以便更容易在 Laravel 应用程序中集成 M-Pesa API
有关 M-Pesa 的更多信息,请参阅 M-Pesa 官方网站:https://www.vm.co.mz/en/M-Pesa2
有关 abdulmueid/mpesa-php-api 的更多信息,请参阅 https://github.com/abdulmueid/mpesa-php-api
1. 安装
您只需执行以下操作
composer require calvinchiulele/mpesa-mz
php artisan vendor:publish
在终端中执行上述命令后,您将看到一个包含您的应用程序中注册的所有服务提供者的列表。选择 CalvinChiulele\MPesaMz\Providers\MPesaMzServiceProvider
并按回车键。现在,如果检查您的配置文件夹,您会找到您的 mpesa-config.php 文件。
2. 配置
在发布必需的配置文件后,您需要在 .env 文件中添加所有必要的密钥,以便配置文件能够正常工作。
.env 应该如下所示
MPESA_PUBLIC_KEY=xxx MPESA_API_HOST="api.sandbox.vm.co.mz" MPESA_API_KEY=xxx MPESA_ORIGIN=xxx MPESA_SERVICE_PROVIDER_CODE=xxx MPESA_INITIATOR_IDENTIFIER=xxx MPESA_SECURITY_CREDENTIAL=xxx
其中 xxx 是您的数据。 注意:您必须使用 MPESA_API_HOST 来反映 "api.sandbox.vm.co.mz" 或 M-Pesa API 的生产 URL
新创建的 config/mpesa-config.php
文件将如下所示
注意:在 .env 文件中添加条目后,您必须在终端中运行 php artisan config:clear
,以便删除配置缓存文件,并将 .env
中添加的值用于 config/mpesa-config.php
<?php return [ /* |-------------------------------------------------------------------------- | Public key for use in M-Pesa API |-------------------------------------------------------------------------- | | Here you may specify the public key provided by Vodacom to you | */ 'public_key' => env('MPESA_PUBLIC_KEY', ''), /* |-------------------------------------------------------------------------- | API host of M-Pesa API |-------------------------------------------------------------------------- | | Here you may specify the API host provided by Vodacom for API operations | */ 'api_host' => env('MPESA_API_HOST', 'api.sandbox.vm.co.mz'), /* |-------------------------------------------------------------------------- | API Key of M-Pesa API |-------------------------------------------------------------------------- | | Here you may specify the API key provided by Vodacom to you | */ 'api_key' => env('MPESA_API_KEY', ''), /* |-------------------------------------------------------------------------- | Origin of M-Pesa API |-------------------------------------------------------------------------- | | Here you may specify the API key provided by Vodacom to you | */ 'origin' => env('MPESA_ORIGIN', ''), /* |-------------------------------------------------------------------------- | Service Provider Code of M-Pesa API |-------------------------------------------------------------------------- | | Here you may specify the service provider code of M-Pesa provided by Vodacom to you | */ 'service_provider_code' => env('MPESA_SERVICE_PROVIDER_CODE', ''), /* |-------------------------------------------------------------------------- | Initiator Identifier of M-Pesa API |-------------------------------------------------------------------------- | | Here you may the initiator identifier provided by Vodacom to you | */ 'initiator_identifier' => env('MPESA_INITIATOR_IDENTIFIER', ''), /* |-------------------------------------------------------------------------- | Security credential of M-Pesa API |-------------------------------------------------------------------------- | | Here you may specify the security credential provided by Vodacom to you | */ 'security_credential' => env('MPESA_SECURITY_CREDENTIAL', '') ];
3. 使用
为了在 Laravel 应用程序中使用此包,您可以使用外观或从 Laravel 服务容器中获取 M-Pesa 服务的实例。
3.1 - 使用 M-Pesa 外观
<?php namespace App\Http\Controllers; use CalvinChiulele\MPesaMz\Facades\MPesaMz; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class ExampleController extends Controller { // more code /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // This call is a C2B transaction, which is used as a standard customer-to-business transaction. Funds from the customer’s mobile money wallet will be deducted and be transferred to the mobile money wallet of the business. To authenticate and authorize this transaction, M-Pesa Payments Gateway will initiate a USSD Push message to the customer's phone to gather and verify the mobile money PIN number. This number is not stored and is used only to authorize the transaction. // $request->msisdn corresponds to the customer's phone number (e.g: 258840000000) // 100 corresponds to the amount of the transaction. Please, do best pratices and not use magic numbers. // $reference corresponds to the reference of the transaction for the customer or business making the transaction. This can be a smartcard number for a TV subscription or a reference number of a utility bill. // $thirdPartyReference corresponds to the unique reference of the third party system. When there are queries about transactions, this will usually be used to track a transaction. $payment = MPesaMz::payment($request->msisdn, 100, $reference, $thirdPartyReference); // This call is a refund or reversal, which is used to reverse a successful transaction. // Using the Transaction ID of a previously successful transaction, M-Pesa Payments Gateway will withdraw the funds from the recipient party’s mobile money wallet and revert the funds to the mobile money wallet of the initiating party of the original transaction. // $payment->getTransactionID() corresponds to the transaction ID of the just made sucessfully transaction. It's used to query transactions on the mobile money platform. // 100 corresponds to the amount to be reversed. $refund = MPesaMz::refund($payment->getTransactionID(), 100); // This call is to query a transaction status, which determines the current status of a particular transaction. Using either the Transaction ID or the Conversation ID of a transaction just made, the M-Pesa Payments Gateway will return information about the transaction’s status. $query = MPesaMz::query($refund->getTransactionID()); // more code } }
3.2 - 从 Laravel 服务容器中使用 M-Pesa 服务
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class ExampleController extends Controller { /** * M-Pesa service class * * @var \CalvinChiulele\MPesaMz\Services\MPesaMz */ protected $mpesaService; /** * Create a new instance * * @return void */ public function __construct() { $this->mpesaService = app('CalvinChiulele\MPesaMz\Services\MPesaMz'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // The documentation on the example above serves for this example. $payment = $this->mpesaService->payment($request->msisdn, 100, $reference, $thirdPartyReference); $refund = $this->mpesaService->refund($payment->getTransactionID(), 100); $query = $this->mpesaService->query($refund->getTransactionID()); // more code } }
4. 测试
- 更新 tests/config/mpesa-config-test.php 中的所需参数
- 在 tests/Services/MPesaMzTest.php 中分别输入测试金额和 MSISDN,分别在 47 和 48 行
- 在 vendor/bin/ 中运行 PHPUnit 9 二进制文件
- 检查手机以获取 M-Pesa 支付请求
当前测试用例创建了一个新的交易,退回了交易并查询了交易状态。在生产环境中运行时,测试可能会产生费用。
5. 许可证
此库在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE 文件。
6. 作者
Calvin Carlos da Conceição Chiulele cchiulele@protonmail.com 和贡献者
此包仍处于开发阶段,因此欢迎提出建议、改进和推荐。