From 7b46d0495844edc9d5fb7bd535c46dfeedc36cd6 Mon Sep 17 00:00:00 2001 From: Judah Wright Date: Wed, 1 May 2019 11:38:30 -0600 Subject: [PATCH 1/3] Fixing bug where the `defaultCard()` method would throw an exception instead of returning null if the billable model had no stripe Id --- src/Billable.php | 4 ++++ tests/Integration/CashierTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Billable.php b/src/Billable.php index 582672db..1656bcea 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -379,6 +379,10 @@ public function cards($parameters = []) */ public function defaultCard() { + if (! $this->hasStripeId()) { + return null; + } + $customer = $this->asStripeCustomer(); foreach ($customer->sources->data as $card) { diff --git a/tests/Integration/CashierTest.php b/tests/Integration/CashierTest.php index 698ff19d..13d5fbd9 100644 --- a/tests/Integration/CashierTest.php +++ b/tests/Integration/CashierTest.php @@ -3,6 +3,7 @@ namespace Laravel\Cashier\Tests\Integration; use DateTime; +use Stripe\Card; use Stripe\Plan; use Stripe\Token; use Carbon\Carbon; @@ -257,6 +258,29 @@ public function test_subscriptions_can_be_created() $this->assertInstanceOf(Carbon::class, $invoice->date()); } + public function test_default_card_returns_null_when_not_customer() + { + $user = User::create([ + 'email' => 'taylor@laravel.com', + 'name' => 'Taylor Otwell', + ]); + + // Assert that the defaultCard method returns null for billable models that are not stripe customers, and no exception is thrown + $this->assertNull($user->defaultCard()); + + // Create the user as a stripe customer + $user->createAsStripeCustomer(); + + // Assert that the defaultCard method returns null if the user is a stripe customer but has no cards + $this->assertNull($user->defaultCard()); + + // Add a card to the stripe customer + $user->updateCard($this->getTestToken()); + + // Now that the user has a card on file, make sure it is returned as expected + $this->assertInstanceOf(Card::class, $user->defaultCard()); + } + public function test_creating_subscription_fails_when_card_is_declined() { $user = User::create([ From d790c42cfec00333f19c31781bb47334e64dddac Mon Sep 17 00:00:00 2001 From: Judah Wright Date: Wed, 1 May 2019 11:59:33 -0600 Subject: [PATCH 2/3] Addressing StyleCi issue --- src/Billable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Billable.php b/src/Billable.php index 1656bcea..c2e19cab 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -380,7 +380,7 @@ public function cards($parameters = []) public function defaultCard() { if (! $this->hasStripeId()) { - return null; + return; } $customer = $this->asStripeCustomer(); From 36e194d1a1a1dcf6d3e96b20dac29e19a8f4d04b Mon Sep 17 00:00:00 2001 From: Judah Wright Date: Thu, 2 May 2019 08:09:54 -0600 Subject: [PATCH 3/3] Removing redundant test assertions --- tests/Integration/CashierTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/Integration/CashierTest.php b/tests/Integration/CashierTest.php index 13d5fbd9..a21d6711 100644 --- a/tests/Integration/CashierTest.php +++ b/tests/Integration/CashierTest.php @@ -267,18 +267,6 @@ public function test_default_card_returns_null_when_not_customer() // Assert that the defaultCard method returns null for billable models that are not stripe customers, and no exception is thrown $this->assertNull($user->defaultCard()); - - // Create the user as a stripe customer - $user->createAsStripeCustomer(); - - // Assert that the defaultCard method returns null if the user is a stripe customer but has no cards - $this->assertNull($user->defaultCard()); - - // Add a card to the stripe customer - $user->updateCard($this->getTestToken()); - - // Now that the user has a card on file, make sure it is returned as expected - $this->assertInstanceOf(Card::class, $user->defaultCard()); } public function test_creating_subscription_fails_when_card_is_declined()