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();
|
||||
}
|
||||
}
|
||||
88
app/Services/Checkout/CheckoutPaymentService.php
Normal file
88
app/Services/Checkout/CheckoutPaymentService.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Checkout;
|
||||
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Tenant;
|
||||
use LogicException;
|
||||
|
||||
class CheckoutPaymentService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CheckoutSessionService $sessions,
|
||||
private readonly CheckoutAssignmentService $assignment,
|
||||
) {
|
||||
}
|
||||
|
||||
public function initialiseStripe(CheckoutSession $session, array $payload = []): array
|
||||
{
|
||||
if ($session->provider !== CheckoutSession::PROVIDER_STRIPE) {
|
||||
$this->sessions->selectProvider($session, CheckoutSession::PROVIDER_STRIPE);
|
||||
}
|
||||
|
||||
// TODO: integrate Stripe PaymentIntent creation and return client_secret + publishable key
|
||||
return [
|
||||
'session_id' => $session->id,
|
||||
'status' => $session->status,
|
||||
'message' => 'Stripe integration pending implementation.',
|
||||
];
|
||||
}
|
||||
|
||||
public function confirmStripe(CheckoutSession $session, array $payload = []): CheckoutSession
|
||||
{
|
||||
if ($session->provider !== CheckoutSession::PROVIDER_STRIPE) {
|
||||
throw new LogicException('Cannot confirm Stripe payment on a non-Stripe session.');
|
||||
}
|
||||
|
||||
// TODO: verify PaymentIntent status with Stripe SDK and update session metadata
|
||||
$this->sessions->markProcessing($session);
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function initialisePayPal(CheckoutSession $session, array $payload = []): array
|
||||
{
|
||||
if ($session->provider !== CheckoutSession::PROVIDER_PAYPAL) {
|
||||
$this->sessions->selectProvider($session, CheckoutSession::PROVIDER_PAYPAL);
|
||||
}
|
||||
|
||||
// TODO: integrate PayPal Orders API and return order id + approval link
|
||||
return [
|
||||
'session_id' => $session->id,
|
||||
'status' => $session->status,
|
||||
'message' => 'PayPal integration pending implementation.',
|
||||
];
|
||||
}
|
||||
|
||||
public function capturePayPal(CheckoutSession $session, array $payload = []): CheckoutSession
|
||||
{
|
||||
if ($session->provider !== CheckoutSession::PROVIDER_PAYPAL) {
|
||||
throw new LogicException('Cannot capture PayPal payment on a non-PayPal session.');
|
||||
}
|
||||
|
||||
// TODO: call PayPal capture endpoint and persist order/subscription identifiers
|
||||
$this->sessions->markProcessing($session);
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function finaliseFree(CheckoutSession $session): CheckoutSession
|
||||
{
|
||||
if ($session->provider !== CheckoutSession::PROVIDER_FREE) {
|
||||
$this->sessions->selectProvider($session, CheckoutSession::PROVIDER_FREE);
|
||||
}
|
||||
|
||||
$this->sessions->markProcessing($session);
|
||||
$this->assignment->finalise($session, ['source' => 'free']);
|
||||
|
||||
return $this->sessions->markCompleted($session);
|
||||
}
|
||||
|
||||
public function attachTenantAndResume(CheckoutSession $session, Tenant $tenant): CheckoutSession
|
||||
{
|
||||
$this->sessions->attachTenant($session, $tenant);
|
||||
$this->sessions->refreshExpiration($session);
|
||||
|
||||
return $session;
|
||||
}
|
||||
}
|
||||
216
app/Services/Checkout/CheckoutSessionService.php
Normal file
216
app/Services/Checkout/CheckoutSessionService.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Checkout;
|
||||
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use RuntimeException;
|
||||
|
||||
class CheckoutSessionService
|
||||
{
|
||||
private int $sessionTtlMinutes;
|
||||
|
||||
private int $historyRetention;
|
||||
|
||||
public function __construct(?int $sessionTtlMinutes = null, ?int $historyRetention = null)
|
||||
{
|
||||
$this->sessionTtlMinutes = $sessionTtlMinutes ?? (int) config('checkout.session_ttl_minutes', 30);
|
||||
$this->historyRetention = $historyRetention ?? (int) config('checkout.status_history_max', 25);
|
||||
}
|
||||
|
||||
public function createOrResume(?User $user, Package $package, array $context = []): CheckoutSession
|
||||
{
|
||||
return DB::transaction(function () use ($user, $package, $context) {
|
||||
$existing = $this->findActiveSession($user, $package);
|
||||
|
||||
if ($existing) {
|
||||
$this->refreshExpiration($existing);
|
||||
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$session = new CheckoutSession();
|
||||
$session->id = (string) Str::uuid();
|
||||
$session->status = CheckoutSession::STATUS_DRAFT;
|
||||
$session->provider = CheckoutSession::PROVIDER_NONE;
|
||||
$session->user()->associate($user);
|
||||
$session->tenant()->associate($context['tenant'] ?? null);
|
||||
$session->package()->associate($package);
|
||||
$session->package_snapshot = $this->packageSnapshot($package);
|
||||
$session->currency = Arr::get($session->package_snapshot, 'currency', 'EUR');
|
||||
$session->amount_subtotal = Arr::get($session->package_snapshot, 'price', 0);
|
||||
$session->amount_total = Arr::get($session->package_snapshot, 'price', 0);
|
||||
$session->locale = $context['locale'] ?? app()->getLocale();
|
||||
$session->expires_at = now()->addMinutes($this->sessionTtlMinutes);
|
||||
$session->status_history = [];
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_DRAFT, 'session_created');
|
||||
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
});
|
||||
}
|
||||
|
||||
public function updatePackage(CheckoutSession $session, Package $package): CheckoutSession
|
||||
{
|
||||
return DB::transaction(function () use ($session, $package) {
|
||||
$session->package()->associate($package);
|
||||
$session->package_snapshot = $this->packageSnapshot($package);
|
||||
$session->amount_subtotal = Arr::get($session->package_snapshot, 'price', 0);
|
||||
$session->amount_total = Arr::get($session->package_snapshot, 'price', 0);
|
||||
$session->provider = CheckoutSession::PROVIDER_NONE;
|
||||
$session->status = CheckoutSession::STATUS_DRAFT;
|
||||
$session->stripe_payment_intent_id = null;
|
||||
$session->stripe_customer_id = null;
|
||||
$session->stripe_subscription_id = null;
|
||||
$session->paypal_order_id = null;
|
||||
$session->paypal_subscription_id = null;
|
||||
$session->provider_metadata = [];
|
||||
$session->failure_reason = null;
|
||||
$session->expires_at = now()->addMinutes($this->sessionTtlMinutes);
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_DRAFT, 'package_switched');
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
});
|
||||
}
|
||||
|
||||
public function selectProvider(CheckoutSession $session, string $provider): CheckoutSession
|
||||
{
|
||||
$provider = strtolower($provider);
|
||||
|
||||
if (! in_array($provider, [CheckoutSession::PROVIDER_STRIPE, CheckoutSession::PROVIDER_PAYPAL, CheckoutSession::PROVIDER_FREE], true)) {
|
||||
throw new RuntimeException("Unsupported checkout provider [{$provider}]");
|
||||
}
|
||||
|
||||
$session->provider = $provider;
|
||||
$session->status = $provider === CheckoutSession::PROVIDER_FREE
|
||||
? CheckoutSession::STATUS_PROCESSING
|
||||
: CheckoutSession::STATUS_AWAITING_METHOD;
|
||||
$session->failure_reason = null;
|
||||
$session->expires_at = now()->addMinutes($this->sessionTtlMinutes);
|
||||
$this->appendStatus($session, $session->status, 'provider_selected');
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function markRequiresCustomerAction(CheckoutSession $session, string $reason = null): CheckoutSession
|
||||
{
|
||||
$session->status = CheckoutSession::STATUS_REQUIRES_CUSTOMER_ACTION;
|
||||
$session->failure_reason = $reason;
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_REQUIRES_CUSTOMER_ACTION, $reason ?? 'requires_action');
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function markProcessing(CheckoutSession $session, array $metadata = []): CheckoutSession
|
||||
{
|
||||
$session->status = CheckoutSession::STATUS_PROCESSING;
|
||||
if (! empty($metadata)) {
|
||||
$session->provider_metadata = array_merge($session->provider_metadata ?? [], $metadata);
|
||||
}
|
||||
$session->failure_reason = null;
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_PROCESSING, 'processing');
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function markCompleted(CheckoutSession $session, ?CarbonInterface $completedAt = null): CheckoutSession
|
||||
{
|
||||
$session->status = CheckoutSession::STATUS_COMPLETED;
|
||||
$session->completed_at = $completedAt ?? now();
|
||||
$session->failure_reason = null;
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_COMPLETED, 'completed');
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function markFailed(CheckoutSession $session, string $reason): CheckoutSession
|
||||
{
|
||||
$session->status = CheckoutSession::STATUS_FAILED;
|
||||
$session->failure_reason = $reason;
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_FAILED, $reason);
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function cancel(CheckoutSession $session, string $reason = 'cancelled'): CheckoutSession
|
||||
{
|
||||
$session->status = CheckoutSession::STATUS_CANCELLED;
|
||||
$session->failure_reason = $reason;
|
||||
$this->appendStatus($session, CheckoutSession::STATUS_CANCELLED, $reason);
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function refreshExpiration(CheckoutSession $session): CheckoutSession
|
||||
{
|
||||
$session->expires_at = now()->addMinutes($this->sessionTtlMinutes);
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function attachTenant(CheckoutSession $session, Tenant $tenant): CheckoutSession
|
||||
{
|
||||
$session->tenant()->associate($tenant);
|
||||
$session->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
protected function appendStatus(CheckoutSession $session, string $status, ?string $reason = null): void
|
||||
{
|
||||
$history = $session->status_history ?? [];
|
||||
$history[] = [
|
||||
'status' => $status,
|
||||
'reason' => $reason,
|
||||
'at' => now()->toIso8601String(),
|
||||
];
|
||||
|
||||
if (count($history) > $this->historyRetention) {
|
||||
$history = array_slice($history, -1 * $this->historyRetention);
|
||||
}
|
||||
|
||||
$session->status_history = $history;
|
||||
}
|
||||
|
||||
protected function packageSnapshot(Package $package): array
|
||||
{
|
||||
return [
|
||||
'id' => $package->getKey(),
|
||||
'name' => $package->name,
|
||||
'type' => $package->type,
|
||||
'price' => (float) $package->price,
|
||||
'currency' => $package->currency ?? 'EUR',
|
||||
'features' => $package->features,
|
||||
'limits' => $package->limits,
|
||||
];
|
||||
}
|
||||
|
||||
protected function findActiveSession(?User $user, Package $package): ?CheckoutSession
|
||||
{
|
||||
if (! $user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return CheckoutSession::query()
|
||||
->where('user_id', $user->getKey())
|
||||
->where('package_id', $package->getKey())
|
||||
->active()
|
||||
->orderByDesc('created_at')
|
||||
->first();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user