I am doing a eCommerce website and it’s almost done. I want to integrate razorpay gateway to website. How can I implement the razor pay integration code in PHP? Can anyone showing an example code for integration of razor pay using php code?
Share
Swathi
Razorpay allows you to grow your business and power your finance. Razorpay is a user-friendly payment gateway that provides payment solutions in an easy way. To integrate Razorpay gateway in your e-commerce project using PHP, follow the below steps:
Step 1: Create Razorpay Account
Create a Razorpay account and generate KeyID and secret key. This is done by selecting the setting option->API key. Then it asks KeyID and secret key.
To test payment functionality let us keep creating the Razorpay account in Test mode. If you want to test in live mode then KYC is asked, so that KYC form filling should be completed and activation of your account will be done.
This is done after account creation and in the Razorpay dashboard. To know the process of creating a Razorpay account, go through the below link.
How to create an account on Razorpay?
Step2: Update Razorpay config details:
If we use “Test app” to integrate the Razorpay gateway, we need to update config.php with key ID and secret key from Razorpay.
Step 3: Create a form with Item details.
In index.php file, we will create HTML code with item details and customer details to send to perform payment. We have pay.php on action to handle the 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>
Step 4: Handle item details for payment.
Step 5:Perform payment with Razorpay:
We will include Razorpay API checkout.js and handle payment functionality. The sample Php code appears like this:
<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>
I hope the above sample code might help you. You may also read:
Do you need math for software engineering?