Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 969
Jonathan Black Avatar asked Mar 22 '13 23:03

Jonathan Black


People also ask

What does bad request mean on PayPal?

If anything goes wrong in the data given, paypal api will return a 400 - Bad request error.

What is a PayPal debug ID?

The Debug ID is a comma separated list of the debug IDs received from PayPal. To troubleshoot the payment, you can contact PayPal support and they can use the Debug ID information to identify the problem. When the problem is resolved, you can make the payment manually.

What is a PayPal request ID?

To enforce idempotency on REST API POST calls, use the PayPal-Request-Id request header, which contains a unique user-generated ID that the server stores for a period of time.

Does PayPal use API?

PayPal offers APIs for new and legacy integrations.


1 Answers

I only started messing with the SDK and API today and ran into this issue right away. I mean, if I'm going to create my own form to handle payments, I'd like to give my user's feedback if anything went wrong.

In any case, I did find some hidden info in the inner exception. Maybe this will help.

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

The resulting response:

{"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"} 
like image 141
lance Avatar answered Sep 28 '22 12:09

lance