How to parse this Go Cardless array

113 Views Asked by At

I'm getting the following response back from a Go Cardless web hook and I'm struggling to parse it. The response below is in a variable called $item

When I var_dump($item) I get whats below..

object(GoCardlessPro\Resources\PayoutItem)#87 (6) {
    ["model_name":protected]=>
    string(10) "PayoutItem"
    ["amount":protected]=>
    string(6) "2800.0"
    ["links":protected]=>
    object(stdClass)#44 (1) {
      ["payment"]=>
      string(14) "PM0020P6VREV7P"
    }
    ["type":protected]=>
    string(16) "payment_paid_out"
    ["data":"GoCardlessPro\Resources\BaseResource":private]=>
    object(stdClass)#49 (4) {
      ["type"]=>
      string(16) "payment_paid_out"
      ["amount"]=>
      string(6) "2800.0"
      ["taxes"]=>
      array(0) {
      }
      ["links"]=>
      object(stdClass)#44 (1) {
        ["payment"]=>
        string(14) "PM0020P6VREV7P"
      }
    }
    ["api_response"]=>
    NULL
  }
  [1]=>
  object(GoCardlessPro\Resources\PayoutItem)#88 (6) {
    ["model_name":protected]=>
    string(10) "PayoutItem"
    ["amount":protected]=>
    string(5) "-10.0"
    ["links":protected]=>
    object(stdClass)#39 (1) {
      ["payment"]=>
      string(14) "PM0020P6VREV7P"
    }
    ["type":protected]=>
    string(7) "app_fee"
    ["data":"GoCardlessPro\Resources\BaseResource":private]=>
    object(stdClass)#42 (4) {
      ["type"]=>
      string(7) "app_fee"
      ["amount"]=>
      string(5) "-10.0"
      ["taxes"]=>
      array(0) {
      }
      ["links"]=>
      object(stdClass)#39 (1) {
        ["payment"]=>
        string(14) "PM0020P6VREV7P"
      }
    }
    ["api_response"]=>
    NULL
  }
  [2]=>
  object(GoCardlessPro\Resources\PayoutItem)#89 (6) {
    ["model_name":protected]=>
    string(10) "PayoutItem"
    ["amount":protected]=>
    string(5) "-58.0"
    ["links":protected]=>
    object(stdClass)#46 (1) {
      ["payment"]=>
      string(14) "PM0020P6VREV7P"
    }
    ["type":protected]=>
    string(14) "gocardless_fee"
    ["data":"GoCardlessPro\Resources\BaseResource":private]=>
    object(stdClass)#32 (4) {
      ["type"]=>
      string(14) "gocardless_fee"
      ["amount"]=>
      string(5) "-58.0"
      ["taxes"]=>
      array(1) {
        [0]=>
        object(stdClass)#41 (6) {
          ["amount"]=>
          string(4) "10.0"
          ["currency"]=>
          string(3) "GBP"
          ["destination_amount"]=>
          string(4) "10.0"
          ["destination_currency"]=>
          string(3) "GBP"
          ["exchange_rate"]=>
          NULL
          ["tax_rate_id"]=>
          string(8) "GB_VAT_1"
        }
      }
      ["links"]=>
      object(stdClass)#46 (1) {
        ["payment"]=>
        string(14) "PM0020P6VREV7P"
      }
    }
    ["api_response"]=>
    NULL
  }
}

When I var_export($item[2]) I get whats below..

GoCardlessPro\Resources\PayoutItem::__set_state(array(
   'model_name' => 'PayoutItem',
   'amount' => '-58.0',
   'links' => 
  (object) array(
     'payment' => 'PM0020P6VREV7P',
  ),
   'type' => 'gocardless_fee',
   'data' => 
  (object) array(
     'type' => 'gocardless_fee',
     'amount' => '-58.0',
     'taxes' => 
    array (
      0 => 
      (object) array(
         'amount' => '10.0',
         'currency' => 'GBP',
         'destination_amount' => '10.0',
         'destination_currency' => 'GBP',
         'exchange_rate' => NULL,
         'tax_rate_id' => 'GB_VAT_1',
      ),
    ),
     'links' => 
    (object) array(
       'payment' => 'PM0020P6VREV7P',
    ),
  ),
   'api_response' => NULL,
))

Now I'm looking to access 'taxes' => array ( 0 => (object) array('destination_amount' => '10.0', I've tried echo $item[2]->taxes->destination_amount; echo $item[2]->taxes['destination_amount]; and various other iterations, but all to no avail. I can see that I'm dealing with objects and an array however what I thought I knew is just not working. Can any one point me in the right direction because at the moment, I can't see the wood for the trees.

1

There are 1 best solutions below

0
On

The taxes property of each PayoutItem is an array of tax items.

Because taxes is an array, taxes->destination_amount doesn't mean anything!

You probably want to get the destination_amount for a specific item in the tax array, eg:

echo $item[2]->taxes[0]->destination_amount;

or to iterate through all of the tax items and do something with each:

foreach ($item[2]->taxes as $tax) {
  echo $tax->amount . PHP_EOL;
}