PHPackages                             bryceandy/laravel\_pesapal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. bryceandy/laravel\_pesapal

ActiveLibrary[Payment Processing](/categories/payments)

bryceandy/laravel\_pesapal
==========================

An unofficial Pesapal API integration for Laravel. Multiple payment options including but not limited to M-Pesa, Tigo Pesa, Visa, Mastercard, American Express in Kenya, Tanzania &amp; Uganda.

v2.1.0(4y ago)1811.5k↓38.2%13[1 PRs](https://github.com/bryceandy/laravel_pesapal/pulls)MITPHPPHP ^7.4|^8.0

Since Jan 10Pushed 3y ago3 watchersCompare

[ Source](https://github.com/bryceandy/laravel_pesapal)[ Packagist](https://packagist.org/packages/bryceandy/laravel_pesapal)[ Patreon](https://www.patreon.com/bryceandy)[ RSS](/packages/bryceandy-laravel-pesapal/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (16)Used By (0)

Pesapal package for Laravel apps
================================

[](#pesapal-package-for-laravel-apps)

[![Actions Status](https://github.com/bryceandy/laravel_pesapal/workflows/Tests/badge.svg)](https://github.com/bryceandy/laravel_pesapal/actions)[![Total Downloads](https://camo.githubusercontent.com/1bfb62a304e737fea0a2d33e6682b04426782036a2065e57376b39be649545d6/68747470733a2f2f706f7365722e707567782e6f72672f6272796365616e64792f6c61726176656c5f7065736170616c2f642f746f74616c2e737667)](https://packagist.org/packages/bryceandy/laravel_pesapal)[![Latest Stable Version](https://camo.githubusercontent.com/ae2ea774508bb2295a811f93b9ae068965aa7c1fac4a483964965608fefc55eb/68747470733a2f2f706f7365722e707567782e6f72672f6272796365616e64792f6c61726176656c5f7065736170616c2f762f737461626c652e737667)](https://packagist.org/packages/bryceandy/laravel_pesapal)[![License](https://camo.githubusercontent.com/ddb6b91c6ca611615fc3384e7dcae27eef7be049eb8db786d452383e2a05909a/68747470733a2f2f706f7365722e707567782e6f72672f6272796365616e64792f6c61726176656c5f7065736170616c2f6c6963656e73652e737667)](https://packagist.org/packages/bryceandy/laravel_pesapal)
[![Donate to this project using Patreon](https://camo.githubusercontent.com/30769f9470f623db56c689c8c1722d7bcb51efb1e1128d464958b58f27090872/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617472656f6e2d646f6e6174652d79656c6c6f772e737667)](https://patreon.com/bryceandy)

This package enables Laravel developers to easily make use of the [Pesapal](https://www.pesapal.com) API.

[![Pesapal iFrame](images/iFrame.png)](images/iFrame.png)

Version support
---------------

[](#version-support)

Laravel versionPackage versionMaintenance5.7 - 61.0.0 - 1.0.1No longer maintained7 and above2.\*Actively maintainedInstallation
------------

[](#installation)

Pre-installation requirements

- A running or newly installed Laravel 7.\* or above
- PHP 7.4 or above
- cURL extension installed

Now run

```
composer require bryceandy/laravel_pesapal
```

Configuration
-------------

[](#configuration)

Next we publish the configuration file that comes with the package

```
php artisan vendor:publish --tag=pesapal-config
```

After publishing, you will find a `pesapal.php` file in your `config` directory

Head over to [demo](https://demo.pesapal.com) if you want a testing environment or [live](https://www.pesapal.com) for a live integration and create a business account. You will obtain a key-secret pair for your integration

[![Pesapal Registration](images/register.png)](images/register.png)

Inside your `.env` file, create these environment variables and they will be used to set configuration values available in the published `config/pesapal.php` file

Use the keys you obtained from Pesapal to fill the key and secret. If you are on a live account, set the **is\_live** variable to true.

```
PESAPAL_KEY=yourConsumerKey
PESAPAL_SECRET=yourConsumerSecret
PESAPAL_IS_LIVE=false
PESAPAL_CALLBACK_URL=
```

Thereafter, run the migration command as the package will load a database migration that stores the payment records

```
php artisan migrate
```

Usage
-----

[](#usage)

### Before making a payment, setup a callback page.

[](#before-making-a-payment-setup-a-callback-page)

Create a callback page and register its URL in the `PESAPAL_CALLBACK_URL` environment variable. This can be something like `http://yourwebsite.com/callback`

Once a payment process has been completed by the user, Pesapal will redirect to your site using the url.

### Making a request to Pesapal for a payment.

[](#making-a-request-to-pesapal-for-a-payment)

Pesapal requires a request sent to their API in order to display the form like the one we [see above](#pesapal-package-for-laravel-apps)

This package comes with a route `/pesapal/iframe` where you can post the data as follows:

```
/**
 * Create a form and send the appropriate values. You may as
 * well send url parameters where a view will be returned.
 */
[
    'amount' => 'Required, input should be numbers only',
    'currency' => 'Required, values can be TZS,KES,UGX or USD',
    'description' => 'Required, short description of the payment',
    'type' => 'Required, "MERCHANT" or "ORDER"',
    'reference' => 'Required, should be auto-generated and unique for every transaction',
    'first_name' => 'Optional',
    'last_name' => 'Optional',
    'email' => 'Required if there is no phone number',
    'phone_number' => 'Required if there is no email, include the country code. Example 255784999999',
]
```

For the **type** field, leave the default as MERCHANT. If you use ORDER, be sure to read the Pesapal documentation first.

When the data is posted successfully, you will have a view of the form to make payments.

A new payment record will be recorded in your `pesapal_payments` table, now you can choose the payment option you prefer.

### Fetching the payment status.

[](#fetching-the-payment-status)

After making the payment you will be redirected to the callback URL as mentioned above, and Pesapal will redirect with two query parameters:

- pesapal\_merchant\_reference – this is the same as `$reference` that you posted to Pesapal
- pesapal\_transaction\_tracking\_id - a unique id for the transaction on Pesapal that you can use to track the status of the transaction later

With these two we can now:

A. Use these parameters to query the payment status to display to the user.

Normally on your callback page you can display whatever you need to your customer to show that the payment is being processed.

[![Callback page sample](images/callback.png)](images/callback.png)

But because Pesapal will send the payment tracking Id which you have not recorded, you can save this unique tracking Id for your payment and also query for the payment status.

In the controller method where you display the callback page, query the status:

```
namespace App\Http\Controllers;

use Bryceandy\Laravel_Pesapal\Facades\Pesapal;
use Bryceandy\Laravel_Pesapal\Payment;

class CallbackController extends Controller
{
    public function index()
    {
        $transaction = Pesapal::getTransactionDetails(
            request('pesapal_merchant_reference'), request('pesapal_transaction_tracking_id')
        );

        // Store the paymentMethod, trackingId and status in the database
        Payment::modify($transaction);

        $status = $transaction['status'];
        // also $status = Pesapal::statusByTrackingIdAndMerchantRef(request('pesapal_merchant_reference'), request('pesapal_transaction_tracking_id'));
        // also $status = Pesapal::statusByMerchantRef(request('pesapal_merchant_reference'));

        return view('your_callback_view', compact('status')); // Display this status to the user. Values are (PENDING, COMPLETED, INVALID, or FAILED)
    }
}
```

This way requires you to refresh the page because you may not know when the status has changed.

If this does not have a good user experience, you may setup an 'IPN listener' where Pesapal notifies you when a payment status has changed.

B. Setting up an IPN (Instant Payment Notifications) listener.

**This only applies to merchant accounts**. Create a route for your IPN listener, for example a GET request to /pesapal-ipn-listener

```
// For Laravel 7.*
Route::get('pesapal-ipn-listener', 'IpnController');
// For Laravel 8.* onwards
Route::get('pesapal-ipn-listener', \App\Http\Controllers\IpnController::class);
```

Your IPN Controller could look like this:

```
namespace App\Http\Controllers;

use Bryceandy\Laravel_Pesapal\Facades\Pesapal;
use Bryceandy\Laravel_Pesapal\Payment;
use Illuminate\Support\Facades\Mail;

class IpnController extends Controller
{
    public function __invoke()
    {
        $transaction = Pesapal::getTransactionDetails(
            request('pesapal_merchant_reference'), request('pesapal_transaction_tracking_id')
        );

        // Store the paymentMethod, trackingId and status in the database
        Payment::modify($transaction);

        // If there was a status change and the status is not 'PENDING'
        if(request('pesapal_notification_type') == "CHANGE" && request('pesapal_transaction_tracking_id') != ''){

            //Here you can do multiple things to notify your user that the changed status of their payment
            // 1. Send an email or SMS (if your user doesnt have an email)to your user
            $payment = Payment::whereReference(request('pesapal_merchant_reference'))->first();
            Mail::to($payment->email)->send(new PaymentProcessed(request('pesapal_transaction_tracking_id'), $transaction['status']));
            // PaymentProcessed is an example of a mailable email, it does not come with the package

            // 2. You may also create a Laravel Event & Listener to process a Notification to the user
            // 3. You can also create a Laravel Notification or dispatch a Laravel Job. Possibilities are endless!

            // Finally output a response to Pesapal
            $response = 'pesapal_notification_type=' . request('pesapal_notification_type').
                    '&pesapal_transaction_tracking_id=' . request('pesapal_transaction_tracking_id').
                    '&pesapal_merchant_reference=' . request('pesapal_merchant_reference');

            ob_start();
            echo $response;
            ob_flush();
            exit; // This is mandatory. If you dont exit, Pesapal will not get your response.
        }
    }
}
```

This controller method will be called every time Pesapal sends you an IPN notification until the payment is completed or has failed.

IMPORTANT
---------

[](#important)

### Register IPN settings

[](#register-ipn-settings)

On your Pesapal dashboard find your Account Settings and click IPN Settings.

Fill in your website domain for example `yourWebsite.com` and IPN listener URL, for example `yourWebsite.co.tz/pesapal-ipn-listener`.

This is important so that Pesapal can send IPN notifications.

License
-------

[](#license)

MIT License.

Contributors
------------

[](#contributors)

This package is based from the PHP API of [Pesapal](https://pesapal.com)

- [BryceAndy](http://bryceandy.com) &gt;

Sponsorship
-----------

[](#sponsorship)

If you enjoy using this package, consider contributing to the maintainer

[![Donate to this project using Patreon](https://camo.githubusercontent.com/30769f9470f623db56c689c8c1722d7bcb51efb1e1128d464958b58f27090872/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617472656f6e2d646f6e6174652d79656c6c6f772e737667)](https://patreon.com/bryceandy)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~78 days

Recently: every ~119 days

Total

14

Last Release

1663d ago

Major Versions

v1.0.1 → v2.0.0-beta.12020-06-16

PHP version history (2 changes)v2.0.0-beta.1PHP &gt;=7.4

v2.1.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c5d3f71bbede0e6941e700f5ca5f082eaeb05c1ab5523d4c3895964175c3789f?d=identicon)[bryceandy](/maintainers/bryceandy)

---

Top Contributors

[![bryceandy](https://avatars.githubusercontent.com/u/11915180?v=4)](https://github.com/bryceandy "bryceandy (181 commits)")

---

Tags

laravelpayment-gatewaypayment-integrationpesapal

### Embed Badge

![Health badge](/badges/bryceandy-laravel-pesapal/health.svg)

```
[![Health](https://phpackages.com/badges/bryceandy-laravel-pesapal/health.svg)](https://phpackages.com/packages/bryceandy-laravel-pesapal)
```

###  Alternatives

[omnipay/paypal

PayPal gateway for Omnipay payment processing library

3156.8M53](/packages/omnipay-paypal)[eduardokum/laravel-boleto

Biblioteca com boletos para o laravel

626351.9k2](/packages/eduardokum-laravel-boleto)[tbbc/money-bundle

This is a Symfony bundle that integrates moneyphp/money library (Fowler pattern): https://github.com/moneyphp/money.

1961.9M](/packages/tbbc-money-bundle)[2checkout/2checkout-php

2Checkout PHP Library

83740.3k2](/packages/2checkout-2checkout-php)[smhg/sepa-qr-data

Generate QR code data for SEPA payments

61717.2k5](/packages/smhg-sepa-qr-data)[omnipay/dummy

Dummy driver for the Omnipay payment processing library

271.2M33](/packages/omnipay-dummy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
