New signups land on a 7-day generic trial (Cashier trial_ends_at) without a Stripe customer or subscription. Account is on Starter plan limits during the trial. After 7 days, EnsureAccountReady redirects to /subscribe per the existing flow. - CreateUser sets account.trial_ends_at and plan_id = Starter - EnsureAccountReady allows access when subscribed() OR onGenericTrial() - Account::isOnTrial() includes generic trial check Existing users unaffected: paying users have a subscription; never-paid users continue redirecting to /subscribe.
58 lines
2 KiB
PHP
58 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\User;
|
|
|
|
use App\Enums\Plan\Slug;
|
|
use App\Jobs\PostHog\SyncUser;
|
|
use App\Models\Account;
|
|
use App\Models\Plan;
|
|
use App\Models\User;
|
|
use App\Services\PostHogService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CreateUser
|
|
{
|
|
/**
|
|
* @param array{name: string, email: string, password?: string, google_id?: string, github_id?: string, email_verified_at?: \DateTimeInterface|null, is_invite?: bool, registration_ip?: string|null} $data
|
|
* @param array<string, string> $utmParameters
|
|
*/
|
|
public static function execute(array $data, array $utmParameters = []): User
|
|
{
|
|
$user = DB::transaction(function () use ($data, $utmParameters): User {
|
|
$isInviteRegistration = data_get($data, 'is_invite', false);
|
|
|
|
$starterPlanId = Plan::where('slug', Slug::Starter)->value('id');
|
|
$trialEndsAt = now()->addDays(config('cashier.trial_days', 7));
|
|
|
|
$account = Account::create([
|
|
'name' => data_get($data, 'name')."'s Account",
|
|
'billing_email' => data_get($data, 'email'),
|
|
'plan_id' => $starterPlanId,
|
|
'trial_ends_at' => $trialEndsAt,
|
|
]);
|
|
|
|
$user = User::create(array_merge([
|
|
'name' => data_get($data, 'name'),
|
|
'email' => data_get($data, 'email'),
|
|
'password' => data_get($data, 'password'),
|
|
'google_id' => data_get($data, 'google_id'),
|
|
'github_id' => data_get($data, 'github_id'),
|
|
'email_verified_at' => data_get($data, 'email_verified_at', $isInviteRegistration ? now() : null),
|
|
'account_id' => $account->id,
|
|
'registration_ip' => data_get($data, 'registration_ip'),
|
|
], $utmParameters));
|
|
|
|
$account->update(['owner_id' => $user->id]);
|
|
|
|
return $user;
|
|
});
|
|
|
|
if (PostHogService::isEnabled()) {
|
|
SyncUser::dispatch((string) $user->id);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
}
|