Search code examples
.netrestsdkpaypalpaypal-sandbox

PayPal REST API .net SDK - 400 Bad Requests


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.

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.

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

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

What I've tried:

  • PayPal docs: https://developer.paypal.com/webapps/developer/docs/api/#errors This 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.

All help is much appreciated.


Solution

  • 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);
        }                       
    }
    

    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.


    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)
    

    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.