tumainimosha/laravel-tigopesa-push

Laravel 用于与Tigopesa (TZ) USSD Push集成的包

0.5.0 2022-11-19 14:09 UTC

This package is auto-updated.

Last update: 2024-09-09 08:43:07 UTC


README

Software License Travis Total Downloads

安装

composer require tumainimosha/laravel-tigopesa-push

发布配置文件

发布配置文件以自定义默认包配置。

php artisan vendor:publish --provider="Tumainimosha\TigopesaPush\TigopesaPushServiceProvider" --tag="config"

运行迁移

php artisan migrate

配置

认证

请在 .env 文件中配置您的API参数,如下所示。将下面的示例值替换为集成时提供的值。

TZ_TIGOPESA_PUSH_USERNAME=<your-username>
TZ_TIGOPESA_PUSH_PASSWORD=<your-password>
TZ_TIGOPESA_PUSH_BILLER_MSISDN=<your-biller-msisdn> # Should start with country code 255 followed by 9 digits. eg: 25565000111
TZ_TIGOPESA_PUSH_GET_TOKEN_URL=<your-get-token-url>
TZ_TIGOPESA_PUSH_BILL_PAY_URL=<your-biller-pay-url>

其他配置可在本包发布的配置文件中找到。选项都有详细的注释:

使用方法

快速入门

use Tumainimosha\TigopesaPush\TigopesaPush;

$customerMsisdn = '255652111222';
$amount = 1000;
$txnId = uniqid();

$tigopesaPushService = TigopesaPush::instance();
$response = $tigopesaPushService->postRequest($customerMsisdn, $amount, $txnId);

/** @var bool $success */
$success = $response['ResponseStatus'];

处理回调

默认情况下,此包将交易存储在 tigopesa_push_transactions 表中,并在收到回调时更新其状态。

但是,您可能需要在收到回调后对您的应用程序执行进一步的操作,通过监听在回调时触发的 TigopesaCallbackReceivedHandler::class 事件。

您需要实现自己的事件监听器来监听此事件,并在收到回调后执行任何额外的步骤。

事件具有公共属性 $transaction,其中包含包括状态在内的交易参数。

// EventServiceProvider.php

protected $listen = [
    ...
    \Tumainimosha\TigopesaPush\Events\TigopesaCallbackReceived::class => [
        \App\Listeners\TigopesaCallbackReceivedHandler::class,
    ],
];

// TigopesaCallbackReceivedHandler.php

public function handle(TigopesaCallbackReceived $event)
{
    $transaction = $event->transaction;
    
    // do your custom logic here
}

在运行时自定义配置值

如果您的用例需要,此服务提供流畅的设置器以在运行时更改配置值。

这种情况可能是在同一个项目中您有多个账户,并且您从数据库中获取您的配置值时。

$tigopesaPushService = TigopesaPush::instance();

$tigopesaPushService->setUsername($account->username)
    ->setPassword($account->password)
    ->setBillerMsisdn($account->business_number)
    ->setTokenUrl(config('tigopesa-push.token_url'))
    ->setBillPayUrl(config('tigopesa-push.bill_pay_url'));

测试

使用以下命令运行测试

vendor/bin/phpunit

贡献

有关详细信息,请参阅 CONTRIBUTING

** 需要帮助改进文档和单元测试。欢迎提交拉取请求。

安全

如果您发现任何与安全相关的问题,请通过电子邮件报告,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件