how to Integrate Razorpay payment gateway in Codeigniter
In this tutorial, we will tell you how to integrate the Razorpay payment gateway in Codeigniter Framework(Codeigniter Razorpay payment gateway integrate example).
The Razorpay payment gateway is a popular payment gateway method in India and it is easily used for the project. so many developers prefer that payment gateway method.
Razorpay provides two environments for the user like a Test mode and live mode and it allows online payment transaction like Cards, Net-banking, Wallets & UPI. Developer Friendly API, Fast On-boarding, and No Setup.
Overview
Step 1: Create Database Tables
Step 2: Connect to Database
Step 3: Get and Set RAZOR API Key and SECRET
Step 4: Create Controller
Step 5: Create View File
Step 1: Create Database Tables
In this step, we have to create a table in the database, so we will create a database using the below code.
1 2 3 4 5 6 7 8 | CREATE TABLE `orders` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `product_id` int(11) NOT NULL, `payment_id` int(11) NOT NULL, `amount` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
Step 2: Connect to Database
Go to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'enter here database name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); |
Step 3: Get and Set RAZOR API Key and SECRET
In this step, we have to need the API Key and SECRET Key. so we will go to the official Razor site and after then login gets API Key and SECRET Key.
Step 3: Create Controller
In this step, we will create a Razor.php file in the “application/controller” directory and paste the below code in this controller.
application/controller/Razor.php
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 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Razor extends CI_Controller { public function __construct() { parent::__construct(); $this->load->database(); $this->load->helper(array('url','html','form')); } public function index() { $this->load->view('product_list'); } public function razor_payment_success() { $data = [ 'user_id' => '1', 'product_id' => $this->input->post('product_id'), 'payment_id' => $this->input->post('razorpay_payment_id'), 'amount' => $this->input->post('totalAmount') ]; $insert = $this->db->insert('payments', $data); $arr = array('msg' => 'Payment successfully credited', 'status' => true); } public function razor_payment_thankyou() { $this->load->view('razor_thankyou'); } } |
Step 4: Create View File
So finally, we will create the product_list.php and razor_thankyou.php file in the “application/views/” directory.
application/views/product_list.php
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 | <!DOCTYPE html> <html lang="en"> <head> <title>how to Integrate Razorpay payment gateway in Codeigniter - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-4"> <h4>Product 1</h4> <h5>500 Rs.</h5> <a href="javascript:void(0)" class="btn btn-sm btn-primary buy_now" data-amount="500" data-id="1" data-product="Product 1">Order Now</a> </div> <div class="col-sm-4"> <h4>Product 2</h4> <h5>1000 Rs.</h5> <a href="javascript:void(0)" class="btn btn-sm btn-primary buy_now" data-amount="1000" data-id="2" data-product="Product 2">Order Now</a> </div> <div class="col-sm-4"> <h4>Product 3</h4> <h5>1500 Rs.</h5> <a href="javascript:void(0)" class="btn btn-sm btn-primary buy_now" data-amount="1500" data-id="3" data-product="Product 3">Order Now</a> </div> </div> </div> <script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script> $('body').on('click', '.buy_now', function(e){ var totalAmount = $(this).attr("data-amount"); var product_id = $(this).attr("data-id"); var product_name = $(this).attr("data-product"); var options = { "key": "YOUR_KEY_ID", "currency": "INR", "amount": totalAmount, "name": product_name, "description": "Test Transaction", "handler": function (response){ $.ajax({ url: '<?php echo base_url() ?>'+'payment/razorPaySuccess', type: 'post', dataType: 'json', data: { razorpay_payment_id: response.razorpay_payment_id , totalAmount : totalAmount ,product_id : product_id, }, success: function (msg) { window.location.href = '<?php echo base_url() ?>'+'payment/RazorThankYou'; } }); }, "theme": { "color": "#F37254" } }; var rzp1 = new Razorpay(options); document.getElementById('rzp-button1').onclick = function(e){ rzp1.open(); e.preventDefault(); } </script> </body> </html> |
application/views/razor_thankyou.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html> <html lang="en"> <head> <title>how to Integrate Razorpay payment gateway in Codeigniter - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <h2 style="text-align:center;">Thank You For Payment</h2> </div> </div> </div> </body> </html> |