2Checkout Payment Gateway Integration in Codeigniter
2Checkout Payment Gateway API allows you to accept payment credit cards or debit card on your web application. 2Checkout PHP library helps to connect the Payment API, create a charge against credit card or debit card and payment process. In this article, I will explain you step by step process to integrate 2Checkout Payment Gateway in PHP to accept online payment.
Follow some basic Steps 2Checkout payment gateway
- Create a Form to fill credit card information.
- 2Checkout library to validate card details
- Submit the form with card details and card and process charges
- Update details in database with payment status
Step-1: Create a 2Checkout account and get API keys
- Create a 2Checkout account and login to the dashboard.
- Click user-icon get Seller ID (Account Number)
- Click to the API page.
- There is two type of API keys named
Publishable Key
,Private Key
- Account number (
Seller ID
)
Publishable Key
and Private Key
Before started to implement the 2Checkout Payment Gateway Integration in Codeigniter, look files structure:
- 2checkout-payment-gateway-integration-in-codeigniter
- application
- config
- autoload.php
- database.php
- constants.php
- routes.php
- controllers
- TwoCheckoutPayment.php
- models
- Payment_model.php
- third_party
- 2checkout
- libraries
- TwoCheckoutApi.php
- views
- 2checkout
- index.php
- checkout.php
- success.php
- templates
- header.php
- footer.php
- 2checkout
- config
- system
- index.php
- vendor
- assets
- css
- style.css
- js
- custom.css
- application
Step-2: Create MySQL Database and Table
1- product table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `description` text NOT NULL, `image` varchar(255) NOT NULL, `price` float(10,2) NOT NULL, `status` int(1) NOT NULL, PRIMARY KEY (`product_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; INSERT INTO `product` VALUES (1,'Product One','Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy','http://placehold.it/700x400',10.00,1), (2,'Product Two','Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy','http://placehold.it/700x400',20.00,1), (3,'Product Three','Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy','http://placehold.it/700x400',30.00,1), (4,'Product Four','Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy','http://placehold.it/700x400',40.00,1); |
2- orders table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php CREATE TABLE `orders` ( `id` int(11) NOT NULL AUTO_INCREMENT, `transaction_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `address` text COLLATE utf8_unicode_ci, `product_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `product_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `product_price` float(10,2) NOT NULL, `total` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `currency` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `created_date` varchar(12) COLLATE utf8_unicode_ci NOT NULL, `modified_date` varchar(12) COLLATE utf8_unicode_ci NOT NULL, `status` varchar(25) COLLATE utf8_unicode_ci NOT NULL COMMENT '0=Failed, 1=Success', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ?> |
Open
application/config/autoload.php
file and add/update code like as bellow:
1 2 3 4 5 |
<?php // load library and helper $autoload['libraries'] = array('database', 'session', 'table', 'upload'); $autoload['helper'] = array('url', 'form', 'html', 'date', 'text'); ?> |
Step-3: Define constants
Update file named constants.php inside “application/config/” 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 |
<?php $root = "http://" . $_SERVER['HTTP_HOST']; $currentDir = str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']); $root .= $currentDir; $constants['base_url'] = $root; define('DB_HOSTNAME', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', 'root'); define('DB_NAME', 'demo_DB'); // windows path //define('BASH_PATH', 'C:/xampp/htdocs'.$currentDir); // Ubuntu path define('BASH_PATH', '/var/www'.$currentDir); // Mac Path //define('BASH_PATH', '/Applications/XAMPP/htdocs'.$currentDir); define('HTTP_CSS_PATH', $constants['base_url'] . 'assets/css/'); define('HTTP_JS_PATH', $constants['base_url'] . 'assets/js/'); define('HTTP_IMAGE_PATH', $constants['base_url'] . 'assets/images/'); define('TWOCHECKOUT_SELLER_ID', 'xxxxxxxxxxx'); define('TWOCHECKOUT_PUBLISHABLE_KEY', 'XXXXXXXXXXXXXXX'); define('TWOCHECKOUT_PRIVATE_KEY', 'XXXXXXXXXXXXXXXXX'); define('CURRENCY_CODE', 'usd'); ?> |
Open
application/config/database.php
file and add constants like as bellow:
1 2 3 4 5 6 7 |
<?php // database credentials 'hostname' => DB_HOSTNAME, 'username' => DB_USERNAME, 'password' => DB_PASSWORD, 'database' => DB_NAME, ?> |
Download 2Checkout PHP library
The 2Checkout PHP library is used to process the card transaction using Payment API. All the library files copy and paste
application/third_party
folder.Step-4: Create file (TwoCheckoutApi)
Create a file named TwoCheckoutApi.php inside “application/libraries” 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 |
<?php /* * *** * Version: 1.0.0 * * Description of 2Checkout Payment Gateway Library * * @package: CodeIgniter * @category: Libraries * @author TechArise Team * @email info@techarise.com * * *** */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class TwoCheckoutApi{ var $CI; var $api_error; function __construct(){ $this->api_error = ''; $this->CI =& get_instance(); // Include the 2Checkout library require APPPATH .'third_party/2checkout/Twocheckout.php'; // Set API key Twocheckout::sellerId(TWOCHECKOUT_SELLER_ID); Twocheckout::privateKey(TWOCHECKOUT_PRIVATE_KEY); Twocheckout::sandbox(true); } // create Charge method function createCharge($merchantOrderID, $token, $amount, $billingArr=array()){ // corrency code $currency = CURRENCY_CODE; try { // Charge a credit or a debit card $charge = Twocheckout_Charge::auth(array( "merchantOrderId" => $merchantOrderID, "token" => $token, "currency" => $this->CI->config->item('currency_code'), "total" => $amount, "billingAddr" => array( "name" => $billingArr['name'], "addrLine1" => $billingArr['addrLine'], "city" => $billingArr['city'], "state" => $billingArr['state'], "zipCode" => $billingArr['zipCode'], "country" => $billingArr['country'], "email" => $billingArr['email'], "phoneNumber" => $billingArr['phoneNumber'] ) )); // Retrieve charge information return $charge; }catch(Exception $e) { $this->api_error = $e->getMessage(); return false; } } } ?> |
Step-5: Create a controller file
Create a controller file named TwoCheckoutPayment.php inside “application/controllers” folder.
- The TwoCheckoutPayment controller handles the calendar process.
__construct()
– Loads the required library, helper and model.index()
– load index product list.checkout()
– load checkout form and card detailscallback()
– charges process.success()
– success information .
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php /* * *** * Version: 1.0.0 * * Description of Payment Controller * * @author TechArise Team * * @email info@techarise.com * * *** */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class TwoCheckoutPayment extends CI_Controller { public function __construct() { parent::__construct(); //load library, models etc. $this->load->model('Payment_model', 'payment'); $this->load->library('TwoCheckoutApi'); } // index method public function index() { $data = array(); $data['metaDescription'] = '2Checkout Payment Gateway Integration in Codeigniter'; $data['metaKeywords'] = '2Checkout Payment Gateway Integration in Codeigniter'; $data['title'] = "2Checkout Payment Gateway Integration in Codeigniter - TechArise"; $data['breadcrumbs'] = array('2Checkout Payment Gateway' => '#'); $data['productInfo'] = $this->payment->getProduct(); $this->load->view('2checkout/index', $data); } // checkout page public function checkout($id) { $data = array(); $data['metaDescription'] = '2Checkout Payment Gateway Integration in Codeigniter'; $data['metaKeywords'] = '2Checkout Payment Gateway Integration in Codeigniter'; $data['title'] = "2Checkout Payment Gateway Integration in Codeigniter - TechArise"; $this->payment->setProductID($id); $data['itemInfo'] = $this->payment->getProductDetails(); $this->load->view('2checkout/checkout', $data); } // callback public function callback() { $data = array(); if(!empty($this->input->post('token'))) { $product_id = $this->input->post('product_id'); $product_name = $this->input->post('product_name'); $addlineArr = array( 'name' => $this->input->post('2checkout_name'), 'email' => $this->input->post('2checkout_email'), 'phoneNumber' => '900-000-0001', 'addrLine' => '123 Main Street', 'city' => 'Townsville', 'state' => 'Ohio', 'zipCode' => '43206', 'country' => 'USA', ); $merchantOrderID = strtolower(str_replace('.','',uniqid('', true))); $amount = $this->input->post('amount'); $token = $this->input->post('token'); try { // Charge params $charge = $this->twocheckoutapi->createCharge($merchantOrderID, $token, $amount,$addlineArr); // Check whether the charge is successful if ($charge['response']['responseCode'] == 'APPROVED') { // Order details $orderNumber = $charge['response']['orderNumber']; $total = $charge['response']['total']; $transactionID = $charge['response']['transactionId']; $currency = $charge['response']['currencyCode']; $status = $charge['response']['responseCode']; // Insert order info to database $this->payment->setTransactionID($transactionID); $this->payment->setProductID($product_id); $this->payment->setProductName($product_name); $this->payment->setName($addlineArr['name']); $this->payment->setEmail($addlineArr['email']); $this->payment->setAddress($addlineArr['addrLine']); $this->payment->setPrice($amount); $this->payment->setTotal($amount); $this->payment->setCurrency($currency); $this->payment->setCreatedDate(time()); $this->payment->setModifiedDate(time()); $this->payment->setStatus($status); $orderID = $this->payment->createOrder(); $statusMsg = '<h2>Thanks for your Order!</h2>'; $statusMsg .= '<h4>The transaction was successful. Order details are given below:</h4>'; $statusMsg .= "<p>Order ID: {$merchantOrderID}</p>"; $statusMsg .= "<p>Order Number: {$orderNumber}</p>"; $statusMsg .= "<p>Transaction ID: {$transactionID}</p>"; $statusMsg .= "<p>Order Total: {$total} {$currency}</p>"; } } catch (Twocheckout_Error $e) { $statusMsg = '<h2>Transaction failed!</h2>'; $statusMsg .= '<p>'.$e->getMessage().'</p>'; } } else { $statusMsg = "Error on form submission."; } echo $statusMsg; $this->session->set_flashdata('paymentInfo', $statusMsg); redirect('2checkout/success'); } // success public function success() { $data = array(); $data['metaDescription'] = '2Checkout Payment Gateway Integration in Codeigniter'; $data['metaKeywords'] = '2Checkout Payment Gateway Integration in Codeigniter'; $data['title'] = "2Checkout Payment Gateway Integration in Codeigniter - TechArise"; $data['msg'] = $this->session->flashdata('paymentInfo'); $this->load->view('2checkout/success', $data); } } ?> |
Step 6: Create a model file
Create a model file named Payment_model.php” inside “application/models” 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 |
<?php /** * @package 2Checkout : CodeIgniter 2Checkout Gateway Model * * @author TechArise Team * * @email info@techarise.com * * Description of Contact Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Payment_model extends CI_Model { private $_productID; private $_transactionID; private $_name; private $_email; private $_address; private $_productName; private $_price; private $_total; private $_currency; private $_createdDate; private $_modifiedDate; private $_status; public function setProductID($productID) { $this->_productID = $productID; } public function setTransactionID($transactionID) { $this->_transactionID = $transactionID; } public function setName($name) { $this->_name = $name; } public function setEmail($email) { $this->_email = $email; } public function setAddress($address) { $this->_address = $address; } public function setProductName($productName) { $this->_productName = $productName; } public function setPrice($price) { $this->_price = $price; } public function setTotal($total) { $this->_total = $total; } public function setCurrency($currency) { $this->_currency = $currency; } public function setCreatedDate($createdDate) { $this->_createdDate = $createdDate; } public function setModifiedDate($modifiedDate) { $this->_modifiedDate = $modifiedDate; } public function setStatus($status) { $this->_status = $status; } public function getProduct() { $this->db->select(array('p.product_id', 'p.name', 'p.description', 'p.price', 'p.image')); $this->db->from('product as p'); $query = $this->db->get(); return $query->result_array(); } // getProduct Details public function getProductDetails() { $this->db->select(array('p.product_id', 'p.name', 'p.description', 'p.price', 'p.image')); $this->db->from('product as p'); $this->db->where('p.product_id', $this->_productID); $query = $this->db->get(); return $query->row_array(); } // create Order public function createOrder() { $data = array( 'transaction_id' => $this->_transactionID, 'name' => $this->_name, 'email' => $this->_email, 'address' => $this->_address, 'product_id' => $this->_productID, 'product_name' => $this->_productName, 'product_price' => $this->_price, 'total' => $this->_total, 'currency' => $this->_currency, 'created_date' => $this->_createdDate, 'modified_date' => $this->_modifiedDate, 'status' => $this->_status, ); $this->db->insert('orders', $data); return $last_id = $this->db->insert_id(); } } ?> |
Open
application/config/routes.php
file and add code like as bellow:
1 2 3 4 5 6 |
<?php $route['default_controller'] = 'TwoCheckoutPayment/index'; $route['2checkout/callback'] = 'TwoCheckoutPayment/callback'; $route['2checkout/checkout/(:any)'] = 'TwoCheckoutPayment/checkout/$1'; $route['2checkout/success'] = 'TwoCheckoutPayment/success'; ?> |
Step-7: 2Checkout Token and Validate Card with 2Checkout.js:
1 |
<script type="text/javascript" src="https://www.2checkout.com/checkout/api/2co.min.js"></script> |
Step-8: The following JavaScript code is used to generate token with 2Checkout library.
- Set publishable API key that identifies.
- The successCallback() and tokenRequest() methods creates a single-use token.
- Card details are submitted to the server-side. the success/error message.
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 |
<script> // Called when token created successfully. var successCallback = function(data) { var myForm = document.getElementById('myCCForm'); // Set the token as the value for the token input myForm.token.value = data.response.token.token; // IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop. myForm.submit(); }; // Called when token creation fails. var errorCallback = function(data) { if (data.errorCode === 200) { tokenRequest(); } else { alert(data.errorMsg); } }; var tokenRequest = function() { // Setup token request arguments var args = { sellerId: "<?php print TWOCHECKOUT_SELLER_ID; ?>", publishableKey: "<?php print TWOCHECKOUT_PUBLISHABLE_KEY;?>", ccNo: $("#ccNo").val(), cvv: $("#cvv").val(), expMonth: $("#expMonth").val(), expYear: $("#expYear").val() }; // Make the token request TCO.requestToken(successCallback, errorCallback, args); }; $(function() { // Pull in the public encryption key for our environment TCO.loadPubKey('sandbox'); $("#myCCForm").submit(function(e) { // Call our token request function tokenRequest(); // Prevent form from submitting return false; }); }); </script> |
Step 9: Create a view(header)
Create a view file named header.php inside “application/views/templates” folder
This view contains the header section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header section.
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 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title><?php print $title; ?></title> <link rel="icon" type="image/ico" href="<?php print HTTP_IMAGE_PATH; ?>favicon.ico"> <!-- Bootstrap core CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" /> <!-- Custom fonts for this template --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" /> <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="<?php print HTTP_CSS_PATH; ?>style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;"> <div class="container"> <a class="navbar-brand font-weight-bold" href="https://techarise.com"><h1>Tech Arise</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="https://techarise.com">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://techarise.com/php-free-script-demos/">Live Demo</a> </li> </ul> </div> </div> </nav> |
Step 10: Create a view(footer)
Create a view file named footer.php inside “application/views/templates” folder.
This view contains the footer section of the webpage.
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 |
<!-- Footer --> <footer class="footer bg-light footer-bg-dark"> <div class="container"> <div class="row"> <div class="col-lg-6 h-100 text-center text-lg-left my-auto"> <ul class="list-inline mb-2"> <li class="list-inline-item"> <a href="#">About</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Contact</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> </ul> <p class="text-muted small mb-4 mb-lg-0">Copyright © 2011 - <?php print date('Y', time());?> <a href="https://techarise.com/">TECHARISE.COM</a> All rights reserved.</p> </div> <div class="col-lg-6 h-100 text-center text-lg-right my-auto"> <ul class="list-inline mb-0"> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-facebook fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-twitter-square fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item"> <a href="#"> <i class="fab fa-instagram fa-2x fa-fw"></i> </a> </li> </ul> </div> </div> </div> </footer> <script> var baseurl = "<?php print site_url();?>"; </script> <!-- Bootstrap core JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bs-custom-file-input/dist/bs-custom-file-input.js"></script> <script type="text/javascript" src="https://www.2checkout.com/checkout/api/2co.min.js"></script> <script> // Called when token created successfully. var successCallback = function(data) { var myForm = document.getElementById('myCCForm'); // Set the token as the value for the token input myForm.token.value = data.response.token.token; // IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop. myForm.submit(); }; // Called when token creation fails. var errorCallback = function(data) { if (data.errorCode === 200) { tokenRequest(); } else { alert(data.errorMsg); } }; var tokenRequest = function() { // Setup token request arguments var args = { sellerId: "<?php print TWOCHECKOUT_SELLER_ID; ?>", publishableKey: "<?php print TWOCHECKOUT_PUBLISHABLE_KEY;?>", ccNo: $("#ccNo").val(), cvv: $("#cvv").val(), expMonth: $("#expMonth").val(), expYear: $("#expYear").val() }; // Make the token request TCO.requestToken(successCallback, errorCallback, args); }; $(function() { // Pull in the public encryption key for our environment TCO.loadPubKey('sandbox'); $("#myCCForm").submit(function(e) { // Call our token request function tokenRequest(); // Prevent form from submitting return false; }); }); </script> <script src="<?php print HTTP_JS_PATH; ?>custom.js"></script> </body> </html> |
Step 11: Create a view(index)
Create a view file named index.php inside “application/views/2checkout” 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 |
<?php $this->load->view('templates/header');?> <!-- container --> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>2Checkout Payment Gateway Integration in Codeigniter</h2> </div> <span id="success-msg" class="payment-errors"></span> <div class="row"> <?php foreach($productInfo as $key=>$element) { ?> <div class="col-lg-3 col-md-3 mb-3"> <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()?>2checkout/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> </div> </section> <?php $this->load->view('templates/footer');?> |
Step 12: Create a view(checkout)
Create a view file named checkout.php inside “application/views/2checkout” 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 |
<?php $this->load->view('templates/header');?> <!-- container --> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>2Checkout Payment Gateway Integration in Codeigniter</h2> </div> <span id="success-msg" class="payment-errors"></span> <!-- 2Checkout payment form --> <form action="<?php print site_url(); ?>2checkout/callback" method="POST" id="myCCForm"> <input id="token" name="token" type="hidden" value=""> <input type="hidden" name="amount" value="<?php print number_format($itemInfo['price']);?>"> <input type="hidden" name="product_name" value="<?php print $itemInfo['name'];?>"> <input type="hidden" name="product_id" value="<?php print $itemInfo['product_id'];?>"> <div class="row justify-content-center"> <div class="col-12 col-md-8 col-lg-6 pb-5"> <div class="row"></div> <!--Form with header--> <div class="card border-gray rounded-0"> <div class="card-header p-0"> <div class="bg-gray text-left py-2"> <h5 class="pl-2"><strong>Item Name: </strong><?php print $itemInfo['name'];?></h5> <h6 class="pl-2"><strong>Price: </strong> $<?php print number_format($itemInfo['price']);?> USD</h6> </div> </div> <div class="card-body p-3"> <!--Body--> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text">Full Name</div> </div> <input type="text" class="form-control" id="2checkout-name" name="2checkout_name" placeholder="Full Name *" required> </div> </div> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text">Email</div> </div> <input type="email" class="form-control" id="2checkout-email" name="2checkout_email" placeholder="Email *" required> </div> </div> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text">Card Number</div> </div> <input id="ccNo" type="text" size="20" class="form-control" autocomplete="off" placeholder="1234 5678 9012 3456" required /> </div> </div> <div class="form-row"> <div class="form-group col-md-5"> <input type="text" class="form-control" name="tcheckout_month" placeholder="MM" maxlength="2" id="expMonth" required autocomplete="off"/> </div> <div class="form-group col-md-5"> <input type="text" class="form-control" name="tcheckout_year" placeholder="YYYY" maxlength="4" id="expYear" autocomplete="off" required /> </div> <div class="form-group col-md-2"> <input id="cvv" maxlength="4" type="text" class="form-control" name="cvc" placeholder="CVC" autocomplete="off" required /> </div> </div> <div class="text-right"> <a href="<?php print site_url();?>" id="payBtn" class="btn btn-primary py-2">Back</a> <button type="buttom" id="payBtn" class="btn btn-info py-2">Pay</button> </div> </div> </div> <div> </div> </div> </div> </form> </div> </section> <?php $this->load->view('templates/footer');?> |
Step 13: Create a view(success)
Create a view file named success.php inside “application/views/2checkout” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $this->load->view('templates/header');?> <!-- container --> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>2Checkout Payment Gateway Integration in Codeigniter</h2> </div> <div class="text-xs-center"> <?php if(!empty($msg)) { echo $msg;}?> </div> </div> </section> <?php $this->load->view('templates/footer');?> |