Tutorial – PayPal Payment Gateway Integration in CodeIgniter Framework

256

In this tutorial, we’ll give you the step by step guide on PayPal standard payment gateway integration for CodeIgniter. We’ll provide the full source code and using it you can easily integrate PayPal payment gateway in CodeIgniter.

In this CodeIgniter application, we’ll display some products from the database along with the Buy button. Clicking on Buy button, the buyer would be redirected to the PayPal site. After completion of payment, the buyer will come back to the website. Using PayPal IPN (Instant Payment Notification) service, we will check the payment status and store the transaction details in the database.

Database Tables Creation

We’ll create two tables called products and payments.

products
This table is used to store the products data with some basic information. products table SQL would like the below.

payments
This table is used to store the transaction data. payments table SQL would like the below.

Controller Creation

We’ll create two controllers Products and Paypal. The functionalities of this two controllers are given below.

Products:
This controller has two methods, index(), and buy(). index() method would display the products using the product model along with the Buy button. buy() method helps to generate the PayPal form and redirect buyer to the PayPal site once the Buy button is clicked.

Paypal:
This controller has three methods, success(), cancel(), and ipn(). success() method responsible for displaying the transaction data on payment success. cancel() method is requested on payment cancel by the buyer. ipn() method gets the transaction data by PayPal IPN and insert the transaction data into the database.

Setup PayPal Instant Payment Notification (IPN):
In the ipn() function of Paypal controller, each transaction is verified and inserted into the database. This paypal/ipn URL is called by the PayPal IPN and it must be enabled in PayPal.

Note that: Without PayPal IPN the transaction data will not be inserted into the database.

Model (Product) Creation

Product model has two methods, getRows() and insertTransaction(). getRows() method is used to fetch the product data from the products table of the database. insertTransaction() method is used to insert the transaction data in into the payments table of the database.

View Creation

Into the views directory, we’ll create two folders called products and paypal. products/ holds the view file of the Products controller and paypal/ holds the view files of Paypal controller.

products:
This directory has only one view file (index.php). This file holds the HTML for all the products listing.

paypal:
This directory has two view file, success.php and cancel.php.
The success.php file holds the HTML for displaying the transaction success notification.

The cancel.php file holds the HTML for displaying the transaction cancel notification.

Library Creation

We’ll use the paypal_lib library for generating the PayPal form and submit the form to PayPal. This library has a dependency of a config file called paypallib_config.php. paypal_lib.php file will be placed in the application/libraries/ directory and paypallib_config.php file will be placed in the application/config/ directory.

Don’t forget to insert your PayPal business email in the paypal_config.php file.

Load Database

Don’t forget to load database library to using the database connectivity. With two ways you can load the database library.

    • You can load it manually by this line of code – $this->load->database();

Or

  • You can load it automatically in the application/config/autoload.php file by adding this line of code – $autoload['libraries'] = array('database');

Test PayPal Transaction

Open the Products controller (http://localhost/codeigniter/products) at the browser.
You’ll see the products are listed with the Buy button.
Click on the Buy button and you would be redirected to the PayPal site for payment.
Log into the PayPal with your Sandbox personal account and click on Pay.
On payment success, you would be redirected to the website’s success page and you’ll see the payment details.

Make PayPal Payment Gateway Live

Once your test transaction is worked properly with the PayPal sandbox account, now it time to link your live PayPal account. You only need to make two changes for that. Open the application/config/paypallib_config.php and change the following two configuration value.

  • Change the SANDBOX environment to FALSE for make PayPal payment gateway live.

  • Change the BUSINESS EMAIL with your live PayPal business email.

 

Leave A Reply

Your email address will not be published.