In today’s post we will learn how to integrate Razorpay payment gateway in CodeIgniter Php. It’s not a difficult task. Just follow the step-by-step procedure.
Visit the Codeigniter category to view more queries like this.
We will do it in two steps. In the first step, we will create a view that will be displayed on the front end, and in the second step, we will create a controller that will handle the functions of razorpay payment gateway.
CodeIgniter Code for Razorpay
First We Will Create a View Frontend
Now follow the following steps :-
- Go to applications folder then go to views folder.
- Inside views folder create a file named “registration-form.php” and paste the code given below.
<!doctype html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" href="https://codingbirdsonline.com/wp-content/uploads/2019/12/cropped-coding-birds-favicon-2-1-192x192.png" type="image/x-icon">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<title>Razorpay Payment Gateway: How to integration Razorpay payment gatway in CodeIgniter,PHP | CodingBirdsOnline</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-9 col-md-7 col-lg-5 mx-auto">
<div class="card card-signin my-5">
<div class="card-body">
<h5 class="card-title text-center">Razor Pay Integration in CodeIgniter</h5>
<form action="<?php echo base_url().'register/pay'; ?>" method="post" class="form-signin">
<div class="form-label-group">
<label for="name">Name <span style="color: #FF0000">*</span></label>
<input type="text" name="name" id="name" class="form-control" placeholder="Name" required >
</div> <br/>
<div class="form-label-group">
<label for="email">Email <span style="color: #FF0000">*</span></label>
<input type="text" name="email" id="email" class="form-control" placeholder="Email address" required>
</div> <br/>
<label for="contact">Contact <span style="color: #FF0000">*</span></label>
<div class="form-label-group">
<input type="text" id="contact" name="contact" class="form-control" placeholder="Contact" required>
</div><br/>
<label for="subject">Amount <span style="color: #FF0000">*</span></label>
<div class="form-label-group">
<input type="text" id="amount" name="amount" value="10" readonly class="form-control" placeholder="Amount" required>
</div><br/>
<br/>
<button type="submit" name="sendMailBtn" class="btn btn-lg btn-primary btn-block text-uppercase" >Pay Now</button>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
It will look like this
Now We Will Create Controller To Connect to Razorpay
Now follow the following steps :-
Before creating the controller just define the razorpay constants to the constants.php. These constants are RAZOR_KEY, RAZOR_SECRET_KEY.
- Go to applications folder then go to controller folder.
- Inside views folder create a file named “registration.php” and paste the code given below..
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(APPPATH."libraries/razorpay/razorpay-php/Razorpay.php");
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;
class Register extends CI_Controller {
/**
* This function loads the registration form
*/
public function index()
{$this->load->helper('url');
$this->load->view('registration-form');
}
/**
* This function creates order and loads the payment methods
*/
public function pay()
{
$api = new Api(RAZOR_KEY, RAZOR_SECRET_KEY);
/**
* You can calculate payment amount as per your logic
* Always set the amount from backend for security reasons
*/
$_SESSION['payable_amount'] = 10;
$razorpayOrder = $api->order->create(array(
'receipt' => rand(),
'amount' => $_SESSION['payable_amount'] * 100, /* rupees in paise */
'currency' => 'INR',
'payment_capture' => 1 // auto capture
));
$amount = $razorpayOrder['amount'];
$razorpayOrderId = $razorpayOrder['id'];
$_SESSION['razorpay_order_id'] = $razorpayOrderId;
$data = $this->prepareData($amount,$razorpayOrderId);
$this->load->view('rezorpay',array('data' => $data));
}
/**
* This function verifies the payment,after successful payment
*/
public function verify()
{
$success = true;
$error = "payment_failed";
if (empty($_POST['razorpay_payment_id']) === false) {
$api = new Api(RAZOR_KEY, RAZOR_SECRET_KEY);
try {
$attributes = array(
'razorpay_order_id' => $_SESSION['razorpay_order_id'],
'razorpay_payment_id' => $_POST['razorpay_payment_id'],
'razorpay_signature' => $_POST['razorpay_signature'] );
$api->utility->verifyPaymentSignature($attributes);
} catch(SignatureVerificationError $e) {
$success = false;
$error = 'Razorpay_Error : ' . $e->getMessage();
}
}
if ($success == true) {
echo "<pre>";
print_r($attributes) ;
/**
* Call this function from where ever you want
* to save save data before of after the payment
*/
//$this->setRegistrationData();
//redirect(base_url().'register/success');
}
else {
//redirect(base_url().'register/paymentFailed');
}
}
/**
* This function preprares payment parameters
* @param $amount
* @param $razorpayOrderId
* @return array
*/
public function prepareData($amount,$razorpayOrderId )
{
$data = array(
"key" => RAZOR_KEY,
"amount" => $amount,
"name" => "Coding Birds Online",
"description" => "Learn To Code",
"image" => "https://demo.codingbirdsonline.com/website/img/coding-birds-online/coding-birds-online-favicon.png",
"prefill" => array(
"name" => $this->input->post('name'),
"email" => $this->input->post('email'),
"contact" => $this->input->post('contact'),
),
"notes" => array(
"address" => "Hello World",
"merchant_order_id" => rand(),
),
"theme" => array(
"color" => "#F37254"
),
"order_id" => $razorpayOrderId,
);
return $data;
}
/**
* This function saves your form data to session,
* After successfull payment you can save it to database
*/
public function setRegistrationData()
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$contact = $this->input->post('contact');
$amount = $_SESSION['payable_amount'];
$registrationData = array(
'order_id' => $_SESSION['razorpay_order_id'],
'name' => $name,
'email' => $email,
'contact' => $contact,
'amount' => $amount,
);
// save this to database
}
/**
* This is a function called when payment successful,
* and shows the success message
*/
public function success()
{
$this->load->view('success');
}
/**
* This is a function called when payment failed,
* and shows the error message
*/
public function paymentFailed()
{
$this->load->view('error');
}
}
Now successfully We have created the razorpay payment gateway in CI framework.
Comment below for any queries.
Related Posts:
- Add Domain Or Subdomain In Your Localhost Xampp Server
- Create a Subscription in Stripe Payment Gateway In…
- Upload data using excel sheet in CodeIgniter
- Encryption and decryption in PhP
- Php code to create certificate and export as jpg image
- File and Folder size in php CI
- Difference between two strtotime and date in php and…
- Upload and Extract zip file in Code Igniter
- Finding a file with a specific name with any…
- Select data from sql where field not equal to zero…
… [Trackback]
[…] Find More Information here on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More Information here to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Info to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Here you can find 78108 more Info to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Find More Information here on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Find More Information here on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Here you will find 33446 more Information on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Here you will find 22077 additional Information to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Find More here on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More Info here to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] There you will find 11827 additional Information on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] There you can find 20823 additional Info on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More on to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] There you will find 61034 additional Information on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Information to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] There you can find 48080 additional Information to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Info on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More on that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] There you can find 48001 more Information to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Find More on to that Topic: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]
… [Trackback]
[…] Read More: siteinvokers.com/integrate-razorpay-in-php-codeigniter/ […]