调用 PayU rest api(创建订单)returns html 而不是 json 响应

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

我正在尝试 post 向 PayU 支付网关下订单,使用 post 等 Rest Client 工具,我也遇到了同样的问题。

我正在尝试 post 使用 C#,订单创建成功但响应不符合预期,它应该是 json object 包含插入的订单 ID 和重定向 url ,但当前是 html response!

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.");


    }

我的 C# 代码使用内置 WebRequest 也 returns 相同 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 returns 302(发现)也 ResponseUri 不是预期的 OK 200

默认情况下,其余客户端会自动重定向到此 url,因此我收到了支付页面的 html 内容。

解决方法是:

client.FollowRedirects = false;

希望对大家有用。

此外,我想补充一点,Mohammad 的上述回答是正确的,为了获得响应 URL 我们需要将 AllowAutoRedirect 设置为 false。我一直在尝试在控制台应用程序中实施 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;
    }
}

创建上面的 class 后,我在我的 Main 方法中编写了以下代码:

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

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

我现在在同一个 class 中调用函数 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;
}

上述函数返回的object包含一个header调用位置。该位置的值是您需要的 URL 用于 webcheckout。

在邮递员中,解决方案是关闭重定向,如下图所示: