本文介绍了PayPal REST API .net SDK-400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用沙箱,并使用带有CreditCard对象的PayPal REST .net SDK方法Payment.Create.当所有参数均有效并且使用来自 https://developer的测试抄送号时.paypal.com/webapps/developer/docs/integration/direct/accept-credit-cards/,该方法返回了Payment对象,一切都很好.

I'm working in the sandbox and using the PayPal REST .net SDK method Payment.Create with a CreditCard object. When all parameters are valid and using the test CC number from https://developer.paypal.com/webapps/developer/docs/integration/direct/accept-credit-cards/, the Payment object is returned from that method and all is well.

但是,当参数无效(例如过期日期或沙箱无法识别的抄送号码)时,不会返回Payment对象.而是,该方法引发异常:"HttpConnection执行中的异常:无效的HTTP响应远程服务器返回错误:(400)错误的请求",但没有进一步的解释.

However, when a parameter is not valid, such as a past expiration date or a CC number not recognized by the sandbox, the Payment object is not returned. Instead the method throws an exception: "Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (400) Bad Request", but with no further explanation.

当我在cURL中执行相同的请求时,除了"400错误请求"之外,我还会收到JSON响应.这包括更多有用的消息,例如"VALIDATION_ERROR"和无效的过期时间(不能过去)".

When I execute the same request in cURL, in addition to the "400 Bad Request", I get a JSON response. This includes more helpful messages such as "VALIDATION_ERROR" and "Invalid expiration (cannot be in the past)".

我的问题:有没有办法从SDK取回这些消息?

My question: Is there a way to get these messages back from the SDK?

我尝试过的事情:

  • PayPal文档: https://developer.paypal.com/webapps/developer/docs/api /#错误本文档提到在发生错误的情况下,它们将在响应正文中返回详细信息.不幸的是,它没有提供关于这些SDK是否可以访问它们的线索.
  • 各种Google搜索和SO搜索.
  • SDK随附的PizzaApp示例代码没有任何异常处理方式或对这一问题的深入了解.
  • 我在SDK中看到一个PayPalException对象,但没有发现任何指示应如何使用它或与该问题是否相关的东西.
  • PayPal docs: https://developer.paypal.com/webapps/developer/docs/api/#errorsThis document mentions that in the case of an error, they return the details in the body of the response. Unfortunately, it doesn't give a clue about whether these are accessible by the SDK.
  • Various Google and SO searches.
  • The PizzaApp sample code provided with the SDK has nothing in the way of exception handling or further insight into this problem.
  • I see a PayPalException object in the SDK, but have not found anything that indicates how it should be used or if it's even relevant to this problem.

非常感谢所有帮助.

推荐答案

由于似乎没人知道答案,因此,我研究了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.

这篇关于PayPal REST API .net SDK-400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:59