Skip to content

Mpesa package for Laravel versions 8,9,10,11. The package will allow using more than one shortcodes. The package includes the recent Tax Remmitance and Bill Manager APIs.

License

Notifications You must be signed in to change notification settings

akikadigital/laravel-mpesa-multivendor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Mpesa Multivendor Package by Akika Digital

This Laravel package provides convenient methods for integrating Mpesa Daraja API's functionalities into your Laravel application. The package will allow using more than one shortcodes. It also includes the recent Tax Remmitance and Bill Manager APIs.

Installation

You can install the package via composer:

composer require akika/laravel-mpesa-multivendor

After installing the package, publish the configuration file using the following command:

php artisan mpesa:install

This will generate a mpesa.php file in your config directory where you can set your Mpesa credentials and other configuration options.

.env file Setup

Add the following configurations into the .env file

MPESA_ENV=

The value is either production or sandbox

NOTE: The mpesa.php config file sets the default MPESA_ENV value to sandbox. This will always load sandbox urls.

Function Responses

All responses, except the token generation response, conform to the responses documented on the daraja portal.

Usage

Initializing Mpesa

use Akika\LaravelMpesaMultivendor\Mpesa;

$mpesa = new Mpesa($mpesaShortcode, $consumerKey, $consumerSecret, $apiUsername, $apiPassword);
  • $mpesaShortcode: The shortcode to use for the current operation
  • $consumerKey: Obtained from Daraja portal
  • $consumerSecret: Obtained from Daraja portal
  • $apiUsername: Mpesa portal API user's username
  • $apiPassword: Mpesa portal API user's password

Important Urls

Daraja utilizes the two main urls for callbacks. Timeout Url and Result Url. The two urls will also be used in this package as follows:

  • $resultUrl : Endpoint to send the results in case of success
  • $timeoutUrl : Endpoint to send the results in case of operations timeout

Fetching Token

You can fetch the token required for Mpesa API calls as follows:

$token = $mpesa->getToken();

Getting Account Balance

You can fetch mpesa account balance as follows:

$balance = $mpesa->getBalance($resultUrl, $timeoutUrl);

C2B Transactions

Registering URLs for C2B Transactions

You can register validation and confirmation URLs for C2B transactions:

$response = $mpesa->c2bRegisterUrl($confirmationUrl, $validationUrl);

#### Simulating C2B Transactions

You can simulate payment requests from clients:

```php
$response = $mpesa->c2bSimulate($amount, $phoneNumber, $billRefNumber, $commandID);
  • $commandID is either CustomerPayBillOnline or CustomerBuyGoodsOnline and if not set, the package will assume CustomerPayBillOnline

Initiating STK Push

You can initiate online payment on behalf of a customer:

$response = $mpesa->stkPush($accountNumber, $phoneNumber, $amount, $callbackUrl, $transactionDesc);
  • $transactionDesc can be null

Querying STK Push Status

You can query the result of a STK Push transaction:

$response = $mpesa->stkPushStatus($checkoutRequestID);

Reversing Transactions

You can reverse a C2B M-Pesa transaction:

$response = $mpesa->reverse($transactionId, $amount, $receiverShortCode, $remarks, $resultUrl, $timeoutUrl, $ocassion);
  • $ocassion is an optional field

Business to Customer (B2C) Transactions

You can perform Business to Customer transactions:

$response = $mpesa->b2cTransaction($oversationId, $commandID, $msisdn, $amount, $remarks, $resultUrl, $timeoutUrl, $ocassion);
  • $ocassion is an optional field.

Business to Business (B2B) Transactions

You can perform Business to Business transactions:

$response = $mpesa->b2bPaybill($destShortcode, $amount, $remarks, $accountNumber, $resultUrl, $timeoutUrl, $requester);
  • $requester is an optional field.

QR Code Generation

You can generate QR codes for making payments:

$response = $mpesa->dynamicQR($merchantName, $refNo, $trxCode, $cpi, $size, $amount = null);
  • $amount is an optional field

Bill Manager

You can optin to the bill manager service and send invoices:

$response = $mpesa->billManagerOptin($email, $phoneNumber, $sendReminders, $logoUrl, $callbackUrl);
  • $sendReminders is a boolean field. Allows true or false

$response = $mpesa->sendInvoice($reference, $billedTo, $phoneNumber, $billingPeriod, $invoiceName, $dueDate, $amount, $items);

Tax Remittance

You can remit tax to the government:

$response = $mpesa->taxRemittance($amount, $receiverShortCode, $accountReference, $remarks, $resultUrl, $timeoutUrl);

License

The Laravel Mpesa package is open-sourced software licensed under the MIT license. See the LICENSE file for details.

About

Mpesa package for Laravel versions 8,9,10,11. The package will allow using more than one shortcodes. The package includes the recent Tax Remmitance and Bill Manager APIs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages