Skip to content

Commit

Permalink
Merge pull request #838 from laravel/add-new-has-payment-method
Browse files Browse the repository at this point in the history
[11.x] Add new has payment method
  • Loading branch information
taylorotwell authored Dec 20, 2019
2 parents f64c2f9 + e6aebc3 commit f1ade71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,25 @@ public function createSetupIntent(array $options = [])
}

/**
* Determines if the customer currently has a payment method.
* Determines if the customer currently has a default payment method.
*
* @return bool
*/
public function hasPaymentMethod()
public function hasDefaultPaymentMethod()
{
return (bool) $this->card_brand;
}

/**
* Determines if the customer currently has at least one payment method.
*
* @return bool
*/
public function hasPaymentMethod()
{
return $this->paymentMethods()->isNotEmpty();
}

/**
* Get a collection of the entity's payment methods.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function test_we_can_add_payment_methods()
$this->assertInstanceOf(PaymentMethod::class, $paymentMethod);
$this->assertEquals('visa', $paymentMethod->card->brand);
$this->assertEquals('4242', $paymentMethod->card->last4);
$this->assertTrue($user->hasPaymentMethod());
$this->assertFalse($user->hasDefaultPaymentMethod());
}

public function test_we_can_remove_payment_methods()
Expand All @@ -38,10 +40,12 @@ public function test_we_can_remove_payment_methods()
$paymentMethod = $user->addPaymentMethod('pm_card_visa');

$this->assertCount(1, $user->paymentMethods());
$this->assertTrue($user->hasPaymentMethod());

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

$this->assertCount(0, $user->paymentMethods());
$this->assertFalse($user->hasPaymentMethod());
}

public function test_we_can_remove_the_default_payment_method()
Expand All @@ -52,13 +56,17 @@ public function test_we_can_remove_the_default_payment_method()
$paymentMethod = $user->updateDefaultPaymentMethod('pm_card_visa');

$this->assertCount(1, $user->paymentMethods());
$this->assertTrue($user->hasPaymentMethod());
$this->assertTrue($user->hasDefaultPaymentMethod());

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

$this->assertCount(0, $user->paymentMethods());
$this->assertNull($user->defaultPaymentMethod());
$this->assertNull($user->card_brand);
$this->assertNull($user->card_last_four);
$this->assertFalse($user->hasPaymentMethod());
$this->assertFalse($user->hasDefaultPaymentMethod());
}

public function test_we_can_set_a_default_payment_method()
Expand All @@ -71,6 +79,7 @@ public function test_we_can_set_a_default_payment_method()
$this->assertInstanceOf(PaymentMethod::class, $paymentMethod);
$this->assertEquals('visa', $paymentMethod->card->brand);
$this->assertEquals('4242', $paymentMethod->card->last4);
$this->assertTrue($user->hasDefaultPaymentMethod());

$paymentMethod = $user->defaultPaymentMethod();

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function test_we_can_determine_if_it_has_a_payment_method()
$user = new User;
$user->card_brand = 'visa';

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

$user = new User;

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

public function test_default_payment_method_returns_null_when_the_user_is_not_a_customer_yet()
Expand Down

0 comments on commit f1ade71

Please sign in to comment.