diff --git a/src/Concerns/ManagesPaymentMethods.php b/src/Concerns/ManagesPaymentMethods.php index 85bb4ae4..94d8ebfe 100644 --- a/src/Concerns/ManagesPaymentMethods.php +++ b/src/Concerns/ManagesPaymentMethods.php @@ -49,6 +49,23 @@ public function hasDefaultPaymentMethod() return (bool) $this->pm_type; } + /** + * Determines if the customer currently has a valid default payment method. + * + * @return bool + */ + public function hasValidDefaultPaymentMethod() + { + $paymentMethod = $this->defaultPaymentMethod(); + + // Return false if the payment method is null + if ($paymentMethod === null) { + return false; + } + + return ($this->resolveStripePaymentMethod($paymentMethod->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..8e95307d 100644 --- a/tests/Unit/CustomerTest.php +++ b/tests/Unit/CustomerTest.php @@ -46,6 +46,7 @@ public function test_we_can_determine_if_it_has_a_payment_method() $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()