I have to do integration with a payment gateway when we do checkout we need to process order on our side and then we need to post to payment gateway url. I cannot use redirect in controller b/c need to send data via post to paymentgateway provider after processing order on our server side so in cshtml the default post will be to the action where we process Order after that inside controller i need to do a post again to payment gateway that will change the url and credit card information will be taken and the control come back to our site again after credit card information is taken by provider. i hope i m clear in my question
after saving order i need to move browser to PAYU page this doesnt work b/c MerchantReferenceNumber is sent via post and redirect is access denied.
private void PostFormToPayU()
{
string url = "https://secure.safeshop.co.za/SafePay/Lite/Index.asp";
var webClient = new WebClient();
try
{
NameValueCollection vals = new NameValueCollection();
vals.Add("SafeKey", "{XXXX-XXX-XXX-XX-XXXXXX}");
vals.Add("MerchantReferenceNumber", "Test2");
vals.Add("TransactionAmount", "599");
vals.Add("CurrencyCode", "ZAR");
vals.Add("ReceiptURL", "http://localhost:47638/Home/About");
vals.Add("FailURL", "http://localhost:47638/Home/New");
vals.Add("TransactionType", "Auth");
byte[] responseArray = webClient.UploadValues(url, vals);
Stream s = new MemoryStream(responseArray);
Redirect("https://secure.safeshop.co.za/SafePay/Lite/Index.asp");
//return new System.Net.Response(responseArray, (int)HttpStatusCode.OK);
Console.WriteLine("stop");
}
catch (WebException e)
{
var response = (HttpWebResponse)e.Response;
//byte[] responseBytes = IOUtil.StreamToBytes(response.GetResponseStream());
//return new Response(responseBytes, (int)response.StatusCode);
Console.WriteLine("stop");
}
}
I think that you simply need to setup a web request (WebRequest.Create) to create a HttpWebRequest. This can be created with the URL of the system you are trying to post to.
You will need to wrap up the data coming from your web site into the format of the expected message and write this to the request stream.
This is it in a nutshell, obviously there is more to it in terms of detail.
Also you might want to consider moving this kind of processing out of the controller into some kind of service, as it is fairly innvolved.
Till now i think the best solution is
var context = HttpContext.Current;
context.Response.Clear();
context.Response.Write("<html><head>");
context.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
context.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
for (int i = 0; i < inputValues.Keys.Count; i++)
context.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", HttpUtility.HtmlEncode(inputValues.Keys[i]), HttpUtility.HtmlEncode(inputValues[inputValues.Keys[i]])));
context.Response.Write("</form>");
context.Response.Write("</body></html>");
context.Response.End();
After i saved the order in database i can render html page and submit the form onload(). inputValues is a name value collection i needed to pass to payment gateway.