| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Billing\Billing;
- use App\Billing\UsageLimits;
- use App\Commerce\Payments\PaymentException;
- use App\Models\Plan;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Inertia\Inertia;
- use Inertia\Response;
- use Symfony\Component\HttpFoundation\Response as HttpResponse;
-
- /**
- * The merchant's own plan: see it, start it, change it, cancel it.
- */
- class BillingController extends Controller
- {
- public function index(Request $request, Billing $billing, UsageLimits $usage): Response|RedirectResponse
- {
- $user = $request->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');
- }
- }
|