On button click I want the user to redirect to payumoney gateway and if payment has been done successfully, the data of user to be store in database and redirect user to success page vice a versa..
Currently I am saving data using AJAX call and redirect user on ajax success:function.
AJAX
$('#payNowButton').on("click", function () {
var total = $('#main_total_s5').html();
console.log("Total : " + total);
$.ajax({
url: '/Home/Demo?total=' + total, //Demo method in Home Controller is for payumoney payment gateway.
type: "POST",
data: total,
async: false,
success: function (result) {
window.location.href = result;
if (result > 0) {
PlaceOrder(); //after successful payment, it will call this function to save data in database.
window.location.href = '@Url.Action("Sucess", "Home")';
}
else {
alert("Some error occurred." + result);
}
},
error: function (result) {
window.location.href = '@Url.Action("Failure", "Home")';
}
});
});
Controller
[HttpPost]
public void Demo(OrderCustom orderCustom)
{
string firstName = "";
string amount = "";
string productInfo = "";
string email = "";
string phone = "";
string surl;
string furl;
RemotePost myremotepost = new RemotePost();
string key = "";
string salt = "";
//posting all the parameters required for integration.
myremotepost.Url = "https://secure.payu.in/_payment";
myremotepost.Add("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:/Home/Sucess");//Change the success url here depending upon the port number of your local system.
myremotepost.Add("furl", "http://localhost:/Home/Failure");//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();
}
public class RemotePost
{
private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
public string Url = "";
public string Method = "post";
public string FormName = "form1";
public void Add(string name, string value)
{
Inputs.Add(name, value);
}
public void Post()
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("<html><head>");
System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
for (int i = 0; i < Inputs.Keys.Count; i++)
{
System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
}
System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body></html>");
System.Web.HttpContext.Current.Response.End();
}
}
When I click on button it shows this error : Error Image