Milligran
Milligran

Reputation: 3161

How do I resolve a 400 bad request error using the paypal rest api?

I am currently testing credit card payments using the PayPal payments pro Rest API.

I keep getting the following error:

Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (400) Bad Request. ---> 

I created and activated my PayPal test account and as following:

Credit card
Credit card number:
ccnumber here
Credit card type:
Visa
Expiration date:
3/2019

When I enter the above information in my payment form and submit I keep getting the 400 bad request error.

I have gotton this to work before with the PayPAl joe shopper account but lost the test cc number.

I am 100% sure I have authenticated with the oauth2 credentials

What is the likely cause of this error?:

I am entering a random payer first name and last name .

Does the name have to exsist?

What could be causing the error

Upvotes: 5

Views: 41214

Answers (10)

Vivek javiya
Vivek javiya

Reputation: 197

If you use Paypal Sandbox for testing Driver and on first try with buggy code it shows the client a 400 Bad request error.

Reason:- Paypal sent JWT auth and it was stored in the client's local storage.

Suggestion:- Try to clear the client's local storage cache (You only need to clear the Paypal Token).

This works for me, I hope it works for you :)

Upvotes: 1

Thomas George
Thomas George

Reputation: 366

It is due to the wrong currency or amount you have given. I faced the issue in React JS

I was doing this. in the createOrder method.

purchase_units: [{
                amount: {
                    currency_code: 'PHP',
                    value: totalAmount
                }
            }],

that was the issue. If i gave any currency other than USD i will get the 400 Error. So i removed the currency_code from here

And I moved the currency declaration into my component declaration. like this.

 options={{
            clientId: " Your_ID " ,
            currency: "PHP"
          }}

Here you can give any currency you want. This solves my 400 Error

Upvotes: 0

Lonare
Lonare

Reputation: 4653

Understand your errors PayPal returns detailed data on every exception (except 500 INTERNAL_SERVER_ERROR), explaining what was the exact error you are seeing. You can retrieve that as follows:

Update your exception handling to catch PayPalConnectionException and print $ex->getData() as shown below:

try {
    $payment->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex->getCode(); // Prints the Error Code
    echo $ex->getData(); // Prints the detailed error message 
    die($ex);
} catch (Exception $ex) {
    die($ex);
}

Upvotes: 0

Alan Denke
Alan Denke

Reputation: 59

I recently had a 400 bad request error as well. In my case, I was processing a refund using RefundRequest, and it appears that PayPal recently changed the constraints on the "reason" field (although it isn't documented that I can find) so my lengthy descriptions of what was being returned were no longer acceptable. I found the error only because I saw another field where PayPal was now requiring that it be only single-byte alphanumeric characters. What actually helped though, was shortening the data I was putting through as a reason to simply "Returned items".

Obviously this is an old thread, and I'm sure the original problem was solved long ago, but if using some of the above methods to determine more information about an error don't yield results I would suggest submitting a request with the bare minimum of information until you can get it to go through. Then, start adding additional fields/information back to see if you can identify which field contains the information causing the bad request. That would have worked for my issue, at least.

Upvotes: 0

Ben-Carpenter
Ben-Carpenter

Reputation: 189

Like others here have said, there are several things that can cause the (HTTP 400 - Bad Request).

In my case it was due to using the same invoice number. For temporary testing I just used the ShoppingCartID that I generated with a guid. You could also use a Random number generator as well. Ultimately you will need to generate an invoice number to the clients specifications.

-Good luck

Upvotes: 3

Ember Freak
Ember Freak

Reputation: 12872

I too got struck with this issue, there might be many other possible root cause for this failure but then in my case all my passing argument is correct, but then it failed in sandbox environment, I just changed Payment option from PayPal Balance to Visa then it started working.

enter image description here

enter image description here

Upvotes: 0

Sudhir Bhapkar
Sudhir Bhapkar

Reputation: 949

I had the same issue. In my case it was Credit Card issue which might be over used. So I have taken new Credit card Number from this site Testing Credit card and replaced with the old one. Here is Credit Card Information that I used

 credit_card = new CreditCard()
                        {
                            billing_address = new Address()
                            {
                                city = "Johnstown",
                                country_code = "US",
                                line1 = "52 N Main ST",
                                postal_code = "43210",
                                state = "OH"
                            },
                            cvv2 = "874",
                            expire_month = 11,
                            expire_year = 2018,
                            first_name = "Joe",
                            last_name = "Shopper",
                            number = "4024007185826731", //New Credit card Number, Only Card type should match other details does not matter
                            type = "visa"
                        }

Note: All the Credit Card mentioned in PayPal site are not working, giving same issue for me. If it working for you then it's good otherwise use new testing Credit Card Number. Hope this will help someone.

Thank you! Enjoy Coding!

Upvotes: 0

qoyyim
qoyyim

Reputation: 131

firstly find out what exactly what Your error is

payment = PayPal::SDK::REST::DataTypes::Payment.new(...)
payment.create 
# got Response[400]: Bad Request here
payment.error
# got {"name"=>"VALIDATION_ERROR", "details"=>[{"field"=>"transactions", "issue"=>"

then find out what Your issue here to get more info

https://developer.paypal.com/webapps/developer/docs/api/#validation-issues

Upvotes: 1

Jithin
Jithin

Reputation: 2604

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

When I got the error(PHP SDK), I caught the exception and $ex->getData() returned a json which contains the details about the bad request, in my case the currency code I given was wrong.

    try {
        $payment->create($apiContext);
    } catch (PayPal\Exception\PPConnectionException $ex) {
        var_dump(json_decode($ex->getData()));
        exit(1);
    }

Upvotes: 16

Jürgen Bayer
Jürgen Bayer

Reputation: 3023

I just had the same problem (HTTP 400 - Bad Request). The cause is a REST API request with "errors". In my case I had two causes:

  • I passed amount details to an Amount object but the sum of subtotal, shipping an tax was not equal to the amount total.
  • I passed amount currency values including a fracture, but passed not exactly two decimals but only one. Obviously the REST API is a bit touchy and accepts only either no or two decimals.
  • I passed an item_list in the Transaction object but the sum of the prices in the Item object was not equal to the subtotal given in the amount details.

I don't know a way of finding the actual cause of the error (if there is one).

In your case it could be the name of the payer that you add. Try without.

Edit:

I just tried out: It is indeed the payer info. I passed a payer_info with first and lastname and got HTTP 400 (without everything worked). I assume that payer_info must not be set by us but is set by PayPal when returning a Payment object.

Payer payer = new Payer { payment_method = "paypal", payer_info = new PayerInfo {
   first_name = "Zaphod", last_name = "Beeblebrox"}  }; // => HTTP 400 - Bad Request

Upvotes: 1

Related Questions