@@ -34,6 +34,98 @@ protected function getHeaderActions(): array
3434 })
3535 ->visible (fn (User $ record ) => empty ($ record ->stripe_id )),
3636
37+ Actions \Action::make ('compUltraSubscription ' )
38+ ->label ('Comp Ultra Subscription ' )
39+ ->color ('warning ' )
40+ ->icon ('heroicon-o-sparkles ' )
41+ ->modalHeading ('Comp Ultra Subscription ' )
42+ ->modalSubmitActionLabel ('Comp Ultra ' )
43+ ->form (function (User $ record ): array {
44+ $ existingSubscription = $ record ->subscription ('default ' );
45+ $ hasActiveSubscription = $ existingSubscription && $ existingSubscription ->active ();
46+
47+ $ fields = [];
48+
49+ if ($ hasActiveSubscription ) {
50+ $ currentPlan = 'their current plan ' ;
51+
52+ try {
53+ $ currentPlan = \App \Enums \Subscription::fromStripePriceId (
54+ $ existingSubscription ->items ->first ()?->stripe_price ?? $ existingSubscription ->stripe_price
55+ )->name ();
56+ } catch (\Exception ) {
57+ }
58+
59+ $ fields [] = \Filament \Forms \Components \Placeholder::make ('info ' )
60+ ->label ('' )
61+ ->content ("This user has an active {$ currentPlan } subscription. Choose when to switch them to the comped Ultra plan. " );
62+
63+ $ fields [] = \Filament \Forms \Components \Radio::make ('timing ' )
64+ ->label ('When to switch ' )
65+ ->options ([
66+ 'now ' => 'Immediately — swap now and credit remaining value (swapAndInvoice) ' ,
67+ 'renewal ' => 'At renewal — keep current plan until period ends, then switch (swap) ' ,
68+ ])
69+ ->default ('now ' )
70+ ->required ();
71+ } else {
72+ $ fields [] = \Filament \Forms \Components \Placeholder::make ('info ' )
73+ ->label ('' )
74+ ->content ("This will create a free Ultra subscription for {$ record ->email }. A Stripe customer will be created if one doesn't exist. " );
75+ }
76+
77+ return $ fields ;
78+ })
79+ ->action (function (array $ data , User $ record ): void {
80+ $ compedPriceId = config ('subscriptions.plans.max.stripe_price_id_comped ' );
81+
82+ if (! $ compedPriceId ) {
83+ Notification::make ()
84+ ->danger ()
85+ ->title ('STRIPE_ULTRA_COMP_PRICE_ID is not configured. ' )
86+ ->send ();
87+
88+ return ;
89+ }
90+
91+ $ record ->createOrGetStripeCustomer ();
92+
93+ $ existingSubscription = $ record ->subscription ('default ' );
94+
95+ if ($ existingSubscription && $ existingSubscription ->active ()) {
96+ $ timing = $ data ['timing ' ] ?? 'now ' ;
97+
98+ if ($ timing === 'now ' ) {
99+ $ existingSubscription ->skipTrial ()->swapAndInvoice ($ compedPriceId );
100+ $ message = 'Subscription swapped to comped Ultra immediately. Remaining value has been credited. ' ;
101+ } else {
102+ $ existingSubscription ->skipTrial ()->swap ($ compedPriceId );
103+ $ message = 'Subscription will switch to comped Ultra at the end of the current billing period. ' ;
104+ }
105+
106+ Notification::make ()
107+ ->success ()
108+ ->title ('Comped Ultra subscription applied. ' )
109+ ->body ($ message )
110+ ->send ();
111+ } else {
112+ $ record ->newSubscription ('default ' , $ compedPriceId )->create ();
113+
114+ Notification::make ()
115+ ->success ()
116+ ->title ('Comped Ultra subscription created. ' )
117+ ->body ("Ultra subscription created for {$ record ->email }. " )
118+ ->send ();
119+ }
120+ })
121+ ->visible (function (User $ record ): bool {
122+ if (! config ('subscriptions.plans.max.stripe_price_id_comped ' )) {
123+ return false ;
124+ }
125+
126+ return ! $ record ->hasActiveUltraSubscription ();
127+ }),
128+
37129 Actions \Action::make ('createAnystackLicense ' )
38130 ->label ('Create Anystack License ' )
39131 ->color ('gray ' )
0 commit comments