Disclaimer: This guide is for the old version 1 of the package, for version 2 and above read the documentation on Github https://github.com/bryceandy/laravel_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;
TransactionController.php
within app/Http/Controllersapp/Transaction.php
config/pesapal.php
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
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.
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.
Then inside your .env
file, create these environment variables;
PESPAL_KEY='yourConsumerKey'
PESAPAL_SECRET='yourConsumerSecret'
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!
Problem: signature_invalid | Advice: > |
Hi, there has been an update on this package. Read the documentation on the latest version at https://github.com/bryceandy/laravel_pesapal
Comments work
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
Problem: parameter_absent | Advice: > oauth_parameters_absent | oauth_consumer_key HOW TO GET THESE
Please read again, I have clearly stated from the start where to obtain those keys and where to put them
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
Hi Elson, if you reached here it is a good sign that it works. Just change your account from demo to live
how to change from demo to live?
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
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.
also, thank you for making this tutorial free to use and easy to use. god bless you
You are welcome, I am glad I could help!
What I have to set in action of form in details.blade.php file.. I have default "/iframe" set in form action. Please help.
The action is the default "pesapal/iframe.php", you have to leave it that way so you can make your payments
does it work in kenya ..mpesa to mpesa
Yes it works
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?
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 ?
hey brandy igot an error of Problem: consumer_key_unknown | Advice: > |when i integrate with live API
You have entered the wrong consumer key or secret values
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
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"
Undefined index: scheme