Skip to content

Commit

Permalink
Merge subscription options when swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarrettsmith committed Oct 15, 2019
1 parent 37a5b9a commit fae6cfc
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function updateQuantity($quantity)
* @return $this|\Laravel\Cashier\Subscription
* @throws \Exception
*/
public function swap($plan)
public function swap($plan, array $subscriptionOptions = [])
{
if ($this->onGracePeriod() && $this->braintree_plan === $plan) {
return $this->resume();
Expand All @@ -210,20 +210,20 @@ public function swap($plan)
$plan = BraintreeService::findPlan($plan);

if ($this->wouldChangeBillingFrequency($plan) && $this->prorate) {
return $this->swapAcrossFrequencies($plan);
return $this->swapAcrossFrequencies($plan, $subscriptionOptions);
}

$subscription = $this->asBraintreeSubscription();

$payload = [
$payload = array_merge([
'planId' => $plan->id,
'price' => number_format($plan->price * (1 + ($this->owner->taxPercentage() / 100)), 2, '.', ''),
'neverExpires' => true,
'numberOfBillingCycles' => null,
'options' => [
'prorateCharges' => $this->prorate,
],
];
], $subscriptionOptions);

if ($this->coupon) {
$payload = $this->addCouponToPayload($payload);
Expand Down Expand Up @@ -262,7 +262,7 @@ protected function wouldChangeBillingFrequency($plan)
* @return \Laravel\Cashier\Subscription
* @throws \Exception
*/
protected function swapAcrossFrequencies($plan): self
protected function swapAcrossFrequencies($plan, $subscriptionOptions): self
{
$currentPlan = BraintreeService::findPlan($this->braintree_plan);

Expand All @@ -286,6 +286,8 @@ protected function swapAcrossFrequencies($plan): self
$options = $this->addCouponToPayload($options);
}

$options = array_merge_recursive($options, $subscriptionOptions);

$this->cancelNow();

return $this->owner->newSubscription($this->name, $plan->id)
Expand Down

0 comments on commit fae6cfc

Please sign in to comment.