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

[10.0] Throw exception when no stripe id is found #682

Merged
merged 1 commit into from
Jun 6, 2019
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
47 changes: 34 additions & 13 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Stripe\Customer as StripeCustomer;
use Stripe\BankAccount as StripeBankAccount;
use Stripe\InvoiceItem as StripeInvoiceItem;
use Laravel\Cashier\Exceptions\InvalidStripeCustomer;
use Stripe\Error\InvalidRequest as StripeErrorInvalidRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
Expand Down Expand Up @@ -82,9 +83,7 @@ public function hasCardOnFile()
*/
public function tab($description, $amount, array $options = [])
{
if (! $this->stripe_id) {
throw new InvalidArgumentException(class_basename($this).' is not a Stripe customer. See the createAsStripeCustomer method.');
}
$this->assertCustomerExists();

$options = array_merge([
'customer' => $this->stripe_id,
Expand Down Expand Up @@ -213,17 +212,15 @@ public function subscriptions()
*/
public function invoice(array $options = [])
{
if ($this->stripe_id) {
$parameters = array_merge($options, ['customer' => $this->stripe_id]);
$this->assertCustomerExists();

try {
return StripeInvoice::create($parameters, Cashier::stripeOptions())->pay();
} catch (StripeErrorInvalidRequest $e) {
return false;
}
}
$parameters = array_merge($options, ['customer' => $this->stripe_id]);

return true;
try {
return StripeInvoice::create($parameters, Cashier::stripeOptions())->pay();
} catch (StripeErrorInvalidRequest $e) {
return false;
}
}

/**
Expand All @@ -233,6 +230,8 @@ public function invoice(array $options = [])
*/
public function upcomingInvoice()
{
$this->assertCustomerExists();

try {
$stripeInvoice = StripeInvoice::upcoming(['customer' => $this->stripe_id], Cashier::stripeOptions());

Expand Down Expand Up @@ -266,7 +265,7 @@ public function findInvoice($id)
}

/**
* Find an invoice or throw a 404 error.
* Find an invoice or throw a 404 or 403 error.
*
* @param string $id
* @return \Laravel\Cashier\Invoice
Expand Down Expand Up @@ -307,6 +306,8 @@ public function downloadInvoice($id, array $data)
*/
public function invoices($includePending = false, $parameters = [])
{
$this->assertCustomerExists();

$invoices = [];

$parameters = array_merge(['limit' => 24], $parameters);
Expand Down Expand Up @@ -346,6 +347,8 @@ public function invoicesIncludingPending(array $parameters = [])
*/
public function cards($parameters = [])
{
$this->assertCustomerExists();

$cards = [];

$parameters = array_merge(['limit' => 24], $parameters);
Expand Down Expand Up @@ -391,6 +394,8 @@ public function defaultCard()
*/
public function updateCard($token)
{
$this->assertCustomerExists();

$customer = $this->asStripeCustomer();

$token = StripeToken::retrieve($token, Cashier::stripeOptions());
Expand Down Expand Up @@ -482,6 +487,8 @@ public function deleteCards()
*/
public function applyCoupon($coupon)
{
$this->assertCustomerExists();

$customer = $this->asStripeCustomer();

$customer->coupon = $coupon;
Expand Down Expand Up @@ -536,6 +543,20 @@ public function hasStripeId()
return ! is_null($this->stripe_id);
}

/**
* Determine if the entity has a Stripe customer ID and throw an exception if not.
*
* @return void
*
* @throws \Laravel\Cashier\Exceptions\InvalidStripeCustomer
*/
protected function assertCustomerExists()
{
if (! $this->stripe_id) {
throw InvalidStripeCustomer::nonCustomer($this);
}
}

/**
* Create a Stripe customer for the given model.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Exceptions/InvalidStripeCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Cashier\Exceptions;

use Exception;

class InvalidStripeCustomer extends Exception
{
/**
* Create a new CustomerFailure instance.
*
* @param \Illuminate\Database\Eloquent\Model $owner
* @return self
*/
public static function nonCustomer($owner)
{
return new static(class_basename($owner).' is not a Stripe customer. See the createAsStripeCustomer method.');
}
}
41 changes: 41 additions & 0 deletions tests/Integration/InvoicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Laravel\Cashier\Tests\Integration;

use Stripe\Invoice;
use Laravel\Cashier\Exceptions\InvalidStripeCustomer;

class InvoicesTest extends IntegrationTestCase
{
public function test_require_stripe_customer_for_invoicing()
{
$user = $this->createCustomer('require_stripe_customer_for_invoicing');

$this->expectException(InvalidStripeCustomer::class);

$user->invoice();
}

public function test_invoicing_fails_with_nothing_to_invoice()
{
$user = $this->createCustomer('invoicing_fails_with_nothing_to_invoice');
$user->createAsStripeCustomer();
$user->updateCard('tok_visa');

$response = $user->invoice();

$this->assertFalse($response);
}

public function test_customer_can_be_invoiced()
{
$user = $this->createCustomer('customer_can_be_invoiced');
$user->createAsStripeCustomer();
$user->updateCard('tok_visa');

$response = $user->invoiceFor('Laracon', 49900);

$this->assertInstanceOf(Invoice::class, $response);
$this->assertEquals(49900, $response->total);
}
}