-2

I am using the following code to get the json from Paypal.

  $url =("https://api.sandbox.paypal.com/v2/checkout/orders/$orderID");
  $headers=array("Content-Type: application/json",
    "Authorization: Bearer " .$access_token
 );
 $curl = curl_init();

 $options = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER =>false,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST=> 'GET',
CURLOPT_HTTPAUTH => CURLAUTH_BEARER,
CURLOPT_XOAUTH2_BEARER => $access_token,

];

curl_setopt_array($curl, $options);
$response=curl_exec($curl);
//$response = json_decode(curl_exec($curl));
curl_close($curl);

$response=json_decode($response, true); //array
  //$array = get_object_vars($response);

I get back the following when I print out the response.

object(stdClass)#10 (8) { ["id"]=> string(17) "4SG92296D12008253" ["intent"]=> string(7) "CAPTURE" ["status"]=> string(9) "COMPLETED" ["purchase_units"]=> array(1) { [0]=> object(stdClass)#11 (5) { ["reference_id"]=> string(7) "default" ["amount"]=> object(stdClass)#12 (2) { ["currency_code"]=> string(3) "USD" ["value"]=> string(4) "5.00" } ["payee"]=> object(stdClass)#13 (2) { ["email_address"]=> string(30) "midel-facilitator@model.com" ["merchant_id"]=> string(13) "QUZE7DB6LP5EG" } ["shipping"]=> object(stdClass)#15 (2) { ["name"]=> object(stdClass)#14 (1) { ["full_name"]=> string(10) "test buyer" } ["address"]=> object(stdClass)#16 (5) { ["address_line_1"]=> string(9) "1 Main St" ["admin_area_2"]=> string(8) "San Jose" ["admin_area_1"]=> string(2) "CA" ["postal_code"]=> string(5) "95131" ["country_code"]=> string(2) "US" } } ["payments"]=> object(stdClass)#27 (1) { ["captures"]=> array(1) { [0]=> object(stdClass)#17 (9) { ["id"]=> string(17) "24F97831B4310422F" ["status"]=> string(9) "COMPLETED" ["amount"]=> object(stdClass)#18 (2) { ["currency_code"]=> string(3) "USD" ["value"]=> string(4) "5.00" } ["final_capture"]=> bool(true) ["seller_protection"]=> object(stdClass)#19 (2) { ["status"]=> string(8) "ELIGIBLE" ["dispute_categories"]=> array(2) { [0]=> string(17) "ITEM_NOT_RECEIVED" [1]=> string(24) "UNAUTHORIZED_TRANSACTION" } } ["seller_receivable_breakdown"]=> object(stdClass)#21 (3) { ["gross_amount"]=> object(stdClass)#20 (2) { ["currency_code"]=> string(3) "USD" ["value"]=> string(4) "5.00" } ["paypal_fee"]=> object(stdClass)#22 (2) { ["currency_code"]=> string(3) "USD" ["value"]=> string(4) "0.66" } ["net_amount"]=> object(stdClass)#23 (2) { ["currency_code"]=> string(3) "USD" ["value"]=> string(4) "4.34" } } ["links"]=> array(3) { [0]=> object(stdClass)#24 (3) { ["href"]=> string(69) "https://api.sandbox.paypal.com/v2/payments/captures/24F97831B4310422F" ["rel"]=> string(4) "self" ["method"]=> string(3) "GET" } [1]=> object(stdClass)#25 (3) { ["href"]=> string(76) "https://api.sandbox.paypal.com/v2/payments/captures/24F97831B4310422F/refund" ["rel"]=> string(6) "refund" ["method"]=> string(4) "POST" } [2]=> object(stdClass)#26 (3) { ["href"]=> string(67) "https://api.sandbox.paypal.com/v2/checkout/orders/4SG92296D12008253" ["rel"]=> string(2) "up" ["method"]=> string(3) "GET" } } ["create_time"]=> string(20) "2022-05-18T03:05:09Z" ["update_time"]=> string(20) "2022-05-18T03:05:09Z" } } } } } ["payer"]=> object(stdClass)#29 (4) { ["name"]=> object(stdClass)#28 (2) { ["given_name"]=> string(4) "test" ["surname"]=> string(5) "buyer" } ["email_address"]=> string(24) "midel-buyer@model.com" ["payer_id"]=> string(13) "ZG835WK7PCQH4" ["address"]=> object(stdClass)#30 (1) { ["country_code"]=> string(2) "US" } } ["create_time"]=> string(20) "2022-05-18T03:05:02Z" ["update_time"]=> string(20) "2022-05-18T03:05:09Z" ["links"]=> array(1) { [0]=> object(stdClass)#31 (3) { ["href"]=> string(67) "https://api.sandbox.paypal.com/v2/checkout/orders/4SG92296D12008253" ["rel"]=> string(4) "self" ["method"]=> string(3) "GET" } } }

I am trying to get the value i.e. $5.00 as a value from the response and have the following code to pull the data.

print_r($response['purchase_units'][0]['amount'][2]['value'][4]); 

I get the following notice messages

Notice line 78: Undefined index: value Notice line 78: Trying to access array offset on value of type null

This is the closest I have come to getting it right without massive fatal flaw errors or other error messages. What am I doing wrong? I been banging my head on this for weeks. Any help on this will be great.

Thanks

user2974907
  • 51
  • 1
  • 7
  • Try checking like this: First get response for purchase_units and see it it works fine.`print_r($response->purchase_units)` If it works then use it like this `$p_u=$response->purchase_units; print_r($p_u['amount']); – Optimum Creative May 18 '22 at 11:53
  • 3
    That output looks like it's from json_decode _without_ the `true` argument afterwards. Check you've shown us the right thing. – ADyson May 18 '22 at 11:54
  • But basically, refer to: [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) to understand how to read the data structure of the JSON (or resulting decoded PHP array) and find what you need. – ADyson May 18 '22 at 11:55
  • `["amount"]=> object(stdClass)#12 (2)` tells me, that there is an array with length of 2, so index 0 and 1, index 2 does not exist, hence the error. – Onki Hara May 18 '22 at 11:59
  • 1
    `$response['purchase_units'][0]['amount']` is partially correct, but that amount contains a single object, not an array of objects. You want `$response->purchase_units[0]->amount->value` – aynber May 18 '22 at 11:59
  • ADyson I have ran my code multiple times and get the same output so this is why I posted that ugly output. However, you may be on to something. As for the link I have studied that same tut 100 times and tried the methods and nothing Stackoverflow was my last resort. I will keep at it. – user2974907 May 19 '22 at 02:25
  • Have you tried anyber's suggestion? – ADyson May 19 '22 at 06:21
  • I did a hard look at Optimum Creative solution and plugged at it and got what I needed with the following. `$p_u=$response->purchase_units[0]; if($p_u->amount->value == "5.00") { echo'BINGO'; }` My test came back BINGO. I Thank everyone for their help. – user2974907 May 20 '22 at 08:38

0 Answers0