Razorpay Payment Gateway Integration in Codeigniter using cURL
In this tutorial, We have shared how to integrate Razorpay payment gateway in Codeigniter using cURL. Razorpay is the most popular payment gateway in India. Razorpay provides clean, fast, secure payments services with hassle-free integration with developer-friendly APIs. It allows online businesses to accept and process payment modes like Cards, Net-banking, Wallets & UPI. Developer Friendly API, Fast Onboarding, and No Setup. Razorpay payment gateway is the easiest option for the web developer to implement a payment system on the Web Application.
Step 1: Create Razorpay Account
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.
Step 2: Update Razorpay Config Details
Here in this example, we will use Test App to integrate Razorpay gateway. So we will update config.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 a Controller file Razorpay
Create a model file named “Razorpay.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 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 |
<?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 Razorpay : CodeIgniter Razorpay Gateway * * @author TechArise Team * * @email info@techarise.com * * Description of Razorpay Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Razorpay extends CI_Controller { // construct public function __construct() { parent::__construct(); $this->load->model('Site', 'site'); } // index page public function index() { $data['title'] = 'Razorpay | TechArise'; $data['productInfo'] = $this->site->getProduct(); $this->load->view('razorpay/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().'razorpay/callback'; $data['surl'] = site_url().'razorpay/success';; $data['furl'] = site_url().'razorpay/failed';; $data['currency_code'] = 'INR'; $this->load->view('razorpay/checkout', $data); } // initialized cURL Request private function get_curl_handle($payment_id, $amount) { $url = 'https://api.razorpay.com/v1/payments/'.$payment_id.'/capture'; $key_id = RAZOR_KEY_ID; $key_secret = RAZOR_KEY_SECRET; $fields_string = "amount=$amount"; //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); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/ca-bundle.crt'); return $ch; } // callback method public function callback() { if (!empty($this->input->post('razorpay_payment_id')) && !empty($this->input->post('merchant_order_id'))) { $razorpay_payment_id = $this->input->post('razorpay_payment_id'); $merchant_order_id = $this->input->post('merchant_order_id'); $currency_code = 'INR'; $amount = $this->input->post('merchant_total'); $success = false; $error = ''; try { $ch = $this->get_curl_handle($razorpay_payment_id, $amount); //execute post $result = curl_exec($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($result === false) { $success = false; $error = 'Curl error: '.curl_error($ch); } else { $response_array = json_decode($result, true); // echo "<pre>";print_r($response_array);exit; //Check success response if ($http_status === 200 and isset($response_array['error']) === false) { $success = true; } else { $success = false; if (!empty($response_array['error']['code'])) { $error = $response_array['error']['code'].':'.$response_array['error']['description']; } else { $error = 'RAZORPAY_ERROR:Invalid Response <br/>'.$result; } } } //close connection curl_close($ch); } catch (Exception $e) { $success = false; $error = 'OPENCART_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!'; } } public function success() { $data['title'] = 'Razorpay Success | TechArise'; $this->load->view('razorpay/success', $data); } public function failed() { $data['title'] = 'Razorpay Failed | TechArise'; $this->load->view('razorpay/failed', $data); } } ?> |
Step 5: Create a view file index
Create a view file named “index.php” inside “application/views/razorpay” 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>Razorpay Payment Gateway Integration In Codeigniter using cURL</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/razorpay” 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 |
<?php $this->load->view('templates/header'); ?> <div class="row"> <div class="col-lg-12"> <h2>Razorpay Payment Gateway Integration In Codeigniter using cURL</h2> </div> </div><!-- /.row --> <?php $productinfo = $itemInfo['description']; $txnid = time(); $surl = $surl; $furl = $furl; $key_id = RAZOR_KEY_ID; $currency_code = $currency_code; $total = ($itemInfo['price']* 100); $amount = $itemInfo['price']; $merchant_order_id = $itemInfo['product_id']; $card_holder_name = 'TechArise Team'; $email = 'info@techarise.com'; $phone = '9000000001'; $name = APPLICATION_NAME; $return_url = site_url().'razorpay/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> <form name="razorpay-form" id="razorpay-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_order_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_product_info_id" id="merchant_product_info_id" value="<?php echo $productinfo; ?>"/> <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_total" id="merchant_total" value="<?php echo $total; ?>"/> <input type="hidden" name="merchant_amount" id="merchant_amount" value="<?php echo $amount; ?>"/> </form> <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"> <a href="<?php print site_url();?>" name="reset_add_emp" id="re-submit-emp" class="btn btn-warning"><i class="fa fa-mail-reply"></i> Back</a> <input id="submit-pay" type="submit" onclick="razorpaySubmit(this);" value="Pay Now" class="btn btn-primary" /> </div> </div> <?php $this->load->view('templates/footer'); ?> <script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script> var razorpay_options = { key: "<?php echo $key_id; ?>", amount: "<?php echo $total; ?>", name: "<?php echo $name; ?>", description: "Order # <?php echo $merchant_order_id; ?>", netbanking: true, currency: "<?php echo $currency_code; ?>", prefill: { name:"<?php echo $card_holder_name; ?>", email: "<?php echo $email; ?>", contact: "<?php echo $phone; ?>" }, notes: { soolegal_order_id: "<?php echo $merchant_order_id; ?>", }, handler: function (transaction) { document.getElementById('razorpay_payment_id').value = transaction.razorpay_payment_id; document.getElementById('razorpay-form').submit(); }, "modal": { "ondismiss": function(){ location.reload() } } }; var razorpay_submit_btn, razorpay_instance; function razorpaySubmit(el){ if(typeof Razorpay == 'undefined'){ setTimeout(razorpaySubmit, 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_options); if(razorpay_submit_btn){ razorpay_submit_btn.disabled = false; razorpay_submit_btn.value = "Pay Now"; } } razorpay_instance.open(); } } </script> |
Step 7: Open a file routes
Open “application/config/routes.php” file and add code like as bellow:
1 2 3 4 5 |
<?php // routes $route['default_controller'] = 'razorpay/index'; $route['checkout/(:any)'] = "razorpay/checkout/$1"; ?> |
Mastercard | Visa |
---|---|
5104_0155_5555_5558, 5104_0600_0000_0008 | 4111_1111_1111_1111 |
Mastercard | Visa |
---|---|
5555_5555_5555_4444, 5105_1051_0510_5100 | 4012_8888_8888_1881, 4000_1841_8621_8826 |