2020
Payumoney Integration With Asp.Net MVC
PayuMoney-
It is a payments getway getting popular now a days. Actually there are few other payments getways like Stripe,Paypal. And PayuMoney is also getting more popular now a days.
So in this article we will learn how to use Pay u Money bolt for Payumoney Integration With Asp.Net MVC.
So for this puprpose you must have Client Key and Secret before starting integration of PayuMoney. You can go through https://www.payu.in/developer-guide. There you can generate merchant key for your project. After having merchant key and test credentials we can move to the next steps as described below:-
Step 1 : Paste the code in controller
So for this step you have to add below code snippet in you Asp.Net MVC controller. About the piece of code
surl- It is basically the url where you website will redirect after successfull payment. Or in short its the success url.
furl- It is basically the url where you website will redirect after failure of payment. Failure can happen due to wrong otp or wrong card details. So in that case it will go to furl.
Hash : PayuMoney bolt actually expects few parameters as mandatory. And after surl,furl Hash is a mandatory parameter. It is basically a encrypted form of values. You can use the action provided in code snippet for generating hash.
public ActionResult Index() { string surl = ((Request.ServerVariables["HTTPS"] != "off" && Request.ServerVariables["HTTP_HOST"] != "off") || Request.ServerVariables["SERVER_PORT"] == "443") ? "https://" : "http://"; surl += Request.ServerVariables["HTTP_HOST"] + Request.Url.AbsolutePath.Replace("ClientSlotBooking.aspx", "SuccessPayment.aspx"); string furl = ((Request.ServerVariables["HTTPS"] != "off" && Request.ServerVariables["HTTP_HOST"] != "off") || Request.ServerVariables["SERVER_PORT"] == "443") ? "https://" : "http://"; furl += Request.ServerVariables["HTTP_HOST"] + Request.Url.AbsolutePath.Replace("ClientSlotBooking.aspx", "FailurePayment.aspx"); ViewBag.surl = surl; ViewBag.furl = furl; return View(); } public ActionResult Hash(string txnid,string key,string salt,string amount,string pinfo,string fname,string email,string mobile,string udf5) { byte[] hash; string d = key + "|" + txnid + "|" + amount + "|" + pinfo + "|" + fname+ "|" + email + "|||||" + udf5 + "||||||" + salt; var datab = Encoding.UTF8.GetBytes(d); using (SHA512 shaM = new SHA512Managed()) { hash = shaM.ComputeHash(datab); } return Json( GetStringFromHash(hash) , JsonRequestBehavior.AllowGet); } private static string GetStringFromHash(byte[] hash) { StringBuilder result = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { result.Append(hash[i].ToString("X2").ToLower()); } return result.ToString(); }
Step 2: Add this script in _layout.cs.html
<script id="bolt" src="https://sboxcheckout-static.citruspay.com/bolt/run/bolt.min.js" bolt-color="e34524" bolt-logo="http://boltiswatching.com/wp-content/uploads/2015/09/Bolt-Logo-e14421724859591.png"></script>
Step 3: Paste the code in Index.cs.html
After completing the controller part. We can now move to the html side. So for using PayuMoney bolt we have to create a form and have to add few mandatory fields there as explained in code snippet. And if you missed any of those your PayuMoney bolt will not work.
@{ ViewBag.Title = "Home Page"; } <div class="jumbotron"> <h1>ASP.NET</h1> <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p> <p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p> </div> <div class="row"> <a onclick="launchBOLT()" id="btnRetry" class="btn btn-sm btn-warning btn-flat pull-right">Pay</a> <input type="hidden" id="bookingId" clientidmode="Static" value="1" /> <input type="hidden" id="hash" /> <input type="hidden" id="Totalamount" value="5" /> <input type="hidden" value="abc" clientidmode="Static" id="firstname" /> <input type="hidden" id="Email" /> <input type="hidden" id="Responseurl" /> <input type="hidden" id="Failureurl" /> <input type="hidden" id="saltcode" clientidmode="Static" name="saltcode" value="LXOnGmUFcu" /> <input type="hidden" id="key" clientidmode="Static" name="key" value="98vPTkPH" /> </div> <script> function launchBOLT() { var promise = $.ajax({ url: '/Home/Hash', type: 'post', data: JSON.stringify({ txnid: $('#bookingId').val(), key: $('#key').val(), salt: $('#saltcode').val(), amount: parseInt(5), pinfo: 'abc', fname: $('#firstname').val(), email: $('#Email').val(), mobile: '56457864898', udf5: 'dss', }), contentType: "application/json", dataType: 'json', success: function (json) { } }); promise.done(function (json) { $('#hash').val(json); bolt.launch({ txnid: $('#bookingId').val(), amount: parseInt(5), productinfo: 'abc', firstname: $('#firstname').val(), email: $('#Email').val(), phone: '56458664898', udf5: 'dss', hash: $('#hash').val(), key: $('#key').val(), surl: "@ViewBag.surl", furl:"@ViewBag.furl" }, { responseHandler: function (BOLT) { var salt = $('#saltcode').val(); if (BOLT.response.txnStatus != 'CANCEL') { //Salt is passd here for demo purpose only. For practical use keep salt at server side only. var fr = '<form action=\"' + "@ViewBag.surl" + '\" method=\"post\">' + '<input type=\"hidden\" name=\"key\" value=\"' + BOLT.response.key + '\" />' + '<input type=\"hidden\" name=\"salt\" value=\"' + salt + '\" />' + '<input type=\"hidden\" name=\"txnid\" value=\"' + BOLT.response.txnid + '\" />' + '<input type=\"hidden\" name=\"amount\" value=\"' + BOLT.response.amount + '\" />' + '<input type=\"hidden\" name=\"productinfo\" value=\"' + BOLT.response.productinfo + '\" />' + '<input type=\"hidden\" name=\"firstname\" value=\"' + BOLT.response.firstname + '\" />' + '<input type=\"hidden\" name=\"email\" value=\"' + BOLT.response.email + '\" />' + '<input type=\"hidden\" name=\"udf5\" value=\"' + BOLT.response.udf5 + '\" />' + '<input type=\"hidden\" name=\"mihpayid\" value=\"' + BOLT.response.mihpayid + '\" />' + '<input type=\"hidden\" name=\"status\" value=\"' + BOLT.response.status + '\" />' + '<input type=\"hidden\" name=\"hash\" value=\"' + BOLT.response.hash + '\" />' + '<input type=\"hidden\" name=\"furl\" value=\"' + BOLT.response.furl + '\" />' + '</form>'; var form = jQuery(fr); jQuery('body').append(form); form.submit(); return false; } else if (BOLT.response.txnStatus != 'FAILED') { //Salt is passd here for demo purpose only. For practical use keep salt at server side only. var fr = '<form action=\"' + "@ViewBag.furl" + '\" method=\"post\">' + '<input type=\"hidden\" name=\"key\" value=\"' + $('#bookingId').val() + '\" />' + '<input type=\"hidden\" name=\"salt\" value=\"' + salt + '\" />' + '<input type=\"hidden\" name=\"txnid\" value=\"' + $('#bookingId').val() + '\" />' + '<input type=\"hidden\" name=\"amount\" value=\"' + BOLT.response.amount + '\" />' + '<input type=\"hidden\" name=\"productinfo\" value=\"' + BOLT.response.productinfo + '\" />' + '<input type=\"hidden\" name=\"firstname\" value=\"' + BOLT.response.firstname + '\" />' + '<input type=\"hidden\" name=\"email\" value=\"' + BOLT.response.email + '\" />' + '<input type=\"hidden\" name=\"udf5\" value=\"' + BOLT.response.udf5 + '\" />' + '<input type=\"hidden\" name=\"mihpayid\" value=\"' + BOLT.response.mihpayid + '\" />' + '<input type=\"hidden\" name=\"status\" value=\"' + BOLT.response.status + '\" />' + '<input type=\"hidden\" name=\"hash\" value=\"' + BOLT.response.hash + '\" />' + '<input type=\"hidden\" name=\"furl\" value=\"' + BOLT.response.furl + '\" />' + '</form>'; var form = jQuery(fr); jQuery('body').append(form); form.submit(); return false; } }, catchException: function (BOLT) { console.log(BOLT); alert(BOLT.message); } }); }); } </script>
Final Output
Test Card Detail
Card Type: Master
Card Name: Test
Card Number: 5123456789012346
Expiry Date: 05/21
CVV: 123
OTP : 123456