This is PesaPal Payment API with IPN (Instant Payment Notification) based on PHP programming languange, developed using Codeigniter 3 PHP Framework.
In order to use this API you need to have:
1- Knowledge of PHP Programming Language.
2- Understanding of Codeigniter PHP Framework (Mostly MVC) concepts.
3- Pesapal Developer account/Actual Business Account (Sign up here: http://developer.pesapal.com/ or http://www.pesapal.com/).
4- Codeigniter Core Framework / Codeigniter Project.
This project is based from offical Pesapal PostPesapalDirectOrderV4 in order to use the API you need:
1- consumer_key – merchant key issued by PesaPal to the merchant
2- consumer_secret – merchant secret issued by PesaPal to the merchant
3- Oauth.php File - Download and rename in to .php or use the one come with the API
More can be found here : http://developer.pesapal.com/how-to-integrate/php-sample
Document and reference : http://developer.pesapal.com/how-to-integrate/samples-downloads
Download the file from this repositories and copy them to your existng roject
Application Folder Add in
Config Folder
| route.php
-- add
$route['checkout'] = 'PayPesaPal'; //Buy
$route['buygoods'] = 'PayPesaPal/buyproducts';
$route['pesapal/pay'] = 'PayPesaPal/paynow';
$route['pesapal/paid'] = 'PayPesaPal/paid';
$route['pesapal/response'] = 'PayPesaPal/ipn';
Controller Folder
| PayPesaPal.php
Libraries Folder
| Pesapal.php
| Create Folder -> PesaPalFile
| OAuth.php
Add Table : pesapal_txn
- using file pesapal_txn.sql
To install the API and start using follow the procedure above by coping the files as directed, then follwo the steps below.
Step One
A: Prepair the html form which you will use to collect details from the customer.
-- The API uses form index.php found in views/pesapal
-- The request is called using the index() function found in controller PayPesaPal
-- You can access the form direct or by using route checkout
more on route, url and forms read codeigniter documentation https://www.codeigniter.com/user_guide/
Step Two
A: Submit the form data to Controller PayPesaPal/buyproducts optional use route buygoods
Take all value passed from the checkout form and pass them to paynow function
- Change the access keys to those given to you from the PesaPal account dashboard
$consumer_key = 'Your PesaPal Merchant Consumer Key';
$consumer_secret = 'Your PesaPal Merchant Consumer Secret';
Add the iframelink
$iframelink = 'https://demo.pesapal.com/api/PostPesapalDirectOrderV4';
NB: Remember this changes to
$iframelink = 'https://www.pesapal.com/API/PostPesapalDirectOrderV4';
Once the project is on the live server (Production Phase)
Set-up the values Amount,Description,Names,Email et.c
Then add the callback_url
$callback_url = 'https://www.yourhost.com/pesapal/paid'; //redirect url, the page that will handle the response from pesapal.
The populated iFrame will appear in view paynow and is passed via $data array
$data['iframe_src'] = $iframe_src;
//View
$this->load->view('pesapal/paynow',$data);
Step Three
On the view paynow you will see the PesaPal payment iFrame appear in a position depending with your CSS customization.
-
If you get an error and the PesaPal Form doesn't appear, kindly check your consumer_key, consumer_secret, iframelink, callback_url and OAuth.php if they are well linked.
-
Generate the Payment confirmantion code from: https://demo.pesapal.com/dashboard/my/mobilemoneytest
-
Once you input and click Complete Buttom the payment will be handled to PesaPal and the receival confirmantion will be sent to the callback_url = https://www.yourhost.com/pesapal/paid
-
That will take the request to function paid. There you can define your TimeZone if you wish (place this code above variable $dateTime)
date_default_timezone_set('Your Time Zone');
$dateTime = date('Y-m-d, H:i:s');
Pesapal will pass to our function values reference and pesapal_tracking_id
$reference = $_GET['pesapal_merchant_reference']; // This is our order ID
$pesapal_tracking_id = $_GET['pesapal_transaction_tracking_id']; // PesaPal Payment Reference ID
All values will be stored in table pesapal_txn which is declared at the top before the constructer
private $TXNtable = 'pesapal_txn'; //TXN Table
Once the Data is saved, the function will take the user to 'Payment Complete Page which can be called by function success()
NB: Kindly note this process means the payment has been sent to PesaPal. But incase of DEBIT / CREDIT cards the card charging process can be rejected hence the payment will be incomplete.
That is why in this step our txn_status is set to 'WAITING' and txn_ipn_date, notification_type are 'NULL'
Step Four (IPN)
A: By now you must have set-up the IPN urls form you developer account / business account back-end. If not yet set up from your IPN Settings menu under Account Settings.
- Example of IPN settings details
Web Domain:
www.yourhost.com
IPN Listener URL:
https://www.yourhost.com/pesapal/response
- Once step three is over it will take 60sec for pesapal to confirm if the payment was received successful, and if it's true the IPN urs https://www.yourhost.com/pesapal/response which will access controller PayPesaPal/ipn will be triggered.
B: Set-up the consumer_key and consumer_secret as how you did in STEP TWO then add
$statusrequestAPI = 'https://demo.pesapal.com/api/querypaymentstatus';
- Remember to change to:
https://www.pesapal.com/api/querypaymentstatus' when you are ready to go live!
-
On this step is were we will find out if the payment process completed successful. We will update the payment with variable $status.
-
Set value txn_ipn_date, notification_type, txn_status and flag
-
The txn_status = 'PAID' and flag = 1 this is to indicate that the IPN call was initiated successful.
-
Once this is done the notification_type will be set to 'COMPLETE'
Step Five
From there you can query values from the table and indicate to the Admin dashboard if the product purchase and payment was successful (This page is not provided with this API, you can develop it and access the data from the pesapal_txn or your prefered table)
- Codeigniter - The PHP framework used
- PHP - PHP Language
- MYSQL - MySQL Database
- PesaPal - PesaPal Payment API
Please feel free to contribute, contact me via email.
We will update the API for better code flow or when PesaPal update their API
- Josh L Minga - Initial work - Josh
This project is licensed under the MIT License - see the LICENSE.md file for details