To use this site please enable javascript on your browser! Bryce Andy | Integrate MPesa or Tigopesa on your Laravel Website including other Payment Gateways

Integrate MPesa or Tigopesa on your Laravel Website including other Payment Gateways


Bryce on Feb 2, 2019

Pesapal

Over the last decade, Eastern African countries saw a transition in the way day-to day payments are transacted. Mobile and telecommunication companies have made this possible using mobile money services such as MPesa, AirtelMoney, TigoPesa just to mention a few.

Pesapal Payment Gateway

For web applications it hasn't been easy to integrate mobile money systems as the direct APIs have been expensive and or difficult to use. The emergence of Pesapal has filled this void and made it easier to integrate using their simple API. I have managed to compile the PHP API for Pesapal and created a package that can be used with Laravel.

 

Requirements

If you are not sure you have cURL installed, go to the root of your project and add the following lines inside your composer.json file,

"require": {
        "ext-curl": "*"
    }

 

Install the package

The Laravel package for Pesapal can be found in its github repository on here. To install it inside the root of you project run the following in the terminal;

composer require bryceandy/laravel_pesapal

 

Files and Configuration

After composer has downloaded all files successfully, inside your config/app.php file, add the following in the providers array

'providers' => [
        /*
         * Application Service Providers...
         */
        Bryceandy\Laravel_Pesapal\PesapalServiceProvider::class,
    ]

Then we can publish the package into our project using the command;

php artisan vendor:publish --provider="Bryceandy\Laravel_Pesapal\PesapalServiceProvider"

When it runs successfully, you will now have the following in your project;

NOTE: In order for this migration to work smoothly, make sure you have first_name, last_name and email fields in your users migration and password field be nullable. Lastly in the User model, create a $fillable array and add the previous 3 fields aforementioned.

But now since you have all these files you can modify them however you want to suit your project. Finally you can migrate your tables;

php artisan migrate

 

Your Routes

The 3 new routes you now have under resources/views/pesapal/ all have an important role. 

details.blade.php view will display details that your user will input, and its URL is yourSite.com/details

details

iframe.blade.php view will display payment options as seen below,  to arrive to this view a user needs to fill the details above correctly in the details view. Also try to make the 'reference' detail auto-generated in the controller depending on your project needs.

payment options

lastly, the callback_example.blade.php view will be the page which displays the payment status after a user makes a transaction.

Configuration

Head over to demo if you want a testing environment or live for a live integration and create a business account. You will easily obtain a key-secret pair for your integration.

pesapal.com

Then inside your .env file, create these environment variables;

PESPAL_KEY='yourConsumerKey'
PESAPAL_SECRET='yourConsumerSecret'

end 

Configure Pesapal Settings

For live accounts, in order to receive notifications for every payment from Pesapal, we need to set our IPN listener URL. On your Pesapal dashboard, go to Account Settings and click IPN Settings. Fill in your domain, example yourSite.com, then the IPN listener URL as yourSite.com/pesapal/ipn.php

Thats all there is to it, you can now visit the details page and start making payments!

 


Tags




  • user Bryce 7 months ago

    Comments work
    • user MUSA  4 months ago

      hello in my project user table doesnot include both first name and last name so it is impossible to work with this without these attributes
    • user MUSA  4 months ago

      Problem: parameter_absent | Advice: > oauth_parameters_absent | oauth_consumer_key HOW TO GET THESE
    • user Bryce  3 months ago

      Please read again, I have clearly stated from the start where to obtain those keys and where to put them



  • user Elson 6 months ago

    Sorry, an error occurred while processing your request. We are working to fix it as soon as we can. sory bro i get this error
    • user Bryce  6 months ago

      Hi Elson, if you reached here it is a good sign that it works. Just change your account from demo to live



  • user Elson 6 months ago

    how to change from demo to live?
    • user Bryce  6 months ago

      When you created a Pesapal account you used demo.pesapal.com but now you have to create another one using www.pesapal.com, and do not forget to change your new PESPAL_KEY & PESAPAL_SECRET values in .env



  • user Elson 6 months ago

    second Mr Bryce what you are doing is so good, especially to Tanzania ICT. Its good to see tz developer make simple and easy library for another developer to use their skills and knowledge.



  • user Elson 6 months ago

    also, thank you for making this tutorial free to use and easy to use. god bless you
    • user Bryce  6 months ago

      You are welcome, I am glad I could help!



  • user Jay 5 months ago

    What I have to set in action of form in details.blade.php file.. I have default "/iframe" set in form action. Please help.
    • user Bryce  5 months ago

      The action is the default "pesapal/iframe.php", you have to leave it that way so you can make your payments



  • user Jere 4 months ago

    does it work in kenya ..mpesa to mpesa
    • user Bryce  4 months ago

      Yes it works



  • user Ochieng 4 months ago

    I get an object not found error after submitting the details form. I think it is an issue with iframe. How can i solve it?
    • user Bryce  4 months ago

      Do you have the first_name, last_name attributes in your create_users_table migration and User model? And the password set to nullable() in the same migration ?



  • user 0 4 months ago

    hey brandy igot an error of Problem: consumer_key_unknown | Advice: > |when i integrate with live API
    • user Bryce  3 months ago

      You have entered the wrong consumer key or secret values



  • user 0 4 months ago

    mr bryce you must check this bug your code works fine when developer go the vendor folder functions but the function the view dont work
    • user Bryce  3 months ago

      As per the documentation, you can use or edit the views that you have published. If you published then you will have the views inside "resources/views/pesapal"



  • user MUSA 4 months ago

    Undefined index: scheme



Bryce
Bryce is a web developer based in Dar. Apart from writing code, you can find him in the gym, writing informative articles or playing football.


Copyright © Bryce Andy 2018