-1

I'm trying to retrieve a list of invoices per customer. So far I'm only retrieving the first object of the list. But how do I retrieve the whole list? I'm a beginner in PHP so I really can't figure this out and the docs of Chargebee aren't clarifying anything for me.

Here are the docs: https://apidocs.eu.chargebee.com/docs/api/invoices?prod_cat_ver=2&lang=php#list_invoices

And this is what I tried so far:

$customerID = $_POST['customerID']; 

require_once(dirname(__FILE__) . '/chargebee-php-2.8.3/lib/ChargeBee.php');

ChargeBee_Environment::configure("xxxx","xxxx");

$result = ChargeBee_Invoice::all(array(
  "limit" => 13,
  "sortBy[desc]" => "date",
  "customerId[is]" => (string)$customerID
  ));
  
foreach($result as $entry){
  $invoice = $entry->invoice();
}

$array = (array) $invoice;

echo json_encode($array);

Unfortunately I'm only retrieving the first item of the list. How do I get the whole list in an array, but encoded to a json string?

user2598506
  • 39
  • 1
  • 6
  • You are only assiging one invoice to `$invoice`. You need to create an array and append them - [see here](https://stackoverflow.com/questions/676677/how-to-add-elements-to-an-empty-array-in-php) – DarkBee Apr 08 '22 at 10:45
  • @DarkBee This works. But the output is not as expected. Before I get the output of the invoice. Now I get an array with empty objects in it. Ex: [{}, {}, {}] – user2598506 Apr 08 '22 at 13:10

2 Answers2

2

You keep overwriting your $invoice variable.

Creating and empty array and pushing every invoice onto that should yield the result you want. So try this:

$customerID = $_POST['customerID']; 

require_once(dirname(__FILE__) . '/chargebee-php-2.8.3/lib/ChargeBee.php');

ChargeBee_Environment::configure("xxxx","xxxx");

$result = ChargeBee_Invoice::all(array(
  "limit" => 13,
  "sortBy[desc]" => "date",
  "customerId[is]" => (string)$customerID
  ));

$invoicesArr = [] // create empty array
  
foreach($result as $entry){
  array_push($invoicesArr, $entry->invoice()); // foreach invoice, push it to array
}

echo json_encode($invoicesArr);
geertjanknapen
  • 1,159
  • 8
  • 23
  • This works. But the output is not as expected. Before I get the output of the invoice. Now I get an array with empty objects in it. Ex: [{}, {}, {}] – user2598506 Apr 08 '22 at 13:09
  • 1
    What does `var_dump($entry->invoice())` return? In order to be able to use `json_encode` properly on an object, that said object needs to implement [JsonSerializable](https://www.php.net/manual/en/class.jsonserializable.php) – DarkBee Apr 08 '22 at 13:17
  • 1
    @user2598506 checkout @DarkBee 's comment. I think if you `var_dump($invoicesArr)` before your `echo json_encode(..)` you would see an array of invoices. – geertjanknapen Apr 08 '22 at 13:39
2

You can use the getValues() function given by chargebee. Here is the example.

$result = ChargeBee_Invoice::all(array(
 "limit" => 13,
 "sortBy[desc]" => "date",
 "customerId[is]" => (string)$customerID
));

if (count($result) > 0) {

   $invoices = [];

   foreach ($result as $value) {

       $invoices[] = $value->invoice()->getValues();

   }

   echo json_encode($invoices);

} else {

   echo "Invoices not found";

}