patricpoba / mtn-momo-api-php
MTN Momo API 的 PHP 封装 - https://momodeveloper.mtn.com
Requires
- php: ^7.0|^8.0
- guzzlehttp/guzzle: ^6.5|^7.0
Requires (Dev)
- orchestra/testbench: ^3.5.0|^4.0|^5.0|^6.0|^7.0|^8.0
- phpunit/phpunit: ^6.0|^7.0|^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-09-06 20:55:11 UTC
README
此包帮助您将 MTN MOMO API 集成到您的 Php 或 Laravel 应用程序中。它围绕 MTN Open API 提供了一个更简单的 API 以进行操作。
安装
您需要 PHP 7.0 或更高版本。您可以通过 composer 安装此包
composer require patricpoba/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/patricpoba/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
配置
我们需要设置包以利用我们的 momodeveloper 凭证,通过创建一个 MtnConfig
的实例并将其传递给我们要使用的产品的类(集合、支出或汇款)的构造函数,如下所示。配置可以通过调用 ->setConfig($config)
方法并传递新的配置实例来覆盖产品实例。
use PatricPoba\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 集合的更多信息,请参阅 Momo 集合
collectionPrimaryKey
:开发者门户上集合
产品的主键
。collectionUserId
:对于开发环境,使用沙箱凭证,否则使用开发者门户上的凭证。collectionApiSecret
:对于开发环境,使用沙箱凭证,否则使用开发者门户上的凭证。
use PatricPoba\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
:此操作用于请求消费者(付款方)的付款。付款方将被要求授权付款。一旦付款方授权付款,交易将执行。交易将在付款方授权或拒绝或系统超时之前处于待处理状态。可以通过检查getTransaction()
方法的返回结果中的status
字段来验证交易的状态。
$transactionId = $collection->requestToPay($params);
getTransaction
:使用requestToPay
返回的transactionId
检索交易信息。您可以在交易失败或成功之前间隔调用它。
$transaction = $collection->getTransaction($transactionId);
getBalance
:获取账户余额。
$transaction = $collection->getBalance();
accountHolderActive
:检查账户持有人是否已在系统中注册并活跃。
$transaction = $collection->accountHolderActive($mobileNumber);
支出
支出用于从提供者账户向客户转账。有关 Momo 支出的更多信息,请参阅 Momo 支出
disbursementPrimaryKey
:开发门户上Disbursement
产品的主键。disbursementUserId
:在开发环境中,使用沙箱凭据,否则使用开发门户
上的凭据。disbursementApiSecret
:在开发环境中,使用沙箱凭据,否则使用开发门户
上的凭据。
use PatricPoba\MtnMomo\MtnConfig; use PatricPoba\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 PatricPoba\MtnMomo\MtnConfig; use PatricPoba\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调用都返回PatricPoba\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
变更日志
有关最近变更的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件poba.dev@outlook.com联系,而不是使用问题跟踪器。
鸣谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。