customer(); if ($customer === null) { return; } $instance = $customer->instances()->latest('id')->first(); $currentPlan = $instance?->plan ?? 'start'; $plans = (array) config('provisioning.plans'); $addons = (array) config('provisioning.addons'); [$plan, $amount, $addonKey] = match ($type) { 'upgrade' => [$key, (int) ($plans[$key]['price_cents'] ?? 0), null], 'storage' => [$currentPlan, (int) config('provisioning.storage_addon.price_cents', 0), null], 'addon' => [$currentPlan, (int) ($addons[$key]['price_cents'] ?? 0), $key], default => [null, 0, null], }; // Guard against invalid keys / non-upgrades. $valid = match ($type) { 'upgrade' => isset($plans[$key]) && ($plans[$key]['price_cents'] ?? 0) > ($plans[$currentPlan]['price_cents'] ?? 0), 'storage' => true, 'addon' => isset($addons[$key]), default => false, }; if (! $valid || $plan === null) { return; } $datacenter = $instance?->host?->datacenter ?? $customer->orders()->latest('id')->value('datacenter') ?? 'fsn'; Order::create([ 'customer_id' => $customer->id, 'plan' => $plan, 'type' => $type, 'addon_key' => $addonKey, 'amount_cents' => $amount, 'currency' => 'EUR', 'datacenter' => $datacenter, 'status' => 'pending', ]); $this->dispatch('notify', message: __('billing.purchased')); } private function customer(): ?Customer { $user = auth()->user(); if (! $user) { return null; } // Prefer the explicit link; fall back to email for legacy unlinked accounts. return Customer::query()->where('user_id', $user->id)->first() ?? Customer::query()->where('email', $user->email)->first(); } public function render() { $customer = $this->customer(); $instance = $customer?->instances()->latest('id')->first(); $plans = (array) config('provisioning.plans'); $currentKey = $instance?->plan ?? 'start'; $currentPrice = (int) ($plans[$currentKey]['price_cents'] ?? 0); $upgrades = collect($plans) ->filter(fn ($p, $k) => (int) ($p['price_cents'] ?? 0) > $currentPrice) ->keys()->all(); return view('livewire.billing', [ 'currentKey' => $currentKey, 'current' => $plans[$currentKey] ?? [], 'instance' => $instance, 'plans' => $plans, 'upgrades' => $upgrades, 'storage' => (array) config('provisioning.storage_addon'), 'addons' => (array) config('provisioning.addons'), 'pending' => $customer ? $customer->orders()->where('status', 'pending')->latest('id')->get() : collect(), ]); } }