Manage Subscription Payments using Razorpay Billing in Codeigniter

Recurring/Subscriptions payments also known as AutoPay are automatic payments to authorize and collect charges immediately, Razorpay Subscriptions provides you the platform to set up and manage recurring payments.
Using Razorpay Subscriptions you can automatically charge customers based on a billing cycle that you control. You can easily create and manage subscriptions and get instant alerts on payment activity as well as the status of subscriptions.
In this tutorial, We have share how to integrate Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter with cURL. This is a very simple example, you can just copy paste, and change according to your requirement.
Step 1: Create Razorpay Account and Create your subscription Plan
First we need to create account on Razorpay and generate KeyId and Secret Key. We will keep created Razorpay account in test mode to test payment functionality.
Before started to implement the Manage Subscription Payments using Razorpay Billing in Codeigniter, look files structure:
  • manage-recurring-payments-using-razorpay-in-codeigniter
    • application
      • config
        • autoload.php
        • database.php
        • constants.php
        • routes.php
      • controllers
        • Subscription.php
      • models
        • Subscription_model.php
      • views
        • subscription
          • index.php
          • pay.php
          • razorpayform.php
          • thankyou.php
        • templates
          • header.php
          • footer.php
    • system
    • index.php
    • assets
      • css
        • style.css
Step 2: Update Razorpay Config Details
Here in this example, we will use Test App to integrate Razorpay gateway. So we will update constants.php with KeyID and Secret Key from Razorpay.
Step 3: Open a file constants
Open “application/config/constants.php” file and define constants:
Step 4: Create the database and Table
For this tutorial, you need a MySQL database with the following table:
Step 5: Create Model
Create a model file named Subscription_model.php inside “application/models” folder.
Step 6: Create controllers
Create a controllers file named Subscription.php inside “application/controllers” folder.
  • The controller handles the Subscription process.
  • __construct() – Loads the required library, helper and model.
  • index() – load package data.
  • payForm() – Submit data.
  • initiateSubscriptions() – initiate Subscriptions.
  • get_curl_handle_subscriptions() – manage cURL request.
  • callbacksubscriptions() – callback subscriptions.
  • createSubscription() – create Subscription.
  • thankyou() – return success payment
Step 7: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 8: 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 9: Create views
Create a views file named index.php inside “application/views/subscription folder.
Step 10: Create views
Create a views file named pay.php inside “application/views/subscription folder.
Step 11: Make Ajax Request
We will also need to use jQuery code in views pay.php in "application/views/subscription" directory to make Ajax request to load.
Step 12: Create views
Create a views file named razorpayform.php inside “application/views/subscription folder.
Step 13: Make Ajax Request
We will also need to use jQuery code in views razorpayform.php in "application/views/subscription" directory to make razorpay Ajax request.
Step 14: Create views
Create a views file named thankyou.php inside “application/views/subscription folder.