DEV Community


Paypal payment gateway integration in codeigniter with source code

robertlook profile image Robert Look ・2 min read

We will create payment gateway integration in Codeigniter. We need to download the PayPal payment gateway library for Codeigniter. And place this library into your Codeigniter project.

Explanation of the following few methods:

  1. index() this method would fetch and display the product information.
  2. buyProudct() this method helps to generate the PayPal form and redirect buyer to the PayPal site once the Buy button is clicked.
  3. success() this method will be used to display transaction data after successful payment.
  4. cancel() this method is requested when the user cancels the payment.
  5. ipn() this method receives transaction data by PayPal IPN and inserts the transaction data into the database.

source code

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Products extends CI_Controller
    function  __construct() {
    function index(){
        $data = array();
        //get products inforamtion from database table
        $data['products'] = $this->product->getRows();
        //loav view and pass the products information to view
        $this->load->view('products/index', $data);
    function buyProduct($id){
        //Set variables for paypal form
        $returnURL = base_url().'paypal/success'; //payment success url
        $cancelURL = base_url().'paypal/cancel'; //payment cancel url
        $notifyURL = base_url().'paypal/ipn'; //ipn url
        //get particular product data
        $product = $this->product->getRows($id);
        $userID = 1; //current user id
        $logo = base_url().'Your_logo_url';
        $this->paypal_lib->add_field('return', $returnURL);
        $this->paypal_lib->add_field('cancel_return', $cancelURL);
        $this->paypal_lib->add_field('notify_url', $notifyURL);
        $this->paypal_lib->add_field('item_name', $product['name']);
        $this->paypal_lib->add_field('custom', $userID);
        $this->paypal_lib->add_field('item_number',  $product['id']);
        $this->paypal_lib->add_field('amount',  $product['price']);        
    function success(){
        //get the transaction data
        $paypalInfo = $this->input->get();
        $data['item_number'] = $paypalInfo['item_number']; 
        $data['txn_id'] = $paypalInfo["tx"];
        $data['payment_amt'] = $paypalInfo["amt"];
        $data['currency_code'] = $paypalInfo["cc"];
        $data['status'] = $paypalInfo["st"];
        //pass the transaction data to view
        $this->load->view('paypal/success', $data);
    function cancel(){
        //if transaction cancelled
    function ipn(){
        //paypal return transaction details array
        $paypalInfo    = $this->input->post();
        $data['user_id'] = $paypalInfo['custom'];
        $data['product_id']    = $paypalInfo["item_number"];
        $data['txn_id']    = $paypalInfo["txn_id"];
        $data['payment_gross'] = $paypalInfo["mc_gross"];
        $data['currency_code'] = $paypalInfo["mc_currency"];
        $data['payer_email'] = $paypalInfo["payer_email"];
        $data['payment_status']    = $paypalInfo["payment_status"];
        $paypalURL = $this->paypal_lib->paypal_url;        
        $result    = $this->paypal_lib->curlPost($paypalURL,$paypalInfo);
        //check whether the payment is verified
            //insert the transaction data into the database
Enter fullscreen mode Exit fullscreen mode

Read More:


Editor guide