maina-david/multi-shortcode-mpesa

多个短码 Mpesa 集成

dev-master 2024-09-13 08:16 UTC

This package is auto-updated.

Last update: 2024-09-13 08:16:50 UTC


README

此包旨在帮助 PHP 开发者在不费太多力气的情况下实现各种 Mpesa API。它基于 REST API,其文档可在 https://developer.safaricom.co.ke 查找。

它支持沙盒和生产环境中的多个短码。

安装

您可以通过 composer 安装 PHP SDK 或下载源代码

通过 Composer

安装 SDK 的推荐方式是使用 Composer

composer require maina-david/multi-shortcode-mpesa

可选:服务提供程序将自动注册。或者,您可以在 config/app.php 文件中手动添加服务提供程序

'providers' => [
    // ...
    MainaDavid\MultiShortcodeMpesa\MpesaServiceProvider::class,
];

您应该使用以下命令发布短码迁移和 config/multi-shortcode-mpesa.php 配置文件

php artisan vendor:publish --provider="MainaDavid\MultiShortcodeMpesa\MpesaServiceProvider"

您可以在 config/multi-shortcode-mpesa.php 文件中覆盖默认的 URL。

清除您的配置缓存。此包需要访问 multi-shortcode-mpesa 配置。通常,在开发环境中进行配置缓存是坏做法。如果您已经在本地缓存了配置,请使用以下任一命令清除配置缓存

 php artisan optimize:clear
 # or
 php artisan config:clear

运行迁移:在配置和迁移已发布和配置后,您可以通过运行以下命令创建此包的表

 php artisan migrate

用法

SDK 需要通过使用您在 short_codes 表中保存的其中一个短码进行实例化,并在出于安全原因将它们存储在数据库中时加密以下值。如果无法解密,它将抛出错误。

  • consumer_key。
  • consumer_secret。
  • pass_key。
  • initiator_name。
  • initiator_password。

示例:将短码保存到数据库中

use MainaDavid\MultiShortcodeMpesa\Models\ShortCode;

    /**
     * It validates the request, creates a new shortcode object, encrypts the sensitive data and saves it
     * to the database
     *
     * @param Request request The request object.
     *
     * @return A JSON response with a success message.
     */
    public function storeShortCode(Request $request)
    {
        /* Validating the request. */
        $request->validate([
            'environment' => 'required|in:sandbox,production',
            'direction' => 'required|in:c2b,b2c',
            'shortcode' => 'required|integer',
            'consumer_key' => 'required',
            'consumer_secret' => 'required',
            'pass_key' => 'required_if:direction,c2b',
            'initiator_name' => 'required_if:direction,b2c',
            'initiator_password' => 'required_if:direction,b2c',
        ]);

        $shortcode = new ShortCode();
        $shortcode->environment = $request->environment; //sandbox or production
        $shortcode->direction = $request->direction; // c2b or b2c
        $shortcode->shortcode = $request->shortcode; // mpesa shortcode
        $shortcode->consumer_key = encrypt($request->consumer_key); // shortcode consumer key
        $shortcode->consumer_secret = encrypt($request->consumer_secret); // shortcode consumer secret
        /* Checking if the request has a pass_key and if it does, it encrypts it and saves it to the database. */
        if ($request->has('pass_key')) {
            $shortcode->pass_key = encrypt($request->pass_key); // not required for b2c
        }
        /* Checking if the request has an initiator name and if it does, it encrypts it and saves it to the
        database. */
        if ($request->has('initiator_name')) {
            $shortcode->initiator_name = encrypt($request->initiator_name);  // shortcode api initiator
        }
        /* Checking if the request has an initiator password and if it does, it encrypts it and saves it to the
        database. */
        if ($request->has('initiator_password')) {
            $shortcode->initiator_password = encrypt($request->initiator_password); // shortcode initiator password
        }
        $shortcode->save();

        return response()->json([
            'success' => true,
            'message' => 'Shortcode saved successfully!'
        ], 200);
    }

您可以使用此 SDK 用于生产或沙盒应用程序。对于沙盒,短码环境始终是 ALWAYS sandbox

授权由 SDK 在沙盒和生产环境中完成。您可以使用不同环境中的多个短码无缝使用。

内容

  • stkPush($phonenumber, $amount, $reference = '', $description = ''):启动 STK 推送交易

    • phonenumber:发送金钱的电话号码。 REQUIRED
    • amount:这是要交易的数量。仅支持整数。 REQUIRED
    • reference:这是 CustomerPayBillOnline 交易类型的标识符。 OPTIONAL
    • description:这是可以随请求一起发送的任何附加注释。 OPTIONAL
  • stkPushQuery($CheckoutRequestID):返回交易的状态

    • CheckoutRequestID:已处理结账交易请求的唯一标识符。 REQUIRED
  • b2c($commandID, $amount, $phonenumber, $remarks):从您的企业向客户发送金钱

    • commandID:唯一命令,指定 B2C 交易类型 [SalaryPayment,BusinessPayment,PromotionPayment]。 REQUIRED
    • phonenumber:接收金额的客户手机号码。 REQUIRED
    • amount:发送给客户的金额。仅支持整数。 REQUIRED
    • remarks:与交易关联的任何附加信息。 REQUIRED
  • transactionStatus($TransactionID):它返回交易的状态

    • TransactionID:Mpesa 上交易的唯一标识符。 REQUIRED
  • reverseTransaction($TransactionID, $amount):它撤销交易

    • TransactionID:Mpesa 上交易的唯一标识符。 REQUIRED

Lipa na Mpesa Online

use MainaDavid\MultiShortcodeMpesa\Mpesa;

// Mpesa shortcode
$shortcode = '12345';

// Phone numbers are transformed to the required format by the sdk so no worries about the formatting
$phonenumber = '0722123456';

// Instantiate the class
$mpesa  = new Mpesa($shortcode);

// Use stkpush service
$result = $mpesa->stkPush($phonenumber, $amount);

print_r($result);

检查 Lipa Na M-Pesa Online 付款的状态

use MainaDavid\MultiShortcodeMpesa\Mpesa;

// Mpesa shortcode
$shortcode = '12345';

$CheckoutRequestID = 'ws_CO_260520211133524545';

// Instantiate the class
$mpesa  = new Mpesa($shortcode);

$result = $mpesa->stkPushQuery($CheckoutRequestID);

print_r($result);

企业对客户(付款)

use MainaDavid\MultiShortcodeMpesa\Mpesa;

// Mpesa shortcode
$shortcode = '12345';

$phonenumber = '0722123456';

// Instantiate the class
$mpesa  = new Mpesa($shortcode);

$result = $mpesa->b2c($commandID, $amount, $phonenumber, $remarks);

print_r($result);

检查交易状态

use MainaDavid\MultiShortcodeMpesa\Mpesa;

// Mpesa shortcode
$shortcode = '12345';

$TransactionID = 'QWERTY123456789';

// Instantiate the class
$mpesa  = new Mpesa($shortcode);

$result = $mpesa->transactionStatus($TransactionID);

print_r($result);

撤销交易

use MainaDavid\MultiShortcodeMpesa\Mpesa;

// Mpesa shortcode
$shortcode = '12345';

$TransactionID = 'QWERTY123456789';

// Instantiate the class
$mpesa  = new Mpesa($shortcode);

$result = $mpesa->reverseTransaction($TransactionID);

print_r($result);

示例 SDK 响应

SDK将返回以下格式的成功或错误响应。

成功响应

{
  "status": "success",
  "data": {
    "ConversationID": "AG_20230108_2010364679d05de418cd",
    "OriginatorConversationID": "12425-164354829-1",
    "ResponseCode": "0",
    "ResponseDescription": "Accept the service request successfully."
  }
}

错误响应

{
  "status": "error",
  "data": "Shortcode does not support Business to Customer!"
}