Skip to content

Commit

Permalink
Send data when upcoming invoice is refreshed (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Aug 30, 2021
1 parent 2fc1963 commit 44b49e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/ManagesInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
//
}
Expand Down
39 changes: 22 additions & 17 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,32 @@ 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);
}

$this->owner = $owner;
$this->invoice = $invoice;
$this->refreshData = $refreshData;
}

/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 44b49e9

Please sign in to comment.