Dot-Net

PayPal REST API .net SDK - 400 個錯誤請求

  • October 19, 2017

我在沙箱中工作,並使用 PayPal REST .net SDK 方法 Payment.Create 和 CreditCard 對象。當所有參數都有效並使用來自https://developer.paypal.com/webapps/developer/docs/integration/direct/accept-credit-cards/的測試 CC 編號時,Payment 對象將從該方法返回,並且全部為好吧。

但是,當參數無效時,例如過去的到期日期或沙盒無法辨識的 CC 編號,則不會返回 Payment 對象。相反,該方法會引發異常:“HttpConnection Execute 中的異常:無效的 HTTP 響應遠端伺服器返回錯誤:(400)錯誤請求”,但沒有進一步解釋。

當我在 cURL 中執行相同的請求時,除了“400 Bad Request”之外,我還收到了 JSON 響應。這包括更多有用的消息,例如“VALIDATION_ERROR”和“無效過期(不能是過去)”。

我的問題:有沒有辦法從 SDK 中獲取這些消息?

我試過的:

  • PayPal 文件:https ://developer.paypal.com/webapps/developer/docs/api/#errors 該文件提到,如果出現錯誤,他們會在響應正文中返回詳細資訊。不幸的是,它沒有提供有關 SDK 是否可以訪問這些的線索。
  • 各種 Google 和 SO 搜尋。
  • SDK 提供的 PizzaApp 範常式式碼沒有任何異常處理或進一步深入了解此問題的方式。
  • 我在 SDK 中看到一個 PayPalException 對象,但沒有找到任何指示它應該如何使用或者它是否與這個問題相關的東西。

非常感謝所有幫助。

由於似乎沒有人知道這個問題的答案,我研究了 PayPal 的 .NET SDK for REST API 的原始碼。從這次審查中,似乎在目前版本中,當伺服器返回任何 4xx 或 5xx HTTP 狀態程式碼時,沒有返回錯誤消息的規定。我參考了這個問題的答案,並更改了 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);
   }                       
}

當然,這需要更改呼叫函式以正確解釋錯誤響應。由於我只使用 Payment Create 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)

感謝@lance 在另一個答案中的提醒,以便更仔細地檢查異常。我嘗試使用該解決方案,但無法使其工作。

我今天才開始弄亂 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"} 

引用自:https://stackoverflow.com/questions/15581314