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

[12.x] Fix quantity preserving #999

Merged
merged 3 commits into from
Sep 24, 2020
Merged
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
14 changes: 9 additions & 5 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand All @@ -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)];
});
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down