Skip to content

Commit

Permalink
Cascase Stripe exceptions when invoicing (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jun 25, 2021
1 parent 97d71b5 commit 7d3a7ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
24 changes: 6 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 @@ -76,28 +75,17 @@ 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 = new Invoice($this, $this->stripe()->invoices->create(array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
'customer' => $this->stripe_id,
], $options)));

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

return new Invoice($this, $stripeInvoice);
} catch (StripeInvalidRequestException $exception) {
return false;
return $invoice->chargesAutomatically() ? $invoice->pay() : $invoice->send();
} catch (StripeCardException $exception) {
$payment = new Payment(
$this->stripe()->paymentIntents->retrieve(
$stripeInvoice->refresh()->payment_intent,
$invoice->asStripeInvoice()->refresh()->payment_intent,
['expand' => ['invoice.subscription']]
)
);
Expand Down
20 changes: 20 additions & 0 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,26 @@ public function reverseChargeApplies()
return $this->invoice->customer_tax_exempt === StripeCustomer::TAX_EXEMPT_REVERSE;
}

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

/**
* Determine if the invoice will send an invoice to the customer.
*
* @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
5 changes: 3 additions & 2 deletions tests/Feature/InvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laravel\Cashier\Exceptions\InvalidCustomer;
use Laravel\Cashier\Exceptions\InvalidInvoice;
use Laravel\Cashier\Invoice;
use Stripe\Exception\InvalidRequestException as StripeInvalidRequestException;
use Stripe\InvoiceItem as StripeInvoiceItem;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

Expand All @@ -25,9 +26,9 @@ public function test_invoicing_fails_with_nothing_to_invoice()
$user->createAsStripeCustomer();
$user->updateDefaultPaymentMethod('pm_card_visa');

$response = $user->invoice();
$this->expectException(StripeInvalidRequestException::class);

$this->assertFalse($response);
$user->invoice();
}

public function test_customer_can_be_invoiced()
Expand Down

0 comments on commit 7d3a7ce

Please sign in to comment.