Tony Hensler
Tony Hensler

Reputation: 1492

ChargeBee Invoice PHP API Request, accessing $invoices

I am making the following request to ChargeBee's API via PHP:-

ChargeBee_Environment::configure("chargebee-test","test_uybGuyguyguykynkgYgkfvyt");
$all = ChargeBee_Invoice::all(array(
    "customer_id" => 2uyg23inuy2g3ou,
    "limit"       => 5, 
    "status[is]"  => "paid", 
    "total[lte]"  => 1000,
    "sortBy[asc]" => "date"));

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice);
    echo'</pre>';
}

Each call to $entry->invoice() returns an object with the following structure:

ChargeBee_Invoice Object
(
    [allowed:protected] => Array
        (
            [0] => id
            [1] => poNumber
            [2] => customerId
            [3] => subscriptionId
        )

    [_values:protected] => Array
        (
            [id] => 4
            [customer_id] => 2uyg23inuy2g3ou
            [subscription_id] => 2uyg23inuy2g3ou
            [line_items] => Array
                (
                    [0] => Array
                        (
                            [id] => li_2uyg23inuy2g3ou
                            [date_from] => 1484106779
                        )

                )

            [sub_total] => 200
            [linked_payments] => Array
                (
                    [0] => Array
                        (
                            [txn_id] => txn_2uyg23inuy2g3ou
                            [applied_amount] => 200
                            [applied_at] => 1484106781
                            [txn_status] => success
                            [txn_date] => 1484106781
                            [txn_amount] => 200
                        )

                )

    [_subTypes:protected] => Array
        (
            [line_items] => ChargeBee_InvoiceLineItem
            [discounts] => ChargeBee_InvoiceDiscount
            [taxes] => ChargeBee_InvoiceTax
        )

)

(I have reduced the amount of data above as the returned request is far to long to show here)

This is where I am getting stuck, how am I able to extract data from object?

I have attempted the following:-

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice->allowed);
    echo'</pre>';
}

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice->allowed());
    echo'</pre>';
}

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice->ChargeBee_Invoice);
    echo'</pre>';
}

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice->ChargeBee_Invoice());
    echo'</pre>';
}

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice['ChargeBee_Invoice']);
    echo'</pre>';
}

foreach($all as $entry){
    $invoice = $entry->invoice();
    echo'<pre>';
    print_r($invoice['allowed']);
    echo'</pre>';
}

I have also attempted many variations of the above with the the following code:-

foreach($all as $entry){
    $invoice = $entry->invoice();
    foreach($invoice as $cinvoice) {
        echo'<pre>';
        print_r($cinvoice->allowed);
        echo'</pre>';
    }
}

But everything I try just returns:-

Fatal error:  Uncaught exception 'Exception' with message 'Unknown property

Upvotes: 1

Views: 278

Answers (2)

GarryM
GarryM

Reputation: 527

You want to access directly protected properties. Access to the data in the object ChargeBee_Invoice is implemented by the magic method __get() (source code of parent class ChargeBee_Model). List of available properties you can look at this page. This code help you to start:

<?php
  foreach ($all as $entry)
  {
      /* Get next invoice */
      $invoice = $entry->invoice();

      /* Get properties from invoice */
      echo $invoice->id;
      echo $invoince->subscription_id;

      /* And so on */
  }
?>

Upvotes: 0

yivi
yivi

Reputation: 47370

From the docs you can see the public attributes for an Invoice object.

To access them you just, for example:

echo "<pre>";
foreach($all as $entry){
    $invoice = $entry->invoice();
    echo "Invoice #{$invoice->customerId}  for a total of {$invoice->amountDue} has status '{$invoice->status}'"\n;
}
echo "</pre>";

Test it out, and check the other available properties in the docs.

Upvotes: 1

Related Questions