rizwannasir / mtn-momo-api-php
MTN Momo API 的 PHP 包装器 - https://momodeveloper.mtn.com
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- orchestra/testbench: ^3.5.0
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-26 11:51:33 UTC
README
虽然这个包是 patricpoba/mtn-momo-api-php 的分支,但所有权利归原始作者所有。这个仓库只是为了与 PHP 8.2 的最新版本一起工作。
MTN MoMo API
此包帮助您将 MTN MOMO API 集成到您的 Php 或 Laravel 应用程序中。它围绕 MTN Open API 提供了一个更简单的 API 来使用。
安装
您需要安装 PHP 7.0 或更高版本。您可以通过 composer 安装此包
composer require rizwannasir/mtn-momo-api-php
用法
在生产环境中,在满足KYC要求后,所需的凭据将在 MTN OVA 管理仪表板上为您提供。但在测试环境中,您需要使用 API 创建一个沙盒用户,这个包可以帮助您做到这一点。
创建沙盒环境 API 用户
我们需要获取 用户 ID
和 用户密钥
,为此我们需要使用我们订阅的产品的主密钥,以及指定一个主机。该库附带一个命令行应用程序,可以帮助创建沙盒凭据。它假设您已在 https://momodeveloper.mtn.com
上创建了一个账户,并且您的 Ocp-Apim-Subscription-Key
(主密钥) 位于 https://momodeveloper.mtn.com/developer
。
## On the command line, at the root of your project, run $ php ./vendor/rizwannasir/mtn-momo-api-php/src/SandboxUserProvision.php -k '9cc70894a5d24dba8a8a50fcecbc0568' -c 'https://yourdomain.com'
选项 c
是您的回调主机,选项 k
是您订阅的特定产品的主密钥或 Ocp-Apim-Subscription-Key
。API 密钥
对于产品是唯一的,您将需要为使用的每个产品获取一个 API 密钥
。您应该得到以下类似响应
Your Sandbox credentials : Ocp-Apim-Subscription-Key: f1d075127844476fa3c4636593e60cf8 UserId (X-Reference-Id) : 89ce5960-3f68-4da4-bd69-0584073870b8 ApiKey (ApiSecret) : 46b9302a8ae444c8a7a956bb4c7f2c05 Callback host : https://yourdomain.com
配置
我们需要通过创建 MtnConfig
的实例并将其传递给我们要使用的产品的类(收集、支出或汇款)的构造函数来设置包,如以下示例所示。可以通过调用 ->setConfig($config)
方法并传递新的配置实例来覆盖产品实例的配置。
use RizwanNasir\MtnMomo\MtnConfig; $config = new MtnConfig([ // mandatory credentials 'baseUrl' => 'https://sandbox.momodeveloper.mtn.com', 'currency' => 'EUR', 'targetEnvironment' => 'sandbox', // product specific blocks "collectionApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', "collectionPrimaryKey" => 'aadb6f286e95415db9024c7a4e2c6025', "collectionUserId" => 'b4d4019f-8617-4843-a4b8-ed90941747a3', "disbursementApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', "disbursementPrimaryKey"=> 'aadb6f286e95415db9024c7a4e2c6025', "disbursementUserId" => 'b4d4019f-8617-4843-a4b8-ed90941747a3', "remittanceApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', "remittancePrimaryKey" => 'aadb6f286e95415db9024c7a4e2c6025', "remittanceUserId" => 'b4d4019f-8617-4843-a4b8-ed90941747a3' ]);
收集
收集用于从客户(付款人)请求付款并检查交易状态。 更多关于 Momo Collection
collectionPrimaryKey
:在开发者门户上Collection
产品的主密钥。collectionUserId
:对于开发环境,使用沙盒凭据,否则使用开发者门户上的凭据。collectionApiSecret
:对于开发环境,使用沙盒凭据,否则使用开发者门户上的凭据。
use RizwanNasir\MtnMomo\MtnConfig; $config = new MtnConfig([ // mandatory credentials 'baseUrl' => 'https://sandbox.momodeveloper.mtn.com', 'currency' => 'EUR', 'targetEnvironment' => 'sandbox', // collection credentials "collectionApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', "collectionPrimaryKey" => 'aadb6f286e95415db9024c7a4e2c6025', "collectionUserId" => 'b4d4019f-8617-4843-a4b8-ed90941747a3' ]); $collection = new MtnCollection($config); $params = [ "mobileNumber" => '233540000000', "amount" => '100', "externalId" => '774747234', "payerMessage" => 'some note', "payeeNote" => '1212' ]; $transactionId = $collection->requestToPay($params); $transaction = $collection->getTransaction($transactionId);
收集方法
requestToPay
:此操作用于请求消费者(付款人)的付款。付款人将被要求授权付款。一旦付款人授权付款,交易将执行。交易将在付款人授权或拒绝或系统超时之前保持 PENDING 状态。可以通过检查getTransaction()
方法的返回结果中的status
字段来验证交易的状态。
$transactionId = $collection->requestToPay($params);
getTransaction
:使用requestToPay
返回的transactionId
获取交易信息。您可以定期调用它,直到交易失败或成功。
$transaction = $collection->getTransaction($transactionId);
getBalance
:获取账户余额。
$transaction = $collection->getBalance();
accountHolderActive
:检查账户持有人是否已在系统中注册并处于活跃状态。
$transaction = $collection->accountHolderActive($mobileNumber);
支出
支出用于将资金从服务提供商账户转移到客户账户。了解更多关于Momo支出
disbursementPrimaryKey
:开发平台上的Disbursement
产品的主键。disbursementUserId
:在开发环境中,使用沙箱凭据,否则使用开发平台上的凭据。disbursementApiSecret
:在开发环境中,使用沙箱凭据,否则使用开发平台上的凭据。
use RizwanNasir\MtnMomo\MtnConfig; use RizwanNasir\MtnMomo\MtnDisbursement; $config = new MtnConfig([ // mandatory credentials 'baseUrl' => 'https://sandbox.momodeveloper.mtn.com', 'currency' => 'EUR', 'targetEnvironment' => 'sandbox', // disbursement credentials "disbursementApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', "disbursementPrimaryKey" => 'aadb6f286e95415db9024c7a4e2c6025', "disbursementUserId" => 'b4d4019f-8617-4843-a4b8-ed90941747a3' ]); /** * setup disbursement config */ $disbursement = new MtnDisbursement($config); $params = [ "mobileNumber" => '233540000000', "amount" => '100', "externalId" => '774747234', "payerMessage" => 'some note', "payeeNote" => '1212' ]; /** * Transfer() is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment. The transaction is executed once the payer has authorized the payment. The transaction will be in status PENDING until it is authorized or declined by the payer or it is timed out by the system. */ $transactionId = $disbursement->transfer($params); /** * Status of the transaction can be validated by checking the `status` * field on the result of `getTransaction()` method. */ $transaction = $disbursement->getTransaction($transactionId);
支出方法
transfer
:此操作用于从消费者(付款方)请求付款。付款方将被要求授权付款。一旦付款方授权付款,则执行交易。交易将在付款方授权或拒绝或系统超时前处于待处理状态。可以通过检查getTransaction()
方法的结果中的status
字段来验证交易状态。
$transactionId = $disbursement->transfer($params);
getTransaction
:使用requestToPay
返回的transactionId
获取交易信息。您可以定期调用它,直到交易失败或成功。
$transaction = $disbursement->getTransaction($transactionId);
getBalance
:获取您的支出账户余额。
$transaction = $disbursement->getBalance();
accountHolderActive
:检查账户持有人是否已在系统中注册并处于活跃状态。
$transaction = $disbursement->accountHolderActive($mobileNumber);
汇款
转账操作用于从自己的账户向收款人账户转账金额。
disbursementPrimaryKey
:开发平台上的Disbursement
产品的主键。disbursementUserId
:在开发环境中,使用沙箱凭据,否则使用开发平台上的凭据。disbursementApiSecret
:在开发环境中,使用沙箱凭据,否则使用开发平台上的凭据。
use RizwanNasir\MtnMomo\MtnConfig; use RizwanNasir\MtnMomo\MtnRemittance; $config = new MtnConfig([ // mandatory credentials 'baseUrl' => 'https://sandbox.momodeveloper.mtn.com', 'currency' => 'EUR', 'targetEnvironment' => 'sandbox', // disbursement credentials "remittanceApiSecret" => '3463953c31064e6e8ae634cd94f13c8c', "remittancePrimaryKey" => 'aadb6f286e95415db9024c7a4e2c6025', "remittanceUserId" => 'b4d4019f-8617-4843-a4b8-ed90941747a3' ]); /** * setup remittance config */ $remittance = new MtnRemittance($config); $params = [ "mobileNumber" => '233540000000', "amount" => '100', "externalId" => '774747234', "payerMessage" => 'some note', "payeeNote" => '1212' ]; /** * Transfer operation is used to transfer an amount from the own account to a payee account. * Status of the transaction can validated by using the GET /transfer/{referenceId} */ $transactionId = $remittance->transfer($params); /** * This operation is used to get the status of a transfer. X-Reference-Id * that was passed in the post is used as reference to the request. */ $transaction = $remittance->getTransaction($transactionId); /** * Get the balance of your disbursement account. */ $transaction = $disbursement->getBalance(); /** * Operation is used to check if an account holder is registered and active in the system. */ $transaction = $disbursement->accountHolderActive($mobileNumber);
API 响应
所有API调用都返回RizwanNasir\MtnMomo\Http\ApiResponse对象,如下所述。
/** * Data in api response can also be accessed directly from the object. */ $response->description // 'description' is in api response. /** * Get array format of api response * @return array */ $response->toArray() /** * Get json format of api response * @return string */ $response->toJson() /** * Get the status code of the response * @return numeric */ $response->getStatusCode() /** * Get the headers the response */ $response->getHeaders() /** * Checks if api call was successful ie 200, 201 etc * return bool */ $response->isSuccess()
测试
./vendor/bin/phpunit
变更日志
请参阅CHANGELOG了解最近更改的详细信息。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件poba.dev@outlook.com联系,而不是使用问题跟踪器。
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。