diff --git a/src/Concerns/ManagesInvoices.php b/src/Concerns/ManagesInvoices.php index 73fb31b7..d6091cfb 100644 --- a/src/Concerns/ManagesInvoices.php +++ b/src/Concerns/ManagesInvoices.php @@ -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; @@ -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']] ) ); diff --git a/src/Invoice.php b/src/Invoice.php index d0c77193..09bbc09d 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -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. * diff --git a/tests/Feature/InvoicesTest.php b/tests/Feature/InvoicesTest.php index 37852c71..c704e0eb 100644 --- a/tests/Feature/InvoicesTest.php +++ b/tests/Feature/InvoicesTest.php @@ -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; @@ -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()