thulani / airtel-money-php-sdk
Airtel Money SDK 用于收款、支付和汇款。
Requires
- guzzlehttp/guzzle: ^7.5
- vlucas/phpdotenv: ^5.5
Requires (Dev)
- phpunit/phpunit: ^10.2
This package is not auto-updated.
Last update: 2024-10-02 02:31:32 UTC
README
AirtelMoneyPhpSdk
这是一个用于与非洲 Airtel Money API 集成的 PHP 库。
需求
PHP 7.4 or higher
GuzzleHttp PHP library
PHP dotenv library
安装
您可以通过 composer 安装此包
composer require thulani/airtel-money-php-sdk
用法
授权
在您进行任何 API 调用之前,您需要授权您的应用程序
$airtelMoney->authorize();
默认情况下,此方法将使用创建 AirtelMoney 实例时提供的客户端 ID 和客户端密钥来使用客户端凭证授权获取访问令牌。如果您已经有一个访问令牌,您可以将其作为参数传递
$airtelMoney->authorize('your-access-token');
您还可以提供一个回调函数,该函数将在获取访问令牌后调用
$airtelMoney->authorize(null, function ($token) { // 使用令牌进行某些操作 });
AirtelCollection 类
AirtelCollection 类提供了启动推送请求、请求退款、查询交易状态和处理对账的方法。
初始化类
use Thulani\AirtelMoneyPhpSdk\AirtelCollection;
$config = [ 'client_id' => 'your-client-id', 'client_secret' => 'your-client-secret', 'public_key' => 'your-public-key', 'currency' => 'your-currency-code', 'country' => 'your-country-code', 'env' => 'staging' // 或 'production' ];
$airtel = new AirtelCollection($config);
启动 USSD 推送
initiateUssdPush($amount, $phoneNumber, $transactionId = null, $transactionRef = null, $currency = null, $country = null, $callback = null)
此方法启动 USSD 推送请求。
$amount = 50; $phoneNumber = '712345678'; $transactionRef = 'trx-ref-1234'; // 可选 $transactionId = 'trx-id-1234'; // 可选 $currency = 'ZMW'; // 可选 $country = 'ZM'; // 可选
$result = $airtel->initiateUssdPush($amount, $phoneNumber, $transactionId, $transactionRef, $currency, $country);
// 如果您要传递回调函数 $airtel->initiateUssdPush($amount, $phoneNumber, $transactionId, $transactionRef, $currency, $country, function($result) { // 处理响应 });
请求退款
try { $response = $airtelCollection->requestRefund( $transactionId, $country, $currency, function ($result) { // 使用结果进行某些操作 } ); } catch (\Exception $e) { // 处理错误 }
查询交易状态
try { $response = $airtelCollection->queryTransactionStatus( $transactionId, $country, $currency, function ($result) { // 使用结果进行某些操作 } ); } catch (\Exception $e) { // 处理错误 }
处理对账
$payload = '{"transaction": {"status": "SUCCESS", "transaction_reference": "abc123", "transaction_id": "12345", "msisdn": "26077XXXXXXX", "amount": "5000", "currency": "ZMW", "channel": "AIRTELMM", "narrative": "商品支付"}}';
$result = $airtelCollection->processReconciliation($payload, function ($transaction) { // 使用交易进行某些操作 });
Airtel 支付
use Thulani\AirtelMoneyPhpSdk\AirtelDisbursement;
// 您的 Airtel API 凭据和其他配置选项 $config = [ 'client_id' => 'your_client_id', 'client_secret' => 'your_client_secret', 'public_key' => 'your_public_key', 'currency' => 'ZMW', 'country' => 'ZM', 'env' => 'production', ];
// 使用配置选项初始化 AirtelDisbursement 对象 $airtelDisbursement = new AirtelDisbursement($config);
// 使用从 AirtelService 类继承的 authorize() 方法进行授权 $airtelDisbursement->authorize();
// 调用 initiateDisbursement() 方法 $phoneNumber = '2547xxxxxxxx'; $amount = 100; $transactionRef = 'ABC123'; $result = $airtelDisbursement->initiateDisbursement($phoneNumber, $amount, $transactionRef);
// 如需使用,请使用 $result
回调函数
可以将回调函数传递给任何方法来处理响应数据。
function handleResponse($result) { // 处理结果数据 }
$result = $collection->initiateUssdPush($amount, $phoneNumber, $transactionId, $transactionRef, $currency, $country, 'handleResponse');
许可证
MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。