codex has reworked checkout, but frontend doesnt work
This commit is contained in:
144
app/Services/Checkout/CheckoutAssignmentService.php
Normal file
144
app/Services/Checkout/CheckoutAssignmentService.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Checkout;
|
||||
|
||||
use App\Mail\Welcome;
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CheckoutAssignmentService
|
||||
{
|
||||
/**
|
||||
* Persist the purchase artefacts for a completed checkout session.
|
||||
*
|
||||
* @param array{provider_reference?: string, payload?: array} $options
|
||||
*/
|
||||
public function finalise(CheckoutSession $session, array $options = []): void
|
||||
{
|
||||
DB::transaction(function () use ($session, $options) {
|
||||
$tenant = $session->tenant;
|
||||
$user = $session->user;
|
||||
|
||||
if (! $tenant && $user) {
|
||||
$tenant = $this->ensureTenant($user, $session);
|
||||
}
|
||||
|
||||
if (! $tenant) {
|
||||
Log::warning('Checkout assignment skipped: missing tenant', ['session' => $session->id]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$package = $session->package;
|
||||
if (! $package) {
|
||||
Log::warning('Checkout assignment skipped: missing package', ['session' => $session->id]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$providerReference = $options['provider_reference']
|
||||
?? $session->stripe_payment_intent_id
|
||||
?? $session->paypal_order_id
|
||||
?? 'free';
|
||||
|
||||
$purchase = PackagePurchase::updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => $providerReference,
|
||||
],
|
||||
[
|
||||
'price' => $session->amount_total,
|
||||
'type' => $package->type === 'reseller' ? 'reseller_subscription' : 'endcustomer_event',
|
||||
'purchased_at' => now(),
|
||||
'metadata' => $options['payload'] ?? null,
|
||||
]
|
||||
);
|
||||
|
||||
TenantPackage::updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
],
|
||||
[
|
||||
'price' => $session->amount_total,
|
||||
'active' => true,
|
||||
'purchased_at' => now(),
|
||||
'expires_at' => $this->resolveExpiry($package, $tenant),
|
||||
]
|
||||
);
|
||||
|
||||
if ($user && $user->pending_purchase) {
|
||||
$this->activateUser($user);
|
||||
}
|
||||
|
||||
if ($user) {
|
||||
Mail::to($user)->queue(new Welcome($user));
|
||||
}
|
||||
|
||||
Log::info('Checkout session assigned', [
|
||||
'session' => $session->id,
|
||||
'tenant' => $tenant->id,
|
||||
'package' => $package->id,
|
||||
'purchase' => $purchase->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
protected function ensureTenant(User $user, CheckoutSession $session): ?Tenant
|
||||
{
|
||||
if ($user->tenant) {
|
||||
return $user->tenant;
|
||||
}
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'user_id' => $user->id,
|
||||
'name' => $session->package_snapshot['name'] ?? $user->name,
|
||||
'slug' => Str::slug(($user->name ?: $user->email).' '.now()->timestamp),
|
||||
'email' => $user->email,
|
||||
'is_active' => true,
|
||||
'is_suspended' => false,
|
||||
'event_credits_balance' => 0,
|
||||
'subscription_tier' => 'free',
|
||||
'subscription_status' => 'active',
|
||||
'settings' => [
|
||||
'contact_email' => $user->email,
|
||||
],
|
||||
]);
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
protected function resolveExpiry(Package $package, Tenant $tenant)
|
||||
{
|
||||
if ($package->type === 'reseller') {
|
||||
$hasActive = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('active', true)
|
||||
->exists();
|
||||
|
||||
return $hasActive ? now()->addYear() : now()->addDays(14);
|
||||
}
|
||||
|
||||
return now()->addYear();
|
||||
}
|
||||
|
||||
protected function activateUser(User $user): void
|
||||
{
|
||||
$user->forceFill([
|
||||
'email_verified_at' => $user->email_verified_at ?? now(),
|
||||
'role' => $user->role === 'user' ? 'tenant_admin' : $user->role,
|
||||
'pending_purchase' => false,
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user