1+ <?php
2+
3+ namespace App \Filament \App \Pages ;
4+
5+ use App \Services \SubscriptionService ;
6+ use Filament \Actions \Action ;
7+ use Filament \Notifications \Notification ;
8+ use Filament \Pages \Page ;
9+ use Illuminate \Support \Facades \Auth ;
10+
11+ class PremiumDashboardPage extends Page
12+ {
13+ protected static ?string $ navigationIcon = 'heroicon-o-star ' ;
14+
15+ protected static ?string $ navigationLabel = 'Premium Dashboard ' ;
16+
17+ protected static ?string $ navigationGroup = 'Account ' ;
18+
19+ protected static ?int $ navigationSort = 1 ;
20+
21+ protected static string $ view = 'filament.app.pages.premium-dashboard-page ' ;
22+
23+ protected static ?string $ title = 'Premium Dashboard ' ;
24+
25+ public function mount (): void
26+ {
27+ // Redirect if user is not premium
28+ if (!Auth::user ()->isPremium ()) {
29+ $ this ->redirect (route ('filament.app.pages.subscription ' ));
30+ }
31+ }
32+
33+ protected function getHeaderActions (): array
34+ {
35+ $ user = Auth::user ();
36+ $ actions = [];
37+
38+ if ($ user ->subscription ('premium ' )?->cancelled()) {
39+ $ actions [] = Action::make ('resume ' )
40+ ->label ('Resume Subscription ' )
41+ ->icon ('heroicon-o-play ' )
42+ ->color ('success ' )
43+ ->action ('resumeSubscription ' );
44+ } else {
45+ $ actions [] = Action::make ('cancel ' )
46+ ->label ('Cancel Subscription ' )
47+ ->icon ('heroicon-o-x-mark ' )
48+ ->color ('danger ' )
49+ ->requiresConfirmation ()
50+ ->action ('cancelSubscription ' );
51+ }
52+
53+ return $ actions ;
54+ }
55+
56+ public function cancelSubscription (): void
57+ {
58+ try {
59+ $ subscriptionService = app (SubscriptionService::class);
60+ $ subscriptionService ->cancelPremiumSubscription (Auth::user ());
61+
62+ Notification::make ()
63+ ->title ('Subscription Cancelled ' )
64+ ->body ('Your subscription has been cancelled. You can continue using premium features until the end of your billing period. ' )
65+ ->warning ()
66+ ->send ();
67+
68+ $ this ->redirect (route ('filament.app.pages.subscription ' ));
69+
70+ } catch (\Exception $ e ) {
71+ Notification::make ()
72+ ->title ('Cancellation Error ' )
73+ ->body ('There was an error cancelling your subscription. Please try again. ' )
74+ ->danger ()
75+ ->send ();
76+ }
77+ }
78+
79+ public function resumeSubscription (): void
80+ {
81+ try {
82+ $ subscriptionService = app (SubscriptionService::class);
83+ $ subscriptionService ->resumePremiumSubscription (Auth::user ());
84+
85+ Notification::make ()
86+ ->title ('Subscription Resumed ' )
87+ ->body ('Your premium subscription has been resumed successfully! ' )
88+ ->success ()
89+ ->send ();
90+
91+ } catch (\Exception $ e ) {
92+ Notification::make ()
93+ ->title ('Resume Error ' )
94+ ->body ('There was an error resuming your subscription. Please try again. ' )
95+ ->danger ()
96+ ->send ();
97+ }
98+ }
99+
100+ public function getSubscriptionData (): array
101+ {
102+ $ user = Auth::user ();
103+ $ subscription = $ user ->subscription ('premium ' );
104+
105+ return [
106+ 'is_premium ' => $ user ->isPremium (),
107+ 'on_trial ' => $ user ->onPremiumTrial (),
108+ 'trial_days_remaining ' => $ user ->trialDaysRemaining (),
109+ 'subscription_status ' => $ subscription ?->stripe_status,
110+ 'subscription_ends_at ' => $ subscription ?->ends_at,
111+ 'premium_started_at ' => $ user ->premium_started_at ,
112+ ];
113+ }
114+
115+ public function getPremiumFeatures (): array
116+ {
117+ $ subscriptionService = app (SubscriptionService::class);
118+ return $ subscriptionService ->getPremiumFeaturesStatus (Auth::user ());
119+ }
120+ }
0 commit comments