Reputation: 129
I'm following this tutorial
This is supposed to be my output (desired result):
{
"intent": "CAPTURE",
"purchase_units": [
{
"reference_id": "ABC123",
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
],
"application_context": {
"return_url": "https://example.com/return",
"cancel_url": "https://example.com/cancel"
}
}
However, after a PHP array and converting using json_encode I'm getting this result
{
"intent": "CAPTURE",
"purchase_units": {
"reference_id": "ABC123",
"amount": {
"currency_code": "USD",
"value": 100
}
},
"application_context": {
"return_url": "https:\/\/example.com\/return",
"cancel_url": "https:\/\/example.com\/exit"
}
}
The most noticeable differences can be found here - the desired result has "purchase_units": [{
and }],
while my output is returning "purchase_units": {
and '},' without the square brackets.
This is my PHP code:
$seller = array(
'intent' => 'CAPTURE',
'purchase_units' => array(
'reference_id' => 'ABC123',
'amount' => array(
'currency_code' => 'USD',
'value' => 100.00
),
),
'application_context' => array(
'return_url' => 'https://example.com/return',
'cancel_url' => 'https://example.com/exit',
),
);
Upvotes: 2
Views: 359
Reputation: 164956
purchase_units
needs to be an array of arrays. That is, an outer indexed array which becomes a JSON array holding associative arrays that become JSON objects.
$seller = [
'intent' => 'CAPTURE',
'purchase_units' => [[ // 👈 note the nested array
'reference_id' => 'ABC123',
'amount' => [
'currency_code' => 'USD',
'value' => 100.00
],
]], // 👈 and close it here
'application_context' => [
'return_url' => 'https://example.com/return',
'cancel_url' => 'https://example.com/exit',
],
];
$json = json_encode($seller, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
You don't really need the JSON_PRETTY_PRINT
but you might want to keep the JSON_UNESCAPED_SLASHES
option.
Demo ~ https://3v4l.org/ZdqcR
Upvotes: 1