Razorpay Payment Gateway Integration in PHP

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.

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.

Example: Razorpay Payment Gateway Integration in PHP

₹49.99

  My Test Product"  

See more examples like this at phpzag.

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.

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.

$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.

Pay with Razorpay

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

Contact : support@erps.in (OR) uma@f5craft.com / +91 87784 09644 (OR) +91 93443 88665