Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skoobi's avatar
Level 13

Laravel Array from Paypal Rest API

Hi.

Im struggling to get the data out of the Returned data from Paypal Api..

When a payment is made all is ok and If i call ::

$payment = Payment::get($payment_id, $this->_api_context)->toArray();

return $payment; // for testing all data returned
return $payment['transactions']['related_resources']['transaction_fee']; // trying to get the data

I get

{
   "id": "PAY-2VF66335NA......",
   "intent": "sale",
   "state": "approved",
   "cart": "7JA26864N.../.",
   "payer": {
      "payment_method": "paypal",
      "status": "VERIFIED",
      "payer_info": {
         "email": "test@my_app.co.uk",
         "first_name": "Chris",
         "last_name": "",
         "payer_id": "T26U6W.....",
         "shipping_address": {
            "recipient_name": "Chris",
            "line1": "Spitalfields Arts Market, 112 Brick Lane,",
            "city": "London",
            "state": "London",
            "postal_code": "E1 6RL",
            "country_code": "GB"
         },
         "country_code": "GB"
      }
   },
   "transactions": [
      {
         "amount": {
            "total": "10.00",
            "currency": "GBP",
            "details": {}
         },
         "payee": {
            "merchant_id": "25NU22K....",
            "email": "[email protected]"
         },
         "description": "Mailbox Topup",
         "item_list": {
            "items": [
               {
                  "name": "Mailbox Topup",
                  "price": "10.00",
                  "currency": "GBP",
                  "quantity": 1
               }
            ],
            "shipping_address": {
               "recipient_name": "Chris",
               "line1": "Spitalfields Arts Market, 112 Brick Lane,",
               "city": "London",
               "state": "London",
               "postal_code": "E1 6RL",
               "country_code": "GB"
            }
         },
         "related_resources": [
            {
               "sale": {
                  "id": "5SK92059H7....",
                  "state": "pending",
                  "amount": {
                     "total": "10.00",
                     "currency": "GBP",
                     "details": {
                        "subtotal": "10.00"
                     }
                  },
                  "payment_mode": "INSTANT_TRANSFER",
                  "reason_code": "PAYMENT_REVIEW",
                  "protection_eligibility": "INELIGIBLE",
                  "transaction_fee": {
                     "value": "0.54",
                     "currency": "GBP"
                  },
                  "parent_payment": "PAY-",
                  "create_time": "2018-11-26T13:24:26Z",
                  "update_time": "2018-11-26T13:24:26Z",
                  "links": [
                     {
                        "href": "https://api.sandbox.paypal.com/v1/payments/sale/",
                        "rel": "self",
                        "method": "GET"
                     },
                     {
                        "href": "https://api.sandbox.paypal.com/v1/payments/sale//refund",
                        "rel": "refund",
                        "method": "POST"
                     },
                     {
                        "href": "https://api.sandbox.paypal.com/v1/payments/payment/",
                        "rel": "parent_payment",
                        "method": "GET"
                     }
                  ],
                  "soft_descriptor": "PAYPAL *TESTFACILIT"
               }
            }
         ]
      }
   ],
   "redirect_urls": {
      "return_url": "https://my-app.test/payments/paypal/status?",
      "cancel_url": "https://my-app.test/payments/paypal/status"
   },
   "create_time": "2018-11-26T13:24:26Z",
   "update_time": "2018-11-26T13:24:25Z",
   "links": [
      {
         "href": "https://api.sandbox.paypal.com/v1/payments/payment/",
         "rel": "self",
         "method": "GET"
      }
   ]
}

But when I try accessing the Transaction Fee I get an error Undefined index: related_resources.

How do i get that data?

Many thansk

0 likes
2 replies
realrandyallen's avatar
Level 44

Transactions and related_resources are arrays so if you're looking to always grab the first values of each it'd be like this as an object:

$payment->transactions[0]->related_resources[0]->sale->transaction_fee->value

or array:

$payment['transactions'][0]['related_resources'][0]['sale']['transaction_fee']['value']
skoobi's avatar
Level 13

Thank you very much for your reply. Your right. I completely missed the [ ] object...

Thank you for your help @real

1 like

Please or to participate in this conversation.