Skip to content

Commit

Permalink
Check if the default payment method is valid.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Jun 27, 2024
1 parent 98fa78e commit ca8b3ec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
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

0 comments on commit ca8b3ec

Please sign in to comment.