/** * Handle the event. * * @param mixed $event * @return void */ public function handle($event) { $subscription = $event->billable->subscription(); if (!$subscription || !$subscription->valid()) { return; } $plan = BraintreeService::findPlan($subscription->braintree_plan); BraintreeSubscription::update($subscription->braintree_id, ['price' => $plan->price * (1 + $event->billable->taxPercentage() / 100), 'options' => ['prorateCharges' => false]]); }
/** * Swap the subscription to a new Braintree plan with a different frequency. * * @param string $plan * @return $this */ protected function swapAcrossFrequencies($plan) { $currentPlan = BraintreeService::findPlan($this->braintree_plan); $discount = $this->switchingToMonthlyPlan($currentPlan, $plan) ? $this->getDiscountForSwitchToMonthly($currentPlan, $plan) : $this->getDiscountForSwitchToYearly(); $options = []; if ($discount->amount > 0 && $discount->numberOfBillingCycles > 0) { $options = ['discounts' => ['add' => [['inheritedFromId' => 'plan-credit', 'amount' => (double) $discount->amount, 'numberOfBillingCycles' => $discount->numberOfBillingCycles]]]]; } $this->cancelNow(); return $this->user->newSubscription($this->name, $plan->id)->skipTrial()->create(null, [], $options); }
/** * Get the base subscription payload for Braintree. * * @param \Braintree\Customer $customer * @param array $options * @return array */ protected function getSubscriptionPayload($customer, array $options = []) { $plan = BraintreeService::findPlan($this->plan); if ($this->skipTrial) { $trialDuration = 0; } else { $trialDuration = $this->trialDays ?: 0; } return array_merge(['planId' => $this->plan, 'price' => $plan->price * (1 + $this->user->taxPercentage() / 100), 'paymentMethodToken' => $customer->paymentMethods[0]->token, 'trialPeriod' => $this->trialDays && !$this->skipTrial ? true : false, 'trialDurationUnit' => 'day', 'trialDuration' => $trialDuration], $options); }