2026-03-29 22:24:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
use App\Models\Account;
|
2026-04-14 21:22:36 +00:00
|
|
|
use App\Models\Plan;
|
2026-03-29 22:24:28 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
|
|
|
|
|
|
class BillingController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function subscribe(Request $request): Response|RedirectResponse
|
|
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-03-31 03:40:18 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
if ($account && $account->hasActiveSubscription()) {
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.billing.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Inertia::render('billing/Subscribe', [
|
2026-04-14 21:22:36 +00:00
|
|
|
'plans' => Plan::active()->orderBy('sort')->get(),
|
2026-03-29 22:24:28 +00:00
|
|
|
'trialDays' => config('cashier.trial_days'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 21:22:36 +00:00
|
|
|
public function checkout(Request $request, Plan $plan): SymfonyResponse
|
2026-03-29 22:24:28 +00:00
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-03-31 03:40:18 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-04-14 21:22:36 +00:00
|
|
|
$priceId = $request->input('interval', 'monthly') === 'yearly'
|
|
|
|
|
? $plan->stripe_yearly_price_id
|
|
|
|
|
: $plan->stripe_monthly_price_id;
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-04-14 21:22:36 +00:00
|
|
|
abort_if(! $priceId, 422, 'Plan price not configured');
|
2026-03-31 03:40:18 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
$account->createOrGetStripeCustomer([
|
|
|
|
|
'email' => $account->stripeEmail(),
|
|
|
|
|
'name' => $account->stripeName(),
|
2026-04-14 21:22:36 +00:00
|
|
|
]);
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
$subscription = $account->newSubscription(Account::SUBSCRIPTION_NAME, $priceId)
|
2026-03-29 22:24:28 +00:00
|
|
|
->allowPromotionCodes()
|
2026-04-14 21:22:36 +00:00
|
|
|
->trialDays(config('cashier.trial_days'));
|
2026-03-29 22:24:28 +00:00
|
|
|
|
|
|
|
|
$checkoutSession = $subscription->checkout([
|
|
|
|
|
'success_url' => route('app.billing.processing').'?status=success',
|
|
|
|
|
'cancel_url' => route('app.billing.processing').'?status=cancelled',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
$account->update(['plan_id' => $plan->id]);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
2026-03-29 22:24:28 +00:00
|
|
|
return Inertia::location($checkoutSession->url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function processing(Request $request): Response|RedirectResponse
|
|
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-03-29 22:24:28 +00:00
|
|
|
$status = $request->query('status', 'processing');
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
if ($account && $account->subscribed(Account::SUBSCRIPTION_NAME)) {
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! in_array($status, ['processing', 'success', 'cancelled'])) {
|
|
|
|
|
$status = 'processing';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Inertia::render('billing/Processing', [
|
2026-04-15 01:22:04 +00:00
|
|
|
'accountId' => $account?->id,
|
2026-03-29 22:24:28 +00:00
|
|
|
'status' => $status,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 21:22:36 +00:00
|
|
|
public function index(Request $request): Response
|
2026-03-29 22:24:28 +00:00
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-03-31 03:40:18 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
$subscription = $account->subscription(Account::SUBSCRIPTION_NAME);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
|
|
|
|
return Inertia::render('billing/Index', [
|
2026-04-15 01:22:04 +00:00
|
|
|
'hasSubscription' => $account->subscribed(Account::SUBSCRIPTION_NAME),
|
2026-04-14 21:22:36 +00:00
|
|
|
'onTrial' => $subscription?->onTrial() ?? false,
|
|
|
|
|
'trialEndsAt' => $subscription?->trial_ends_at?->toFormattedDateString(),
|
|
|
|
|
'subscription' => $subscription?->only([
|
|
|
|
|
'stripe_status',
|
|
|
|
|
'ends_at',
|
|
|
|
|
]),
|
2026-04-15 01:22:04 +00:00
|
|
|
'plan' => $account->plan,
|
2026-04-14 21:22:36 +00:00
|
|
|
'plans' => Plan::active()->orderBy('sort')->get(),
|
2026-04-15 01:22:04 +00:00
|
|
|
'invoices' => $account->invoices()->map(fn ($invoice) => [
|
2026-04-14 21:22:36 +00:00
|
|
|
'id' => $invoice->id,
|
|
|
|
|
'date' => $invoice->date()->toFormattedDateString(),
|
|
|
|
|
'total' => $invoice->total(),
|
|
|
|
|
'status' => $invoice->status,
|
|
|
|
|
'invoice_pdf' => $invoice->invoice_pdf,
|
|
|
|
|
]),
|
2026-04-15 01:22:04 +00:00
|
|
|
'defaultPaymentMethod' => $account->defaultPaymentMethod()?->card?->only([
|
2026-04-14 21:22:36 +00:00
|
|
|
'brand',
|
|
|
|
|
'last4',
|
|
|
|
|
'exp_month',
|
|
|
|
|
'exp_year',
|
|
|
|
|
]),
|
|
|
|
|
]);
|
2026-03-29 22:24:28 +00:00
|
|
|
}
|
2026-03-31 03:40:18 +00:00
|
|
|
|
2026-04-14 21:22:36 +00:00
|
|
|
public function swap(Request $request, Plan $plan): RedirectResponse
|
2026-03-31 03:40:18 +00:00
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-03-31 03:40:18 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
|
|
|
|
abort_unless($account->subscribed(Account::SUBSCRIPTION_NAME), 422, 'No active subscription');
|
2026-04-14 21:22:36 +00:00
|
|
|
|
|
|
|
|
$priceId = $request->input('interval', 'monthly') === 'yearly'
|
|
|
|
|
? $plan->stripe_yearly_price_id
|
|
|
|
|
: $plan->stripe_monthly_price_id;
|
|
|
|
|
|
|
|
|
|
abort_if(! $priceId, 422, 'Plan price not configured');
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
$account->subscription(Account::SUBSCRIPTION_NAME)->swap($priceId);
|
|
|
|
|
$account->update(['plan_id' => $plan->id]);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
|
|
|
|
return redirect()->route('app.billing.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function portal(Request $request): RedirectResponse
|
|
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-04-14 21:22:36 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
return $account->redirectToBillingPortal(
|
2026-04-14 21:22:36 +00:00
|
|
|
route('app.billing.index')
|
|
|
|
|
);
|
2026-03-31 03:40:18 +00:00
|
|
|
}
|
2026-03-29 22:24:28 +00:00
|
|
|
}
|