Wednesday 17 October 2018

PayPal Payment Gateway Integration in CodeIgniter

https://youtu.be/ItiSUco29hk

view
 
<div class="col-lg-12">
<!-- List all products -->
<?php if(!empty($products)){ foreach($products as $row){ ?>
    <div class="col-sm-4 col-lg-4 col-md-4">
        <div class="thumbnail">
            <img src="<?php echo base_url('assets/images/'.$row['image']); ?>" />
            <div class="caption">
                <h4 class="pull-right">$<?php echo $row['price']; ?> USD</h4>
                <h4><a href="javascript:void(0);"><?php echo $row['name']; ?></a></h4>
                <p>See more snippets like this online store item at <a href="http://www.codexworld.com">CodexWorld</a>.</p>
            </div>
            <div class="ratings">
                <a href="<?php echo base_url('products/buy/'.$row['id']); ?>">
                    <img src="<?php echo base_url('assets/images/x-click-but01.gif'); ?>" />
                </a>
                <p class="pull-right">15 reviews</p>
                <p>
                    <span class="glyphicon glyphicon-star"></span>
                    <span class="glyphicon glyphicon-star"></span>
                    <span class="glyphicon glyphicon-star"></span>
                    <span class="glyphicon glyphicon-star"></span>
                    <span class="glyphicon glyphicon-star"></span>
                </p>
            </div>
        </div>
    </div>
<?php } }else{ ?>
    <p>Product(s) not found...</p>
<?php ?>
</div>
 
  
controller
 
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Products extends CI_Controller{
    
    function  __construct(){
        parent::__construct();
        
        // Load paypal library & product model
        $this->load->library('paypal_lib');
        $this->load->model('product');
    }
    
    function index(){
        $data = array();
        
        // Get products data from the database
        $data['products'] = $this->product->getRows();
        
        // Pass products data to the view
        $this->load->view('products/index'$data);
    }
    
    function buy($id){
        // Set variables for paypal form
        $returnURL base_url().'paypal/success';
        $cancelURL base_url().'paypal/cancel';
        $notifyURL base_url().'paypal/ipn';
        
        // Get product data from the database
        $product $this->product->getRows($id);
        
        // Get current user ID from the session
        $userID $_SESSION['userID'];
        
        // Add fields to paypal form
        $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']);
        
        // Render paypal form
        $this->paypal_lib->paypal_auto_form();
    }
} 

model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Model{
    
    function __construct() {
        $this->proTable   'products';
        $this->transTable 'payments';
    }
    
    /*
     * Fetch products data from the database
     * @param id returns a single record if specified, otherwise all records
     */
    public function getRows($id ''){
        $this->db->select('*');
        $this->db->from($this->proTable);
        $this->db->where('status''1');
        if($id){
            $this->db->where('id'$id);
            $query  $this->db->get();
            $result $query->row_array();
        }else{
            $this->db->order_by('name''asc');
            $query  $this->db->get();
            $result $query->result_array();
        }
        
        // return fetched data
        return !empty($result)?$result:false;
    }
    
    /*
     * Insert data in the database
     * @param data array
     */
    public function insertTransaction($data){
        $insert $this->db->insert($this->transTable,$data);
        return $insert?true:false;
    }
    
}

0 comments:

Post a Comment