Get details (parent payment) of a refund - php

How is it possible to get the parent payment of a refund, which was notified to me by PayPal?
I got this notification (webhook) from PayPal:
{
"create_time": "2015-02-20T10:56:36Z",
"event_type": "PAYMENT.SALE.REFUNDED",
"id": "WH-XXX-XXX",
"links": [
{
"href": "https://api.paypal.com/v1/notifications/webhooks-events/WH-XXX-XXX",
"method": "GET",
"rel": "self"
},
{
"href": "https://api.paypal.com/v1/notifications/webhooks-events/WH-XXX-XXX/resend",
"method": "POST",
"rel": "resend"
}
],
"resource": {
"amount": {
"currency": "EUR",
"details": {
"subtotal": "-XX.XX",
"tax": "XX.XX"
},
"total": "-XX.XX"
},
"create_time": "2015-02-20T10:55:10Z",
"id": "XXX",
"links": [
{
"href": "https://10.73.133.169:17881/v1/payments/refund/XXX",
"method": "GET",
"rel": "self"
}
],
"state": "completed"
},
"resource_type": "sale",
"summary": "A EUR XX.XX EUR sale payment was refunded"
}
If i take the resource -> id and do the following request with the PayPal PHP-SDK, i get a refund object but without the "parent_payment" field (documented here: https://developer.paypal.com/docs/api/#look-up-a-refund)
PayPal\Api\Refund::get($id, $api);
Every other call like..
PayPal\Api\Sale::get($id, $api);
or
PayPal\Api\Transaction::get($id, $api);
or
PayPal\Api\Payment::get($id, $api);
fails!
What is wrong with my notifications or the methods i am using?
UPDATE: Here is my response from the Refund::get() lookup:
{
"id": "XXX",
"create_time": "2015-02-20T10:55:10Z",
"state": "completed",
"amount": {
"total": "-XX.XX",
"currency": "EUR",
"details": {
"subtotal": "-XX.XX",
"tax": "XX.XX"
}
},
"links": [
{
"href": "https://api.paypal.com/v1/payments/refund/XXX",
"rel": "self",
"method": "GET"
}
]
}

Generally, when you do a lookup on Refund, you will get a response, that includes parent_payment that should have the PaymentID that could be used to retrieve the payment object.
This is how the response would look like:
Refund:get()
{
"id": "3KJ86032KA0123648",
"create_time": "2015-02-23T22:25:31Z",
"update_time": "2015-02-23T22:25:31Z",
"state": "completed",
"amount": {
"total": "-0.01",
"currency": "USD"
},
"sale_id": "4JE73984NP0170710",
"parent_payment": "PAY-0J694581CG996145EKTV2RWA",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/refund/3KJ86032KA0123648",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-0J694581CG996145EKTV2RWA",
"rel": "parent_payment",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/4JE73984NP0170710",
"rel": "sale",
"method": "GET"
}
]
}
Payment:get();
{
"id": "PAY-1XX00578GL815852KKTV2RGI",
"create_time": "2015-02-23T22:24:25Z",
"update_time": "2015-02-23T22:24:28Z",
"state": "approved",
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [
{
"credit_card": {
"type": "visa",
"number": "xxxxxxxxxxxx2259",
"expire_month": "11",
"expire_year": "2019",
"first_name": "Joe",
"last_name": "Shopper"
}
}
]
},
"transactions": [
{
"amount": {
"total": "20.00",
"currency": "USD",
"details": {
"subtotal": "17.50",
"tax": "1.30",
"shipping": "1.20"
}
},
"description": "Payment description",
"invoice_number": "54eba89962365",
"item_list": {
"items": [
{
"name": "Ground Coffee 40 oz",
"price": "7.50",
"currency": "USD",
"quantity": "1",
"description": "Ground Coffee 40 oz",
"tax": "0.30"
},
{
"name": "Granola bars",
"price": "2.00",
"currency": "USD",
"quantity": "5",
"description": "Granola Bars with Peanuts",
"tax": "0.20"
}
]
},
"related_resources": [
{
"sale": {
"id": "40F92773GL716624V",
"create_time": "2015-02-23T22:24:25Z",
"update_time": "2015-02-23T22:24:28Z",
"amount": {
"total": "20.00",
"currency": "USD"
},
"state": "partially_refunded",
"parent_payment": "PAY-1XX00578GL815852KKTV2RGI",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/40F92773GL716624V",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/40F92773GL716624V/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-1XX00578GL815852KKTV2RGI",
"rel": "parent_payment",
"method": "GET"
}
]
}
},
{
"refund": {
"id": "50V96618DN7676452",
"create_time": "2015-02-23T22:24:28Z",
"update_time": "2015-02-23T22:24:28Z",
"state": "completed",
"amount": {
"total": "-0.01",
"currency": "USD"
},
"sale_id": "40F92773GL716624V",
"parent_payment": "PAY-1XX00578GL815852KKTV2RGI",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/refund/50V96618DN7676452",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-1XX00578GL815852KKTV2RGI",
"rel": "parent_payment",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/40F92773GL716624V",
"rel": "sale",
"method": "GET"
}
]
}
}
]
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-1XX00578GL815852KKTV2RGI",
"rel": "self",
"method": "GET"
}
]
}

Related

PHP json_decode - extract specific value from object, within an array, within an object

I have this json:
{"id":"***hidden***","event_version":"1.0","create_time":"2020-07-28T04:09:33.415Z","resource_type":"checkout-order","resource_version":"2.0","event_type":"CHECKOUT.ORDER.APPROVED","summary":"An order has been approved by buyer","resource":{"update_time":"2020-07-28T04:09:05Z","create_time":"2020-07-28T04:08:53Z","purchase_units":[{"reference_id":"default","amount":{"currency_code":"CAD","value":"50.00"},"payee":{"email_address":"***hidden**","merchant_id":"***hidden***"},"custom_id":"THISVALUEIWANT","shipping":{"name":{"full_name":"John Doe"},"address":{"address_line_1":"1 Maire-Victorin","admin_area_2":"Toronto","admin_area_1":"ON","postal_code":"M5A 1E1","country_code":"CA"}},"payments":{"captures":[{"id":"***hidden***","status":"COMPLETED","amount":{"currency_code":"CAD","value":"50.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"CAD","value":"50.00"},"paypal_fee":{"currency_code":"CAD","value":"1.75"},"net_amount":{"currency_code":"CAD","value":"48.25"}},"links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/something","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/something/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/something","rel":"up","method":"GET"}],"create_time":"2020-07-28T04:09:05Z","update_time":"2020-07-28T04:09:05Z"}]}}],"links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/something","rel":"self","method":"GET"}],"id":"something","intent":"CAPTURE","payer":{"name":{"given_name":"John","surname":"Doe"},"email_address":"sb-***hidden","payer_id":"something","address":{"country_code":"CA"}},"status":"COMPLETED"},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something/resend","rel":"resend","method":"POST"}]}';
If i breakdown the structure, it goes like this:
{
"id":"***hidden***",
"event_version":"1.0",
"create_time":"2020-07-28T04:09:33.415Z",
"resource_type":"checkout-order",
"resource_version":"2.0",
"event_type":"CHECKOUT.ORDER.APPROVED",
"summary":"An order has been approved by buyer",
"resource":{
"update_time":"2020-07-28T04:09:05Z",
"create_time":"2020-07-28T04:08:53Z",
"purchase_units":[{
"reference_id":"default",
"amount":{
"currency_code":"CAD",
"value":"50.00"
},
"payee":{
"email_address":"***hidden***",
"merchant_id":"***hidden***"},
"custom_id":"THISISTHEVALUEIWANT",
...
I'd like to extract and store the value of custom_id in a php variable. Problem is I struggle in finding the way to extract this specific item which is, to my understanding, an object, within an array, within an object...
I've tried multiple combinations, I tried foreach loops, nothing worked so far.
The only thing that actually kind of worked is this:
$json = json_decode($jsonobj, true); // json is stored in $jsonobj variable
foreach($json as $elem) {
foreach ($elem['purchase_units'] as $item ) {
echo $item['custom_id'];
}
}
Which lets me get the value I want, but at the same time it throws me a whole bunch of E_WARNING type 2 error:
Invalid argument supplied for foreach()...
Illegal string offset 'purchase_units'...
Can you tell my how I could reach this specific value without flooding my php error log?
Thank you very much!
The following should work for you. I have had to deal with a complexe object structure like this and managed to do it quite simply.
First of all, it's important to note that the original JSON you have provided does not match the formatted sample you have built based on the original. More specifically, the custom_id is not inside of payee. Instead, it's at the same level as payee. It goes like this ...
...
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "CAD",
"value": "50.00"
},
"payee": {
"email_address": "***hidden**",
"merchant_id": "***hidden***"
},
"custom_id": "THISVALUEIWANT", <--- This is what you need
"shipping": { ...
The solution I am providing will be based on the original JSON.
<?php
$json = <<<JSON
{
"id": "***hidden***",
"event_version": "1.0",
"create_time": "2020-07-28T04:09:33.415Z",
"resource_type": "checkout-order",
"resource_version": "2.0",
"event_type": "CHECKOUT.ORDER.APPROVED",
"summary": "An order has been approved by buyer",
"resource": {
"update_time": "2020-07-28T04:09:05Z",
"create_time": "2020-07-28T04:08:53Z",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "CAD",
"value": "50.00"
},
"payee": {
"email_address": "***hidden**",
"merchant_id": "***hidden***"
},
"custom_id": "THISVALUEIWANT",
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "1 Maire-Victorin",
"admin_area_2": "Toronto",
"admin_area_1": "ON",
"postal_code": "M5A 1E1",
"country_code": "CA"
}
},
"payments": {
"captures": [
{
"id": "***hidden***",
"status": "COMPLETED",
"amount": {
"currency_code": "CAD",
"value": "50.00"
},
"final_capture": true,
"seller_protection": {
"status": "ELIGIBLE",
"dispute_categories": [
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
]
},
"seller_receivable_breakdown": {
"gross_amount": {
"currency_code": "CAD",
"value": "50.00"
},
"paypal_fee": {
"currency_code": "CAD",
"value": "1.75"
},
"net_amount": {
"currency_code": "CAD",
"value": "48.25"
}
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/something",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/something/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/something",
"rel": "up",
"method": "GET"
}
],
"create_time": "2020-07-28T04:09:05Z",
"update_time": "2020-07-28T04:09:05Z"
}
]
}
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/something",
"rel": "self",
"method": "GET"
}
],
"id": "something",
"intent": "CAPTURE",
"payer": {
"name": {
"given_name": "John",
"surname": "Doe"
},
"email_address": "sb-***hidden",
"payer_id": "something",
"address": {
"country_code": "CA"
}
},
"status": "COMPLETED"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something/resend",
"rel": "resend",
"method": "POST"
}
]
}
JSON;
$object = json_decode($json);
$purchase_units = $object->resource->purchase_units ?? [];
$custom_ids = array_column($purchase_units, 'custom_id');
print_r($custom_ids);
The output will be:
Array
(
[0] => THISVALUEIWANT
)
Now a little explanation. After decoding the JSON object, I go straight for the property I need using $purchase_units = $object->resource->purchase_units ?? [];
The null coalesce operator will allow defaulting the $purchase_units variable to an empty array if anything in $object->resource->purchase_units is not present.
The array_column function allows extracting a specific column or property from all the items contained in an array. You can read more about it at https://www.php.net/manual/en/function.array-column.php

PHP Getting a value from a Multi Layered JSON Array

I seem to be having some trouble with JSON arrays. I was wondering if someone could please assist me.
I have the following JSON Array, and I want to get the figure in invoice_number
$json = '
{
"id": "PAY-7MP775806F4135612LLCHE4I",
"intent": "sale",
"state": "approved",
"cart": "9KA22662P3559221J",
"payer":
{
"payment_method": "paypal",
"status": "VERIFIED",
"payer_info":
{
"email": "accounts-buyer#traxprint.com",
"first_name": "test",
"last_name": "buyer",
"payer_id": "ZR6SRXGS252RG",
"shipping_address":
{
"recipient_name": "test buyer",
"line1": "1 Cheeseman Ave Brighton East",
"city": "Melbourne",
"state": "Victoria",
"postal_code": "3001",
"country_code": "AU"
},
"phone": "0364424947",
"country_code": "AU"
}
},
"transactions":
[
{
"amount":
{
"total": "3.00",
"currency": "USD",
"details":
{
"subtotal": "3.00"
}
},
"payee":
{
"merchant_id": "DSTEYCMCDUL3Y"
},
"description": "Payment description",
"invoice_number": "abc1231522823790",
"item_list":
{
"items":
[
{
"name": "Subscribe",
"sku": "sub1",
"price": "3.00",
"currency": "USD",
"tax": "0.00",
"quantity": 1
}
],
"shipping_address":
{
"recipient_name": "test buyer",
"line1": "1 Cheeseman Ave Brighton East",
"city": "Melbourne",
"state": "Victoria",
"postal_code": "3001",
"country_code": "AU"
}
},
"related_resources":
[
{
"sale":
{
"id": "41B66647LJ233225Y",
"state": "completed",
"amount":
{
"total": "3.00",
"currency": "USD",
"details":
{
"subtotal": "3.00"
}
},
"payment_mode": "INSTANT_TRANSFER",
"protection_eligibility": "ELIGIBLE",
"protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
"transaction_fee":
{
"value": "0.37",
"currency": "USD"
},
"parent_payment": "PAY-7MP775806F4135612LLCHE4I",
"create_time": "2018-04-04T06:36:59Z",
"update_time": "2018-04-04T06:36:59Z",
"links":
[
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
"rel": "parent_payment",
"method": "GET"
}
]
}
}
]
}
],
"create_time": "2018-04-04T06:36:33Z",
"links":
[
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
"rel": "self",
"method": "GET"
}
]
}';
The code I was trying to use is but it comes up with error Undefined property: stdClass::$invoice_number
$decoded = json_decode($json);
echo $Info = $decoded->transactions[0]->payee->invoice_number;
Could someone please tell me why this does not work?
Thanks
Robert
echo $Info = $decoded->transactions[0]->invoice_number;

How to link PayPal webhook and user?

I think I'm over looking something incredibly simple here.
When I create a new billing agreement through the PayPal PHP SDK I'm returned to my store by the following URL..
/paypal-success?success=true&token=EC-3K353628LH3197445
I have created a WebHook for the Subscription Created event that posts JSON as follows
{
"id": "WH-5UR49681MC703253M-3S161814MB664705V",
"event_version": "1.0",
"create_time": "2018-03-17T17:21:47.120Z",
"resource_type": "Agreement",
"event_type": "BILLING.SUBSCRIPTION.CREATED",
"summary": "A billing subscription was created",
"resource": {
"agreement_details": {
"outstanding_balance": {
"value": "0.00"
},
"num_cycles_remaining": "0",
"num_cycles_completed": "0",
"next_billing_date": "2018-03-17T10:00:00Z",
"last_payment_date": "2018-03-17T17:21:44Z",
"last_payment_amount": {
"value": "1.00"
},
"final_payment_due_date": "1970-01-01T00:00:00Z",
"failed_payment_count": "0"
},
"description": "x",
"links": [{
"href": "api.sandbox.paypal.com/v1/payments/billing-agreements/I-55KA5N9GPPJW",
"rel": "self",
"method": "GET"
}],
"shipping_address": {
"recipient_name": "test buyer",
"line1": "Spitalfields Arts Market, 112 Brick Lane,",
"city": "London",
"state": "London",
"postal_code": "E1 6RL",
"country_code": "GB"
},
"id": "I-55KA5N9GPPJW",
"state": "Active",
"payer": {
"payment_method": "paypal",
"status": "verified",
"payer_info": {
"email": "jamie-buyer#x.com",
"first_name": "test",
"last_name": "buyer",
"payer_id": "Q3TZFN4F7NHJ6",
"shipping_address": {
"recipient_name": "test buyer",
"line1": "Spitalfields Arts Market, 112 Brick Lane,",
"city": "London",
"state": "London",
"postal_code": "E1 6RL",
"country_code": "GB"
}
}
},
"plan": {
"curr_code": "USD",
"links": [],
"payment_definitions": [{
"type": "REGULAR",
"frequency": "Month",
"frequency_interval": "1",
"amount": {
"value": "49.99"
},
"cycles": "0",
"charge_models": [{
"type": "TAX",
"amount": {
"value": "0.00"
}
},
{
"type": "SHIPPING",
"amount": {
"value": "0.00"
}
}]
}],
"merchant_preferences": {
"setup_fee": {
"value": "1.00"
},
"auto_bill_amount": "YES",
"max_fail_attempts": "0"
}
},
"start_date": "2018-03-17T00:00:00Z"
},
"links": [{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-5UR49681MC703253M-3S161814MB664705V",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-5UR49681MC703253M-3S161814MB664705V/resend",
"rel": "resend",
"method": "POST"
}]
}
That token generated when the user is redirected back to my store does not appear to be in the WebHooks JSON.
How do you link your PayPal users once they are redirected back to your store to the incoming WebHooks?
The token returned in your success url is used to activate the agreement after the user accepts the agreement on PayPal. Once you activate the agreement you can retrieve the agreement id. That id is in your web hook under resources/id. "id": "I-55KA5N9GPPJW". There may be a better way to deal with this but I created a subscription table that associates the agreement id to the user id and a transactions table that has the agreement id and the associated webhook information.

json stripping in php

Hi I am trying to get the invoice_number object out of a json file. I am using json_decode in php and getting an undefined property error. I am unsure what I am doing wrong.
$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "hullsantarun-buyer#gmail.com", "first_name": "test", "last_name": "buyer", "payer_id": "xxxxx", "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" }, "country_code": "GB", "billing_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } } }, "transactions": [ { "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00", "shipping": "0.00" } }, "payee": { "merchant_id": "xxx", "email": "facilitator#gmail.com" }, "description": "Pay for all your santa fun", "invoice_number": "57c4007c11a60", "item_list": { "items": [ { "name": "wayne : \u00a310 ; ", "price": "10.00", "currency": "GBP", "quantity": 1 } ], "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } }, "related_resources": [ { "sale": { "id": "23V89902U7365974Y", "state": "completed", "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "0.54", "currency": "GBP" }, "parent_payment": "PAY-2C432486DK345933DK7CAA7I", "create_time": "2016-08-29T09:29:53Z", "update_time": "2016-08-29T09:29:54Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "billing_plan_units": [ { "billing_plan_approved": false } ], "redirect_urls": { "return_url": "https://www.hullsantarun.org/php/pay.php?success=true&paymentId=PAY-2C432486DK345933DK7CAA7I", "cancel_url": "https://www.hullsantarun.org/php/pay.php?success=false" }, "create_time": "2016-08-29T09:29:54Z", "update_time": "2016-08-29T09:29:53Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "self", "method": "GET" } ] }';
$obj = json_decode($json);
print $obj->{'invoice_number'};
invoice_number is a property of an object in the transactions array.
This will work:
$obj->transactions[0]->invoice_number;
You can always do:
print_r($obj);
to see how the object actually looks.
Try this it's works for you.
I have try your given json response and i do some change in your code and it's work try..
$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "hullsantarun-buyer#gmail.com", "first_name": "test", "last_name": "buyer", "payer_id": "xxxxx", "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" }, "country_code": "GB", "billing_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } } }, "transactions": [ { "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00", "shipping": "0.00" } }, "payee": { "merchant_id": "xxx", "email": "facilitator#gmail.com" }, "description": "Pay for all your santa fun", "invoice_number": "57c4007c11a60", "item_list": { "items": [ { "name": "wayne : \u00a310 ; ", "price": "10.00", "currency": "GBP", "quantity": 1 } ], "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } }, "related_resources": [ { "sale": { "id": "23V89902U7365974Y", "state": "completed", "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "0.54", "currency": "GBP" }, "parent_payment": "PAY-2C432486DK345933DK7CAA7I", "create_time": "2016-08-29T09:29:53Z", "update_time": "2016-08-29T09:29:54Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "billing_plan_units": [ { "billing_plan_approved": false } ], "redirect_urls": { "return_url": "https://www.hullsantarun.org/php/pay.php?success=true&paymentId=PAY-2C432486DK345933DK7CAA7I", "cancel_url": "https://www.hullsantarun.org/php/pay.php?success=false" }, "create_time": "2016-08-29T09:29:54Z", "update_time": "2016-08-29T09:29:53Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "self", "method": "GET" } ] }';
$obj = json_decode($json);
print $obj->transactions[0]->invoice_number;
OR
echo $obj->transactions[0]->invoice_number;

unable to get json_decode field

I want to get title for each song from the json below in php.I am unable to get all the names .I tried with the code below but its not displaying anything.I really appreciate any help.Thanks in Advance.
code:
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=(json_decode($json, true));
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
echo $asset->title;
}
}
json:
{
"feed": {
"author": {
"name": {
"label": "iTunes Store"
},
"uri": {
"label": "http://www.apple.com/itunes/"
}
},
"entry": [
{
"im:name": {
"label": "Happy (From \"Despicable Me 2\")"
},
"im:image": [
{
"label": "http://a923.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1645.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1228.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "G I R L"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/g-i-r-l/id823593445?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Columbia Records, a Division of Sony Music Entertainment, 2013 Back Lot Music, under exclusive license to Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "Happy (From \"Despicable Me 2\") - Pharrell Williams"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2",
"attributes": {
"im:id": "823593456"
}
},
"im:artist": {
"label": "Pharrell Williams",
"attributes": {
"href": "https://itunes.apple.com/us/artist/pharrell-williams/id14934728?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-03T00:00:00-07:00",
"attributes": {
"label": "March 3, 2014"
}
}
},
{
"im:name": {
"label": "All of Me"
},
"im:image": [
{
"label": "http://a1221.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1943.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a982.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Love in the Future (Deluxe Edition)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/love-in-future-deluxe-edition/id679297685?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Getting Out Our Dreams and Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "All of Me - John Legend"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a355.phobos.apple.com/us/r1000/041/Music4/v4/90/cf/04/90cf0482-07c0-fca0-549f-c1ea62c4bdef/mzaf_6715619947923767616.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2",
"attributes": {
"im:id": "679297849"
}
},
"im:artist": {
"label": "John Legend",
"attributes": {
"href": "https://itunes.apple.com/us/artist/john-legend/id16586443?uo=2"
}
},
"category": {
"attributes": {
"im:id": "15",
"term": "R&B/Soul",
"scheme": "https://itunes.apple.com/us/genre/music-r-b-soul/id15?uo=2",
"label": "R&B/Soul"
}
},
"im:releaseDate": {
"label": "2013-08-30T00:00:00-07:00",
"attributes": {
"label": "August 30, 2013"
}
}
},
{
"im:name": {
"label": "Let It Go"
},
"im:image": [
{
"label": "http://a261.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a983.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a262.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Frozen (Deluxe Edition) [Original Motion Picture Soundtrack]"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/frozen-deluxe-edition-original/id728903889?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Walt Disney Records"
},
"title": {
"label": "Let It Go - Idina Menzel"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a878.phobos.apple.com/us/r1000/023/Music/v4/0a/eb/c2/0aebc299-44e3-3465-3cdf-5bab336b547a/mzaf_7970981567593232494.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2",
"attributes": {
"im:id": "728904000"
}
},
"im:artist": {
"label": "Idina Menzel",
"attributes": {
"href": "https://itunes.apple.com/us/artist/idina-menzel/id3297504?uo=2"
}
},
"category": {
"attributes": {
"im:id": "16",
"term": "Soundtrack",
"scheme": "https://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2",
"label": "Soundtrack"
}
},
"im:releaseDate": {
"label": "2013-11-25T00:00:00-07:00",
"attributes": {
"label": "November 25, 2013"
}
}
},
{
"im:name": {
"label": "Dark Horse (feat. Juicy J)"
},
"im:image": [
{
"label": "http://a1657.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a731.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1498.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "PRISM (Deluxe Version)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/prism-deluxe-version/id690928033?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Capitol Records, LLC"
},
"title": {
"label": "Dark Horse (feat. Juicy J) - Katy Perry"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1930.phobos.apple.com/us/r1000/022/Music/v4/a4/ac/63/a4ac6341-59e3-bcfe-b7f5-370100eea063/mzaf_4835051646631223146.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2",
"attributes": {
"im:id": "690928460"
}
},
"im:artist": {
"label": "Katy Perry",
"attributes": {
"href": "https://itunes.apple.com/us/artist/katy-perry/id64387566?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2013-10-22T00:00:00-07:00",
"attributes": {
"label": "October 22, 2013"
}
}
},
{
"im:name": {
"label": "The Man"
},
"im:image": [
{
"label": "http://a1169.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a243.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1234.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Lift Your Spirit"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/lift-your-spirit/id798928288?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Aloe Blacc Recording, Inc. under exclusive license to XIX Recordings LLC/Interscope Records"
},
"title": {
"label": "The Man - Aloe Blacc"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a85.phobos.apple.com/us/r1000/033/Music6/v4/8a/09/dc/8a09dcf0-4845-81ae-6936-a94972ad94e7/mzaf_5332424013433716082.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2",
"attributes": {
"im:id": "798928362"
}
},
"im:artist": {
"label": "Aloe Blacc",
"attributes": {
"href": "https://itunes.apple.com/us/artist/aloe-blacc/id4750752?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-11T00:00:00-07:00",
"attributes": {
"label": "March 11, 2014"
}
}
}
],
"updated": {
"label": "2014-03-26T14:09:41-07:00"
},
"rights": {
"label": "Copyright 2008 Apple Inc."
},
"title": {
"label": "iTunes Store: Top Songs"
},
"icon": {
"label": "http://itunes.apple.com/favicon.ico"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?cc=us&id=38&popId=1"
}
},
{
"attributes": {
"rel": "self",
"href": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
],
"id": {
"label": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
}
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=json_decode($json);
$titles = array();
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
$titles[] = $asset->title->label;
}
}

Categories