/** * Create a new authentication controller instance. * * @param \Laravel\Spark\Contracts\Repositories\UserRepository $users * @param \Laravel\Spark\Contracts\Repositories\TeamRepository $teams * @return void */ public function __construct(UserRepository $users, TeamRepository $teams) { $this->users = $users; $this->teams = $teams; $this->plans = Spark::plans(); $this->middleware('guest', ['except' => 'getLogout']); }
/** * Create the subscription on Stripe. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function createSubscriptionOnStripe(Request $request, $user) { $plan = Spark::plans()->find($request->plan); $subscription = $user->subscription($plan->id); if ($plan->hasTrial()) { $subscription->trialFor(Carbon::now()->addDays($plan->trialDays)); } if ($request->coupon) { $subscription->withCoupon($request->coupon); } $subscription->create($request->stripe_token, ['email' => $user->email]); }
/** * Change the user's subscription plan. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function changeSubscriptionPlan(Request $request) { $this->validate($request, ['plan' => 'required']); $plan = Spark::plans()->find($request->plan); if ($plan->price() === 0) { $this->cancelSubscription(); } else { Auth::user()->subscription($request->plan)->maintainTrial()->prorate()->swapAndInvoice(); } event(new SubscriptionPlanChanged(Auth::user())); return $this->users->getCurrentUser(); }
/** * Update the subscription for the user. * * @param \Laravel\Spark\Http\Requests\UpdateSubscriptionRequest $request * @return Response */ public function update(UpdateSubscriptionRequest $request) { $plan = Spark::plans()->where('id', $request->plan)->first(); // This method is used both for updating subscriptions and for resuming cancelled // subscriptions that are still within their grace periods as this swap method // will be used for either of these situations without causing any problems. if ($plan->price === 0) { return $this->destroy($request); } else { $request->user()->subscription()->swap($request->plan); } event(new SubscriptionUpdated($request->user()->fresh())); }
/** * Change the user's subscription plan. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function changeSubscriptionPlan(Request $request) { $this->validate($request, ['plan' => 'required']); $plan = Spark::plans()->find($request->plan); if ($plan->price() === 0) { $this->cancelSubscription(); } elseif (Spark::$swapSubscriptionsWith) { $this->callCustomUpdater(Spark::$swapSubscriptionsWith, $request, [Auth::user()]); } else { Auth::user()->subscription('main')->swap($request->plan); } event(new SubscriptionPlanChanged(Auth::user())); return $this->users->getCurrentUser(); }
/** * Create the subscription on Stripe. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param \Stripe\Customer|null $stripeCustomer * @return void */ public function createSubscriptionOnStripe(Request $request, $user, $stripeCustomer = null) { $plan = Spark::plans()->find($request->plan); $subscription = $user->subscription($plan->id); if ($plan->hasTrial() && $stripeCustomer === null) { $subscription->trialFor(Carbon::now()->addDays($plan->trialDays)); } if ($request->coupon) { $subscription->withCoupon($request->coupon); } if (Spark::$createSubscriptionsWith) { $this->callCustomUpdater(Spark::$createSubscriptionsWith, $request, [$user, $subscription, $stripeCustomer]); } else { $this->createDefaultSubscription($request, $user, $subscription, $stripeCustomer); } }
/** * Create the subscription on Stripe. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function createSubscriptionOnStripe(Request $request, $user) { $plan = Spark::plans()->find($request->plan); $subscription = $user->newSubscription('main', $plan->id); if ($plan->hasTrial() && !$user->stripe_id) { $subscription->trialDays($plan->trialDays); } if ($request->coupon) { $subscription->withCoupon($request->coupon); } if (Spark::$createSubscriptionsWith) { $this->callCustomUpdater(Spark::$createSubscriptionsWith, $request, [$user, $subscription, $stripeCustomer]); } else { $subscription->create($request->stripe_token); } }
/** * Validate that the plan is eligible based on team restrictions. * * @param \Illuminate\Validation\Validator $valdiator * @return void */ protected function validatePlanEligibility($validator) { $plan = Spark::plans()->where('id', $this->plan)->first(); // If the desired plan is free, we will always need to let the user switch to that // plan since we'll want the user to always be able to cancel this subscription // without preventing them. So, we will just return here if it's a free plan. if (!$plan || $plan->price === 0) { return; } $this->callCustomCallback($validator, $plan); // If the user is ineligible for a plan based on their team member or collaborator // count, we will prevent them switching to this plan and send an error message // back to the client informing them of this limitation and they can upgrade. if (!$this->userIsEligibleForPlan($plan)) { $validator->errors()->add('plan', trans('spark::validation.eligibility')); } }
/** * Get the current coupon for the authenticated user. * * Used to display current discount on settings -> subscription tab. * * @return \Illuminate\Http\Response */ public function getCouponForUser() { Stripe::setApiKey(config('services.stripe.secret')); if (count(Spark::plans()) === 0) { abort(404); } try { $customer = StripeCustomer::retrieve(Auth::user()->stripe_id); if ($customer->discount) { return response()->json(Coupon::fromStripeCoupon(StripeCoupon::retrieve($customer->discount->coupon->id))); } else { abort(404); } } catch (Exception $e) { abort(404); } }
/** * Get the tab configuration for the "subscription" tab. * * @param bool $force * @return \Laravel\Spark\Ux\Settings\Tab|null */ public function subscription($force = false) { return new Tab('Subscription', 'spark::settings.tabs.subscription', 'fa-credit-card', function () use($force) { return count(Spark::plans()->paid()) > 0 || $force; }); }
/** * Get the full plan array for the specified plan. * * @return \Laravel\Spark\Plan|null */ public function plan() { if ($this->plan) { return Spark::plans()->where('id', $this->plan)->first(); } }
/** * Get the all of the plans defined for the appliation. * * @return Response */ public function all() { return response()->json(Spark::plans()); }
/** * Get the available billing plans for the given entity. * * @return \Illuminate\Support\Collection */ public function availablePlans() { return Spark::plans(); }