/** * Get a collection of the entity's invoices. * * @param bool $includePending * @param array $parameters * * @return \Illuminate\Support\Collection */ public function invoices($includePending = false, $parameters = []) { $invoices = new Collection(); $customer = $this->asBraintreeCustomer(); $parameters = array_merge([TransactionSearch::customerId()->is($customer->id)], $parameters); $braintreeTransactions = Transaction::search($parameters); $subscriptionIds = []; foreach ($braintreeTransactions as $braintreeTransaction) { $subscriptionIds[] = $braintreeTransaction->subscriptionId; } $braintreeSubscriptions = BraintreeSubscription::fetch([], array_unique($subscriptionIds)); // Here we will loop through the Braintree invoices and create our own custom Invoice // instances that have more helper methods and are generally more convenient to // work with than the plain Braintree objects are. Then, we'll return the array. if (!is_null($braintreeSubscriptions)) { foreach ($braintreeSubscriptions as $subscription) { if ($subscription->status == BraintreeSubscription::ACTIVE || $includePending) { foreach ($subscription->transactions as $transaction) { $invoices->push(new Invoice($this, $subscription, $transaction)); } } } } return $invoices->sortByDesc(function ($invoice) { return $invoice->date(); }); }