trypost/app/Http/Controllers/App/BillingController.php
Paulo Castellano b4f61be6ef
Welcome: pre-subscription funnel and member subscription-required screen (#243)
* Rename pre-subscription onboarding funnel to Welcome.

Move the ICP steps to /welcome, drop the social-connect checkout gate, hold unpaid members on a subscription-required screen, and keep legacy /onboarding URLs working until the post-subscription checklist lands.

Closes #237

Co-authored-by: Cursor <cursoragent@cursor.com>

* Drop legacy /onboarding ICP URL aliases.

Unfinished users re-enter Welcome via EnsureAccountReady on next login; /onboarding stays free for the post-subscription checklist.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Simplify Welcome PostHog event names.

Use welcome.persona/goals/referral and drop the unused checkout case — begin checkout stays on the frontend as checkout.started / begin_checkout.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Slim welcome goal options to match the #204 set.

Drop team_collaboration, automate_api, and track_performance so the goals step stays at nine choices.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Split Welcome AI goals into TryPost AI and MCP assistants.

Rewrite ai_content for in-app generation and add use_mcp so Claude/ChatGPT/Cursor intent is captured separately across locales.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Harden Welcome goals gate and drop dead checkout UI.

Treat removed goal values as incomplete so mid-funnel users re-select, remove the unused canCheckout branch, and fix the pt-BR welcome progress label.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Add password visibility toggle to the login form.

Match the register eye control so users can reveal their password while signing in.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Point the sidebar community link to Discord.

Replace the X stay-updated entry with Join Discord and the trypost.it/discord invite.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Rename the sidebar Discord link to Discord community.

Softer label that matches the other support nav items.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix broken Turkish Discord community translation.

An unescaped apostrophe left a parse error in lang/tr/sidebar.php.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 11:34:50 -03:00

166 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\App;
use App\Models\Account;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Gate;
use Inertia\Inertia;
use Inertia\Response;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Throwable;
class BillingController extends Controller
{
public function subscribe(): RedirectResponse
{
return redirect()->route('app.welcome.persona');
}
public function processing(Request $request): Response|RedirectResponse
{
if (config('trypost.self_hosted')) {
return redirect()->route('app.calendar');
}
$account = $request->user()->account;
$sessionId = $request->query('session_id');
// Consume the checkout session once: `fromCheckout` is true only the first
// time this session_id is seen, so a back-button/refresh to the success URL
// can't re-fire `checkout.completed`. `Cache::add` is atomic — it returns
// true only when the key didn't exist yet.
$fromCheckout = is_string($sessionId) && $sessionId !== ''
&& Cache::add("checkout_tracked:{$sessionId}", true, now()->addDay());
return Inertia::render('billing/Processing', [
'subscriptionActive' => $account && $account->subscribed(Account::SUBSCRIPTION_NAME),
'fromCheckout' => $fromCheckout,
'persona' => $request->user()->persona?->value,
'conversion' => $fromCheckout && $account?->stripe_id
? fn () => $this->buildConversionData($account, $sessionId)
: null,
]);
}
/**
* @return array{value: float, currency: string, transaction_id: string}|null
*/
private function buildConversionData(Account $account, string $sessionId): ?array
{
try {
$session = $account->stripe()->checkout->sessions->retrieve(
$sessionId,
['expand' => ['line_items.data.price']],
);
} catch (Throwable) {
return null;
}
if (data_get($session, 'customer') !== $account->stripe_id) {
return null;
}
$unitAmount = data_get($session, 'line_items.data.0.price.unit_amount');
$currency = data_get($session, 'line_items.data.0.price.currency');
$transactionId = data_get($session, 'id');
if (! is_int($unitAmount) || ! is_string($currency) || ! is_string($transactionId)) {
return null;
}
return [
'value' => $unitAmount / 100,
'currency' => strtoupper($currency),
'transaction_id' => $transactionId,
];
}
public function index(Request $request): Response|RedirectResponse
{
if (config('trypost.self_hosted')) {
return redirect()->route('app.calendar');
}
$account = $request->user()->account;
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
$subscription = $account->subscription(Account::SUBSCRIPTION_NAME);
return Inertia::render('settings/account/Billing', [
'hasSubscription' => $account->subscribed(Account::SUBSCRIPTION_NAME),
'onTrial' => $account->isOnTrial(),
'trialEndsAt' => $account->activeTrialEndsAt(),
'subscription' => $subscription?->only([
'stripe_status',
'ends_at',
]),
'plan' => $account->plan,
'workspaceCount' => $account->workspaces()->count(),
'invoices' => $account->invoices()->map(fn ($invoice) => [
'id' => $invoice->id,
'date' => $invoice->date(),
'total' => $invoice->total(),
'status' => $invoice->status,
'invoice_pdf' => $invoice->invoice_pdf,
]),
'defaultPaymentMethod' => $account->displayablePaymentMethod(),
]);
}
public function swapToYearly(Request $request): RedirectResponse
{
if (config('trypost.self_hosted')) {
return redirect()->route('app.calendar');
}
$account = $request->user()->account;
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
abort_unless($account->subscribed(Account::SUBSCRIPTION_NAME), SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY, 'No active subscription');
$plan = $account->plan;
$yearlyPriceId = $plan?->stripe_yearly_price_id;
abort_if($yearlyPriceId === null, SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY, 'No annual price configured');
$subscription = $account->subscription(Account::SUBSCRIPTION_NAME);
abort_if($subscription === null, SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY, 'No active subscription');
if ($subscription->stripe_price === $yearlyPriceId) {
return redirect()->route('app.billing.index');
}
$authorization = Gate::inspect('swapPlan', [$account]);
if ($authorization->denied()) {
return back()->with('flash.error', $authorization->message());
}
$subscription->swap($yearlyPriceId);
return redirect()->route('app.billing.index')
->with('flash.success', __('billing.flash.switched_to_yearly'));
}
public function portal(Request $request): RedirectResponse
{
if (config('trypost.self_hosted')) {
return redirect()->route('app.calendar');
}
$account = $request->user()->account;
abort_unless($request->user()->isAccountOwner(), SymfonyResponse::HTTP_FORBIDDEN);
return $account->redirectToBillingPortal(
route('app.billing.index')
);
}
}