Search code examples
c#asp.nethttpwebrequesthttpwebresponse

HttpWebRequest.GetResponse returning html tag from payment gateway


i am trying to integrate payment gateway in my asp.net web following this link (https://docs.payfort.com/docs/merchant-page-two/build/index.html).

Following is my sample code:

protected void btnPay_Click(object sender, EventArgs e)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    string data = string.Format("service_command={0}&access_code={1}&merchant_identifier={2}&merchant_reference={3}&language={4}&signature={5}&token_name={6}&expiry_date={7}&card_number={8}&card_security_code={9}&card_holder_name={10}&remember_me={11}&return_url={12}",
        "TOKENIZATION", "zx0IPmPy5jp1vAz", "CycHZxVj", "XYZ9239-yu898","en", "d7c185c475ac0e3", "Op9Vmp", "1705", "4005550000000001","123", "John Smith","NO", "http://localhost:1093/Default.aspx");
    byte[] bytes = encoding.GetBytes(data);

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://sbcheckout.payfort.com/FortAPI/paymentPage");
    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    httpRequest.ContentLength = bytes.Length;
    using (Stream stream = httpRequest.GetRequestStream())
    {
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();
    }
    StreamReader rdr = new StreamReader(httpRequest.GetResponse().GetResponseStream());
    string result = rdr.ReadToEnd();
    rdr.Close();
    httpRequest.GetResponse().Close();
}

enter image description here

Now what i dont understand here is, the response i am getting is the same HTML of my page with all the parameters in the action attribute of the form tag, i have no idea what is this and what i have to do with it. Can anyone please help me with this. I am attaching the response which i am getting, please have a look at it as well.

Thank You

click on the link to view response


Solution

  • The documentation states:

    Remember - The Merchant should develop a form that does not send data to his website but directly submits the form to PayFort.

    You are doing what they are asking you NOT to do.

    I don't think this is meant to be used as an API. Your form should be submitting the information directly to their system. They will then use the return_url parameter to display the next page in your workflow. The URL will contain details of the result.

    You can always reach out to them and ask for an alternate, backend-friendly integration.