簡體   English   中英

調用PayU rest api(創建訂單)返回html而不是json響應

[英]Calling PayU rest api (create order) returns html instead of json response

我正在嘗試向 PayU 支付網關發布訂單,使用諸如郵遞員之類的 Rest Client 工具,我也遇到了同樣的問題。

在此處輸入圖片說明

我正在嘗試使用 C# 發布,訂單創建成功但響應不符合預期,它應該是一個包含插入的訂單 ID 和重定向 url 的 json 對象,但當前是 html 響應!

C# 代碼響應: 在此處輸入圖片說明

我的 C# 代碼使用 restsharp 庫:

 public IRestResponse<CreateOrderResponseDTO> CreateOrder(CreateOrderDTO orderToCreate)
    {

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        var actionUrl = "/api/v2_1/orders/";

        var client = new RestClient(_baseUrl);

        var request = new RestRequest(actionUrl, Method.POST)
        {
            RequestFormat = DataFormat.Json
        };

        request.AddJsonBody(orderToCreate);


        request.AddHeader("authorization", $"Bearer {_accessToken}");
        request.AddHeader("Content-Type", "application/json");

        var response = client.Execute<CreateOrderResponseDTO>(request);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            return response;
        }

        throw new Exception("order not inserted check the data.");


    }

我使用內置WebRequest C# 代碼也返回相同的 html:

 public string Test(string url, CreateOrderDTO order)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "application/json";

        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _accessToken);

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            streamWriter.Write(new JavaScriptSerializer().Serialize(order));
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            return result;
        }
    }

誰能告訴我在這里錯過了什么?

經過一些嘗試,我發現 PayU rest api 返回302 (找到), ResponseUri也沒有按預期返回OK 200

默認情況下,rest 客戶端會自動重定向到此 url,因此我收到了付款頁面的 html 內容。

解決辦法是:

client.FollowRedirects = false;

希望這對任何人有用。

在郵遞員中,解決方案是關閉重定向,如下圖所示:

郵遞員重定向 turoff

另外,我想補充一點,Mohammad 的上述答案是正確的,因為我們需要將 AllowAutoRedirect 設置為 false 來獲取響應 URL。 我一直在嘗試在控制台應用程序中實施 PayU LatAM WebCheckout,但我遇到了類似的問題。 我從這里給出的答案中得到了一些靈​​感: How to get Location header with WebClient after POST request

根據答案,我寫了一個示例代碼:

public class NoRedirectWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var temp = base.GetWebRequest(address) as HttpWebRequest;
        temp.AllowAutoRedirect = false;
        return temp;
    }
}

創建上述類后,我在我的 Main 方法中編寫了以下代碼:

var request = MakeRequestLocation(new NoRedirectWebClient());
var psi = new ProcessStartInfo("chrome.exe");

psi.Arguments = request.ResponseHeaders["Location"];
Process.Start(psi);

我現在在同一個類中調用函數 MakeRequestLocation。

private static WebClient MakeRequestLocation(WebClient webClient)
{
    var loginUrl = @"https://sandbox.checkout.payulatam.com/ppp-web-gateway-payu/";
    NameValueCollection formData = new NameValueCollection
    {
        {"ApiKey", "4Vj8eK4rloUd272L48hsrarnUA" },
        {"merchantId", "508029" },
        {"accountId", "512321" },
        {"description", "Test PAYU" },
        {"referenceCode", "SomeRandomReferenceCode" },
        {"amount", "2" },
        {"tax", "0" },
        {"taxReturnBase", "0" },
        {"currency", "USD" },
        {"signature", "Signature generated via MD5 sum" },
        {"buyerFullName", "test" },
        {"buyerEmail", "test@test.com" },
        {"responseUrl", @"http://www.test.com/response" },
        {"confirmationUrl",  @"http://www.test.com/confirmation" }
    };
    webClient.UploadValues(loginUrl, "POST", formData);

    return webClient;
}

上述函數返回的對象包含一個名為 location 的標頭。 location 的值是 webcheckout 所需的 URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM