diff --git a/src/Concerns/ManagesPaymentMethods.php b/src/Concerns/ManagesPaymentMethods.php index 85bb4ae4..e52cfe56 100644 --- a/src/Concerns/ManagesPaymentMethods.php +++ b/src/Concerns/ManagesPaymentMethods.php @@ -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. * diff --git a/tests/Feature/PaymentMethodsTest.php b/tests/Feature/PaymentMethodsTest.php index 97a4a054..bbbbc975 100644 --- a/tests/Feature/PaymentMethodsTest.php +++ b/tests/Feature/PaymentMethodsTest.php @@ -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()); @@ -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() @@ -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(); diff --git a/tests/Unit/CustomerTest.php b/tests/Unit/CustomerTest.php index eb60f3ff..5ede2f00 100644 --- a/tests/Unit/CustomerTest.php +++ b/tests/Unit/CustomerTest.php @@ -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()