Skip to content

Commit a64b347

Browse files
committed
[16.x] Return PaymentMethod when already default; accept StripePaymentMethod instance
- Avoid returning null from updateDefaultPaymentMethod when the provided payment method is already the customer's default (return a PaymentMethod instance instead). - Accept Stripe\\PaymentMethod instances directly to avoid an extra retrieval. - Updated test_we_can_set_a_default_payment_method to exercise the "already default" path.
1 parent aef9813 commit a64b347

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Concerns/ManagesPaymentMethods.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,16 @@ public function updateDefaultPaymentMethod(StripePaymentMethod|string $paymentMe
171171

172172
$customer = $this->asStripeCustomer();
173173

174-
$stripePaymentMethod = $this->resolveStripePaymentMethod($paymentMethod);
174+
$stripePaymentMethod = $paymentMethod instanceof StripePaymentMethod
175+
? $paymentMethod
176+
: $this->resolveStripePaymentMethod($paymentMethod);
175177

176178
// If the customer already has the payment method as their default, we can bail out
177179
// of the call now. We don't need to keep adding the same payment method to this
178180
// model's account every single time we go through this specific process call.
181+
// We will return current PaymentMethod set in invoice settings
179182
if ($stripePaymentMethod->id === $customer->invoice_settings->default_payment_method) {
180-
return null;
183+
return new PaymentMethod($this, $stripePaymentMethod);
181184
}
182185

183186
$paymentMethod = $this->addPaymentMethod($stripePaymentMethod);

tests/Feature/PaymentMethodsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ public function test_we_can_set_a_default_payment_method()
118118
$this->assertEquals('4242', $paymentMethod->card->last4);
119119
$this->assertTrue($user->hasDefaultPaymentMethod());
120120

121+
$paymentMethod = $user->updateDefaultPaymentMethod($paymentMethod->asStripePaymentMethod());
122+
123+
$this->assertInstanceOf(PaymentMethod::class, $paymentMethod);
124+
$this->assertEquals('visa', $paymentMethod->card->brand);
125+
$this->assertEquals('4242', $paymentMethod->card->last4);
126+
$this->assertTrue($user->hasDefaultPaymentMethod());
127+
121128
$paymentMethod = $user->defaultPaymentMethod();
122129

123130
$this->assertInstanceOf(PaymentMethod::class, $paymentMethod);

0 commit comments

Comments
 (0)