Skip to content

Commit

Permalink
[12.x] Fix quantity preserving (#999)
Browse files Browse the repository at this point in the history
* Fix quantity preserving

* Apply fixes from StyleCI (#998)

* Update Subscription.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
driesvints and taylorotwell authored Sep 24, 2020
1 parent 9ca4eb2 commit 44ed976
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
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

0 comments on commit 44ed976

Please sign in to comment.