feat: enhance pricing page with feature limits and trial management
- Add comprehensive feature limits enforcement middleware - Implement subscription dashboard with usage analytics - Create reusable plan card component with feature badges - Add trial configuration support with limit overrides - Fix payment controller null safety issues - Improve pricing page UI with proper feature display
This commit is contained in:
@@ -2,28 +2,91 @@
|
||||
|
||||
namespace App\Livewire\Dashboard;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use App\Models\ActivationKey;
|
||||
use App\Models\Plan;
|
||||
use App\Models\PlanTier;
|
||||
use App\Services\Payments\PaymentOrchestrator;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Livewire\Component;
|
||||
|
||||
class Pricing extends Component
|
||||
{
|
||||
public $plans;
|
||||
|
||||
public $planTiers;
|
||||
|
||||
public $activation_key;
|
||||
|
||||
public $selectedProvider = 'stripe';
|
||||
|
||||
public $selectedBillingCycle = null;
|
||||
|
||||
public $selectedTier = null;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->plans = config('app.plans');
|
||||
$this->loadPlans();
|
||||
$this->planTiers = PlanTier::with('plans')->orderBy('sort_order')->get();
|
||||
}
|
||||
|
||||
public function choosePlan($pricing_id): void
|
||||
private function loadPlans(): void
|
||||
{
|
||||
$this->redirect(route('checkout', $pricing_id));
|
||||
$query = Plan::active()
|
||||
->ordered()
|
||||
->with(['planFeatureLimits.planFeature', 'planProviders', 'trialConfiguration', 'planTier']);
|
||||
|
||||
if ($this->selectedTier) {
|
||||
$query->where('plan_tier_id', $this->selectedTier);
|
||||
}
|
||||
|
||||
$this->plans = $query->get();
|
||||
}
|
||||
|
||||
public function filterByTier($tierId = null): void
|
||||
{
|
||||
$this->selectedTier = $tierId;
|
||||
$this->loadPlans();
|
||||
}
|
||||
|
||||
public function choosePlan($planId, $provider = 'stripe'): void
|
||||
{
|
||||
$plan = Plan::findOrFail($planId);
|
||||
|
||||
if (! $plan?->supportsProvider($provider)) {
|
||||
session()->flash('error', "This plan doesn't support {$provider} payments.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirect(route('checkout.enhanced', [
|
||||
'plan' => $planId,
|
||||
'provider' => $provider,
|
||||
]));
|
||||
}
|
||||
|
||||
public function startTrial($planId, $provider = 'stripe'): void
|
||||
{
|
||||
$plan = Plan::findOrFail($planId);
|
||||
|
||||
if (! $plan?->hasTrial()) {
|
||||
session()->flash('error', "This plan doesn't offer trials.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $plan?->supportsProvider($provider)) {
|
||||
session()->flash('error', "This plan doesn't support {$provider} payments for trials.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirect(route('checkout.trial', [
|
||||
'plan' => $planId,
|
||||
'provider' => $provider,
|
||||
]));
|
||||
}
|
||||
|
||||
public function activateKey(): void
|
||||
@@ -42,53 +105,125 @@ class Pricing extends Component
|
||||
->first();
|
||||
|
||||
if ($activation) {
|
||||
if ($activation->price_id !== null) {
|
||||
$result = $this->addSubscription($activation->price_id);
|
||||
}
|
||||
if ($result) {
|
||||
$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.');
|
||||
try {
|
||||
$result = $this->activateSubscriptionKey($activation);
|
||||
if ($result) {
|
||||
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.');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Activation key error: '.$e->getMessage());
|
||||
session()->flash('error', 'An error occurred while activating your key. Please contact support.');
|
||||
}
|
||||
} else {
|
||||
session()->flash('error', 'Invalid or already activated key.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function addSubscription($price_id): bool
|
||||
private function activateSubscriptionKey(ActivationKey $activation): bool
|
||||
{
|
||||
try {
|
||||
$plan = Plan::query()->where('pricing_id', $price_id)->firstOrFail();
|
||||
// Use PaymentOrchestrator for activation key processing
|
||||
$orchestrator = app(PaymentOrchestrator::class);
|
||||
|
||||
// Find the plan associated with this activation key
|
||||
$plan = null;
|
||||
if ($activation->plan_id) {
|
||||
$plan = Plan::find($activation->plan_id);
|
||||
} elseif ($activation->price_id) {
|
||||
// Fallback to legacy pricing_id lookup
|
||||
$plan = Plan::where('pricing_id', $activation->price_id)->first();
|
||||
}
|
||||
|
||||
if (! $plan) {
|
||||
Log::error('No plan found for activation key: '.$activation->id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create subscription using orchestrator
|
||||
$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();
|
||||
$subscription = $orchestrator->createSubscriptionFromActivationKey($user, $activation, $plan);
|
||||
|
||||
$ends_at = $plan->monthly_billing == 1 ? now()->addMonth() : now()->addYear();
|
||||
$user->subscription('default')->cancelAt($ends_at);
|
||||
if ($subscription) {
|
||||
$activation->is_activated = true;
|
||||
$activation->user_id = $user->id;
|
||||
$activation->save();
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error('Activation key processing failed: '.$e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available providers for a plan
|
||||
*/
|
||||
public function getPlanProviders($planId): array
|
||||
{
|
||||
$plan = $this->plans->firstWhere('id', $planId);
|
||||
|
||||
return $plan ? $plan->getAllowedProviders() : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plan features with limits
|
||||
*/
|
||||
public function getPlanFeatures($planId): array
|
||||
{
|
||||
$plan = $this->plans->firstWhere('id', $planId);
|
||||
|
||||
if (! $plan) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $plan->getFeaturesWithLimits();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plan has trial available
|
||||
*/
|
||||
public function planHasTrial($planId): bool
|
||||
{
|
||||
$plan = $this->plans->firstWhere('id', $planId);
|
||||
|
||||
return $plan ? $plan->hasTrial() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get trial configuration for plan
|
||||
*/
|
||||
public function getTrialConfig($planId): ?array
|
||||
{
|
||||
$plan = $this->plans->firstWhere('id', $planId);
|
||||
if (! $plan || ! $plan->hasTrial()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$config = $plan->getTrialConfig();
|
||||
|
||||
return [
|
||||
'duration_days' => $config->trial_duration_days,
|
||||
'requires_payment_method' => $config->trial_requires_payment_method,
|
||||
'auto_converts' => $config->trial_auto_converts,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get billing cycle display text
|
||||
*/
|
||||
public function getBillingCycleDisplay($plan): string
|
||||
{
|
||||
return $plan->getBillingCycleDisplay();
|
||||
}
|
||||
|
||||
public function render(): Factory|View
|
||||
{
|
||||
return view('livewire.dashboard.pricing');
|
||||
|
||||
237
app/Livewire/Dashboard/SubscriptionDashboard.php
Normal file
237
app/Livewire/Dashboard/SubscriptionDashboard.php
Normal file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Dashboard;
|
||||
|
||||
use App\Models\PlanUsage;
|
||||
use App\Models\TrialExtension;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
|
||||
class SubscriptionDashboard extends Component
|
||||
{
|
||||
public $subscription;
|
||||
|
||||
public $plan;
|
||||
|
||||
public $usageData;
|
||||
|
||||
public $upgradePaths;
|
||||
|
||||
public $trialExtensions;
|
||||
|
||||
public $loading = true;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->loadSubscriptionData();
|
||||
}
|
||||
|
||||
public function loadSubscriptionData(): void
|
||||
{
|
||||
$this->loading = true;
|
||||
|
||||
$user = Auth::user();
|
||||
$this->subscription = $user->subscription('default');
|
||||
|
||||
if ($this->subscription) {
|
||||
$this->plan = $this->subscription->plan->load([
|
||||
'planFeatureLimits.planFeature',
|
||||
'trialConfiguration',
|
||||
'planTier',
|
||||
]);
|
||||
|
||||
$this->loadUsageData();
|
||||
$this->loadUpgradePaths();
|
||||
$this->loadTrialExtensions();
|
||||
}
|
||||
|
||||
$this->loading = false;
|
||||
}
|
||||
|
||||
private function loadUsageData(): void
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->usageData = PlanUsage::where('user_id', Auth::id())
|
||||
->where('plan_id', $this->subscription->plan_id)
|
||||
->with('planFeature')
|
||||
->get()
|
||||
->map(function ($usage) {
|
||||
$limit = $this->plan->planFeatureLimits
|
||||
->where('plan_feature_id', $usage->plan_feature_id)
|
||||
->first();
|
||||
|
||||
return [
|
||||
'feature' => $usage->planFeature,
|
||||
'usage' => $usage->usage_value,
|
||||
'period' => $usage->period_type,
|
||||
'limit' => $limit,
|
||||
'remaining' => $limit ? $limit->getRemainingUsage($usage->usage_value, $this->subscription->onTrial()) : 0,
|
||||
'percentage_used' => $limit && $limit->limit_value ?
|
||||
min(100, ($usage->usage_value / $limit->limit_value) * 100) : 0,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private function loadUpgradePaths(): void
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->upgradePaths = $this->plan->getUpgradePath();
|
||||
}
|
||||
|
||||
private function loadTrialExtensions(): void
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->trialExtensions = TrialExtension::where('subscription_id', $this->subscription->id)
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function requestTrialExtension(): void
|
||||
{
|
||||
if (! $this->subscription || ! $this->subscription->onTrial()) {
|
||||
session()->flash('error', 'You can only request extensions for active trials.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$trialConfig = $this->plan->getTrialConfig();
|
||||
if (! $trialConfig) {
|
||||
session()->flash('error', 'This plan does not support trial extensions.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$currentExtensions = $this->trialExtensions->count();
|
||||
if (! $trialConfig->canExtendTrial($currentExtensions)) {
|
||||
session()->flash('error', 'You have reached the maximum number of trial extensions.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Create trial extension request
|
||||
TrialExtension::create([
|
||||
'subscription_id' => $this->subscription->id,
|
||||
'user_id' => Auth::id(),
|
||||
'original_ends_at' => $this->subscription->trial_ends_at,
|
||||
'extension_days' => $trialConfig->trial_duration_days,
|
||||
'status' => 'pending',
|
||||
'reason' => 'User requested extension via dashboard',
|
||||
]);
|
||||
|
||||
session()->flash('success', 'Trial extension request submitted successfully.');
|
||||
$this->loadTrialExtensions();
|
||||
}
|
||||
|
||||
public function cancelSubscription(): void
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->subscription->cancel();
|
||||
session()->flash('success', 'Subscription cancelled successfully.');
|
||||
$this->loadSubscriptionData();
|
||||
}
|
||||
|
||||
public function pauseSubscription(): void
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Implement pause logic based on your business requirements
|
||||
session()->flash('info', 'Pause functionality coming soon.');
|
||||
}
|
||||
|
||||
public function resumeSubscription(): void
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->subscription->resume();
|
||||
session()->flash('success', 'Subscription resumed successfully.');
|
||||
$this->loadSubscriptionData();
|
||||
}
|
||||
|
||||
public function getSubscriptionStatus(): string
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return 'No Subscription';
|
||||
}
|
||||
|
||||
return match ($this->subscription->status) {
|
||||
'active' => 'Active',
|
||||
'trialing' => 'Trial',
|
||||
'cancelled' => 'Cancelled',
|
||||
'past_due' => 'Past Due',
|
||||
'unpaid' => 'Unpaid',
|
||||
default => 'Unknown',
|
||||
};
|
||||
}
|
||||
|
||||
public function getSubscriptionStatusColor(): string
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return 'gray';
|
||||
}
|
||||
|
||||
return match ($this->subscription->status) {
|
||||
'active' => 'green',
|
||||
'trialing' => 'blue',
|
||||
'cancelled' => 'red',
|
||||
'past_due' => 'yellow',
|
||||
'unpaid' => 'red',
|
||||
default => 'gray',
|
||||
};
|
||||
}
|
||||
|
||||
public function getDaysRemaining(): int
|
||||
{
|
||||
if (! $this->subscription) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$endsAt = $this->subscription->trial_ends_at ?? $this->subscription->ends_at;
|
||||
|
||||
return $endsAt ? max(0, $endsAt->diffInDays(now())) : 0;
|
||||
}
|
||||
|
||||
public function getNextBillingDate(): string
|
||||
{
|
||||
if (! $this->subscription || $this->subscription->cancelled()) {
|
||||
return 'N/A';
|
||||
}
|
||||
|
||||
return $this->subscription->ends_at?->format('M j, Y') ?? 'N/A';
|
||||
}
|
||||
|
||||
public function getUsagePercentage($usageData): int
|
||||
{
|
||||
return (int) round($usageData['percentage_used']);
|
||||
}
|
||||
|
||||
public function getUsageColor($percentage): string
|
||||
{
|
||||
return match (true) {
|
||||
$percentage >= 90 => 'red',
|
||||
$percentage >= 75 => 'yellow',
|
||||
$percentage >= 50 => 'blue',
|
||||
default => 'green',
|
||||
};
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.dashboard.subscription-dashboard');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user