0

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 ) ) ) ) ) ) ) 
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Andre Reid
  • 95
  • 2
  • 11
  • If you are using toJson method from laravel, I think it returns string instead of object? Use json_decode with assoc=true then access the member like a regular associative array – Gabriel B.R Apr 15 '18 at 06:09
  • @ Gabriel B.R I tried you solution, it generates the same error message. – Andre Reid Apr 15 '18 at 06:14
  • There's no `transactions` key in the `print_r($data)` you posted, and in the posted code, it seems, you should get values like: `data[0][yourKey]` – Ashish Ranjan Apr 15 '18 at 07:23

3 Answers3

0

Your JSON is invalid. json_decode() will return null. Thats why you get the error message.

details":{
    "subtotal":"0.99"
}

Missing double quote

After fixing your json i can access $json->transactions no problem

If your json gets decoded to an array use

$json['transactions'];

If your json is an array you can access the first total amount for example with

$json['transactions'][0]['amount']['total'];

OR with object

$json_dec = json_decode($json);
$json_dec->transactions[0]->amount->total;

In your case with $data it seems you have gone into the transactions array already.

So

$data[0]['amount']['total'];
sietse85
  • 1,488
  • 1
  • 10
  • 26
0

The response looks like an Associate Array rather than an Object. Try: print_r($data[transactions]);

You get an error like: Notice: Trying to get property of non-object when you are not trying to access an Object. We access arrays like: someArray['transactions'] and Object like: someObject->transactions

Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
-1

First you have an error in json

corrected by json:

{
    "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"
                }
            ]
        }]
    }]
}

Secondly, in order to arrive as you want $data->transaction, you first need to decode this json with the function json_decode() and then you will be able to access this way $data->transaction

Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7