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 12:46:18 +00:00
|
|
|
if (config('trypost.self_hosted')) {
|
|
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
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-15 12:46:18 +00:00
|
|
|
public function checkout(Request $request, Plan $plan): SymfonyResponse|RedirectResponse
|
2026-03-29 22:24:28 +00:00
|
|
|
{
|
2026-04-15 12:46:18 +00:00
|
|
|
if (config('trypost.self_hosted')) {
|
|
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
$user = $request->user();
|
|
|
|
|
$account = $user->account;
|
2026-03-31 03:40:18 +00:00
|
|
|
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
abort_unless($user->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'price_id' => ['required', 'string'],
|
|
|
|
|
]);
|
2026-03-29 22:24:28 +00:00
|
|
|
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
$priceId = $request->input('price_id');
|
2026-03-29 22:24:28 +00:00
|
|
|
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
abort_unless(
|
|
|
|
|
$priceId === $plan->stripe_monthly_price_id || $priceId === $plan->stripe_yearly_price_id,
|
|
|
|
|
422,
|
|
|
|
|
'Invalid price for this plan',
|
|
|
|
|
);
|
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([
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
'success_url' => route('app.billing.processing'),
|
|
|
|
|
'cancel_url' => route('app.subscribe'),
|
2026-03-29 22:24:28 +00:00
|
|
|
]);
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 12:46:18 +00:00
|
|
|
public function processing(Request $request): Response|RedirectResponse
|
2026-03-29 22:24:28 +00:00
|
|
|
{
|
2026-04-15 12:46:18 +00:00
|
|
|
if (config('trypost.self_hosted')) {
|
|
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
$account = $request->user()->account;
|
2026-03-29 22:24:28 +00:00
|
|
|
|
|
|
|
|
return Inertia::render('billing/Processing', [
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
'subscriptionActive' => $account && $account->subscribed(Account::SUBSCRIPTION_NAME),
|
2026-03-29 22:24:28 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 12:46:18 +00:00
|
|
|
public function index(Request $request): Response|RedirectResponse
|
2026-03-29 22:24:28 +00:00
|
|
|
{
|
2026-04-15 12:46:18 +00:00
|
|
|
if (config('trypost.self_hosted')) {
|
|
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2026-05-03 16:44:13 +00:00
|
|
|
return Inertia::render('settings/account/Billing', [
|
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 12:46:18 +00:00
|
|
|
if (config('trypost.self_hosted')) {
|
|
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
$request->validate([
|
|
|
|
|
'price_id' => ['required', 'string'],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$priceId = $request->input('price_id');
|
2026-05-03 19:52:28 +00:00
|
|
|
$subscription = $account->subscription(Account::SUBSCRIPTION_NAME);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
feat: redesign billing, onboarding, sidebar, and settings architecture
- 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
2026-04-15 03:33:38 +00:00
|
|
|
abort_unless(
|
|
|
|
|
$priceId === $plan->stripe_monthly_price_id || $priceId === $plan->stripe_yearly_price_id,
|
|
|
|
|
422,
|
|
|
|
|
'Invalid price for this plan',
|
|
|
|
|
);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
2026-05-03 19:52:28 +00:00
|
|
|
$currentPlan = $account->plan;
|
|
|
|
|
$isOnYearly = $currentPlan && $subscription->stripe_price === $currentPlan->stripe_yearly_price_id;
|
|
|
|
|
$isTargetMonthly = $priceId === $plan->stripe_monthly_price_id;
|
|
|
|
|
|
|
|
|
|
abort_if(
|
|
|
|
|
$isOnYearly && $isTargetMonthly,
|
|
|
|
|
422,
|
|
|
|
|
'Cannot downgrade from yearly to monthly billing.',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$subscription->swap($priceId);
|
2026-04-15 01:22:04 +00:00
|
|
|
$account->update(['plan_id' => $plan->id]);
|
2026-04-14 21:22:36 +00:00
|
|
|
|
2026-05-03 19:52:28 +00:00
|
|
|
return redirect()->route('app.billing.index')
|
|
|
|
|
->with('flash.success', __('billing.flash.plan_changed', ['plan' => $plan->name]));
|
2026-04-14 21:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function portal(Request $request): RedirectResponse
|
|
|
|
|
{
|
2026-04-15 12:46:18 +00:00
|
|
|
if (config('trypost.self_hosted')) {
|
|
|
|
|
return redirect()->route('app.calendar');
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|