Skip to Content
PHPCODE
CODE Razorpay Payment Gateway Integration in PHP
php code / September 8, 2018

CODE Razorpay Payment Gateway Integration in PHP

NOTE :- Your Razorpay Keyid and Your Razorpay Key Secret Library Used
NOTE :- libs used this one razorpay-php
Step 1 : Created config File

$keyId = 'Your Razorpay Keyid';
$keySecret = 'Your Razorpay Key Secret';
$displayCurrency = 'INR';
error_reporting(E_ALL);
ini_set('display_errors', 1);

Step 2 :Created header.php File

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- jQuery -->
Step 3 :Created footer.php File
<div class="insert-post-ads1" style="margin-top:20px;">
</div>
</div>
</body></html>
Step 4 :- Created Container.php file
</head>
<body class="">
<div role="navigation" class="navbar navbar-default navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a href="https://phpcodeinfomation.blogspot.com/p/php-script.html" class="navbar-brand">PHPCODE</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="https://phpcodeinfomation.blogspot.com/p/php-script.html">Home</a></li>
          </ul>
      </div><!--/.nav-collapse -->
      </div>
    </div>
<div class="container" style="min-height:500px;">
<div class=''>
</div>
 Step 5 :Created index.php file
<?php
include('header.php');
?>
<title>phpcode : CODE Razorpay Payment Gateway Integration in PHP</title>
<?php include('container.php');?>
<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://phpcodeinfomation.blogspot.com/p/php-script.html">PHPCODE</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>
<?php include('footer.php');?>
Step 6 : Created pay.php file
<?php
include('header.php');
?>
<title>phpcode : code Razorpay Payment Gateway Integration in PHP</title>
<?php include('container.php');?>
<div class="container">
<div class="row">
<h2>Example: Razorpay Payment Gateway Integration in PHP</h2>
<br><br><br>
<?php
require('config.php');
require('razorpay-php/Razorpay.php');
session_start();
use RazorpayApiApi;
$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'];
if ($displayCurrency !== 'INR') {
    $url = "https://api.fixer.io/latest?symbols=$displayCurrency&base=INR";
    $exchange = json_decode(file_get_contents($url), true);
$displayAmount = $exchange['rates'][$displayCurrency] * $amount / 100;
}
$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,
];
if ($displayCurrency !== 'INR')
{
    $data['display_currency']  = $displayCurrency;
    $data['display_amount']    = $displayAmount;
}
$json = json_encode($data);
require("checkout/manual.php");
?>
</div>
<?php include('footer.php');?>
Step 7 :-Created verify.php file
<?php
require('config.php');
session_start();
require('razorpay-php/Razorpay.php');
use RazorpayApiApi;
use RazorpayApiErrorsSignatureVerificationError;
$success = true;
$error = "Payment Failed";
if (empty($_POST['razorpay_payment_id']) === false)
{
    $api = new Api($keyId, $keySecret);
try
    {
        // Please note that the razorpay order ID must
        // come from a trusted source (session here, but
        // could be database or something else)
        $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)
{
    $html = "<p>Your payment was successful</p>
             <p>Payment ID: {$_POST['razorpay_payment_id']}</p>";
}
else
{
    $html = "<p>Your payment failed</p>
             <p>{$error}</p>";
}
echo $html;
Step 8 :checkout folder created and manual.php Created
<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>
Step 9  : show image
PHPCODE © 2024