Search code examples
c#asp.netintegrationstreamreaderpayfort

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..

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.

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 +
         "&[email protected]" +
         "&amount=" + VALUE +
         "&currency=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.

Any insight would be appreciated.


Solution

  • After investigation i found that we can just include a form from front-side like this example: Pay-fort SDK example,

    And no need to render the HTML response from back-side.