In this tutorial, we will tell you how to integration paypal payment gateway in codeigniter Framework.
The Paypal payment gateway is a popular payment gateway method and it is easily used for the project. so many developers prefer that payment gateway method.
PayPal is a secure payment method and it’s work-based using API. it provides two environments for the user like a sandbox and live.
Overview
Step 1: Download Paypal Payment Gateway Library
Step 2: Create Database Tables
Step 3: Create Controller
Step 4: Create a Model
Step 5: Create View File
Step 1: Download Paypal Payment Gateway Library
If you want to download the PayPal payment Gateway library then you can download it on https://github.com/xpertphp/paypal-payment-gateway-library-for-codeigniter git repository.
Step 2: Create Database Tables
In this step, we have to create two tables in the database, so we will create a database using the below code.
CREATE TABLE `products` ( `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;
CREATE TABLE `payments` ( `payment_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `product_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 Controller
In this step, we will create a Products.php file in the “application/controller” directory and paste the below code in this controller.
application/controller/Products.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('product'); $this->load->database(); } function index(){ $data = array(); //get products inforamtion from database table $data['products'] = $this->product->getProducts(); //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 $failURL = base_url().'paypal/fail'; //payment fail url $notifyURL = base_url().'paypal/ipn'; //ipn url //get particular product data $product = $this->product->getProducts($id); $userID = 1; //current user 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', $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']); $this->paypal_lib->image($logo); $this->paypal_lib->paypal_auto_form(); } function paymentSuccess(){ //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/paymentSuccess', $data); } function paymentFail(){ //if transaction cancelled $this->load->view('paypal/paymentFail'); } 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 if(preg_match("/VERIFIED/i",$result)){ //insert the transaction data into the database $this->product->storeTransaction($data); } } } ?>
Step 4: Create a Model
In this step, we will create a Product.php file in the “application/models” directory and paste the below code in this model.
application/models/Product.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Product extends CI_Model{ public function __construct() { $this->load->database(); } //get and return product rows public function getProducts($id = ''){ $this->db->select('id,name,image,price'); $this->db->from('products'); 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 !empty($result)?$result:false; } //insert transaction data public function storeTransaction($data = array()){ $insert = $this->db->insert('payments',$data); return $insert?true:false; } } ?>
Step 5: Create View File
So finally, we need to create two directories in the “application/views/” directory like product and PayPal. so we will create that directory and create files in that directory.
application/views/products/index.php
<!DOCTYPE html> <html> <head> <title>How To Integration Paypal Payment Gateway In Codeigniter - XpertPhp</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($products)): foreach($products as $product): ?> <div class="thumbnail"> <img src="<?php echo base_url().'assets/images/'.$product['image']; ?>" alt=""> <div class="caption"> <h4 class="pull-right">$<?php echo $product['price']; ?></h4> <h4><a href="javascript:void(0);"><?php echo $product['name']; ?></a></h4> </div> <a href="<?php echo base_url().'products/buyProduct/'.$product['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 - XpertPhp</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">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 - XpertPhp</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">Transaction Detalis</h2> <div class="row"> <p>Sorry! Your last transaction was cancelled.</p> </div> </div> </body> </html>
Note:- Paypal Payment Gateway Live
If you want to change the sandbox to live then you can follow the below details.
now we will open the “application/config/paypallib_config.php” file and change the sandbox, and business email, pin the config file. See the below changes in a config file.
//Change the SANDBOX environment to FALSE $config['sandbox'] = FALSE; //Change the BUSINESS EMAIL $config['business'] = 'live_business@email.com';