user(); // Coming back from the payment page: attach the new subscription, then show a clean address. if (is_string($request->query('session_id'))) { try { $attached = $billing->completeCheckout($user, $request->query('session_id')); } catch (PaymentException $e) { report($e); $attached = false; } Inertia::flash('toast', $attached ? ['type' => 'success', 'message' => __('Your plan is active. Thank you!')] : ['type' => 'error', 'message' => __('We could not confirm the payment. If you were charged, it will show here shortly.')]); return to_route('billing'); } // Ask Stripe for the latest state, so this page is right even without webhooks. $billing->refresh($user); $subscription = $user->subscription()->with('plan')->first(); return Inertia::render('billing/index', [ 'usage' => $usage->for($user), 'plans' => Plan::query()->where('price', '>', 0)->orderBy('sort')->get()->map(fn (Plan $plan) => $plan->toDisplay()), 'subscription' => $subscription !== null && $subscription->stripe_subscription_id !== null ? [ 'active' => $subscription->isActive(), 'status' => $subscription->status, 'plan' => $subscription->plan?->key, 'current_period_end' => $subscription->current_period_end?->toIso8601String(), 'cancel_at_period_end' => $subscription->cancel_at_period_end, ] : null, ]); } /** * Start a paid plan, or move to a different one. */ public function subscribe(Request $request, Billing $billing): HttpResponse { $validated = $request->validate([ 'plan' => ['required', 'string', Rule::exists('plans', 'key')->where(fn ($query) => $query->where('price', '>', 0))], ]); $user = $request->user(); $plan = Plan::query()->where('key', $validated['plan'])->firstOrFail(); try { // Already paying: switch plans in place. Stripe works out the price difference. if ($user->subscription?->isActive()) { $billing->changePlan($user, $plan); return $this->back('success', __('You are now on the :plan plan.', ['plan' => $plan->name])); } $link = $billing->startCheckout($user, $plan, route('billing'), route('billing')); } catch (PaymentException $e) { report($e); return $this->back('error', __('We could not reach the payment service. Please try again in a moment.')); } // The payment page is on another website, so this is a full browser redirect. return Inertia::location($link->url); } /** * Stop renewing. The plan stays until the end of the period already paid for. */ public function cancel(Request $request, Billing $billing): RedirectResponse { return $this->change($request, $billing, true, __('Your plan will end on the date shown. You can resume it any time before then.')); } /** * Undo a cancellation before the period ends. */ public function resume(Request $request, Billing $billing): RedirectResponse { return $this->change($request, $billing, false, __('Your plan will keep renewing.')); } private function change(Request $request, Billing $billing, bool $cancel, string $message): RedirectResponse { try { $billing->setCancel($request->user(), $cancel); } catch (PaymentException $e) { report($e); return $this->back('error', __('We could not reach the payment service. Please try again in a moment.')); } return $this->back('success', $message); } private function back(string $type, string $message): RedirectResponse { Inertia::flash('toast', ['type' => $type, 'message' => $message]); return to_route('billing'); } }