how to Integrate Razorpay payment gateway in Codeigniter – onlinecode
In this post we will give you information about how to Integrate Razorpay payment gateway in Codeigniter – onlinecode. Hear we will give you detail about how to Integrate Razorpay payment gateway in Codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.
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 TablesStep 2: Connect to DatabaseStep 3: Get and Set RAZOR API Key and SECRETStep 4: Create ControllerStep 5: Create View File
Step 1: Create Database TablesIn this step, we have to create a table in the database, so we will create a database using the below code.
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 DatabaseGo to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.
$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 SECRETIn 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
<?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
<!DOCTYPE html> <html lang="en"> <head> <title>how to Integrate Razorpay payment gateway in Codeigniter - onlinecode</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 > <div > <div > <h4>Product 1</h4> <h5>500 Rs.</h5> <a href="javascript:void(0)" data-amount="500" data-id="1" data-product="Product 1">Order Now</a> </div> <div > <h4>Product 2</h4> <h5>1000 Rs.</h5> <a href="javascript:void(0)" data-amount="1000" data-id="2" data-product="Product 2">Order Now</a> </div> <div > <h4>Product 3</h4> <h5>1500 Rs.</h5> <a href="javascript:void(0)" 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
<!DOCTYPE html> <html lang="en"> <head> <title>how to Integrate Razorpay payment gateway in Codeigniter - onlinecode</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 > <div > <div > <h2 style="text-align:center;">Thank You For Payment</h2> </div> </div> </div> </body> </html>
Hope this code and post will helped you for implement how to Integrate Razorpay payment gateway in Codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs