Razorpay Payment Gateway Integration in PHP

Razorpay is the most user friendly payment gateway that’s provides payment solutions in India . It’s a new entry in payment gateways and getting popular very fast due to its friction less transactions functionality for web and mobile. Razorpay provides clean, fast, secure payments services with hassle free integration with developer friendly APIs. It allows online business to accept and process payments payments modes like Debit Card, Credit Card, Net Banking, UPI and PrePaid Digital Wallets.

Also, read:

So if you’re looking for tutorial to integrate Razorpay payment gateway in your project using PHP, then you’re here at right place. In this tutorial you will learn how to integrate Razorpay payment gateway using PHP in easy steps with live example.

Step1: 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.

Step2: 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.


Step3: Create Form with Item Details
In index.php file, we will create HTML of test item with item details and customer details to send to perform payment. Here we have pay.php on action to handle payment.

<div class="container">
	<div class="row">		
		<div class="col-sm-12">	
			<h2>Example: Razorpay Payment Gateway Integration in PHP</h2>
			<br><br>
			<div class="col-sm-4 col-lg-4 col-md-4">
				<div class="thumbnail">
					<img src="prod.gif" alt="">
					<div class="caption">
						<h4 class="pull-right">₹49.99</h4>
						<h4><a href="#">My Test Product"</a></h4>
						<p>See more examples like this at <a target="_blank" href="https://www.phpzag.com/">phpzag</a>.</p>
					</div>
					<form id="checkout-selection" action="pay.php" method="POST">		
						<input type="hidden" name="item_name" value="My Test Product">
						<input type="hidden" name="item_description" value="My Test Product Description">
						<input type="hidden" name="item_number" value="3456">
						<input type="hidden" name="amount" value="49.99">
						<input type="hidden" name="address" value="ABCD Address">
						<input type="hidden" name="currency" value="INR">	
						<input type="hidden" name="cust_name" value="phpzag">								
						<input type="hidden" name="email" value="test@phpzag.com">	
						<input type="hidden" name="contact" value="9999999999">								
						<input type="submit" class="btn btn-primary" value="Buy Now">					
					</form>						
				</div>
			</div>
		</div>
	</div>	
</div>

Step4: Handle Item Details for Payment
In pay.php file, we will include config.php and Razorpay API files. We will also start session to store razorpay_order_id into session. We will create order data with payment amount and currency to generate Razorpay order id.

<?php
require('config.php');
require('razorpay-php/Razorpay.php');
session_start();
use Razorpay\Api\Api;
$api = new Api($keyId, $keySecret);
$orderData = [
    'receipt'         => 3456,
    'amount'          => $_POST['amount'] * 100,
    'currency'        => $_POST['currency'],
    'payment_capture' => 1
];
$razorpayOrder = $api->order->create($orderData);
$razorpayOrderId = $razorpayOrder['id'];
$_SESSION['razorpay_order_id'] = $razorpayOrderId;
$displayAmount = $amount = $orderData['amount'];
?>

We will store all payment details into an array to encode into JSON and return to manual.php to perform payment.

<?php
$data = [
    "key"               => $keyId,
    "amount"            => $amount,
    "name"              => $_POST['item_name'],
    "description"       => $_POST['item_description'],
    "image"             => "",
    "prefill"           => [
    "name"              => $_POST['cust_name'],
    "email"             => $_POST['email'],
    "contact"           => $_POST['contact'],
    ],
    "notes"             => [
    "address"           => $_POST['address'],
    "merchant_order_id" => "12312321",
    ],
    "theme"             => [
    "color"             => "#F37254"
    ],
    "order_id"          => $razorpayOrderId,
];
?>

Step5: Perfrom Payment with Razorpay
In manual.php file, we will have JSON data to perform payment. We will include Razorpay API checkout.js and handle payment functionality.

<button id="rzp-button1" class="btn btn-primary">Pay with Razorpay</button>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<form name='razorpayform' action="verify.php" method="POST">
    <input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id">
    <input type="hidden" name="razorpay_signature"  id="razorpay_signature" >
</form>
<script>
var options = <?php echo $json?>;
options.handler = function (response){
    document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id;
    document.getElementById('razorpay_signature').value = response.razorpay_signature;
    document.razorpayform.submit();
};
options.theme.image_padding = false;
options.modal = {
    ondismiss: function() {
        console.log("This code runs when the popup is closed");
    },
    escape: true,
    backdropclose: false
};
var rzp = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e){
    rzp.open();
    e.preventDefault();
}
</script>

You may also like:


You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download

One thought on “Razorpay Payment Gateway Integration in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *