Codeigniter Paypal Integration Example

Today, We want to share with you paypal integration in codeigniter.In this post we will show you payfast payment gateway integration in codeigniter, hear for razorpay payment gateway integration in codeigniter we will give you demo and example for implement.In this post, we will learn about Paypal Payment Gateway Integration In Php with an example.

Next PayPal Payment Gateway Integration in CodeIgniter

Step 1 : Download Paypal Payment Gateway Library

https://github.com/tutsmake/paypal-payment-gateway-library-for-codeigniter

paypal_lib.php file => will be placed in the application/libraries/ directory
paypallib_config.php file => will be placed in the application/config/ directory.

Step 2 : Create Database Tables

Step 2: items table

CREATE TABLE `items` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `price` float(10,2) NOT NULL,
 `status` tinyint(1) NOT NULL DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

payments table

CREATE TABLE `payments` (
 `payment_id` int(11) NOT NULL AUTO_INCREMENT,
 `visitor_id` int(11) NOT NULL,
 `item_id` int(11) NOT NULL,
 `txn_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `payment_gross` float(10,2) NOT NULL,
 `currency_code` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
 `payer_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `payment_status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`payment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step 3 : Create New Controller

application/controller/paypalController.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Products extends CI_Controller
{
    function  __construct() {
        parent::__construct();
        $this->load->library('paypal_lib');
        $this->load->model('item');
        $this->load->database();
    }
     
    function index(){
        $response = array();
        //get items inforamtion from database table
        $response['items'] = $this->item->getProducts();
        //loav view and pass the items information to view
        $this->load->view('items/index', $response);
    }
     
    function buyProduct($id){
        //Set variables for paypal form
        $returnURL = base_url().'paypal/success'; //payment success url
        $failURL = base_url().'paypal/fail'; //payment fail url
        $notifyURL = base_url().'paypal/ipn'; //ipn url
        //get particular item response
        $item = $this->item->getProducts($id);
        $visitorID = 1; //current visitor id
        $logo = base_url().'Your_logo_url';
         
        $this->paypal_lib->add_field('return', $returnURL);
        $this->paypal_lib->add_field('fail_return', $failURL);
        $this->paypal_lib->add_field('notify_url', $notifyURL);
        $this->paypal_lib->add_field('item_name', $item['name']);
        $this->paypal_lib->add_field('custom', $visitorID);
        $this->paypal_lib->add_field('item_number',  $item['id']);
        $this->paypal_lib->add_field('amount',  $item['price']);        
        $this->paypal_lib->image($logo);
         
        $this->paypal_lib->paypal_auto_form();
    }
 
     function paymentSuccess(){
 
        //get the transaction data
        $paymentData = $this->input->get();
           
        $response['item_number'] = $paymentData['item_number']; 
        $response['txn_id'] = $paymentData["tx"];
        $response['payment_amt'] = $paymentData["amt"];
        $response['currency_code'] = $paymentData["cc"];
        $response['status'] = $paymentData["st"];
         
        //pass the transaction response to view
        $this->load->view('paypal/paymentSuccess', $response);
     }
      
     function paymentFail(){
        //if transaction cancelled
        $this->load->view('paypal/paymentFail');
     }
      
     function ipn(){
        //paypal return transaction details array
        $paymentData    = $this->input->post();
 
        $response['visitor_id'] = $paymentData['custom'];
        $response['item_id']    = $paymentData["item_number"];
        $response['txn_id']    = $paymentData["txn_id"];
        $response['payment_gross'] = $paymentData["mc_gross"];
        $response['currency_code'] = $paymentData["mc_currency"];
        $response['payer_email'] = $paymentData["payer_email"];
        $response['payment_status']    = $paymentData["payment_status"];
 
        $paypalURL = $this->paypal_lib->paypal_url;        
        $solution    = $this->paypal_lib->curlPost($paypalURL,$paymentData);
         
        if(preg_match("/VERIFIED/i",$solution)){
            $this->item->storeTransaction($response);
        }
    }
}

Step 4 : Create Paypal Model

application/models/Paypal.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Model{
    public function __construct()
    {
        $this->load->database();
    }

    public function getProducts($id = ''){
        $this->db->select('id,name,image,price');
        $this->db->from('items');
        if($id){
            $this->db->where('id',$id);
            $query = $this->db->get();
            $solution = $query->row_array();
        }else{
            $this->db->order_by('name','asc');
            $query = $this->db->get();
            $solution = $query->result_array();
        }
        return !empty($solution)?$solution:false;
    }
 
    public function storeTransaction($response = array()){
        $insert = $this->db->insert('payments',$response);
        return $insert?true:false;
    }
}

Step 5 : Create View

application/views/items/index.php

<!DOCTYPE html>
<html>
<head>
  <title>Codeigniter Paypal Integration Example - www.pakainfo.com</title>
  <!-- Latest CSS -->
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
</head>
<body>
  <div class="container">
    <h2 class="mt-3 mb-3">Products</h2>
    <div class="row">
        <?php if(!empty($items)): foreach($items as $item): ?>
        <div class="thumbnail">
            <img src="<?php echo base_url().'assets/images/'.$item['image']; ?>" alt="">
            <div class="caption">
                <h4 class="pull-right">$<?php echo $item['price']; ?></h4>
                <h4><a href="javascript:void(0);"><?php echo $item['name']; ?></a></h4>
            </div>
            <a href="<?php echo base_url().'items/buyProduct/'.$item['id']; ?>"><img src="<?php echo base_url(); ?>assets/images/buy-button" style="width: 70px;"></a>
        </div>
        <?php endforeach; endif; ?>
    </div>
  </div>
</body>
</html>

application/views/paypal/paymentSuccess.php

<!DOCTYPE html>
<html>
<head>
  <title>Transaction Successfull - Codeigniter Paypal Integration Example - www.pakainfo.com</title>

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
</head>
<body>
  <div class="container">
    <h2 class="mt-3 mb-3">Transaction Detalis</h2>
    <div class="row">
          <span>Your payment was successful done, thank you for purchase.</span><br/>
          <span>Item Number : 
              <strong><?php echo $item_number; ?></strong>
          </span><br/>
          <span>TXN ID : 
              <strong><?php echo $txn_id; ?></strong>
          </span><br/>
          <span>Amount Paid : 
              <strong>$<?php echo $payment_amt.' '.$currency_code; ?></strong>
          </span><br/>
          <span>Payment Status : 
              <strong><?php echo $status; ?></strong>
        </span><br/>
    </div>
  </div>
</body>
</html>

application/views/paypal/paymentFail.php

<!DOCTYPE html>
<html>
<head>
  <title>Transaction Fail - Codeigniter Paypal Integration Example - www.pakainfo.com</title>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
</head>
<body>
  <div class="container">
    <h2 class="mt-3 mb-3">Transaction Detalis</h2>
    <div class="row">
       <p>Sorry! Your last transaction was cancelled.</p>
    </div>
  </div>
</body>
</html>

Note:- Paypal Payment Gateway Live
So Open the application/config/paypallib_config.php and change the following two configuration values

Read Also:  CodeIgniter Stripe Payment Gateway Api Integration

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

$config['sandbox'] = FALSE;

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

$config['business'] = '[email protected]';

I hope you get an idea about stripe payment gateway integration in codeigniter .
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Laravel 6 - Simple Pagination Example Tutorial

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *