- Sidebar reorganized: Workspace group (connections, hashtags, labels, API keys, settings) and Account group (settings, usage, billing) - Account group only visible to owner and hidden in self-hosted mode - Onboarding simplified: role -> account (connect socials) -> completed -> redirect to /subscribe. Removed Subscription setup step. - Subscribe page redesigned with 4 plan cards, monthly/yearly toggle, trial info, and per-plan features list - Billing page redesigned following Sendkit layout (sections with sidebar labels) - Processing page uses usePoll with immediate watch for subscription activation - Cancel URL redirects directly to /subscribe - Account settings page with name and billing_email (syncs with Stripe) - Usage page with ring meters for all plan limits - Settings layout tabs only for user pages (profile, password, notifications). Workspace/API keys/billing are standalone pages. - GoogleAuthButton extracted as reusable component - WorkspaceRole TypeScript enum for type-safe role checks in frontend - Trial period changed to 7 days - Fixed onboarding loop when user confirms email - All 1101 tests passing
147 lines
4.7 KiB
PHP
147 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
use App\Models\Account;
|
|
use App\Models\Plan;
|
|
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
|
|
{
|
|
$account = $request->user()->account;
|
|
|
|
if ($account && $account->hasActiveSubscription()) {
|
|
return redirect()->route('app.billing.index');
|
|
}
|
|
|
|
return Inertia::render('billing/Subscribe', [
|
|
'plans' => Plan::active()->orderBy('sort')->get(),
|
|
'trialDays' => config('cashier.trial_days'),
|
|
]);
|
|
}
|
|
|
|
public function checkout(Request $request, Plan $plan): SymfonyResponse
|
|
{
|
|
$user = $request->user();
|
|
$account = $user->account;
|
|
|
|
abort_unless($user->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
|
|
|
$request->validate([
|
|
'price_id' => ['required', 'string'],
|
|
]);
|
|
|
|
$priceId = $request->input('price_id');
|
|
|
|
abort_unless(
|
|
$priceId === $plan->stripe_monthly_price_id || $priceId === $plan->stripe_yearly_price_id,
|
|
422,
|
|
'Invalid price for this plan',
|
|
);
|
|
|
|
$account->createOrGetStripeCustomer([
|
|
'email' => $account->stripeEmail(),
|
|
'name' => $account->stripeName(),
|
|
]);
|
|
|
|
$subscription = $account->newSubscription(Account::SUBSCRIPTION_NAME, $priceId)
|
|
->allowPromotionCodes()
|
|
->trialDays(config('cashier.trial_days'));
|
|
|
|
$checkoutSession = $subscription->checkout([
|
|
'success_url' => route('app.billing.processing'),
|
|
'cancel_url' => route('app.subscribe'),
|
|
]);
|
|
|
|
$account->update(['plan_id' => $plan->id]);
|
|
|
|
return Inertia::location($checkoutSession->url);
|
|
}
|
|
|
|
public function processing(Request $request): Response
|
|
{
|
|
$account = $request->user()->account;
|
|
|
|
return Inertia::render('billing/Processing', [
|
|
'subscriptionActive' => $account && $account->subscribed(Account::SUBSCRIPTION_NAME),
|
|
]);
|
|
}
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
$account = $request->user()->account;
|
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
|
|
|
$subscription = $account->subscription(Account::SUBSCRIPTION_NAME);
|
|
|
|
return Inertia::render('billing/Index', [
|
|
'hasSubscription' => $account->subscribed(Account::SUBSCRIPTION_NAME),
|
|
'onTrial' => $subscription?->onTrial() ?? false,
|
|
'trialEndsAt' => $subscription?->trial_ends_at?->toFormattedDateString(),
|
|
'subscription' => $subscription?->only([
|
|
'stripe_status',
|
|
'ends_at',
|
|
]),
|
|
'plan' => $account->plan,
|
|
'plans' => Plan::active()->orderBy('sort')->get(),
|
|
'invoices' => $account->invoices()->map(fn ($invoice) => [
|
|
'id' => $invoice->id,
|
|
'date' => $invoice->date()->toFormattedDateString(),
|
|
'total' => $invoice->total(),
|
|
'status' => $invoice->status,
|
|
'invoice_pdf' => $invoice->invoice_pdf,
|
|
]),
|
|
'defaultPaymentMethod' => $account->defaultPaymentMethod()?->card?->only([
|
|
'brand',
|
|
'last4',
|
|
'exp_month',
|
|
'exp_year',
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function swap(Request $request, Plan $plan): RedirectResponse
|
|
{
|
|
$account = $request->user()->account;
|
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
|
abort_unless($account->subscribed(Account::SUBSCRIPTION_NAME), 422, 'No active subscription');
|
|
|
|
$request->validate([
|
|
'price_id' => ['required', 'string'],
|
|
]);
|
|
|
|
$priceId = $request->input('price_id');
|
|
|
|
abort_unless(
|
|
$priceId === $plan->stripe_monthly_price_id || $priceId === $plan->stripe_yearly_price_id,
|
|
422,
|
|
'Invalid price for this plan',
|
|
);
|
|
|
|
$account->subscription(Account::SUBSCRIPTION_NAME)->swap($priceId);
|
|
$account->update(['plan_id' => $plan->id]);
|
|
|
|
return redirect()->route('app.billing.index');
|
|
}
|
|
|
|
public function portal(Request $request): RedirectResponse
|
|
{
|
|
$account = $request->user()->account;
|
|
|
|
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
|
|
|
return $account->redirectToBillingPortal(
|
|
route('app.billing.index')
|
|
);
|
|
}
|
|
}
|