From 927d6d53be9f3907c7ebee46f05fd8aa71ba0800 Mon Sep 17 00:00:00 2001 From: Leo Lutz Date: Thu, 8 Feb 2024 08:00:26 -0700 Subject: [PATCH] [15.x] Fix payment behavior not being passed (#1649) * Add failing test for #1646 * Pass payment_behavior from Subscription to SubscriptionItem --- src/Concerns/InteractsWithPaymentBehavior.php | 13 +++++++++++++ src/Subscription.php | 15 ++++++++++++--- .../SubscriptionsWithMultiplePricesTest.php | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Concerns/InteractsWithPaymentBehavior.php b/src/Concerns/InteractsWithPaymentBehavior.php index e7f38161..5653c2b0 100644 --- a/src/Concerns/InteractsWithPaymentBehavior.php +++ b/src/Concerns/InteractsWithPaymentBehavior.php @@ -70,4 +70,17 @@ public function paymentBehavior() { return $this->paymentBehavior; } + + /** + * Set the payment behavior for any subscription updates. + * + * @param string $paymentBehavior + * @return $this + */ + public function setPaymentBehavior($paymentBehavior) + { + $this->paymentBehavior = $paymentBehavior; + + return $this; + } } diff --git a/src/Subscription.php b/src/Subscription.php index 252dc94a..f18be08e 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -435,7 +435,10 @@ public function incrementQuantity($count = 1, $price = null) $this->guardAgainstIncomplete(); if ($price) { - $this->findItemOrFail($price)->setProrationBehavior($this->prorationBehavior)->incrementQuantity($count); + $this->findItemOrFail($price) + ->setPaymentBehavior($this->paymentBehavior) + ->setProrationBehavior($this->prorationBehavior) + ->incrementQuantity($count); return $this->refresh(); } @@ -478,7 +481,10 @@ public function decrementQuantity($count = 1, $price = null) $this->guardAgainstIncomplete(); if ($price) { - $this->findItemOrFail($price)->setProrationBehavior($this->prorationBehavior)->decrementQuantity($count); + $this->findItemOrFail($price) + ->setPaymentBehavior($this->paymentBehavior) + ->setProrationBehavior($this->prorationBehavior) + ->decrementQuantity($count); return $this->refresh(); } @@ -502,7 +508,10 @@ public function updateQuantity($quantity, $price = null) $this->guardAgainstIncomplete(); if ($price) { - $this->findItemOrFail($price)->setProrationBehavior($this->prorationBehavior)->updateQuantity($quantity); + $this->findItemOrFail($price) + ->setPaymentBehavior($this->paymentBehavior) + ->setProrationBehavior($this->prorationBehavior) + ->updateQuantity($quantity); return $this->refresh(); } diff --git a/tests/Feature/SubscriptionsWithMultiplePricesTest.php b/tests/Feature/SubscriptionsWithMultiplePricesTest.php index 3b5a9cde..9ed09cd4 100644 --- a/tests/Feature/SubscriptionsWithMultiplePricesTest.php +++ b/tests/Feature/SubscriptionsWithMultiplePricesTest.php @@ -319,6 +319,23 @@ public function test_subscription_item_quantity_changes_can_be_prorated() $this->assertEquals(2000, $user->upcomingInvoice()->rawTotal()); } + public function test_subscription_item_quantity_change_can_be_thrown() + { + $user = $this->createCustomer('subscription_item_quantity_change_can_be_thrown'); + + $subscription = $user->newSubscription('main', [self::$priceId, self::$otherPriceId]) + ->quantity(1, self::$otherPriceId) + ->create('pm_card_visa'); + + $this->assertEquals(2000, ($invoice1 = $user->invoices()->first())->rawTotal()); + + $user->updateDefaultPaymentMethod('pm_card_chargeCustomerFail'); + + $this->expectException(\Stripe\Exception\CardException::class); + + $subscription->errorIfPaymentFails()->alwaysInvoice()->updateQuantity(2, self::$otherPriceId); + } + /** * Create a subscription with a single price. *