PayU Money with ASP.NET

We were struggling with PayUMoney integration with .Net code however steps are simple PayU provides API and documentation implementation. There are Couple of setting which is not in PayU documentation. You can use following document step by step complete process to set up your Payment gateway.

Here are the steps you need to follow to Integrated PayU with .NET (C#):

Create Pay now button in Your UI. These images below show example how we have done in our Application. You need to just change the connection string in your Case.

1

By clicking on “Pay Now” Application call PayNow() method

You have to write method like below. This method is provide by Paynow

Let me describe what parameter you will have to change in your case.

Note : The one in lined  in yellow are input parameter you to pass PayU function. These parameter would be in same syntax as shown below however it would change for your payU account and code.

Let me again define the parameters.

  1. In most of the case when you are setting payment mechanism in your application these are the parameter you ask customer to fill. You have to pass these parameter to Pay you. You can find in case of our application we have used below parameter which are being passed to pay you to process payment.
UserName = labTest.PatientName;
Productprice = labTest.PayablePrice.ToString();
productInfo = "Lab Product Purchase Online";
Useremail = labTest.Email;
phoneNumber = 9134567892;
  1. var DomainName = Web.HttpContext.Current.Request.Url.Host;

This is for fetching the URL of application where application is hosted. In   case your Application is hosted at www.abcd.com it will return abcd.

myremotepost.Add("surl", "http://" + DomainName);

Here it will input your Url as http://abcd.com

  1. All these three setting you will get from your PayU account.
<add key="MERCHANT_KEY" value="fB7m8s"/>
<add key="SALT" value="eRis5Chv"/>
<add key="PAYU_BASE_URL" value="https://test.payu.in/_payment"/>

Next step is to call  PayNow() method


public void PayNow()
{
//initialize
tokenContext.setPaymentMessageSession(null);
ViewData["PaymentMessage"] = null;
EMedical.DataModel.UserLabTest labTest = tokenContext.getCurrentUserTest();
if (labTest != null)
{
string firstName = labTest.PatientName;
string amount = labTest.PayablePrice.ToString();
string productInfo = "Lab Product Purchase Online";
string email = labTest.Email;
string phone = labTest.ContactNumber;
RemotePost myremotepost = new RemotePost();
string key = ConfigurationManager.AppSettings["MERCHANT_KEY"];
string salt = ConfigurationManager.AppSettings["SALT"];
var DomainName = System.Web.HttpContext.Current.Request.Url.Host;
//posting all the parameters required for integration.
myremotepost.Url = ConfigurationManager.AppSettings["PAYU_BASE_URL"];
myremotepost.Add("key", ConfigurationManager.AppSettings["MERCHANT_KEY"]);
string txnid = Generatetxnid();
myremotepost.Add("txnid", txnid);
myremotepost.Add("amount", amount);
myremotepost.Add("productinfo", productInfo);
myremotepost.Add("firstname", firstName);
myremotepost.Add("phone", phone);
myremotepost.Add("email", email);
//myremotepost.Add("surl", "http://localhost:3677/User/Return");//Change the success url here depending upon the port number of your local system.
//myremotepost.Add("furl", "http://localhost:3677/User/Return");//Change the failure url here depending upon the port number of your local system.
myremotepost.Add("surl", "http://" + DomainName + "/User/Return");//Change the success url here depending upon the port number of your local system.
myremotepost.Add("furl", "http://" + DomainName + "/User/Return");//Change the failure url here depending upon the port number of your local system.
myremotepost.Add("service_provider", "payu_paisa");
string hashString = key + "|" + txnid + "|" + amount + "|" + productInfo + "|" + firstName + "|" + email + "|||||||||||" + salt;
//string hashString = "3Q5c3q|2590640|3053.00|OnlineBooking|vimallad|ladvimal@gmail.com|||||||||||mE2RxRwx";
string hash = Generatehash512(hashString);
myremotepost.Add("hash", hash);
myremotepost.Post();
}
}

view raw

payu-pay-now

hosted with ❤ by GitHub

Please define PayUMoney Merchant key and SALT in your web config file like below.


<appSettings>
<!– Pay U Money –>
<add key=”MERCHANT_KEY” value=”fB7m8s”/>
<add key=”SALT” value=”eRis5Chv”/>
<add key=”PAYU_BASE_URL” value=”https://test.payu.in/_payment”/>
<!–<add key=”MERCHANT_KEY” value=”oHwauK”/>
<add key=”SALT” value=”iciXDaDA”/>
<add key=”PAYU_BASE_URL” value=”https://secure.payu.in/_payment”/>–>
<add key=”action” value=””/>
<add key=”hashSequence” value=”key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10″/>

Clicking on save executed  PayNow() Method and redirected  to User PayU login screen.

2

User need to fill payment details selecting payment method.3

After payment user can see its transaction details on PuyUMoney. 4

Once payment is complete  PayUMoney redirect to Retun() Method and read transactionId

from FormCollection


[AllowAnonymous]
public ActionResult Return(FormCollection form=null)
{
try
{
string[] merc_hash_vars_seq;
string merc_hash_string = string.Empty;
string merc_hash = string.Empty;
string order_id = string.Empty;
string hash_seq = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
if (form["status"].ToString() == "success")
{
merc_hash_vars_seq = hash_seq.Split('|');
Array.Reverse(merc_hash_vars_seq);
merc_hash_string = ConfigurationManager.AppSettings["SALT"] + "|" + form["status"].ToString();
foreach (string merc_hash_var in merc_hash_vars_seq)
{
merc_hash_string += "|";
merc_hash_string = merc_hash_string + (form[merc_hash_var] != null ? form[merc_hash_var] : "");
}
Response.Write(merc_hash_string);
merc_hash = Generatehash512(merc_hash_string).ToLower();
//if (merc_hash != form["hash"])
//{
//    Response.Write("Hash value did not matched");
//}
//else
//{
order_id = Request.Form["txnid"];
//ViewData["Message"] = "Status is successful. Hash value is matched";
//Response.Write("<br/>Hash value matched");
//Hash value did not matched
//    }
}
else
{
tokenContext.setPaymentMessageSession("Payment Fails! Order Pending to save.");
//Response.Write("Hash value did not matched");
// osc_redirect(osc_href_link(FILENAME_CHECKOUT, 'payment' , 'SSL', null, null,true));
}
}
catch (Exception ex)
{
ViewData["PaymentMessage"] = "Payment Fails! Order Pending to save.";
//Response.Write("<span style='color:red'>" + ex.Message + "</span>");
}
return RedirectToAction("PrescriptionDashBoard");
}

Using the payment method returned by PayU you can keep track of transaction ID and show success or failure message in your UI.

Complete Code


<appSettings>
<!– Pay U Money –>
<add key=”MERCHANT_KEY” value=”fB7m8s”/>
<add key=”SALT” value=”eRis5Chv”/>
<add key=”PAYU_BASE_URL” value=”https://test.payu.in/_payment”/>
<add key=”action” value=””/>
<add key=”hashSequence” value=”key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10″/>
</appSettings>


public void PayNow()
{
//initialize
tokenContext.setPaymentMessageSession(null);
ViewData["PaymentMessage"] = null;
EMedical.DataModel.UserLabTest labTest = tokenContext.getCurrentUserTest();
if (labTest != null)
{
string firstName = labTest.PatientName;
string amount = labTest.PayablePrice.ToString();
string productInfo = "Lab Product Purchase Online";
string email = labTest.Email;
string phone = labTest.ContactNumber;
RemotePost myremotepost = new RemotePost();
string key = ConfigurationManager.AppSettings["MERCHANT_KEY"];
string salt = ConfigurationManager.AppSettings["SALT"];
var DomainName = System.Web.HttpContext.Current.Request.Url.Host;
//posting all the parameters required for integration.
myremotepost.Url = ConfigurationManager.AppSettings["PAYU_BASE_URL"];
myremotepost.Add("key", ConfigurationManager.AppSettings["MERCHANT_KEY"]);
string txnid = Generatetxnid();
myremotepost.Add("txnid", txnid);
myremotepost.Add("amount", amount);
myremotepost.Add("productinfo", productInfo);
myremotepost.Add("firstname", firstName);
myremotepost.Add("phone", phone);
myremotepost.Add("email", email);
myremotepost.Add("surl", "http://" + DomainName + "/User/Return");//Change the success url here depending upon the port number of your local system.
myremotepost.Add("furl", "http://" + DomainName + "/User/Return");//Change the failure url here depending upon the port number of your local system.
myremotepost.Add("service_provider", "payu_paisa");
string hashString = key + "|" + txnid + "|" + amount + "|" + productInfo + "|" + firstName + "|" + email + "|||||||||||" + salt;
string hash = Generatehash512(hashString);
myremotepost.Add("hash", hash);
myremotepost.Post();
}
}
[AllowAnonymous]
public ActionResult Return(FormCollection form=null)
{
try
{
string[] merc_hash_vars_seq;
string merc_hash_string = string.Empty;
string merc_hash = string.Empty;
string order_id = string.Empty;
string hash_seq = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
if (form["status"].ToString() == "success")
{
merc_hash_vars_seq = hash_seq.Split('|');
Array.Reverse(merc_hash_vars_seq);
merc_hash_string = ConfigurationManager.AppSettings["SALT"] + "|" + form["status"].ToString();
foreach (string merc_hash_var in merc_hash_vars_seq)
{
merc_hash_string += "|";
merc_hash_string = merc_hash_string + (form[merc_hash_var] != null ? form[merc_hash_var] : "");
}
Response.Write(merc_hash_string);
merc_hash = Generatehash512(merc_hash_string).ToLower();
order_id = Request.Form["txnid"];
}
else
{
tokenContext.setPaymentMessageSession("Payment Fails! Order Pending to save.");
}
}
catch (Exception ex)
{
ViewData["PaymentMessage"] = "Payment Fails! Order Pending to save.";
}
return RedirectToAction("ActionName");
}

view raw

Payu-dotnet.cs

hosted with ❤ by GitHub