PayPal Express Checkout Payment Gateway Integration with Codeigniter
In this tutorial, We have share how to integrate PayPal Express Checkout payment gateway in Codeigniter. The tutorial covered in easy steps with a live example.PayPal is a widely used payment gateway that allows you to accept payment online in the web application. Express Checkout is a fast and very easy way for buyers to pay for the product they are buying.PayPal Express Checkout payment gateway is very useful when you want to provide a user-friendly user interface to the user for the online payment.
Step 1: Create PayPal Account
First we need to create sandbox account on Sandbox account to run this demo example.
Step 2: Update PayPal Config Details
Here in this example, we will use Test App to integrate PayPal Express gateway. So we will update constants.php with KeyID and Secret Key from PayPal.
Step 3: Open file constants
Open “application/config/constants.php” file and add code like as bellow:
1 2 3 4 5 6 7 8 9 |
<?php // TEST Paypal Express credential define('PRO_PAYPAL', 0); define("PAYPAL_CLIENTID", "XXXXXXXXXX"); define("PAYPAL_SECRET", "XXXXXXXXXX"); define("PAYPAL_BASE_URL", "https://api.sandbox.paypal.com/v1/"); define("PAYPAL_ENV", "sandbox"); define('CURRENCY', 'USD'); ?> |
Step 4: Create a Controller file PayPal Express
Create a controller file named “Paypal.php” inside “application/controllers” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * @package PayPal Express : CodeIgniter PayPal Express Gateway * * @author TechArise Team * * @email info@techarise.com * * Description of PayPal Express Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Paypal extends CI_Controller { // construct public function __construct() { parent::__construct(); $this->load->model('Site', 'site'); } // index page public function index() { $data['title'] = 'Paypal | TechArise'; $data['productInfo'] = $this->site->getProduct(); $this->load->view('paypal/index', $data); } // checkout page public function checkout($id) { $data['title'] = 'Checkout payment | TechArise'; $this->site->setProductID($id); $data['itemInfo'] = $this->site->getProductDetails(); $data['return_url'] = site_url().'paypal/callback'; $data['surl'] = site_url().'paypal/success';; $data['furl'] = site_url().'paypal/failed';; $data['currency_code'] = 'INR'; $this->load->view('paypal/checkout', $data); } // success method public function callback() { $data['title'] = 'Paypal Success | TechArise'; $paymentID = $this->input->post('paymentID'); $payerID = $this->input->post('payerID'); $token = $this->input->post('token'); $pid = $this->input->post('pid'); if(!empty($paymentID) && !empty($payerID) && !empty($token) && !empty($pid) ){ $data['paymentID'] = $paymentID; $data['payerID'] = $payerID; $data['token'] = $token; $data['pid'] = $pid; $this->load->view('paypal/success', $data); } else { $this->load->view('paypal/failed', $data); } } } ?> |
Step 5: Create a view file index
Create a view file named “index.php” inside “application/views/paypal” folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php $this->load->view('templates/header'); ?> <div class="row"> <div class="col-lg-12"> <h2>Paypal Express Checkout Integration with Codeigniter</h2> </div> </div><!-- /.row --> <div class="row"> <?php foreach($productInfo as $key=>$element) { ?> <div class="col-lg-6 col-md-6 mb-6"> <div class="card h-100"> <a href="#"><img src="<?php print $element['image'];?>" alt="product 10" title="product 10" class="card-img-top"></a> <div class="card-body"> <h4 class="card-title"> <a href="#"><?php print $element['name'];?></a> </h4> <h5>₹<?php print $element['price'];?></h5> <p class="card-text"><?php print $element['description'];?></p> </div> <div class="card-footer text-right"> <a href="<?php site_url()?>checkout/<?php print $element['product_id'];?>" class="add-to-cart btn-success btn btn-sm" data-productid="<?php print $element['product_id'];?>" title="Add to Cart"><i class="fa fa-shopping-cart fa-fw"></i> Buy Now</a> </div> </div> </div> <?php } ?> </div> <?php $this->load->view('templates/footer'); ?> |
Step 6: Create a view file checkout
Create a view file named “checkout.php” inside “application/views/paypal” folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<?php $this->load->view('templates/header'); ?> <div class="row"> <div class="col-lg-12"> <h2>Paypal Express Checkout Integration with Codeigniter</h2> </div> </div><!-- /.row --> <?php $amount = $itemInfo['price']; $merchant_order_id = $itemInfo['product_id']; $return_url = site_url().'paypal/callback'; ?> <div class="row"> <div class="col-lg-12"> <?php if(!empty($this->session->flashdata('msg'))){ ?> <div class="alert alert-success"> <?php echo $this->session->flashdata('msg'); ?> </div> <?php } ?> <?php if(validation_errors()) { ?> <div class="alert alert-danger"> <?php echo validation_errors(); ?> </div> <?php } ?> </div> </div> <div class="row"> <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"> <table class="table table-bordered table-hover table-striped print-table order-table" style="font-size:11px;"> <thead class="bg-primary"> <tr> <th width="15%" class="text-left" style="vertical-align: inherit">Image</th> <th width="30%" class="text-left" style="vertical-align: inherit">Name</th> <th width="15%" class="text-left" style="vertical-align: inherit">Price</th> <th width="15%" class="text-right" style="vertical-align: inherit">Qty</th> <th width="15%" class="text-right" style="vertical-align: inherit">Sub Total</th> </tr> </thead> <tbody> <tr> <td class="text-left"><img width="80" height="80" src="<?php print $itemInfo['image'];?>"></td> <td class="text-left"><?php print $itemInfo['name'];?></td> <td class="text-left"><?php print $itemInfo['price'];?></td> <td class="text-right">1</td> <td class="text-right"><?php print $itemInfo['price'];?></td> </tr> </tbody> </table> </div> </div> <div class="row"> <div class="col-lg-12 text-right"> <div id="paypal-button-container"></div> <div id="paypal-button"></div> <script src="https://www.paypalobjects.com/api/checkout.js"></script> <script> paypal.Button.render({ env: '<?php echo PAYPAL_ENV; ?>', client: { <?php if(PRO_PAYPAL) { ?> production: '<?php echo PAYPAL_CLIENTID; ?>' <?php } else { ?> sandbox: '<?php echo PAYPAL_CLIENTID; ?>' <?php } ?> }, payment: function (data, actions) { return actions.payment.create({ transactions: [{ amount: { total: '<?php echo $itemInfo['price']; ?>', currency: '<?php echo CURRENCY; ?>' } }] }); }, onAuthorize: function (data, actions) { return actions.payment.execute() .then(function () { window.location = "<?php echo PAYPAL_BASE_URL .$return_url;?>?paymentID="+data.paymentID+"&payerID="+data.payerID+"&token="+data.paymentToken+"&pid=<?php echo $merchant_order_id; ?>"; }); } }, '#paypal-button'); </script> </div> </div> <?php $this->load->view('templates/footer'); ?> |
Step 7: Open file constants
Open “application/config/routes.php” file and add code like as bellow:
1 2 3 4 5 |
<?php // routes $route['default_controller'] = 'paypal/index'; $route['checkout/(:any)'] = "paypal/checkout/$1"; ?> |