tfs/mpesa

此包将使您能够在Laravel项目中轻松消费Safaricom Mpesa Daraja API

v2.1.0 2023-10-25 22:17 UTC

This package is auto-updated.

Last update: 2024-09-26 00:30:07 UTC


README

Issues Stars

此包将使您能够轻松消费Safaricom Mpesa Daraja API。它专为Laravel开发者设计。

安装Laravel Mpesa Daraja API包

推荐通过Composer安装Safaricom Mpesa Daraja API的Laravel包。

composer require tfs/mpesa

运行vendor:publish artisan命令

php artisan vendor:publish --provider="TFS\Mpesa\MpesaServiceProvider"

发布后,您将找到config/mpesa.php配置文件。您现在可以适当调整配置。此外,出于安全考虑,将配置添加到您的.env文件中。

将以下文件添加到您的.env

MPESA_CONSUMER_KEY=
MPESA_CONSUMER_SECRET=
MPESA_SHORTCODE=
MPESA_PASSKEY=
MPESA_CALLBACK_URL=

MPESA_INITIATOR_NAME=
MPESA_INITIATOR_PASSWORD=
MPESA_B2C_CONSUMER_KEY=
MPESA_B2C_CONSUMER_SECRET=
MPESA_B2C_SHORTCODE=

MPESA_MODE=sandbox

用法

Mpesa Express

use TFS\Mpesa\Mpesa;

...
$response = Mpesa::mpesa_express($phone, $amount, $AccountReference, $TransactionDesc, $callback = null);
...

eg.

$response = Mpesa::mpesa_express("254723077827", 1, "AccountReference", "TransactionDesc");

B2C

use TFS\Mpesa\Mpesa;

...
$response = Mpesa::b2c($phone, $amount, $occassion, $remarks, $callback = null, $command_id = null);
...

eg.

$response = Mpesa::b2c("254708374149", 10, "Test occassion", "Test remarks");

余额

use TFS\Mpesa\Mpesa;

...

$result = Mpesa::balance($partyA, $remarks, $callback, $identifierType, $consumer_key, $consumer_secret, $initiator_name, $initiator_password, $queueTimeOutURL = null);
...

eg.

$partyA = config('mpesa.live.b2c_shortcode');
$remarks = "Balance";
$callback = config('mpesa.balance_callback_url');
$identifierType = 4; //1 – MSISDN, 2 – Till Number, 4 – Organization short code!
$consumer_key = config("mpesa." . config('mpesa.mode') . ".b2c_consumer_key");
$consumer_secret = config("mpesa." . config('mpesa.mode') . ".b2c_consumer_secret");
$consumer_key = config("mpesa." . config('mpesa.mode') . ".b2c_consumer_key");
$initiator_name = config("mpesa." . config('mpesa.mode') . ".initiator_name");
$initiator_password = config("mpesa." . config('mpesa.mode') . ".initiator_password");

$result = Mpesa::balance($partyA, $remarks, $callback, $identifierType, $consumer_key, $consumer_secret, $initiator_name, $initiator_password);

C2B注册URL

use TFS\Mpesa\Mpesa;

...

$result = Mpesa::c2b_register_url($ValidationURL, $ConfirmationURL, $ResponseType, $ShortCode, $consumer_key = null, $consumer_secret = null)
...

eg.

$ValidationURL = "https://24seven.co.ke/api/payments/c2b-validation";
$ConfirmationURL = "https://24seven.co.ke/api/payments/c2b-confirmation";
$ResponseType = "Completed";//Canceled
$ShortCode = config("mpesa." . config('mpesa.mode') . ".shortcode");

$result = Mpesa::balance($partyA, $remarks, $callback, $identifierType, $consumer_key, $consumer_secret, $initiator_name, $initiator_password, $queueTimeOutURL = null);