如何从c#中的payfort api获取json结果

本文关键字:api 获取 json 结果 payfort 中的 | 更新日期: 2023-09-27 18:04:37

我面临的问题是,payfort API应该返回给我一些json。但相反,我得到一些html,有一些隐藏的字段。我没有看到里面写错误

我的代码

string access_code = string.Empty,
            amount = string.Empty,
            currency = string.Empty,
            customer_email = string.Empty,
            merchant_reference = string.Empty,
            order_description = string.Empty,
            language = string.Empty,
            merchant_identifier = string.Empty,
            signature = string.Empty,
            command = string.Empty;
        access_code = "X78979879h8h8h8";
        amount = "1000";
        command = "AUTHORIZATION";
        currency = "AED";
        customer_email = "zetawars@hotmail.com";
        language = "en";
        merchant_identifier = "RaskdQuCc";
        merchant_reference = "ASASASASASADSS";
        order_description = "Some order details";
        signature = "";
        string signature_string = "PASSaccess_code="+access_code+"amount="+amount+"command="+command+"currency="+currency+"customer_email"+customer_email+"language"+language+"merchant_identifier"+merchant_identifier+"merchant_reference"+merchant_reference+"order_description"+order_description+"PASS";
        signature = getHashSha256(signature_string);
        string url = "https://sbcheckout.payfort.com/FortAPI/paymentPage";
        string param = "access_code" + access_code + "&amount=" + amount + "&currency=" + currency + 
            "&customer_email=" + customer_email + "&merchant_reference=" + merchant_reference + 
            "&order_description=" + order_description + "&language=" + language + "merchant_identifier=" 
            + merchant_identifier + "&signature=" + signature + "&command=" + command;

        using (WebClient wc = new WebClient())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/json";
            //wc.Headers.Add("Content-Type", "application/json");
            string HtmlResult = wc.UploadString(url, param);
        }

如何从c#中的payfort api获取json结果

试着在标题下面添加值

wc.Headers[HttpRequestHeader.Authorization]
wc.Headers[HttpRequestHeader.TenantId]
wc.Headers[HttpRequestHeader.Client-Type]
wc.Headers[HttpRequestHeader.Protocol]

适合我!!

首先编码响应

string responseString = Encoding.UTF8.GetString(response);

使用HtmlDocument (htmllagilitypack)

var html = new HtmlDocument();
var tokenValue = "";
html.LoadHtml(responseString);

加载到html后,你可以查询字段和值。

var tokenFormIdElement = 
          html.DocumentNode.SelectSingleNode("//input[@name='token']");
tokenValue = tokenFormIdElement.GetAttributeValue("value", "");