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
    • system
    • index.php
    • vendor
    • assets
      • css
        • style.css
      • js
        • custom.css
Step-2: Create MySQL Database and Table
1- product table
2- orders table
Open application/config/autoload.php file and add/update code like as bellow:
Step-3: Define constants
Update file named constants.php inside “application/config/” folder.
Open application/config/database.php file and add constants like as bellow:
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.
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 details
  • callback() – charges process.
  • success() – success information .
Step 6: Create a model file
Create a model file named Payment_model.php” inside “application/models” folder.
Open application/config/routes.php file and add code like as bellow:
Step-7: 2Checkout Token and Validate Card with 2Checkout.js:
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.
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.
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.
Step 11: Create a view(index)
Create a view file named index.php inside “application/views/2checkout” folder.
Step 12: Create a view(checkout)
Create a view file named checkout.php inside “application/views/2checkout” folder.
Step 13: Create a view(success)
Create a view file named success.php inside “application/views/2checkout” folder.