Integrate Razorpay UPI in ASP.NET to Fetch Bank Details by IFSC Code

Integrate Razorpay UPI in ASP.NET to Fetch Bank Details by IFSC Code
Integrate Razorpay UPI in ASP.NET to Fetch Bank Details by IFSC Code

In the world of digital payments, integrating UPI (Unified Payments Interface) has become a necessity for many web applications. Razorpay offers a seamless way to integrate UPI payments in your ASP.NET applications. In this guide, we’ll walk you through the steps to fetch bank details using an IFSC code through Razorpay's API.

Prerequisites

  • Basic knowledge of ASP.NET and C#.
  • A Razorpay account with API keys.
  • Visual Studio or any other IDE for development.
  • NuGet Package Manager to install required libraries.

Step 1: Create a Razorpay Account

First, sign up on the Razorpay website. After logging in, navigate to the API Keys section under the Settings tab to generate your API Key ID and Secret.

Step 2: Set Up ASP.NET Project

Create a new ASP.NET Web Application in Visual Studio:

File > New Project > ASP.NET Web Application > MVC

Install Required Packages

Open the Package Manager Console and run:

Install-Package RestSharp

The RestSharp package will be used for making HTTP requests to the Razorpay API.

Step 3: Razorpay API Integration

Use the Razorpay API to fetch bank details using the IFSC code. The endpoint for this operation is:

GET https://ifsc.razorpay.com/{IFSCCode}

This endpoint returns a JSON response containing bank details.

ASP.NET Controller Code

Create a controller named RazorpayController.cs:


using System;
using System.Net;
using System.Web.Mvc;
using RestSharp;

namespace RazorpayIntegration.Controllers
{
    public class RazorpayController : Controller
    {
        private const string RazorpayBaseUrl = "https://ifsc.razorpay.com/";

        public ActionResult FetchBankDetails(string ifsc)
        {
            if (string.IsNullOrEmpty(ifsc))
                return Json(new { error = "IFSC code is required" }, JsonRequestBehavior.AllowGet);

            var client = new RestClient(RazorpayBaseUrl);
            var request = new RestRequest(ifsc, Method.GET);

            IRestResponse response = client.Execute(request);
            
            if (response.StatusCode == HttpStatusCode.OK)
                return Json(new { data = response.Content }, JsonRequestBehavior.AllowGet);
            else
                return Json(new { error = "Invalid IFSC code or request failed" }, JsonRequestBehavior.AllowGet);
        }
    }
    

ASP.NET View Code

Create a simple Razor view FetchBankDetails.cshtml:


@{
    ViewBag.Title = "Fetch Bank Details";
}

<h2>Fetch Bank Details Using IFSC Code</h2>
<input type="text" id="ifscCode" placeholder="Enter IFSC Code" />
<button onclick="getBankDetails()">Get Details</button>

<div id="result"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function getBankDetails() {
    var ifsc = $('#ifscCode').val();
    $.ajax({
        url: '/Razorpay/FetchBankDetails?ifsc=' + ifsc,
        method: 'GET',
        success: function (response) {
            $('#result').html(response.data || response.error);
        },
        error: function () {
            alert('Error fetching details');
        }
    });
}
</script>
    

Step 4: Testing the Integration

Run your application and navigate to the view. Enter a valid IFSC code (e.g., HDFC0000123) and click the Get Details button. The bank details will be displayed on the screen.

Conclusion

Integrating Razorpay UPI in your ASP.NET application allows you to easily fetch bank details using IFSC codes. This functionality is useful in scenarios like pre-filling payment forms or validating bank details during a transaction. With Razorpay's simple API, the integration process is smooth and efficient.

FAQs

1. Do I need to authenticate for Razorpay’s IFSC API?

No, fetching bank details using the IFSC API doesn’t require authentication.

2. Can I use this method for production?

Yes, but ensure that you handle exceptions and validate user inputs properly before using it in a live environment.

3. What happens if an invalid IFSC code is entered?

The API will return an error message indicating that the IFSC code is not found.

Post a Comment

0 Comments