PHPackages                             mhassan654/laravel-pesapal-sdk - 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. mhassan654/laravel-pesapal-sdk

ActiveLibrary

mhassan654/laravel-pesapal-sdk
==============================

Laravel package to manage current pesapal payment api, can be used for web and restful apis

v0.5.3(1y ago)12.1kMITPHPPHP ^7.4|^8.0|^8.1

Since Mar 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mhassan654/laravel-pesapal-sdk)[ Packagist](https://packagist.org/packages/mhassan654/laravel-pesapal-sdk)[ Docs](https://github.com/mhassan654/pesapal)[ RSS](/packages/mhassan654-laravel-pesapal-sdk/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (7)Used By (0)

Pesapal Laravel 7,8,9,10 Web &amp; Restful API
==============================================

[](#pesapal-laravel-78910-web--restful-api)

[![Screenshot 2024-03-19 at 11 52 03](https://private-user-images.githubusercontent.com/26597730/313985977-1b6d0751-c5a5-4bd1-9760-68463477c823.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzU3MzU0OTksIm5iZiI6MTc3NTczNTE5OSwicGF0aCI6Ii8yNjU5NzczMC8zMTM5ODU5NzctMWI2ZDA3NTEtYzVhNS00YmQxLTk3NjAtNjg0NjM0NzdjODIzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjA0MDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwNDA5VDExNDYzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTkzYzI3YTcxNTA3OTQ5OWU3MjZiMTRkY2I2NWYyNzUwYjM0NGE1ZTY4OGU2MDNhZTNkZGRjYTRiYjY4MDdjZmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Nfv31TnNnAeUwCOIrdws6eUiL3HZCj51e04yYm6Hjt4)](https://private-user-images.githubusercontent.com/26597730/313985977-1b6d0751-c5a5-4bd1-9760-68463477c823.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzU3MzU0OTksIm5iZiI6MTc3NTczNTE5OSwicGF0aCI6Ii8yNjU5NzczMC8zMTM5ODU5NzctMWI2ZDA3NTEtYzVhNS00YmQxLTk3NjAtNjg0NjM0NzdjODIzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjA0MDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwNDA5VDExNDYzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTkzYzI3YTcxNTA3OTQ5OWU3MjZiMTRkY2I2NWYyNzUwYjM0NGE1ZTY4OGU2MDNhZTNkZGRjYTRiYjY4MDdjZmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Nfv31TnNnAeUwCOIrdws6eUiL3HZCj51e04yYm6Hjt4)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5ead1b2a03e178a7b1e5c1a0c80c324d6b84171590a108a7fa12d19f74a728f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d68617373616e3635342f7065736170616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mhassan654/pesapal)[![Total Downloads](https://camo.githubusercontent.com/cc4a26a5bf4ccf7a9afe38b45399113ee305bbcdb948b4cce9f80b2f7b7e1f1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d68617373616e3635342f7065736170616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mhassan654/pesapal)[![GitHub Actions](https://github.com/mhassan654/pesapal/actions/workflows/main.yml/badge.svg)](https://github.com/mhassan654/pesapal/actions/workflows/main.yml/badge.svg)

Laravel 7,8,9,10 package for Pesapal Api. this package has been developed to utilize the current pesapal payment api.

It can be used on both web and restful apis.

Installation
------------

[](#installation)

Add this package uis

You can install the package via composer:

```
composer require mhassan654/laravel-pesapal-sdk
```

Usage
-----

[](#usage)

### Update your config (for Laravel 5.4 and below)

[](#update-your-config-for-laravel-54-and-below)

Add the service provider to the providers array in config/app.php:

`Mhassan654\Pesapal\PesapalServiceProvider::class,`

Add the facade to the aliases array in config/app.php:

`'Pesapal' => Mhassan654\Pesapal\Facades\Pesapal::class,`

### Publish the package configuration (for Laravel 5.4 and below)

[](#publish-the-package-configuration-for-laravel-54-and-below)

Publish the configuration file and migrations by running the provided console command:

`php artisan vendor:publish --provider="Mhassan654\Pesapal\PesapalServiceProvider" --tag='config'`

Setup
-----

[](#setup)

### Pesapal IPN

[](#pesapal-ipn)

For the url of the route use /pesapal-ipn eg mysite.com/pesapal-ipn as the IPN on the Pesapal Merchant settings dashboard

### Environmental Variables

[](#environmental-variables)

PESAPAL\_CONSUMER\_KEY `pesapal consumer key`

PESAPAL\_CONSUMER\_SECRET `pesapal consumer secret`

PESAPAL\_CURRENCY `ISO code for the currency`

PESAPAL\_IPN `controller method to call for instant notifications IPN  as relative path from App\Http\Controllers\ eg "TransactionController@confirmation"`

PESAPAL\_CALLBACK\_ROUTE `route name to handle the callback eg Route::get('donepayment', ['as' => 'paymentsuccess', 'uses'=>'PaymentsController@paymentsuccess']);  The route name is "paymentsuccess"`

### Config

[](#config)

**live** - Live or Demo environment

The ENV Variables can also be set from here.

Usage
-----

[](#usage-1)

At the top of your controller include the facade
`use Pesapal;`

### Example Code...Better Example..Haha

[](#example-codebetter-examplehaha)

Assuming you have a Payment Model

```
use App\Models\PesapalPayment;
use App\Models\PesapalPaymentIpn;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Mhassan654\Pesapal\Exceptions\PesapalException;
use Mhassan654\Pesapal\Pesapal;
use Random\RandomException;

/**
 *
 */
class PesapalPaymentController extends Controller
{
    /**
     * @var Pesapal
     */
    protected Pesapal $pesapal;

    /**
     * @param Pesapal $pesapal
     * @return void
     */
    public function  __construct(Pesapal $pesapal)
    {
        $this->pesapal = $pesapal;
    }

    /**
     * @return JsonResponse
     * @throws PesapalException
     * @throws RandomException
     */
    public function payment(Request $request)
    {//initiates payment

        $transaction_id =  (new Pesapal)->random_reference();
        $payments = new PesapalPayment;
        $payments -> merchant_reference =$transaction_id;
        $payments -> status = 'NEW';
        $payments -> amount = $request->amount;
        $payments -> save();

        $billing_object = (object)[
            "email_address" => $request->billing_address['email_address'],
            "phone_number" => $request->billing_address['phone_number'],
            "country_code" => $request->billing_address['country_code'],
            "first_name" => $request->billing_address['first_name'],
            "middle_name" => $request->billing_address['middle_name'],
            "last_name" => $request->billing_address['last_name'],
            "line_1" =>  $request->billing_address['line_1'],
            "line_2" =>  $request->billing_address['line_2'],
            "city" => $request->billing_address['city'],
            "state" => $request->billing_address['state'],
            "postal_code" => null,
            "zip_code" => null
        ];

        $payment_request = [ // the defaults will be overidden if set in $params
            'id' => $transaction_id,
            'amount' => $request->amount,
            'currency'=>'UGX',
            'description' => $request->description,
            'callback_url' => $request->callback_url,
            "notification_id" => $request->notification_id,
            "branch"=> "Project Code - Kampala",
            'billing_address' => $billing_object
        ];

        $pesapal_payment= (new Pesapal)->makePayment($payment_request);
        $decode_response = json_decode($pesapal_payment);

        // check for parameter errors
        if ($decode_response->error && $decode_response->error->code == 'invalid_api_request_parameters') {
            return $this->customFailResponseWithPayload($decode_response->error->message);
        }

        if ($decode_response->status == "200"){
            $payments = PesapalPayment::where('merchant_reference',$decode_response->merchant_reference)->first();
            $payments -> order_tracking_id = $decode_response->order_tracking_id;
            $payments -> status = 'PENDING';
            $payments -> update();
        }
        return $this->customSuccessResponseWithPayload($decode_response);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function paymentsuccess(Request $request)//just tells u payment has gone thru..but not confirmed
    {
        $trackingid = $request->input('order_tracking_id');
        $ref = $request->input('merchant_reference');

        $payments = PesapalPayment::where('merchant_reference',$ref)->first();
        $payments -> trackingid = $trackingid;
        $payments -> status = 'PENDING';
        $payments -> save();

        $payments=PesapalPayment::all();
        return $this->customSuccessResponseWithPayload($payments);
    }

    /**
     * @param Request $request
     * @return void
     */
    public function paymentConfirmation(Request $request)
    {
        $trackingid = $request->input('OrderTrackingId');
        $merchant_reference = $request->input('OrderMerchantReference');
        $pesapal_notification_type= $request->input('OrderNotificationType');

        //use the above to retrieve payment status now..
        $this->checkpaymentstatus($trackingid,$merchant_reference,$pesapal_notification_type);
    }

    //Confirm status of transaction and update the DB

    /**
     * @param $trackingid
     * @param $merchant_reference
     * @param $pesapal_notification_type
     * @return string
     * @throws PesapalException
     */
    public function checkpaymentstatus($trackingid, $merchant_reference, $pesapal_notification_type){

        $status_response=$this->pesapal->getTransactionStatus($trackingid);
        $status = json_decode($status_response);

        if ($status->status == "200"){
            $payments = PesapalPayment::where('order_tracking_id',$trackingid)->first();
            $payments -> status = $status->status;

//            Pesapal status code representing the payment_status_description.
//            0 - INVALID
//            1 - COMPLETED
//            2 - FAILED
//            3 - REVERSED
            $payments -> status_code = $status->status_code;
            $payments -> payment_method = $status->payment_method;
            $payments -> description = $status->description;
            $payments -> created_date = $status->created_date;
            $payments -> message = $status->message;
            $payments -> payment_account = $status->payment_account;
            $payments -> merchant_reference = $status->merchant_reference;
            $payments -> currency = $status->currency;
            $payments -> amount = $status->amount;
            $payments -> update();
            return $this->customSuccessResponseWithPayload("success");
        }
        return $this->customFailResponseWithPayload("something went wrong");
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function registerIPN(Request $request)
    {
        try {
            $url = $request->url;
            $notification_type = $request->ipn_notification_type;

            $ipn_data = $this->pesapal->registerIPN($url, $notification_type);

           $resp = json_decode($ipn_data);

            $new_ipn = new PesapalPaymentIpn;
            $new_ipn->url = $resp->url;
            $new_ipn->created_date = $resp->created_date;
            $new_ipn->ipn_id = $resp->ipn_id;

            if ( $new_ipn->save()){
                return response()->json($new_ipn);
            }

            return \response()->json("something went wrong");
        }catch (\Exception $exception){
            return \response()->json($exception->getMessage());
        }
    }

    /**
     * @return
     */
    public function getRegisteredIPN()
    {
        $respo_data = $this->pesapal->getRegisterIPNlist();
        $decode_data = json_decode($respo_data);
        return \response()->json($decode_data);
    }

    public function ipnReceiver($OrderTrackingId, $status, $payment_method, $OrderMerchantReference)
    {
        $payments = PesapalPayment::where('order_tracking_id',$OrderTrackingId)->first();
        if ($payments){
            $payments -> status = $status;
            $payments -> payment_method = $payment_method;
            $payments ->update();
        }

    }

}
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [MUWONGE HASSAN](https://github.com/mhassan654)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 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 ~16 days

Total

5

Last Release

724d ago

PHP version history (2 changes)v0.1.0PHP ^7.4|^8.0

v0.5.2PHP ^7.4|^8.0|^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/a95bf30c6de57abfdd3a8d75e4c129585fc5f1101f3b7fb5b71664f1acbeba6d?d=identicon)[Muwonge Hassan](/maintainers/Muwonge%20Hassan)

---

Top Contributors

[![hmuwonge](https://avatars.githubusercontent.com/u/223309859?v=4)](https://github.com/hmuwonge "hmuwonge (14 commits)")

---

Tags

laravellaravel 7laravel 8laravel 9paymentsairtelvisamastercardpesapalmtnmhassan654Ezee Money

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[itsmurumba/laravel-mpesa

Laravel Package for Mpesa Daraja API

191.6k](/packages/itsmurumba-laravel-mpesa)[prevailexcel/laravel-nowpayments

A Laravel Package for NOWPayments

1414.2k](/packages/prevailexcel-laravel-nowpayments)

PHPackages © 2026

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