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

[15.x] Check if the default payment method is valid. #1683

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions src/Concerns/ManagesPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function hasDefaultPaymentMethod()
return (bool) $this->pm_type;
}

/**
* Determines if the customer currently has a valid default payment method.
*
* @return bool
*/
public function hasValidDefaultPaymentMethod()
{
return !(($this->defaultPaymentMethod() === null) ||
(
($this->defaultPaymentMethod()->id !== null) &&
($this->resolveStripePaymentMethod($this->defaultPaymentMethod()->id)=== null)
));
}

/**
* Determines if the customer currently has at least one payment method of an optional type.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function test_we_can_delete_the_default_payment_method()
$this->assertCount(1, $user->paymentMethods());
$this->assertTrue($user->hasPaymentMethod());
$this->assertTrue($user->hasDefaultPaymentMethod());
$this->assertTrue($user->hasValidDefaultPaymentMethod());

$user->deletePaymentMethod($paymentMethod->asStripePaymentMethod());

Expand All @@ -104,6 +105,7 @@ public function test_we_can_delete_the_default_payment_method()
$this->assertNull($user->pm_last_four);
$this->assertFalse($user->hasPaymentMethod());
$this->assertFalse($user->hasDefaultPaymentMethod());
$this->assertFalse($user->hasValidDefaultPaymentMethod());
}

public function test_we_can_set_a_default_payment_method()
Expand All @@ -117,6 +119,7 @@ public function test_we_can_set_a_default_payment_method()
$this->assertEquals('visa', $paymentMethod->card->brand);
$this->assertEquals('4242', $paymentMethod->card->last4);
$this->assertTrue($user->hasDefaultPaymentMethod());
$this->assertTrue($user->hasValidDefaultPaymentMethod());

$paymentMethod = $user->defaultPaymentMethod();

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public function test_we_can_determine_if_it_has_a_payment_method()
$user->pm_type = 'visa';

$this->assertTrue($user->hasDefaultPaymentMethod());
$this->assertTrue($user->hasValidDefaultPaymentMethod());

$user = new User;

$this->assertFalse($user->hasDefaultPaymentMethod());
$this->assertFalse($user->hasValidDefaultPaymentMethod());
}

public function test_default_payment_method_returns_null_when_the_user_is_not_a_customer_yet()
Expand Down
Loading