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
- subscription
- config
- system
- index.php
- assets
- css
- style.css
- application
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:
1 2 3 4 |
<?php define('RAZOR_KEY_ID', 'XXXXXXXXXX'); define('RAZOR_KEY_SECRET', 'XXXXXXXXXX'); ?> |
Step 4: Create the database and Table
For this tutorial, you need a MySQL database with the following table:
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 |
-- -- Table structure for table `package` -- CREATE TABLE `package` ( `id` int(11) NOT NULL COMMENT 'PRIMARY_KEY', `plan_id` varchar(100) DEFAULT NULL, `name` varchar(255) NOT NULL COMMENT 'package name', `type` varchar(255) DEFAULT NULL COMMENT 'section', `duration` varchar(50) DEFAULT NULL, `fee` float(10,2) NOT NULL DEFAULT 0.00, `status` int(1) NOT NULL DEFAULT 0 COMMENT '0=inactive, 1=active' ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `package` -- INSERT INTO `package` (`id`, `plan_id`, `name`, `type`, `duration`, `fee`, `status`) VALUES (1, 'plan_DLG6Q8WJp23N5i', 'BRONZE', 'Yearly', 'year', 333.00, 1), (2, 'plan_DLG7EwbaF0qfaN', 'SILVER', 'Yearly', 'year', 555.00, 1), (3, 'plan_DLG7qIBBqMyuCP', 'GOLD', 'Yearly', 'year', 999.00, 1); -- -- Indexes for table `package` -- ALTER TABLE `package` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for table `package` -- ALTER TABLE `package` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'PRIMARY_KEY', AUTO_INCREMENT=4; |
Step 5: Create Model
Create a model file named Subscription_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 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * Version: 1.0.0 * * Description of Subscriptions model * * @author TechArise Team * * @email info@techarise.com * **/ // Subscriptions class class Subscription_model extends CI_Model { // Declare variables private $_packageID; public function setPackageID($packageID) { $this->_packageID = $packageID; } // get Subscription Package public function getSubscriptionPackage() { $this->db->select(array('p.id', 'p.plan_id', 'p.name', 'p.type', 'p.fee', 'p.duration')); $this->db->from('package as p'); $this->db->where('p.id', $this->_packageID); $query = $this->db->get(); return $query->row_array(); } } ?> |
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
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * Version: 1.0.0 * * Description of Subscriptions Controller * * @author TechArise Team * * @email info@techarise.com * **/ // Subscriptions class class Subscription extends CI_Controller { //Load libraries in Constructor. public function __construct() { parent::__construct(); $this->load->library('pagination'); $this->load->model('Subscription_model', 'subscription'); } public function index() { $data['metaDescription'] = 'Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter'; $data['metaKeywords'] = 'Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter'; $data['title'] = "Manage Recurring Subscriptions Payments using Razorpay in Codeigniter - TechArise"; $data['breadcrumbs'] = array('Manage Recurring Subscriptions Payments using Razorpay in Codeigniter' => '#'); $this->load->view('subscription/index', $data); } // payForm method public function payForm() { $data['metaDescription'] = 'Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter'; $data['metaKeywords'] = 'Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter'; $data['title'] = "Manage Recurring Subscriptions Payments using Razorpay in Codeigniter - TechArise"; $data['breadcrumbs'] = array('Manage Recurring Subscriptions Payments using Razorpay in Codeigniter' => '#'); $subscriptionID = urldecode(base64_decode($this->input->get('rpiud'))); $this->subscription->setPackageID($subscriptionID); $package = $this->subscription->getSubscriptionPackage(); if (!empty($subscriptionID)) { if (!empty($subscriptionID)) { $subsArray = array( 'package' => $package['name'], 'price' => $package['fee'], 'plan_id' => $package['id'], 'package_type' => $package['type'], 'plan' => $package['plan_id'], ); $this->session->set_userdata('ci_subscription_keys', $subsArray); } $data['subscription_keys'] = $this->session->userdata('ci_subscription_keys'); } $this->load->view('subscription/pay', $data); } // initiateSubscriptions public function initiateSubscriptions() { $json = array(); $firstName = $this->input->post('full_name'); $email = $this->input->post('pay_email'); $contactNum = $this->input->post('pay_phone'); $address = $this->input->post('pay_address'); if (empty($firstName)) { $json['error']['fullname'] = 'Please enter full name.'; } if (empty(trim($email))) { $json['error']['email'] = 'Please enter valid email.'; } else if (!preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)) { $json['error']['email'] = 'Please enter valid email.'; } if (empty(trim($contactNum))) { $json['error']['contactno'] = 'Please enter valid contact no.'; } else if (strlen($contactNum) < 7 || !is_numeric($contactNum)) { $json['error']['contactno'] = 'Please enter valid contact no.'; } if (empty($address)) { $json['error']['address'] = 'Please enter address'; } if(empty($json['error'])){ if (!empty($this->session->userdata('ci_subscription_keys')['plan'])) { $planID = $this->session->userdata('ci_subscription_keys')['plan']; $note = $this->session->userdata('ci_subscription_keys')['package']; $subscriptionData = array( 'plan_id' => $planID, 'customer_notify' => 1, 'total_count' => '10', 'notes' => array( 'name' => $note, ), ); $ch = $this->get_curl_handle_subscriptions($subscriptionData); $result = curl_exec($ch); $data = json_decode($result); $json['subscription_id'] = $data->id; $json['plan_id'] = $data->plan_id; // store value in session $this->session->set_userdata( array( 'subscription_id' => $data->id, 'plan_id' => $data->plan_id, 'created_at' => $data->created_at, 'charge_at' => $data->charge_at, 'start_at' => $data->start_at, ) ); } } $this->output->set_header('Content-Type: application/json'); echo json_encode($json); } // initialized cURL Request subscription private function get_curl_handle_subscriptions($subscriptionData) { $url = 'https://api.razorpay.com/v1/subscriptions'; $key_id = RAZOR_KEY_ID; $key_secret = RAZOR_KEY_SECRET; //cURL Request $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERPWD, $key_id . ':' . $key_secret); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POST, 1); $data = $subscriptionData; $params = http_build_query($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); return $ch; } // callback method public function callbacksubscriptions() { if (!empty($this->input->post('razorpay_payment_id')) && !empty($this->input->post('merchant_order_id'))) { $success = true; $error = ''; try { // store temprary data $dataFlesh = array( 'txnid' => $this->input->post('merchant_trans_id'), 'card_holder_name' => $this->input->post('card_holder_name_id'), 'productinfo' => $this->input->post('merchant_product_info_id'), 'surl' => $this->input->post('merchant_surl_id'), 'furl' => $this->input->post('merchant_furl_id'), 'order_id' => $this->input->post('merchant_order_id'), 'razorpay_payment_id' => $this->input->post('razorpay_payment_id'), 'merchant_subscription_id' => $this->input->post('merchant_subscription_id'), 'merchant_amount' => $this->input->post('merchant_amount'), 'merchant_plan_id' => $this->input->post('merchant_plan_id'), 'created_at' => time(), ); $this->session->set_flashdata('paymentInfo', $dataFlesh); } catch (Exception $e) { $success = false; $error = 'Request to Razorpay Failed'; } if ($success === true) { if (!empty($this->session->userdata('ci_subscription_keys'))) { $this->session->unset_userdata('ci_subscription_keys'); } if (!$order_info['order_status_id']) { redirect($this->input->post('merchant_surl_id')); } else { redirect($this->input->post('merchant_surl_id')); } } else { redirect($this->input->post('merchant_furl_id')); } } else { echo 'An error occured. Contact site administrator, please!'; } } // create subscription public function createSubscription() { $fullName = $this->input->post('full_name'); $email = $this->input->post('pay_email'); $contactNum = $this->input->post('pay_phone'); $address = $this->input->post('pay_address'); if (!empty($this->session->userdata('ci_subscription_keys'))) { $packageamount = $this->session->userdata('ci_subscription_keys')['price']; $packageid = $this->session->userdata('ci_subscription_keys')['plan_id']; $servicetype = $this->session->userdata('ci_subscription_keys')['package_type']; $totalamount = $this->session->userdata('ci_subscription_keys')['price']; } $surl = site_url().'subscription/thankyou'; $furl = site_url().'subscription/failed'; $productinfo = $packageid; $dataFlesh = array( 'txnid' => time(), 'card_holder_name' => $fullName, 'amount' => $totalamount, 'email' => $email, 'phone' => $contactNum, 'productinfo' => $productinfo, 'surl' => $surl, 'furl' => $furl, 'currency_code' => 'INR', 'order_id' => time(), 'lang' => 'en', 'store_name' => 'Soolegal', 'return_url' => site_url() . 'subscription/callbacksubscriptions', 'payment_type' => 'create_subscriptions', 'subscription_id' => $this->session->userdata('subscription_id'), 'plan_id' => $this->session->userdata('plan_id'), 'created_at' => $this->session->userdata('created_at'), 'charge_at' => $this->session->userdata('charge_at'), 'date_end_plan_at' => strtotime("+1 years", $this->session->userdata('charge_at')), 'start_at' => $this->session->userdata('start_at'), 'package' => $this->session->userdata('ci_subscription_keys')['package'], 'price' => $this->session->userdata('ci_subscription_keys')['price'], 'package_plan_id' => $this->session->userdata('ci_subscription_keys')['plan_id'], 'package_type' => $this->session->userdata('ci_subscription_keys')['package_type'], ); $this->session->set_userdata('subscription_ci_seesion_key', $dataFlesh); $payInfo = $dataFlesh; $json['payInfo'] = $payInfo; $json['msg'] = 'success'; $this->output->set_header('Content-Type: application/json'); $this->load->view('subscription/razorpayform', $json); } // thankyou public function thankyou() { $data['metaDescription'] = 'Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter'; $data['metaKeywords'] = 'Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter'; $data['title'] = "Thank You | TechArise"; $this->load->view('subscription/thankyou.php', $data); } } ?> |
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.
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 |
<?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 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.
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 |
<!-- 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> </body> </html> |
Step 9: Create views
Create a views file named index.php inside “application/views/subscription 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
<?php $this->load->view('templates/header');?> <style type="text/css"> .pricing .card { border: none; border-radius: 1rem; transition: all 0.2s; box-shadow: 0 0.5rem 1rem 0 rgba(0, 0, 0, 0.1); } .pricing hr { margin: 1.5rem 0; } .pricing .card-title { margin: 0.5rem 0; font-size: 20px; letter-spacing: .1rem; font-weight: bold; } .pricing .card-price { font-size: 3rem; margin: 0; } .pricing .card-price .period { font-size: 0.8rem; } .pricing ul li { margin-bottom: 1rem; } .pricing .text-muted { opacity: 0.7; } .pricing .btn { font-size: 80%; border-radius: 5rem; letter-spacing: .1rem; font-weight: bold; padding: 1rem; opacity: 0.7; transition: all 0.2s; } /* Hover Effects on Card */ @media (min-width: 992px) { .pricing .card:hover { margin-top: -.25rem; margin-bottom: .25rem; box-shadow: 0 0.5rem 1rem 0 rgba(0, 0, 0, 0.3); } .pricing .card:hover .btn { opacity: 1; } } .pb-5, .py-5{ padding-bottom: 15px!important; padding-top: 15px!important; } </style> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter</h2> </div> <section class="pricing py-5"> <div class="container"> <div class="row"> <!-- Free Tier --> <div class="col-lg-4"> <div class="card mb-5 mb-lg-0"> <div class="card-body"> <h5 class="card-title text-primary text-uppercase text-center">Bronze</h5> <h6 class="card-price text-center">$333<span class="period">/per year</span></h6> <hr> <ul class="fa-ul"> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Single User</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>5GB Storage</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Unlimited Public Projects</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Community Access</li> <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Unlimited Private Projects</li> <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Dedicated Phone Support</li> <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Free Subdomain</li> <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Monthly Status Reports</li> </ul> <a href="<?php print site_url().'subscription/pay?rpiud='.urlencode(base64_encode(1));?>" class="btn btn-block btn-primary text-uppercase">Sign UP</a> </div> </div> </div> <!-- Plus Tier --> <div class="col-lg-4"> <div class="card mb-5 mb-lg-0"> <div class="card-body"> <h5 class="card-title text-success text-uppercase text-center">Silver</h5> <h6 class="card-price text-center">$555<span class="period">/per year</span></h6> <hr> <ul class="fa-ul"> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span><strong>5 Users</strong></li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>50GB Storage</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Unlimited Public Projects</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Community Access</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Unlimited Private Projects</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Dedicated Phone Support</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Free Subdomain</li> <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Monthly Status Reports</li> </ul> <a href="<?php print site_url().'subscription/pay?rpiud='.urlencode(base64_encode(2));?>" class="btn btn-block btn-primary text-uppercase">Sign Up</a> </div> </div> </div> <!-- Pro Tier --> <div class="col-lg-4"> <div class="card"> <div class="card-body"> <h5 class="card-title text-warning text-uppercase text-center">Gold</h5> <h6 class="card-price text-center">₹999<span class="period">/per year</span></h6> <hr> <ul class="fa-ul"> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span><strong>Unlimited Users</strong></li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>150GB Storage</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Unlimited Public Projects</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Community Access</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Unlimited Private Projects</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Dedicated Phone Support</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span><strong>Unlimited</strong> Free Subdomains</li> <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Monthly Status Reports</li> </ul> <a href="<?php print site_url().'subscription/pay?rpiud='.urlencode(base64_encode(3));?>" class="btn btn-block btn-primary text-uppercase">Sign Up</a> </div> </div> </div> </div> </div> </section> </div> </section> <?php $this->load->view('templates/footer');?> |
Step 10: Create views
Create a views file named pay.php inside “application/views/subscription 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
<?php $this->load->view('templates/header'); if(!empty($subscription_keys['package'])){ $package = $subscription_keys['package']; } else { $package = ''; } if(!empty($subscription_keys['price'])){ $price = $subscription_keys['price']; } else { $price = ''; } ?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Manage Recurring Subscriptions Payments using Razorpay Billing in Codeigniter</h2> </div> <form id="checkout-fee-frm" class="checkout-fee-frm"> <div class="row"> <aside class="col-sm-8"> <div class="card"> <article class="card-group-item"> <header class="card-header"> <h6 class="title">Sign Up </h6> </header> <div class="filter-content"> <div class="card-body"> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text"><i class="fa fa-user text-muted"></i></div> </div> <input type="text" class="form-control" id="pay-subs-fullname" name="full_name" placeholder="Full Name *" value=""> </div> </div> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text"><i class="fa fa-envelope text-muted"></i></div> </div> <input type="text" class="form-control" id="pay-subs-email" name="pay_email" placeholder="Email *" value=""> </div> </div> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text"><i class="fa fa-phone text-muted"></i></div> </div> <input type="text" class="form-control" id="pay-subs-contactno" name="pay_phone" placeholder="Contact No *" value=""> </div> </div> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text"><i class="fa fa-map-marker text-muted"></i></div> </div> <input type="text" class="form-control" id="pay-subs-address" name="pay_address" placeholder="Address *" value=""> </div> </div> </div> <!-- card-body.// --> </div> </article> <!-- card-group-item.// --> </div> <!-- card.// --> </aside> <!-- col.// --> <aside class="col-sm-4"> <div class="card"> <article class="card-group-item"> <header class="card-header"> <h6 class="title">Order Summary <a href="<?php print site_url();?>" class="btn btn-primary float-right btn-sm">Back</a></h6> </header> </article> <!-- card-group-item.// --> <article class="card-group-item"> <div class="filter-content"> <div class="card-body"> <div style="font-weight: normal; font-size: 15px;"> <label for="Check2"><?php print $package;?> </label> <span class="float-right"><?php print $price; ?>/</span> </div> <!-- form-check.// --> <hr> <div style="font-weight: bold; font-size: 18px;"> <label for="Check3">Total</label> <span class="float-right"><?php print $price; ?>/</span> </div> <!-- form-check.// --> </div> <!-- card-body.// --> </div> </article> <!-- card-group-item.// --> <button type="button" class="btn btn-primary" id="razor-subscription-pay-now"> <i class="fa fa-credit-card"></i> Pay Now </button> </div> <!-- card.// --> </aside> <!-- col.// --> </div> <!-- row.// --> </form> </div> </section> <span id="render-subscription-pay-info"></span> <?php $this->load->view('templates/footer');?> <script type="text/javascript"> jQuery(document).on('click', '#razor-subscription-pay-now', function () { jQuery.ajax({ url: '<?php echo base_url(); ?>subscription/initiateSubscriptions', type: 'post', data: jQuery('#checkout-fee-frm input[type=\'radio\']:checked, #checkout-fee-frm select, #checkout-fee-frm input[type=\'text\'], #checkout-fee-frm input[type=\'hidden\'], #checkout-fee-frm input[type=\'email\']'), dataType: 'json', beforeSend: function () { jQuery('#razor-subscription-pay-now').button('loading'); }, complete: function () { jQuery('#razor-subscription-pay-now').button('reset'); }, success: function (json) { $('.text-danger').remove(); if (json['error']) { for (i in json['error']) { var element = $('#pay-subs-' + i.replace('_', '-')); if ($(element).parent().hasClass('form-group')) { $(element).parent().after('<small class="text-danger" style="float:left;">' + json['error'][i] + '</small>'); } else if ($(element).parent().hasClass('input-group')) { $(element).parent().after('<small class="text-danger" style="float:left;">' + json['error'][i] + '</small>'); } else { $(element).after('<small class="text-danger" style="float:left;">' + json['error'][i] + '</small>'); } } } else { jQuery.ajax({ url: '<?php echo base_url(); ?>subscription/createSubscription', type: 'post', data: jQuery('#checkout-fee-frm input[type=\'radio\']:checked, #checkout-fee-frm select, #checkout-fee-frm input[type=\'text\'], #checkout-fee-frm input[type=\'hidden\'], #checkout-fee-frm input[type=\'email\']'), dataType: 'html', success: function (html) { jQuery('span#render-subscription-pay-info').html(html); }, error: function (xhr, ajaxOptions, thrownError) { console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); } }); } }, error: function (xhr, ajaxOptions, thrownError) { console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); } }); }); </script> |
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.
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 |
<script type="text/javascript"> jQuery(document).on('click', '#razor-subscription-pay-now', function () { jQuery.ajax({ url: '<?php echo base_url(); ?>subscription/initiateSubscriptions', type: 'post', data: jQuery('#checkout-fee-frm input[type=\'radio\']:checked, #checkout-fee-frm select, #checkout-fee-frm input[type=\'text\'], #checkout-fee-frm input[type=\'hidden\'], #checkout-fee-frm input[type=\'email\']'), dataType: 'json', beforeSend: function () { jQuery('#razor-subscription-pay-now').button('loading'); }, complete: function () { jQuery('#razor-subscription-pay-now').button('reset'); }, success: function (json) { $('.text-danger').remove(); if (json['error']) { for (i in json['error']) { var element = $('#pay-subs-' + i.replace('_', '-')); if ($(element).parent().hasClass('form-group')) { $(element).parent().after('<small class="text-danger" style="float:left;">' + json['error'][i] + '</small>'); } else if ($(element).parent().hasClass('input-group')) { $(element).parent().after('<small class="text-danger" style="float:left;">' + json['error'][i] + '</small>'); } else { $(element).after('<small class="text-danger" style="float:left;">' + json['error'][i] + '</small>'); } } } else { jQuery.ajax({ url: '<?php echo base_url(); ?>subscription/createSubscription', type: 'post', data: jQuery('#checkout-fee-frm input[type=\'radio\']:checked, #checkout-fee-frm select, #checkout-fee-frm input[type=\'text\'], #checkout-fee-frm input[type=\'hidden\'], #checkout-fee-frm input[type=\'email\']'), dataType: 'html', success: function (html) { jQuery('span#render-subscription-pay-info').html(html); }, error: function (xhr, ajaxOptions, thrownError) { console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); } }); } }, error: function (xhr, ajaxOptions, thrownError) { console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); } }); }); </script> |
Step 12: Create views
Create a views file named razorpayform.php inside “application/views/subscription 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 |
<?php $button_confirm = 'Pay Now'; $txnid = $payInfo['txnid']; $productinfo = $payInfo['productinfo']; $surl = $payInfo['surl']; $furl = $payInfo['furl']; $key_id = RAZOR_KEY_ID; $currency_code = $payInfo['currency_code']; $total = (round($payInfo['amount']) * 100); $amount = round($payInfo['amount']); $merchant_order_id = $payInfo['order_id']; $card_holder_name = $payInfo['card_holder_name']; $email = $payInfo['email']; $phone = $payInfo['phone']; $name = $payInfo['store_name']; $lang = $payInfo['lang']; $return_url = site_url() . 'subscription/callbacksubscriptions'; $merchant_subscription_id = $payInfo['subscription_id']; $merchant_plan_id = $payInfo['plan_id']; $package = $payInfo['package']; $productinfo = $payInfo['productinfo']; ?> <form name="razorpay-subscription-form" id="razorpay-subscription-form" action="<?php echo $return_url; ?>" method="POST"> <input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id" /> <input type="hidden" name="merchant_order_id" id="merchant_plan_id" value="<?php echo $merchant_order_id; ?>"/> <input type="hidden" name="merchant_trans_id" id="merchant_trans_id" value="<?php echo $txnid; ?>"/> <input type="hidden" name="merchant_surl_id" id="merchant-surl-id" value="<?php echo $surl; ?>"/> <input type="hidden" name="merchant_furl_id" id="merchant-furl-id" value="<?php echo $furl; ?>"/> <input type="hidden" name="card_holder_name_id" id="card-holder-name-id" value="<?php echo $card_holder_name; ?>"/> <input type="hidden" name="merchant_amount" id="merchant-amount" value="<?php echo $amount; ?>"/> <input type="hidden" name="merchant_subscription_id" id="merchant-subscription-id" value="<?php echo $merchant_subscription_id; ?>"/> <input type="hidden" name="merchant_plan_id" id="merchant_plan_id" value="<?php echo $merchant_plan_id; ?>"/> <input type="hidden" name="merchant_product_info_id" id="merchant_product_info_id" value="<?php echo $productinfo; ?>"/> </form> <div class="buttons"> <div class="pull-right"> <input style="display:none;" id="click-subscription-pay" type="submit" onclick="razorpaySubscriptionSubmit(this);" value="<?php echo $button_confirm; ?>" class="btn btn-primary" /> </div> </div> <script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script> var razorpay_subscription_options = { key: "<?php echo $key_id; ?>", name: "<?php echo $package; ?>", description: "<?php echo $package; ?> - subscription", subscription_id: "<?php echo $merchant_subscription_id; ?>", prefill: { "name": "<?php echo $card_holder_name; ?>", "email": "<?php echo $email; ?>", "contact": "<?php echo $phone; ?>" }, notes: { "order_id": "<?php echo $merchant_order_id; ?>" }, handler: function (transaction) { document.getElementById('razorpay_payment_id').value = transaction.razorpay_payment_id; document.getElementById('razorpay-subscription-form').submit(); }, "modal": { "ondismiss": function () { location.reload() } } }; var razorpay_submit_btn, razorpay_instance; function razorpaySubscriptionSubmit(el) { if (typeof Razorpay == 'undefined') { setTimeout(razorpaySubscriptionSubmit, 200); if (!razorpay_submit_btn && el) { razorpay_submit_btn = el; el.disabled = true; el.value = 'Please wait...'; } } else { if (!razorpay_instance) { razorpay_instance = new Razorpay(razorpay_subscription_options); if (razorpay_submit_btn) { razorpay_submit_btn.disabled = false; razorpay_submit_btn.value = "<?php echo $button_confirm; ?>"; } } razorpay_instance.open(); } } jQuery("#click-subscription-pay").trigger("click"); </script> |
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.
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 |
<script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script> var razorpay_subscription_options = { key: "<?php echo $key_id; ?>", name: "<?php echo $package; ?>", description: "<?php echo $package; ?> - subscription", subscription_id: "<?php echo $merchant_subscription_id; ?>", prefill: { "name": "<?php echo $card_holder_name; ?>", "email": "<?php echo $email; ?>", "contact": "<?php echo $phone; ?>" }, notes: { "order_id": "<?php echo $merchant_order_id; ?>" }, handler: function (transaction) { document.getElementById('razorpay_payment_id').value = transaction.razorpay_payment_id; document.getElementById('razorpay-subscription-form').submit(); }, "modal": { "ondismiss": function () { location.reload() } } }; var razorpay_submit_btn, razorpay_instance; function razorpaySubscriptionSubmit(el) { if (typeof Razorpay == 'undefined') { setTimeout(razorpaySubscriptionSubmit, 200); if (!razorpay_submit_btn && el) { razorpay_submit_btn = el; el.disabled = true; el.value = 'Please wait...'; } } else { if (!razorpay_instance) { razorpay_instance = new Razorpay(razorpay_subscription_options); if (razorpay_submit_btn) { razorpay_submit_btn.disabled = false; razorpay_submit_btn.value = "<?php echo $button_confirm; ?>"; } } razorpay_instance.open(); } } jQuery("#click-subscription-pay").trigger("click"); </script> |
Step 14: Create views
Create a views file named thankyou.php inside “application/views/subscription folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $this->load->view('templates/header');?> <section class="showcase"> <div class="container"> <div class="text-center"> <h1 class="display-3">Thank You!</h1> <p class="lead"><strong>Please check your email</strong> for further instructions on how to complete your account setup.</p> <hr> <p> Having trouble? <a href="mailto:info@techarise.com">Contact us</a> </p> <p class="lead"> <a class="btn btn-primary btn-sm" href="<?php print site_url();?>" role="button">Continue to homepage</a> </p> </div> </div> </section> <?php $this->load->view('templates/footer');?> |