datavisionint / laravel-mlipa
Laravel 适用于 M-Lipa 支付网关的包
1.1.3
2024-04-17 10:32 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
您可以通过 composer 安装此包
composer require datavisionint/laravel-mlipa
默认情况下允许日志记录,因此需要日志表。要发布日志表,请运行
php artisan vendor:publish --tag="mlipa-migrations"
php artisan migrate
如果您不使用日志记录,并希望自定义其他配置,请使用以下命令发布配置
php artisan vendor:publish --tag="mlipa-config"
这是发布配置文件的包含内容
use DatavisionInt\Mlipa\Models\MlipaCollection; use DatavisionInt\Mlipa\Models\MlipaPayout; return [ /** * The client key from the mlipa developer portal */ 'client_key' => env('MLIPA_CLIENT_KEY'), /** * The client secret from the mlipa developer portal */ 'client_secret' => env('MLIPA_CLIENT_SECRET'), /** * Webhook route * * The route exposed in your application that will be used to receive webhooks */ 'webhook_route' => 'mlipa/webhook', /** * Webhook route name * * The route name used for webhook when using route() method */ 'webhook_route_name' => 'mlipa.webhook', /** * Payout route * * The route exposed in your application that will be used to verify payouts */ 'payout_verification_route' => 'mlipa/payouts/verification', /** * Payout route name * * The route name used for payout verification when using route() method */ 'payout_verification_route_name' => 'mlipa.payouts.verification', /** * When set to false errors will be thrown when they occur and you will have to * handle them yourself using try catch blocks, and when set to true, the errors * will be silently handled and false status will be returned. The error can be found * in the logs. */ 'handle_errors' => true, /** * When set to true, the transactions will be logged to database, and you will be * required to create the table by running the migration */ 'log_requests' => true, /** * When set to true, the transactions will be logged to database, and you will be * required to create the table by running the migration */ 'log_events' => true, /** * The model to be used for payouts, incase you wish to change the model usde or database * table name, extend this model and then change the $table property. */ 'payout_model' => MlipaPayout::class, /** * The model to be used for collections. Incase you wish to change the model used * or database table name, extend this model and then change $table property */ 'collection_model' => MlipaCollection::class, /** * The root URL of the M-lipa requests */ 'root_url' => 'https://developer.mlipa.co.tz', /** * Endpoints for transactions */ 'endpoints' => [ /** * Authentication endpoint * * The endpoint used for requesting the session token */ 'authentication' => '/v2/auth/token', /** * PushUSSD endpoint * * The endpoint used for initiating PushUSSD */ 'pushussd' => '/v2/pushussd/create', /** * Billing endpoint * * The endpoint used for initiating billing transaction */ 'billing' => '/v2/billings/create', /** * Payout endpoint * * The endpoint used for initiating a payout request */ 'payout' => '/v2/payouts/create', /** * Collection reconcilliation endpoint * * The endpoint used for collection reconcilliation */ 'collection_reconcilliation' => '/v2/reconciliation/collection', /** * Payout reconcilliation endpoint * * The endpoint used for payout reconcilliation */ 'payout_reconcilliation' => '/v2/reconciliation/payout', ], /** * Default heades sent to M-Lipa */ 'default_headers' => [ /** * Accept application/json response from M-Lipa */ 'Accept' => 'application/json', /** * The content type we are sending is application/json */ 'Content-Type' => 'application/json', ], ];
身份验证
此包使用 OAuth2,请访问 M-Lipa 仪表板,然后生成客户端密钥和客户端密钥。然后根据需要更新 .env 文件中的变量。
MLIPA_CLIENT_KEY="9a2db4c7-3e98-**************" MLIPA_CLIENT_SECRET="XeGdmOzj34E2eNZfmQY6Q**************"
用法
启动 USSD 收集
$response = Mlipa::initiatePushUssd( amount: 450000, msisdn: "255754881199" );
启动计费收集
$response = Mlipa::initiateBilling( amount: 450000, msisdn: "255754881199" );
启动退款
$response = Mlipa::initiatePayout( amount: 450000, msisdn: "255754881199", name: "John Doe" );
对收集进行核对
$response = Mlipa::reconcileCollection( reference: "28199122321", );
对退款进行核对
$response = Mlipa::reconcilePayout( reference: "28199122321", );
自定义验证流程
默认情况下,验证将返回 true。为了在您服务提供者的 boot 方法中进行自定义,请实现一个返回 true 的回调。否则,如果未返回或返回 falsy 值,则验证将失败。如果您已禁用退款模型,则默认情况下 $isTransactionValid 将为 true;如果已定义模型,则将引用与退款进行比较;如果存在,则 $isTransactionValid 将为 true,如果不存在,则 $isTransactionValid 将为 false。
Mlipa::verifyPayoutUsing(function(string $reference, bool $isTransactionValid): bool{ // some code to verify the transaction return $isTransactionValid; });
Webhook 事件
您可以订阅 webhook 事件,并通过订阅事件使用回调。您可以在 EventServiceProvider 中订阅事件。
protected $listen = [ BillingFailed::class => [ MyListener::class ], BillingSuccess::class => [ MyListener::class ], PushUssdFailed::class => [ MyListener::class ], PushUssdSuccess::class => [ MyListener::class ], PayoutSuccess::class => [ MyListener::class ], PayoutFailed::class => [ MyListener::class ] ];