Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.x] Create invoices separately #1409

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/Concerns/ManagesInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use LogicException;
use Stripe\Exception\CardException as StripeCardException;
use Stripe\Exception\InvalidRequestException as StripeInvalidRequestException;
use Stripe\Invoice as StripeInvoice;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand Down Expand Up @@ -118,30 +117,18 @@ public function invoicePrice($price, $quantity = 1, array $tabOptions = [], arra
*/
public function invoice(array $options = [])
{
$this->assertCustomerExists();

$parameters = array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
'customer' => $this->stripe_id,
], $options);

try {
/** @var \Stripe\Invoice $invoice */
$stripeInvoice = $this->stripe()->invoices->create($parameters);
$invoice = $this->createInvoice(array_merge($options, [
'pending_invoice_items_behavior' => 'include_and_require',
]));

if ($stripeInvoice->collection_method === StripeInvoice::COLLECTION_METHOD_CHARGE_AUTOMATICALLY) {
$stripeInvoice = $stripeInvoice->pay();
} else {
$stripeInvoice = $stripeInvoice->sendInvoice();
}

return new Invoice($this, $stripeInvoice);
return $invoice->chargesAutomatically() ? $invoice->pay() : $invoice->send();
} catch (StripeInvalidRequestException $exception) {
return false;
} catch (StripeCardException $exception) {
$payment = new Payment(
$this->stripe()->paymentIntents->retrieve(
$stripeInvoice->refresh()->payment_intent,
$invoice->asStripeInvoice()->refresh()->payment_intent,
['expand' => ['invoice.subscription']]
)
);
Expand All @@ -150,6 +137,27 @@ public function invoice(array $options = [])
}
}

/**
* Create an invoice within Stripe.
*
* @param array $options
* @return \Laravel\Cashier\Invoice
*/
public function createInvoice(array $options = [])
{
$this->assertCustomerExists();

$parameters = array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
'customer' => $this->stripe_id,
'pending_invoice_items_behavior' => 'exclude',
], $options);

$stripeInvoice = $this->stripe()->invoices->create($parameters);

return new Invoice($this, $stripeInvoice);
}

/**
* Get the customer's upcoming invoice.
*
Expand Down
66 changes: 66 additions & 0 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,26 @@ public function reverseChargeApplies()
return $this->invoice->customer_tax_exempt === StripeCustomer::TAX_EXEMPT_REVERSE;
}

/**
* Determine if the invoice will be charged automatically.
*
* @return bool
*/
public function chargesAutomatically()
{
return $this->invoice->collection_method === StripeInvoice::COLLECTION_METHOD_CHARGE_AUTOMATICALLY;
}

/**
* Determine if the invoice will be be sent by email.
*
* @return bool
*/
public function sendsInvoice()
{
return $this->invoice->collection_method === StripeInvoice::COLLECTION_METHOD_SEND_INVOICE;
}

/**
* Get all of the "invoice item" line items.
*
Expand Down Expand Up @@ -477,6 +497,52 @@ public function invoiceLineItems()
})->all();
}

/**
* Add an invoice item to this invoice.
*
* @param string $description
* @param int $amount
* @param array $options
* @return \Stripe\InvoiceItem
*/
public function tab($description, $amount, array $options = [])
{
$item = $this->owner()->tab($description, $amount, array_merge($options, ['invoice' => $this->invoice->id]));

$this->refresh();

return $item;
}

/**
* Add an invoice item for a specific Price ID to this invoice.
*
* @param string $price
* @param int $quantity
* @param array $options
* @return \Stripe\InvoiceItem
*/
public function tabPrice($price, $quantity = 1, array $options = [])
{
$item = $this->owner()->tabPrice($price, $quantity, array_merge($options, ['invoice' => $this->invoice->id]));

$this->refresh();

return $item;
}

/**
* Refresh the invoice.
*
* @return $this
*/
public function refresh()
{
$this->invoice = $this->invoice->refresh();

return $this;
}

/**
* Refresh the invoice with expanded objects.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/InvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public function test_invoicing_fails_with_nothing_to_invoice()
$this->assertFalse($response);
}

public function test_invoices_can_be_created()
{
$user = $this->createCustomer('invoices_can_be_created');
$user->createAsStripeCustomer();

$invoice = $user->createInvoice();

$this->assertInstanceOf(Invoice::class, $invoice);
$this->assertEquals(0, $invoice->rawTotal());

$invoice->tab('Laracon', 49900);

$this->assertEquals(49900, $invoice->rawTotal());
}

public function test_customer_can_be_invoiced()
{
$user = $this->createCustomer('customer_can_be_invoiced');
Expand Down