/** * Create a new authentication controller instance. * * @param \iBourgeois\Spark\Contracts\Repositories\UserRepository $users * @param \iBourgeois\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']); }
/** * 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 * @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); } }
/** * 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 \iBourgeois\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; }); }