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

[9.0] Fixing defaultCard() exception when not Stripe customer #660

Merged
merged 3 commits into from
May 3, 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
4 changes: 4 additions & 0 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ public function cards($parameters = [])
*/
public function defaultCard()
{
if (! $this->hasStripeId()) {
return;
}

$customer = $this->asStripeCustomer();

foreach ($customer->sources->data as $card) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/CashierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Cashier\Tests\Integration;

use DateTime;
use Stripe\Card;
use Stripe\Plan;
use Stripe\Token;
use Carbon\Carbon;
Expand Down Expand Up @@ -257,6 +258,17 @@ 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());
driesvints marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_creating_subscription_fails_when_card_is_declined()
{
$user = User::create([
Expand Down