我正在沙箱中工作,并使用PayPal REST .net SDK方法Payment.Create和CreditCard对象。当所有参数都是有效的,并且使用来自https://developer.paypal.com/webapps/developer/docs/integration/direct/accept-credit-cards/的测试CC号时,该方法将返回支付对象,并且all是好的。
但是,如果参数无效,例如过去的过期日期或沙箱无法识别的CC号,则不会返回支付对象。相反,该方法抛出一个异常:“HttpConnection执行中的异常:无效的HTTP远程服务器返回一个错误:(400)不好的请求”,但没有进一步解释。
当我在cURL中执行相同的请求时,除了“400个坏请求”之外,我还会得到一个JSON响应。这包括更有用的消息,如"VALIDATION_ERROR“和”无效过期(不能在过去)“。
我的问题是:有没有办法从SDK中获得这些消息?
我试过的是:
所有的帮助都非常感谢。
发布于 2013-03-26 19:21:23
由于似乎没有人知道答案,所以我深入研究了贝宝的REST的.NET SDK的源代码。从本文中可以看出,在当前版本中,服务器返回任何4xx或5xx HTTP状态代码时都没有返回错误消息的规定。我引用了this question的答案,并修改了SDK以允许在适用时返回错误响应。以下是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);
}
}当然,这需要更改调用函数才能正确解释错误响应。因为我只使用支付创建API,所以我选择了一些不适用于一般用途的快捷方式。但是,基本思想是,我创建了PaymentError和Detail类,然后修改了Payment.Create和PayPalResource.ConfigureAndExecute方法,以填充和传回PaymentError对象。
三年后,PayPal的API错误响应处理有了一些改进。由于其他一些问题,我不得不重新处理我的应用程序,并删除之前的代码。我发现您现在可以捕获一个PayPal.Exception.PaymentsException,然后将JSON响应字符串反序列化为一个PayPal.Exception.PaymentsError对象。
Catch ex As PayPal.Exception.PaymentsException
Dim tmpPmtErr As PayPal.Exception.PaymentsError = _
JsonConvert.DeserializeObject(Of PayPal.Exception.PaymentsError)(ex.Response)多亏了“兰斯在另一个答案”中的提醒,大家才能更仔细地检查例外情况。我试图使用该解决方案,但无法使其工作。
发布于 2013-08-24 06:36:45
我今天才开始处理SDK和API,然后马上就遇到了这个问题。我的意思是,如果我要创建自己的表单来处理付款,我想给我的用户的反馈,如果有任何问题。
无论如何,我确实在内部异常中找到了一些隐藏的信息。也许这个能帮上忙。
catch (PayPal.Exception.PayPalException ex)
{
if (ex.InnerException is PayPal.Exception.ConnectionException)
{
context.Response.Write(((PayPal.Exception.ConnectionException)ex.InnerException).Response);
}
else
{
context.Response.Write(ex.Message);
}
}由此产生的答复:
{"name":"VALIDATION_ERROR","details":[{"field":"payer.funding_instruments[0].credit_card.number","issue":"Must be numeric"}],"message":"Invalid request - see details","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR","debug_id":"0548e52ef9d95"} 发布于 2017-10-19 21:42:01
这似乎已经改变了,虽然其中许多答案指向了正确的方向,但没有一个对我有效。这相当于我目前最不满意的答案:
catch (PayPal.PaymentsException ex)
{
context.Response.Write(ex.Response);
}答复如下:
{"name":"VALIDATION_ERROR","details":[{"field":"start_date","issue":"Agreement start date is required, should be valid and greater than the current date. Should be consistent with ISO 8601 Format"}],"message":"Invalid request. See details.","information_link":"https://developer.paypal.com/docs/api/payments.billing-agreements#errors","debug_id":"8c56c13fccd49"}https://stackoverflow.com/questions/15581314
复制相似问题