Laravel\Cashier\Subscription::swap PHP Method

swap() public method

Swap the subscription to a new Braintree plan.
public swap ( string $plan )
$plan string
    public function swap($plan)
    {
        if ($this->onGracePeriod() && $this->braintree_plan === $plan) {
            return $this->resume();
        }
        if (!$this->active()) {
            return $this->user->newSubscription($this->name, $plan)->skipTrial()->create();
        }
        $plan = BraintreeService::findPlan($plan);
        if ($this->wouldChangeBillingFrequency($plan)) {
            return $this->swapAcrossFrequencies($plan);
        }
        $subscription = $this->asBraintreeSubscription();
        $response = BraintreeSubscription::update($subscription->id, ['planId' => $plan->id, 'price' => $plan->price * (1 + $this->user->taxPercentage() / 100), 'neverExpires' => true, 'numberOfBillingCycles' => null, 'options' => ['prorateCharges' => true]]);
        if ($response->success) {
            $this->fill(['braintree_plan' => $plan->id, 'ends_at' => null, 'trial_ends_at' => null])->save();
        } else {
            throw new Exception('Braintree failed to swap plans: ' . $response->message);
        }
        return $this;
    }