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

[11.x] Fix quantity methods #919

Merged
merged 2 commits into from
Apr 17, 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
101 changes: 60 additions & 41 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ public function scopeNotOnGracePeriod($query)
*/
public function incrementQuantity($count = 1, $plan = null)
{
$this->guardAgainstIncomplete();

if ($plan) {
$this->findItemOrFail($plan)->incrementQuantity($count);

return $this->refresh();
}

$this->guardAgainstMultiplePlans();

$this->updateQuantity($this->quantity + $count, $plan);

return $this;
Expand All @@ -408,6 +418,16 @@ public function incrementQuantity($count = 1, $plan = null)
*/
public function incrementAndInvoice($count = 1, $plan = null)
{
$this->guardAgainstIncomplete();

if ($plan) {
$this->findItemOrFail($plan)->incrementQuantity($count);

return $this->refresh();
}

$this->guardAgainstMultiplePlans();

$this->incrementQuantity($count, $plan);

$this->invoice();
Expand All @@ -426,9 +446,17 @@ public function incrementAndInvoice($count = 1, $plan = null)
*/
public function decrementQuantity($count = 1, $plan = null)
{
$this->updateQuantity(max(1, $this->quantity - $count), $plan);
$this->guardAgainstIncomplete();

return $this;
if ($plan) {
$this->findItemOrFail($plan)->decrementQuantity($count);

return $this->refresh();
}

$this->guardAgainstMultiplePlans();

return $this->updateQuantity(max(1, $this->quantity - $count), $plan);
}

/**
Expand All @@ -442,45 +470,27 @@ public function decrementQuantity($count = 1, $plan = null)
*/
public function updateQuantity($quantity, $plan = null)
{
if ($this->incomplete()) {
throw SubscriptionUpdateFailure::incompleteSubscription($this);
}
$this->guardAgainstIncomplete();

$this->guardAgainstMultiplePlans($plan);

$item = null;

if ($this->hasSinglePlan()) {
$stripeSubscription = $this->asStripeSubscription();
if ($plan) {
$this->findItemOrFail($plan)->updateQuantity($quantity);

$stripeSubscription->quantity = $quantity;

$stripeSubscription->proration_behavior = $this->prorateBehavior();

$stripeSubscription->save();

$this->quantity = $quantity;

$this->save();

$item = $this->items()->first();
} elseif ($plan) {
$item = $this->findItemOrFail($plan);
return $this->refresh();
}

if ($item) {
$stripeSubscriptionItem = $item->asStripeSubscriptionItem();
$this->guardAgainstMultiplePlans();

$stripeSubscriptionItem->quantity = $quantity;
$stripeSubscription = $this->asStripeSubscription();

$stripeSubscriptionItem->proration_behavior = $this->prorateBehavior();
$stripeSubscription->quantity = $quantity;

$stripeSubscriptionItem->save();
$stripeSubscription->proration_behavior = $this->prorateBehavior();

$item->quantity = $quantity;
$stripeSubscription->save();

$item->save();
}
$this->quantity = $quantity;

$this->save();

return $this;
}
Expand Down Expand Up @@ -556,9 +566,7 @@ public function swap($plans, $options = [])
throw new InvalidArgumentException('Please provide at least one plan when swapping.');
}

if ($this->incomplete()) {
throw SubscriptionUpdateFailure::incompleteSubscription($this);
}
$this->guardAgainstIncomplete();

$items = $this->mergeItemsThatShouldBeDeletedDuringSwap(
$this->parseSwapPlans($plans)
Expand Down Expand Up @@ -689,9 +697,7 @@ protected function getSwapOptions(Collection $items, $options)
*/
public function addPlan($plan, $quantity = 1, $options = [])
{
if ($this->incomplete()) {
throw SubscriptionUpdateFailure::incompleteSubscription($this);
}
$this->guardAgainstIncomplete();

if ($this->items->contains('stripe_plan', $plan)) {
throw SubscriptionUpdateFailure::duplicatePlan($this, $plan);
Expand Down Expand Up @@ -980,17 +986,30 @@ public function latestPayment()
: null;
}

/**
* Make sure a subscription is not incomplete when performing changes.
*
* @return void
*
* @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
*/
public function guardAgainstIncomplete()
{
if ($this->incomplete()) {
throw SubscriptionUpdateFailure::incompleteSubscription($this);
}
}

/**
* Make sure a plan argument is provided when the subscription is a multi plan subscription.
*
* @param string|null $plan
* @return void
*
* @throws \InvalidArgumentException
*/
protected function guardAgainstMultiplePlans($plan)
public function guardAgainstMultiplePlans()
{
if (is_null($plan) && $this->hasMultiplePlans()) {
if ($this->hasMultiplePlans()) {
throw new InvalidArgumentException(
'This method requires a plan argument since the subscription has multiple plans.'
);
Expand Down
86 changes: 82 additions & 4 deletions src/SubscriptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Model;
use Laravel\Cashier\Concerns\Prorates;
use Laravel\Cashier\Exceptions\SubscriptionUpdateFailure;
use Stripe\SubscriptionItem as StripeSubscriptionItem;

/**
Expand Down Expand Up @@ -40,6 +39,87 @@ public function subscription()
return $this->belongsTo(Subscription::class);
}

/**
* Increment the quantity of the subscription item.
*
* @param int $count
* @return $this
*
* @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
*/
public function incrementQuantity($count = 1)
{
$this->updateQuantity($this->quantity + $count);

return $this;
}

/**
* Increment the quantity of the subscription item, and invoice immediately.
*
* @param int $count
* @return $this
*
* @throws \Laravel\Cashier\Exceptions\IncompletePayment
* @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
*/
public function incrementAndInvoice($count = 1)
{
$this->incrementQuantity($count);

$this->subscription->invoice();

return $this;
}

/**
* Decrement the quantity of the subscription item.
*
* @param int $count
* @return $this
*
* @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
*/
public function decrementQuantity($count = 1)
{
$this->updateQuantity(max(1, $this->quantity - $count));

return $this;
}

/**
* Update the quantity of the subscription item.
*
* @param int $quantity
* @return $this
*
* @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
*/
public function updateQuantity($quantity)
{
$this->subscription->guardAgainstIncomplete();

$stripeSubscriptionItem = $this->asStripeSubscriptionItem();

$stripeSubscriptionItem->quantity = $quantity;

$stripeSubscriptionItem->proration_behavior = $this->prorateBehavior();

$stripeSubscriptionItem->save();

$this->quantity = $quantity;

$this->save();

if ($this->subscription->hasSinglePlan()) {
$this->subscription->quantity = $quantity;

$this->subscription->save();
}

return $this;
}

/**
* Swap the subscription item to a new Stripe plan.
*
Expand All @@ -51,9 +131,7 @@ public function subscription()
*/
public function swap($plan, $options = [])
{
if ($this->subscription->incomplete()) {
throw SubscriptionUpdateFailure::incompleteSubscription($this->subscription);
}
$this->subscription->guardAgainstIncomplete();

$options = array_merge([
'plan' => $plan,
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/MultiplanSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,42 @@ public function test_subscription_item_quantities_can_be_updated()
$this->assertSame(5, $item->quantity);
}

public function test_subscription_item_quantities_can_be_incremented()
{
$user = $this->createCustomer('subscription_item_quantities_can_be_updated');

$subscription = $user->newSubscription('main', [self::$planId, self::$otherPlanId])->create('pm_card_visa');

$subscription->incrementQuantity(3, self::$otherPlanId);

$item = $subscription->findItemOrFail(self::$otherPlanId);

$this->assertSame(4, $item->quantity);

$item->incrementQuantity(3);

$this->assertSame(7, $item->quantity);
}

public function test_subscription_item_quantities_can_be_decremented()
{
$user = $this->createCustomer('subscription_item_quantities_can_be_updated');

$subscription = $user->newSubscription('main', [self::$planId, self::$otherPlanId])
->quantity(5, self::$otherPlanId)
->create('pm_card_visa');

$subscription->decrementQuantity(2, self::$otherPlanId);

$item = $subscription->findItemOrFail(self::$otherPlanId);

$this->assertSame(3, $item->quantity);

$item->decrementQuantity(2);

$this->assertSame(1, $item->quantity);
}

public function test_multiple_plans_can_be_swapped()
{
$user = $this->createCustomer('multiple_plans_can_be_swapped');
Expand Down