I have a JSON object that was returned from a request made to paypal sandbox API. However, I am having difficulties accessing some of the members of in this object. For example, I would like to access total property of related_resources object which is located in the transaction object.
$result = $payment->execute($execute, $apiContext);
$data = json_decode($result);
//$data = $result->toJSON();
I get the following error
Notice: Trying to get property of non-object in path_to_file on line 67
when I try to access the payer object.
print_r($data->transactions);
The I also the same error message whenever I attempt to access the properties nested inside the transaction object. For example:
echo $data->transaction[0]->amount[0]->total;
I followed the steps found at the following link How do I extract data from JSON with PHP? to find a solution but my efforts were futile.
The format of the JSON data is:
{
"id":"PAY-5S764194SH917514SLLJNSZA","intent":"sale","state":"approved","cart":"5LM08388X5236923U",
"transactions":
[
{
"amount":
{
"total":"0.99","currency":"USD",
"details":{}
},
"payee":
{
"merchant_id":"MJBN5EPGYYLZE","email":"Test-Business-facilitator@gmail.com"
},
"item_list":
{
"shipping_address":
{
"recipient_name":"Test Personal","line1":"1 Main St","city":"San Jose","state":"CA","postal_code":"95131","country_code":"US"
}
},
"related_resources":
[
{
"sale":
{
"id":"9N957492L93533703","state":"completed",
"amount":{
"total":"0.99","currency":"USD",
"details":
{
"subtotal":"0.99"
}
}
},"transaction_fee":{"value":"0.33","currency":"USD"},
"links":
[
{
"href":"https://","rel":"self","method":"GET"
},
{
"href":"","rel":"refund","method":"POST"
},
{
"href":"https:/","method":"GET"
}
]
}
]
}
]
}
This is the output of doing the following:
$data = json_decode($result, true);
print_r($data);
Array ( [0] => Array ( [amount] => Array ( [total] => 0.99 [currency] => USD [details] => Array ( ) ) [payee] => Array ( [merchant_id] => MJBN5EPGYYLZE [email] => Test-Business-facilitator@discoverytechnologiesja.com ) [item_list] => Array ( [shipping_address] => Array ( [recipient_name] => Test Personal [line1] => 1 Main St [city] => San Jose [state] => CA [postal_code] => 95131 [country_code] => US ) ) [related_resources] => Array ( [0] => Array ( [sale] => Array ( [id] => 0BK223494W308401A [state] => completed [amount] => Array ( [total] => 0.99 [currency] => USD [details] => Array ( [subtotal] => 0.99 ) ) [payment_mode] => INSTANT_TRANSFER [protection_eligibility] => ELIGIBLE [protection_eligibility_type] => ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE [transaction_fee] => Array ( [value] => 0.33 [currency] => USD ) [parent_payment] => PAY-8W230634SJ018825BLLJPUFA [create_time] => 2018-04-15T07:07:37Z [update_time] => 2018-04-15T07:07:37Z [links] => Array ( [0] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/sale/0BK223494W308401A [rel] => self [method] => GET ) [1] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/sale/0BK223494W308401A/refund [rel] => refund [method] => POST ) [2] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-8W230634SJ018825BLLJPUFA [rel] => parent_payment [method] => GET ) ) ) ) ) ) )