且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PayPal REST API .net SDK - 400 个错误请求

更新时间:2022-04-03 05:15:03

由于似乎没有人知道这个问题的答案,所以我深入研究了 PayPal 的 .NET SDK for REST API 的源代码.从这个审查来看,似乎从当前版本开始,当服务器返回任何 4xx 或 5xx HTTP 状态代码时,没有返回错误消息的规定.我参考了这个问题的答案 并更改了 SDK 以允许在适用时返回错误响应.这是 HttpConnection.cs 的相关部分.

Since no one seems to know the answer to this, I dug into the source code of PayPal's .NET SDK for REST API. From this review, it appears that as of the current version, there is no provision for returning the error messages when any 4xx or 5xx HTTP status code is returned by the server. I referenced the answers to this question and have altered the SDK to allow for returning the error response when applicable. Here's the relevant portion of HttpConnection.cs.

catch (WebException ex)
{
    if (ex.Response is HttpWebResponse)
    {
        HttpStatusCode statusCode = ((HttpWebResponse)ex.Response).StatusCode;
        logger.Info("Got " + statusCode.ToString() + " response from server");
        using (WebResponse wResponse = (HttpWebResponse)ex.Response)
        {
            using (Stream data = wResponse.GetResponseStream ())
            {
                string text = new StreamReader (data).ReadToEnd ();
                return text;
            }
        }                           
    }
    if (!RequiresRetry(ex))
    {
        // Server responses in the range of 4xx and 5xx throw a WebException
        throw new ConnectionException("Invalid HTTP response " + ex.Message);
    }                       
}

当然,这需要更改调用函数以正确解释错误响应.由于我只使用 Payment Create API,因此我采用了一些不适用于一般用途的快捷方式.然而,基本的想法是我创建了 PaymentError 和 Detail 类,然后更改 Payment.Create 和 PayPalResource.ConfigureAndExecute 方法来填充和传回 PaymentError 对象.

Of course, this requires changes to the calling function to properly interpret the error response. Since I'm only using the Payment Create API, I took some shortcuts that wouldn't work for general use. However, the basic idea is that I created PaymentError and Detail classes, then altered the Payment.Create and PayPalResource.ConfigureAndExecute methods to populate and pass back a PaymentError object.

三年后,PayPal 的 API 错误响应处理有了一些改进.由于其他一些问题,我不得不重新编写我的应用程序,并删除之前的代码.我发现您现在可以捕获 PayPal.Exception.PaymentsException,然后将 JSON 响应字符串反序列化为 PayPal.Exception.PaymentsError 对象.

Three years later and there is some improvement in PayPal's API error response handling. Because of some other issues I had to re-work my application, and rip out the prior code. I've found that you can now trap for a PayPal.Exception.PaymentsException, then deserialize the JSON Response string into a PayPal.Exception.PaymentsError object.

Catch ex As PayPal.Exception.PaymentsException
    Dim tmpPmtErr As PayPal.Exception.PaymentsError = _
        JsonConvert.DeserializeObject(Of PayPal.Exception.PaymentsError)(ex.Response)

感谢@lance 在另一个答案中的提醒,以便更仔细地检查异常.我尝试使用该解决方案,但无法使其发挥作用.

Thanks to @lance in the other answer for the heads-up to examine the exceptions more closely. I attempted to use that solution, but could not make it work.