Files
zemailnator/app/Livewire/Dashboard/Pricing.php
idevakk 3892c48ef2 feat: upgrade filament to v4 and ensure 100% test coverage
- Upgrade Filament framework from v3 to v4
   - Update all Filament resources and pages for v4 compatibility
   - Fix test suite to maintain 100% pass rate (321 tests passing)
   - Add visibility condition for ticket close action (only when not closed)
   - Update dependencies and build assets for new Filament version
   - Maintain backward compatibility while leveraging v4 improvements
2025-11-14 01:42:07 -08:00

95 lines
3.0 KiB
PHP

<?php
namespace App\Livewire\Dashboard;
use Exception;
use Log;
use App\Models\ActivationKey;
use App\Models\Plan;
use Livewire\Component;
class Pricing extends Component
{
public $plans;
public $activation_key;
public function mount(): void
{
$this->plans = config('app.plans');
}
public function choosePlan($pricing_id): void
{
$this->redirect(route('checkout', $pricing_id));
}
public function activateKey(): void
{
$this->validate([
'activation_key' => 'required|alpha_num|max:30',
], [
'activation_key.required' => 'You must enter an activation key.',
'activation_key.alpha_num' => 'The activation key may only contain letters and numbers (no special characters).',
'activation_key.max' => 'The activation key must not exceed 30 characters.',
]);
$trimmedKey = trim($this->activation_key);
$activation = ActivationKey::where('activation_key', $trimmedKey)
->where('is_activated', false)
->first();
if ($activation) {
if ($activation->price_id !== null) {
$result = $this->addSubscription($activation->price_id);
}
if ($result === true) {
$activation->is_activated = true;
$activation->user_id = auth()->id();
$activation->save();
session()->flash('success', 'Activation key is valid and has been activated. Refresh page to see changes.');
$this->reset('activation_key');
} else {
session()->flash('error', 'Something went wrong. Kindly drop a mail at contact@zemail.me to activate your subscription manually.');
}
} else {
session()->flash('error', 'Invalid or already activated key.');
}
}
private function addSubscription($price_id): bool
{
try {
$plan = Plan::where('pricing_id', $price_id)->firstOrFail();
$user = auth()->user();
$user->createOrGetStripeCustomer();
$user->updateStripeCustomer([
'address' => [
'postal_code' => '10001',
'country' => 'US',
],
'name' => $user->name,
'email' => $user->email,
]);
$user->creditBalance($plan->price * 100, 'Premium Top-up for plan: ' . $plan->name);
$balance = $user->balance();
$user->newSubscription('default', $plan->pricing_id)->create();
if ($plan->monthly_billing == 1) {
$ends_at = now()->addMonth();
} else {
$ends_at = now()->addYear();
}
$user->subscription('default')->cancelAt($ends_at);
return true;
} catch (Exception $e) {
Log::error($e->getMessage());
return false;
}
}
public function render()
{
return view('livewire.dashboard.pricing');
}
}