From bfe4e8b210bcb91722971c8fc776fdee802f4200 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Sun, 29 Aug 2021 20:36:32 +0200 Subject: [PATCH] Send data when upcoming invoice is refreshed --- src/Concerns/ManagesInvoices.php | 2 +- src/Invoice.php | 39 ++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Concerns/ManagesInvoices.php b/src/Concerns/ManagesInvoices.php index d9ab99f2..1cd93b8b 100644 --- a/src/Concerns/ManagesInvoices.php +++ b/src/Concerns/ManagesInvoices.php @@ -170,7 +170,7 @@ public function upcomingInvoice(array $options = []) try { $stripeInvoice = $this->stripe()->invoices->upcoming($parameters); - return new Invoice($this, $stripeInvoice); + return new Invoice($this, $stripeInvoice, $parameters); } catch (StripeInvalidRequestException $exception) { // } diff --git a/src/Invoice.php b/src/Invoice.php index e72d8441..dcc46752 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -60,16 +60,24 @@ class Invoice implements Arrayable, Jsonable, JsonSerializable */ protected $refreshed = false; + /** + * The data that will be sent when the invoice is refreshed. + * + * @var array + */ + protected $refreshData = []; + /** * Create a new invoice instance. * * @param \Illuminate\Database\Eloquent\Model $owner * @param \Stripe\Invoice $invoice + * @param array $refreshData * @return void * * @throws \Laravel\Cashier\Exceptions\InvalidInvoice */ - public function __construct($owner, StripeInvoice $invoice) + public function __construct($owner, StripeInvoice $invoice, array $refreshData = []) { if ($owner->stripe_id !== $invoice->customer) { throw InvalidInvoice::invalidOwner($invoice, $owner); @@ -77,6 +85,7 @@ public function __construct($owner, StripeInvoice $invoice) $this->owner = $owner; $this->invoice = $invoice; + $this->refreshData = $refreshData; } /** @@ -385,28 +394,24 @@ protected function refreshWithExpandedData() return; } + $expand = [ + 'account_tax_ids', + 'discounts', + 'lines.data.tax_amounts.tax_rate', + 'total_discount_amounts.discount', + 'total_tax_amounts.tax_rate', + ]; + if ($this->invoice->id) { $this->invoice = Cashier::stripe()->invoices->retrieve($this->invoice->id, [ - 'expand' => [ - 'account_tax_ids', - 'discounts', - 'lines.data.tax_amounts.tax_rate', - 'total_discount_amounts.discount', - 'total_tax_amounts.tax_rate', - ], + 'expand' => $expand, ]); } else { // If no invoice ID is present then assume this is the customer's upcoming invoice... - $this->invoice = Cashier::stripe()->invoices->upcoming([ + $this->invoice = Cashier::stripe()->invoices->upcoming(array_merge($this->refreshData, [ 'customer' => $this->owner->stripe_id, - 'expand' => [ - 'account_tax_ids', - 'discounts', - 'lines.data.tax_amounts.tax_rate', - 'total_discount_amounts.discount', - 'total_tax_amounts.tax_rate', - ], - ]); + 'expand' => $expand, + ])); } $this->refreshed = true;