55026

PayPal with REST PHP returning error 400

Question:

I am using the REST API in PHP to try and create a PayPal purchase, but I only get a generic 400 error whenever I run it. Apparently this means bad request, but there are barely any details given. Here is the main portion of my code:

define("PP_CONFIG_PATH", "../vendor/"); $apiContext = new ApiContext(new OAuthTokenCredential('-redacted-', '-redacted-')); // id, secret $addr = new Address(); $addr->setLine1($_POST['addr1']); if(isset($_POST['addr2']) && !empty($_POST['addr2'])) $addr->setLine2($_POST['addr2']); $addr->setCity($_POST['city']); $addr->setCountry_code($_POST['country']); $addr->setPostal_code($_POST['zip']); $addr->setState($_POST['state']); $addr->setPhone('9179261285'); // TODO put in actual phone $card = new CreditCard(); $card->setNumber($_POST['card_num']); $card->setExpire_month($_POST['expire_mon']); $card->setExpire_year($_POST['expire_yr']); $card->setCvv2($_POST['cvv2']); $card->setFirst_name($_GET['fname']); $card->setLast_name($_GET['lname']); $card->setBilling_address($addr); $fi = new FundingInstrument(); $fi->setCredit_card($card); $payer = new Payer(); $payer->setPayment_method('credit_card'); $payer->setFunding_instruments(array($fi)); $cost = $_POST['plan'] == 1 ? '19.95' : '29.95'; $amountDetails = new AmountDetails(); $amountDetails->setSubtotal($cost); $amountDetails->setTax('0.00'); $amountDetails->setShipping('0.00'); $amount = new Amount(); $amount->setCurrency('USD'); $amount->setTotal($cost); $amount->setDetails($amountDetails); $transaction = new Transaction(); $transaction->setAmount($amount); $transaction->setDescription('MyTrustCo membership subscription.'); $payment = new Payment(); $payment->setIntent('sale'); $payment->setPayer($payer); $payment->setTransactions(array($transaction)); try { $response = $payment->create($apiContext); } catch (PPConnectionException $e) { echo "<br />exception:<br />" . $e->getMessage() . "<br />"; } echo "response: " . $response; die();

The last part of the code outputs:

exception: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. got response response:

I have no idea what could be causing this error. I was following this tutorial: <a href="https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment" rel="nofollow">https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment</a>

Answer1:

I was having the same problem but ultimately uncovered the root cause by capturing Exception instead of PPConnectionException (as per the SDK sample code which fooled both of us). Once I did this, I was able to dump the exception details which revealed that PayPal was complaining that my transaction amount was not formatted correctly - I only had a single digit after the decimal point. Looking at your code above, this is not likely to be the case, but changing the exception handler should reveal the real problem quickly.

You may also be susceptible to the formatting problem for other values though in future, which you can resolve with the following:

money_format('%!i', $amount)

Recommend

  • using undo/redo in Swing for an MVC model?
  • Post Compile error, check Console? [duplicate]
  • DbEntityValidationException After Upgrading to EF 4.1
  • How to prevent autogeneration of JAXBElement for xs:choice using xsdtojava?
  • What SQL query to search attributes in JSON data in MySQL
  • What is the maximum size of a page-locked host memory allocation?
  • Visual Print Design for .NET [closed]
  • Monopoly pick random card and pop array
  • get removable sd card path in android
  • iTextSharp - Copying elements from one PDF to another
  • Hiding a form, switch to a second form, close second form and unhide first form
  • Angular Material2 not all styles are applied
  • How to make swipe on horizontal recyclerview bring only the next item into view - Android
  • Prompting user to select directory in batch file
  • Resize image in ionic cards
  • Piping output from aplay to arecord in centos
  • HTML5 How to replace WebAudio API for Internet Explorer for javascript games?
  • How to bind products dynamically in snippet carousel in Angular4
  • Using the sendgrid node-module, how do I send a SendGrid Template Engine email?
  • Preventing kernel timeout on second cuda device (no screen attached)
  • Leaflet map not sizing to dimensions of target element
  • Check if a card with a name is present in a CardLayout
  • flip box height of back side, also doesnot work on IE [duplicate]
  • No enclosing instance of the type MainActivity is accessible in scope
  • how sabre sd boots directly from uboot without SPL
  • Adding a billing method with google cloud results in “unexpected error”
  • multiple iron-collapse not working, expands only first
  • Fermi architecture possible solution to my comparative study?
  • Botframework - resize bot chat in width [duplicate]
  • Save and retrieve an image file in LibGDX
  • windows batch file combine csv in a folder by column
  • How to clip image to ellipse in XAML
  • Retrieving a contacts notes
  • Installing apk from within application in android
  • Cannot style mat-tab without ::ng-deep and !important
  • How to merge two Request in Laravel
  • Pyglet uses too much cpu
  • Encrypting credit card details using AngularJS in Braintree
  • Unique SMS sender id?
  • Conditional In-Line CSS for IE and Others?