Reputation:
I have this code for paypal single payout call, and want to add a php variable where the price is at 'value':".$planPrice." If instead of $planPrice I put any number for example: "1000" it works without a problem and I get the notification, but when I add the php variable it says this:
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid JSON String' in C:\xampp\htdocs\website\paypal_payouts\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib\PayPal\Validation\JsonValidator.php:29 Stack trace: #0 C:\xampp\htdocs\website\paypal_payouts\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php(50): PayPal\Validation\JsonValidator::validate('{\r\n ...') #1 C:\xampp\htdocs\website\paypal_payouts\CreateSinglePayout.php(67): PayPal\Common\PayPalModel->__construct('{\r\n ...') #2 C:\xampp\htdocs\website\admin\reports\paynow.php(19): include('C:\xampp\htdocs...') #3 {main} thrown in C:\xampp\htdocs\website\paypal_payouts\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib\PayPal\Validation\JsonValidator.php on line 29
This is the code in php:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency("{
'value':".$planPrice.",
'currency':'USD'
}"));
Upvotes: 2
Views: 276
Reputation: 72336
You should never generate JSON by hand.
Use the PHP function json_encode()
. It takes care of quotes, commas, matching parentheses and everything:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(
new \PayPal\Api\Currency(
json_encode(array(
'value' => $planPrice,
'currency' => 'USD',
))
)
);
Even more, you don't even have to generate JSON
. Create a new PayPal\Api\Currency
object and use its methods setValue()
and setCurrency()
to set its properties.
$amount = new \PayPal\Api\Currency();
$amount->setValue($planPrice);
$amount->setCurrency('USD');
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount($amount)
;
I never used PayPal API but I guess the constructor that accepts a piece of JSON is used to construct the object using the response received from the server. You should always construct empty objects and set their properties (the same way you already do for $senderItem
) when you build a request.
Upvotes: 2
Reputation: 19523
You don't need to pass a JSON string to Paypal\Api\Currency
, it would be simpler to use an array:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency([
'value' => $planPrice,
'currency' => 'USD'
]));
But if you must use JSON, it needs to be valid. JSON only allows double quotes for strings and key names:
$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency('{
"value":'.$planPrice.',
"currency":"USD"
}'));
(I'm assuming here that "value"
should be set to a JSON number. If it's supposed to be a JSON string, replace that third-to-last line with "value": "' . $planPrice . '",
instead.)
Upvotes: 2