abdulmueid/mpesa

M-Pesa莫桑比克API的PHP SDK

v2.0.0 2020-05-17 01:44 UTC

This package is auto-updated.

Last update: 2024-09-20 15:29:50 UTC


README

本项目旨在提供易于使用且最新的M-Pesa莫桑比克API的PHP封装。

M-Pesa API目标版本:v1x

安装

使用composer安装

composer require abdulmueid/mpesa

使用方法

  1. 从文件加载配置。

    $config = \abdulmueid\mpesa\Config::loadFromFile('/path/to/config.php');

    在examples文件夹中查看示例配置文件。

  2. 使用配置创建一个事务。

    $transaction = new \abdulmueid\mpesa\Transaction($config);
  3. 执行API操作并传递适当的参数。

    1. 启动C2B付款收款。

      $c2b = $transaction->c2b(
          float $amount,
          string $msisdn,
          string $reference,
          string $third_party_reference
      );
    2. 启动B2C付款。

      $b2c = $transaction->b2c(
          float $amount,
          string $msisdn,
          string $reference,
          string $third_party_reference
      );
    3. 启动B2B付款。

      $b2b = $transaction->b2b(
           float $amount,
           string $receiver_party_code,
           string $reference,
           string $third_party_reference
      );
    4. 启动撤销。

      $reversal = $transaction->reversal(
          float $amount,  
          string $transaction_id,
          string $third_party_reference
      );
    5. 查询事务。

      $query = $transaction->query(
          string $query_reference,
          string $third_party_reference
      );
  4. 检查响应

    所有事务都返回TransactionResponse对象。该对象具有以下公共方法

    1. getCode() - 返回响应代码,例如INS-0

    2. getDescription() - 返回描述。

    3. getTransactionID() - 返回事务ID。

    4. getConversationID() - 返回对话ID。

    5. getTransactionStatus() - 返回事务状态。仅在调用query()事务时填充。

    6. getResponse() - 返回从M-Pesa服务器接收到的完整响应JSON对象。适用于调试任何问题或M-Pesa API的未记录行为。

在典型场景中,检查成功事务的代码应如下所示

$c2b = $transaction->c2b(...);

if($c2b->getCode() === 'INS-0') {
    // Transaction Successful, Do something here
}

测试

此存储库提供单元测试以验证对象及其与M-Pesa的交互。

要运行测试,

  1. 打开phpunit.xml文件并添加M-Pesa提供的凭据/参数。
  2. 运行phpunit
  3. 检查手机是否有USSD提示以批准测试事务。

所有测试都使用1MT作为测试金额。

许可协议

此库在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。