ChargeBee发票PHP API请求,访问$发票

时间:2017-01-31 10:07:54

标签: php api oop object

我通过PHP向ChargeBee的API发出以下请求: -

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>';
}

每次调用$entry->invoice()都会返回一个具有以下结构的对象:

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
        )

)

(我已经减少了上面的数据量,因为此处显示的返回请求很长)

这是我遇到困难的地方,我如何从对象中提取数据?

我尝试过以下方法: -

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>';
}

我还尝试了以上代码的上述许多变体: -

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

但我尝试的所有东西都会返回: -

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

非常感谢任何帮助或推动正确的方向

2 个答案:

答案 0 :(得分:1)

docs您可以看到Invoice对象的公共属性。

要访问它们,例如:

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>";

测试它,并检查文档中的其他可用属性。

答案 1 :(得分:-1)

您想直接访问受保护的属性。访问对象 ChargeBee_Invoice 中的数据是通过魔术方法__get()(source code of parent class ChargeBee_Model)实现的。您可以查看this page的可用属性列表。 此代码可帮助您开始:

<?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 */
  }
?>