[英]How to render HTML response that returned from a Payment gateway using asp.net mvc
I am trying to integrate with a Payment gateway called PayFort, everything went okay and the method I that I used returns HTML code, which will be the page that a user should see to proceed in the payment process..我正在尝试与名为 PayFort 的支付网关集成,一切顺利,我使用的方法返回 HTML 代码,这将是用户在支付过程中应该看到的页面。
What I need is how to render that HTML response into the browser, I investigated about some solutions and all of them are using StreamReader and Writer I tried it by calling the Payment method URL directly by the browser and it worked perfectly, but when I tried to call it from JS/Ajax it didn't do any action, it didn't launch the HTML response.我需要的是如何将 HTML 响应呈现到浏览器中,我调查了一些解决方案,所有解决方案都在使用 StreamReader 和 Writer 我通过浏览器直接调用 Payment 方法 URL 进行了尝试,它运行良好,但是当我尝试时要从 JS/Ajax 调用它,它没有执行任何操作,也没有启动 HTML 响应。
Below is the code that I used to integrate with the Payment Gateway:下面是我用来与支付网关集成的代码:
public string TryPayment(int ID)
{
var BaseURL = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
setConfig();
api_url = Command.GetAPIURL(Command.IntegrationTypes.Redirect, true);
package = Umbraco.Content(ID);
int price = Convert.ToInt32(package.Value("price"));
VALUE = price;
MyReference = ("MyReference" + (DateTime.Now).ToString()).Replace(" ", "").Replace(":", "").Replace("/", "");
createSignature(MyReference, VALUE);
var newdata = "command=PURCHASE" +
"&access_code=My Code" +
"&merchant_identifier=My Identifier" +
"&merchant_reference=" + MyReference +
"&customer_email=Name@email.com" +
"&amount=" + VALUE +
"¤cy=JOD&language=ar" +
"&return_url=" + BaseURL + "umbraco/surface/FortResponse/working" +
"&signature=" + signature;
byte[] dataBytes = Encoding.UTF8.GetBytes(newdata);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://sbcheckout.payfort.com/FortAPI/paymentPage");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.ContentLength = dataBytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
using (Stream requestBody = request.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(response.CharacterSet)))
{
return reader.ReadToEnd();
}
}
Which works when I call it by the browser but doesn't when I call it by JS/Ajax.当我通过浏览器调用它时它有效,但当我通过 JS/Ajax 调用它时不起作用。
Any insight would be appreciated.任何见解将不胜感激。
After investigation i found that we can just include a form from front-side like this example: Pay-fort SDK example ,经过调查,我发现我们可以像这个例子一样从前端包含一个表单: Pay-fort SDK example ,
And no need to render the HTML response from back-side.并且无需从后端呈现 HTML 响应。
Try below ways试试下面的方法
1) Use javaScript success: function(data) { window.location.href="nextstep.aspx"; }
1)使用javaScript success: function(data) { window.location.href="nextstep.aspx"; }
success: function(data) { window.location.href="nextstep.aspx"; }
or或者
2) Insert/update response into database. 2) 将响应插入/更新到数据库中。 Redirect user from payment page to VerifyPayment page and get database value here.将用户从支付页面重定向到 VerifyPayment 页面并在此处获取数据库值。
Note: Deploy your application and check, sometimes Ajax calls does not work in local.注意:部署您的应用程序并检查,有时 Ajax 调用在本地不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.