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

Add PaymentMethod type parameter to ManagesPaymentMethods #1055

Closed
wants to merge 3 commits 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
29 changes: 18 additions & 11 deletions src/Concerns/ManagesPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ public function hasDefaultPaymentMethod()
}

/**
* Determines if the customer currently has at least one payment method.
* Determines if the customer currently has at least one payment method of the given type.
*
* @param string $type
* @return bool
*/
public function hasPaymentMethod()
public function hasPaymentMethod($type = 'card')
{
return $this->paymentMethods()->isNotEmpty();
return $this->paymentMethods([], $type)->isNotEmpty();
}

/**
* Get a collection of the entity's payment methods.
*
* @param array $parameters
* @param string $type
* @return \Illuminate\Support\Collection|\Laravel\Cashier\PaymentMethod[]
*/
public function paymentMethods($parameters = [])
public function paymentMethods($parameters = [], $type = 'card')
{
if (! $this->hasStripeId()) {
return collect();
Expand All @@ -61,7 +63,7 @@ public function paymentMethods($parameters = [])

// "type" is temporarily required by Stripe...
$paymentMethods = StripePaymentMethod::all(
['customer' => $this->stripe_id, 'type' => 'card'] + $parameters,
['customer' => $this->stripe_id, 'type' => $type] + $parameters,
$this->stripeOptions()
);

Expand Down Expand Up @@ -221,9 +223,13 @@ public function updateDefaultPaymentMethodFromStripe()
*/
protected function fillPaymentMethodDetails($paymentMethod)
{
if ($paymentMethod->type === 'card') {
$this->card_brand = $paymentMethod->card->brand;
$this->card_last_four = $paymentMethod->card->last4;
$type = $paymentMethod->type;
$this->card_brand = $type;
$pmCustomAttributes = $paymentMethod->$type;
if (isset($pmCustomAttributes->last4)) {
$this->card_last_four = $pmCustomAttributes->last4;
} else {
$this->card_last_four = null;
}

return $this;
Expand Down Expand Up @@ -251,13 +257,14 @@ protected function fillSourceDetails($source)
}

/**
* Deletes the entity's payment methods.
* Deletes the entity's payment methods of the given type.
*
* @param string $type
* @return void
*/
public function deletePaymentMethods()
public function deletePaymentMethods($type = 'card')
{
$this->paymentMethods()->each(function (PaymentMethod $paymentMethod) {
$this->paymentMethods([], $type)->each(function (PaymentMethod $paymentMethod) {
$paymentMethod->delete();
});

Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public function test_we_can_set_a_default_payment_method()

$this->assertInstanceOf(PaymentMethod::class, $paymentMethod);
$this->assertEquals('visa', $paymentMethod->card->brand);
$this->assertEquals('card', $user->card_brand);
$this->assertEquals('4242', $paymentMethod->card->last4);
$this->assertEquals('4242', $user->card_last_four);
}

public function test_legacy_we_can_retrieve_an_old_default_source_as_a_default_payment_method()
Expand Down Expand Up @@ -141,7 +143,7 @@ public function test_we_can_sync_the_default_payment_method_from_stripe()

$user = $user->updateDefaultPaymentMethodFromStripe();

$this->assertEquals('visa', $user->card_brand);
$this->assertEquals('card', $user->card_brand);
$this->assertEquals('4242', $user->card_last_four);
}

Expand Down