Skip to content

Commit

Permalink
[13.x] Throw payment exceptions on item swap (#1157)
Browse files Browse the repository at this point in the history
* Throw payment exceptions on item swap

* Refactor expands
  • Loading branch information
driesvints authored May 18, 2021
1 parent aa2444b commit 93eb220
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/SubscriptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public function updateQuantity($quantity)
'payment_behavior' => $this->paymentBehavior(),
'proration_behavior' => $this->prorateBehavior(),
'quantity' => $quantity,
'expand' => ['subscription.latest_invoice.payment_intent'],
]);

$this->fill([
Expand All @@ -125,9 +124,7 @@ public function updateQuantity($quantity)
}

if ($this->subscription->hasIncompletePayment()) {
(new Payment(
$stripeSubscriptionItem->subscription->latest_invoice->payment_intent
))->validate();
optional($this->subscription->latestPayment())->validate();
}

return $this;
Expand All @@ -146,32 +143,30 @@ public function swap($plan, array $options = [])
{
$this->subscription->guardAgainstIncomplete();

$options = array_merge([
$stripeSubscriptionItem = $this->updateStripeSubscriptionItem(array_merge([
'plan' => $plan,
'quantity' => $this->quantity,
'payment_behavior' => $this->paymentBehavior(),
'proration_behavior' => $this->prorateBehavior(),
'tax_rates' => $this->subscription->getPlanTaxRatesForPayload($plan),
], $options);

$item = StripeSubscriptionItem::update(
$this->stripe_id,
$options,
$this->subscription->owner->stripeOptions()
);
], $options));

$this->fill([
'stripe_plan' => $plan,
'quantity' => $item->quantity,
'quantity' => $stripeSubscriptionItem->quantity,
])->save();

if ($this->subscription->hasSinglePlan()) {
$this->subscription->fill([
'stripe_plan' => $plan,
'quantity' => $item->quantity,
'quantity' => $stripeSubscriptionItem->quantity,
])->save();
}

if ($this->subscription->hasIncompletePayment()) {
optional($this->subscription->latestPayment())->validate();
}

return $this;
}

Expand Down

0 comments on commit 93eb220

Please sign in to comment.