PayPal with REST PHP returning error 400

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();
if(isset($_POST['addr2']) && !empty($_POST['addr2'])) $addr->setLine2($_POST['addr2']);
$addr->setPhone('9179261285'); // TODO put in actual phone

$card = new CreditCard();

$fi = new FundingInstrument();

$payer = new Payer();

$cost = $_POST['plan'] == 1 ? '19.95' : '29.95';

$amountDetails = new AmountDetails();

$amount = new Amount();

$transaction = new Transaction();
$transaction->setDescription('MyTrustCo membership subscription.');

$payment = new Payment();

try {
    $response = $payment->create($apiContext);
} catch (PPConnectionException $e) {
    echo "<br />exception:<br />" . $e->getMessage() . "<br />";

echo "response: " . $response;

The last part of the code outputs:

Got Http response code 400 when accessing
got response

I have no idea what could be causing this error. I was following this tutorial:


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)

Need Your Help

How to upload a txt file containing json data to php server and save data to mysql?

php android mysql json

I m making an android application to back up sms and upload file backed up file containing json data to php server for further saving the data to mysql. I have backed up data in .txt file on sd car...

Backup local svn repository to Google Drive

svn repository backup google-drive-api

I have been using Tortoise SVN and VisualSVNManager to create local svn repositories for my projects. Is there a way to back up those repositories to Google Drive? I know that you can use