Razorpay-integration-in-asp .net-MVC
Steps-
create asp.net mvc framework project in Visual Studio 2019
install Razorpay
paste it all code in the project as it is.
PaymentController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcFrameRazorPay.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_live_jR2zihWG8pg9Hw", "5zLPpowUbjr7mJ5FAOfuljf4");
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_live_jR2zihWG8pg9Hw",
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_live_jR2zihWG8pg9Hw", "5zLPpowUbjr7mJ5FAOfuljf4");
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();
}
}
}
PaymentInitiateModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcFrameRazorPay.Models
{
public class PaymentInitiateModel
{
public string name { get; set; }
public string email { get; set; }
public string contactNumber { get; set; }
public string address { get; set; }
public int amount { get; set; }
}
}
Index.cshtml
@*Create this model*@
@model MvcFrameRazorPay.Models.PaymentInitiateModel
@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Payment Initiate</title>
@*Link this css*@
<link href="~/css/style.css" rel="stylesheet" />
</head>
<body>
<div class="container login-container">
<div class="login-card">
<div class="login-card-body">
@using (Html.BeginForm("CreateOrder", "Payment", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="row">
<label for="name">Name</label>
@Html.TextBoxFor(x => x.name, new { @class = "form-control" })
</div>
<div class="row">
<label for="email">Email</label>
@Html.TextBoxFor(x => x.email, new { @class = "form-control" })
</div>
<div class="row">
<label for="contactNumber">Contact Number</label>
@Html.TextBoxFor(x => x.contactNumber, new { @class = "form-control" })
</div>
<div class="row">
<label for="address">Address</label>
@Html.TextBoxFor(x => x.address, new { @class = "form-control" })
</div>
<div class="row">
<label for="amount">Amount</label>
@Html.TextBoxFor(x => x.amount, new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Submit</button>}
</div>
</div>
</div>
</body>
</html>
PaymentPage.cshtml
@model MvcFrameRazorPay.Controllers.PaymentController.OrderModel
<!-- // Click this button automatically when this page load using javascript -->
<!-- You can hide this button -->
<button id="rzp-button1" hidden>Pay</button>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
"key": "@Html.DisplayFor(model => model.razorpayKey)", // Enter the Key ID generated from the Dashboard
"amount": "@Html.DisplayFor(model => model.amount)", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise
"currency": "@Html.DisplayFor(model => model.currency)",
"name": "@Html.DisplayFor(model => model.name)",
"description": "@Html.DisplayFor(model => model.description)",
"image": "https://example.com/your_logo", // You can give your logo url
"order_id": "@Html.DisplayFor(model => model.orderId)",
"handler": function (response){
// After payment successfully made response will come here
// Set the data in hidden form
document.getElementById('rzp_paymentid').value = response.razorpay_payment_id;
document.getElementById('rzp_orderid').value = response.razorpay_order_id;
// // Let's submit the form automatically
document.getElementById('rzp-paymentresponse').click();
},
"prefill": {
"name": "@Html.DisplayFor(model => model.name)",
"email": "@Html.DisplayFor(model => model.email)",
"contact": "@Html.DisplayFor(model => model.contactNumber)"
},
"notes": {
"address": "@Html.DisplayFor(model => model.address)"
},
"theme": {
"color": "#F37254"
}
};
var rzp1 = new Razorpay(options);
//<!-- onload function -->
window.onload = function(){
document.getElementById('rzp-button1').click();
};
document.getElementById('rzp-button1').onclick = function(e){
rzp1.open();
e.preventDefault();
}
</script>
<!-- This form is hidden, and submit when payment successfully made -->
@using (Html.BeginForm("Complete", "Payment"))
{
@Html.AntiForgeryToken()
@Html.Hidden("rzp_paymentid")
@Html.Hidden("rzp_orderid")
<button type="submit" id="rzp-paymentresponse" class="btn btn-primary" hidden>Submit</button>}
Success.cshtml
@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Success</title>
</head>
<body>
<div>
Success page
</div>
</body>
</html>
Failed.cshtml
@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Failed</title>
</head>
<body>
<div>
Failed page
</div>
</body>
</html>
Razorpay-integration-in-asp.net-mvc framework
Reviewed by Rikesh
on
May 08, 2022
Rating:
No comments: