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

[14.x] Cascade Stripe exceptions when invoicing #1210

Merged
merged 1 commit into from
Jun 25, 2021
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
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