In this article, you will learn Razorpay Payment Gateway Integration in ASP.NET MVC web application or an eCommerce website using C#. With Razorpay, you have access to all payment modes, including credit and debit cards, UPI, and popular mobile wallets.
- You may be interested in Paypal Payment Gateway Integration in ASP.NET Core
To check the Razorpay Payment Gateway demo, please click here:
How to integrate Razorpay Payment Gateway in ASP.NET
The Razorpay Payment Gateway enables you to accept payments via debit card, credit card, net banking (supports 3D Secure), UPI, or through any of our supported wallets. Refer to the Payment Methods section for a list of payment methods we support.
Find the below steps to integrate Razorpay in your website:-
Step 1: Create/ Login to your Razorpay account dashboard and go to Settings -> API Keys Tab and Generate a key for the selected mode (Test or Live) as shown below:
Note: You have to generate separate API Keys for the test and live modes. No real money is used in test mode.
After generating the keys from the Dashboard, download and save them securely. If you do not remember your API Keys, you need to re-generate it from the Dashboard and replace it wherever required.

Step 2: Go to Tools -> Open the Nuget Package Manager and search Razorpay. You will find the Razorpay package with the latest version. Now you need to install it in your ASP.NET MVC application.

Step 3: Create an Order
For creating order in Razorpay make sure that you have the details of mandatory fields as follows:-
- Customer Name.
- Email ID.
- Contact/Mobile Number.
- Address.
- Total Amount.
- Receipt Number (will be a unique id that you need to create for every order).
Find the below code respectively:-
PaymentController
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace razorpaydemo.Controllers { public class PaymentController : Controller { // GET: Payment public ActionResult Index() { return View(); } [HttpPost] public ActionResult CreateOrder(Models.PaymentInitiateModel _requestData) { // Generate random receipt number for order Random randomObj = new Random(); string transactionId = randomObj.Next(10000000, 100000000).ToString(); Razorpay.Api.RazorpayClient client = new Razorpay.Api.RazorpayClient("rzp_test_umbrFAbJ3slyJ", "su9eXFaihGucmKECVRcRk0Q"); Dictionary<string, object> options = new Dictionary<string, object>(); options.Add("amount", _requestData.amount * 100); // Amount will in paise options.Add("receipt", transactionId); options.Add("currency", "INR"); options.Add("payment_capture", "0"); // 1 - automatic , 0 - manual //options.Add("notes", "-- You can put any notes here --"); Razorpay.Api.Order orderResponse = client.Order.Create(options); string orderId = orderResponse["id"].ToString(); // Create order model for return on view OrderModel orderModel = new OrderModel { orderId = orderResponse.Attributes["id"], razorpayKey = "rzp_test_umbrFAbVJ3slyJ", amount = _requestData.amount * 100, currency = "INR", name = _requestData.name, email = _requestData.email, contactNumber = _requestData.contactNumber, address = _requestData.address, description = "Testing description" }; // Return on PaymentPage with Order data return View("PaymentPage", orderModel); } public class OrderModel { public string orderId { get; set; } public string razorpayKey { get; set; } public int amount { get; set; } public string currency { get; set; } public string name { get; set; } public string email { get; set; } public string contactNumber { get; set; } public string address { get; set; } public string description { get; set; } } [HttpPost] public ActionResult Complete() { // Payment data comes in url so we have to get it from url // This id is razorpay unique payment id which can be use to get the payment details from razorpay server string paymentId = Request.Params["rzp_paymentid"]; // This is orderId string orderId = Request.Params["rzp_orderid"]; Razorpay.Api.RazorpayClient client = new Razorpay.Api.RazorpayClient("rzp_test_umbrFAbVJ3lyJ", "su9eXFaihGucMmKEVRcRk0Q"); Razorpay.Api.Payment payment = client.Payment.Fetch(paymentId); // This code is for capture the payment Dictionary<string, object> options = new Dictionary<string, object>(); options.Add("amount", payment.Attributes["amount"]); Razorpay.Api.Payment paymentCaptured = payment.Capture(options); string amt = paymentCaptured.Attributes["amount"]; //// Check payment made successfully if (paymentCaptured.Attributes["status"] == "captured") { // Create these action method return RedirectToAction("Success"); } else { return RedirectToAction("Failed"); } } public ActionResult Success() { return View(); } public ActionResult Failed() { return View(); } } }
References:
Conclusion
I hope you liked this article on Razorpay Payment Gateway Integration in ASP.NET MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave a Reply