diff --git a/src/Subscription.php b/src/Subscription.php index 8ec898de..531d8944 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -556,7 +556,7 @@ public function extendTrial(CarbonInterface $date) /** * Swap the subscription to new Stripe plans. * - * @param string|string[] $plans + * @param string|array $plans * @param array $options * @return $this * @@ -611,7 +611,7 @@ public function swap($plans, $options = []) /** * Swap the subscription to new Stripe plans, and invoice immediately. * - * @param string|string[] $plans + * @param string|array $plans * @param array $options * @return $this * @@ -628,17 +628,21 @@ public function swapAndInvoice($plans, $options = []) /** * Parse the given plans for a swap operation. * - * @param string|string[] $plans + * @param array $plans * @return \Illuminate\Support\Collection */ - protected function parseSwapPlans($plans) + protected function parseSwapPlans(array $plans) { - return collect($plans)->mapWithKeys(function ($options, $plan) { + $isSinglePlanSwap = $this->hasSinglePlan() && count($plans) === 1; + + return collect($plans)->mapWithKeys(function ($options, $plan) use ($isSinglePlanSwap) { $plan = is_string($options) ? $options : $plan; + $options = is_string($options) ? [] : $options; return [$plan => array_merge([ 'plan' => $plan, + 'quantity' => $isSinglePlanSwap ? $this->quantity : 1, 'tax_rates' => $this->getPlanTaxRatesForPayload($plan), ], $options)]; }); diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index c9010e78..a25e6ca6 100644 --- a/tests/Feature/SubscriptionsTest.php +++ b/tests/Feature/SubscriptionsTest.php @@ -213,6 +213,32 @@ public function test_swapping_subscription_with_coupon() $this->assertEquals(static::$couponId, $subscription->asStripeSubscription()->discount->coupon->id); } + public function test_swapping_subscription_and_preserving_quantity() + { + $user = $this->createCustomer('swapping_subscription_and_preserving_quantity'); + $subscription = $user->newSubscription('main', static::$planId) + ->quantity(5, static::$planId) + ->create('pm_card_visa'); + + $subscription = $subscription->swap(static::$otherPlanId); + + $this->assertSame(5, $subscription->quantity); + $this->assertSame(5, $subscription->asStripeSubscription()->quantity); + } + + public function test_swapping_subscription_and_adopting_new_quantity() + { + $user = $this->createCustomer('swapping_subscription_and_adopting_new_quantity'); + $subscription = $user->newSubscription('main', static::$planId) + ->quantity(5, static::$planId) + ->create('pm_card_visa'); + + $subscription = $subscription->swap([static::$otherPlanId => ['quantity' => 3]]); + + $this->assertSame(3, $subscription->quantity); + $this->assertSame(3, $subscription->asStripeSubscription()->quantity); + } + public function test_declined_card_during_subscribing_results_in_an_exception() { $user = $this->createCustomer('declined_card_during_subscribing_results_in_an_exception');