-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathCustomerLicenseController.php
More file actions
199 lines (165 loc) · 6.46 KB
/
CustomerLicenseController.php
File metadata and controls
199 lines (165 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace App\Http\Controllers;
use App\Models\Plugin;
use App\Models\PluginLicense;
use App\Models\SubLicense;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class CustomerLicenseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(): View
{
$user = Auth::user();
// Dashboard summary data
$licenseCount = $user->licenses()->count();
$isEapCustomer = $user->isEapCustomer();
$activeSubscription = $user->subscription();
$pluginLicenseCount = $user->pluginLicenses()->count();
// Get subscription plan name
$subscriptionName = null;
if ($activeSubscription) {
if ($activeSubscription->stripe_price) {
try {
$subscriptionName = \App\Enums\Subscription::fromStripePriceId($activeSubscription->stripe_price)->name();
} catch (\RuntimeException) {
$subscriptionName = ucfirst($activeSubscription->type);
}
} else {
$subscriptionName = ucfirst($activeSubscription->type);
}
}
// For renewal CTA when no subscription
$renewalLicenseKey = null;
if (! $activeSubscription) {
$highestTierLicense = $user->licenses()
->whereIn('policy_name', ['max', 'pro', 'mini'])
->orderByRaw("CASE policy_name WHEN 'max' THEN 1 WHEN 'pro' THEN 2 WHEN 'mini' THEN 3 END")
->first();
$renewalLicenseKey = $highestTierLicense?->key;
}
// Connected accounts info
$hasGitHub = $user->hasGitHubToken();
$hasDiscord = $user->hasDiscordConnected();
$connectedAccountsCount = ($hasGitHub ? 1 : 0) + ($hasDiscord ? 1 : 0);
$connectedAccountsDescription = match (true) {
$hasGitHub && $hasDiscord => 'GitHub & Discord',
$hasGitHub => 'GitHub connected',
$hasDiscord => 'Discord connected',
default => 'No accounts connected',
};
// Total purchases (licenses + plugins + products)
$productLicenseCount = $user->productLicenses()->count();
$totalPurchases = $licenseCount + $pluginLicenseCount + $productLicenseCount;
$developerAccount = $user->developerAccount;
return view('customer.dashboard', compact(
'licenseCount',
'isEapCustomer',
'activeSubscription',
'subscriptionName',
'pluginLicenseCount',
'renewalLicenseKey',
'connectedAccountsCount',
'connectedAccountsDescription',
'totalPurchases',
'developerAccount'
));
}
public function list(): View
{
$user = Auth::user();
$licenses = $user->licenses()->orderBy('created_at', 'desc')->get();
// Fetch sub-licenses assigned to this user's email (excluding those from licenses they own)
$assignedSubLicenses = SubLicense::query()
->with('parentLicense')
->where('assigned_email', $user->email)
->whereHas('parentLicense', function ($query) use ($user): void {
$query->where('user_id', '!=', $user->id);
})->latest()
->get();
return view('customer.licenses.list', compact('licenses', 'assignedSubLicenses'));
}
public function show(string $licenseKey): View
{
$user = Auth::user();
$license = $user->licenses()
->with('subLicenses')
->where('key', $licenseKey)
->firstOrFail();
return view('customer.licenses.show', compact('license'));
}
public function update(Request $request, string $licenseKey): RedirectResponse
{
$user = Auth::user();
$license = $user->licenses()->where('key', $licenseKey)->firstOrFail();
$request->validate([
'name' => ['nullable', 'string', 'max:255'],
]);
$license->update([
'name' => $request->name,
]);
return to_route('customer.licenses.show', $licenseKey)
->with('success', 'License name updated successfully!');
}
public function rotatePluginLicenseKey(): RedirectResponse
{
$user = Auth::user();
$user->regeneratePluginLicenseKey();
return to_route('customer.purchased-plugins.index')
->with('success', 'Your plugin license key has been rotated. Please update your Composer configuration with the new key.');
}
public function claimFreePlugins(): RedirectResponse
{
$user = Auth::user();
// Check if offer has expired
if (now()->gt('2026-05-31 23:59:59')) {
return to_route('dashboard')
->with('error', 'This offer has expired.');
}
// Verify eligibility
if (! $user->isEligibleForFreePluginsOffer()) {
return to_route('dashboard')
->with('error', 'You are not eligible for this offer.');
}
// Get the free plugins
$freePlugins = Plugin::query()
->whereIn('name', User::FREE_PLUGINS_OFFER)
->get();
if ($freePlugins->isEmpty()) {
return to_route('dashboard')
->with('error', 'The free plugins are not currently available.');
}
$claimedCount = 0;
foreach ($freePlugins as $plugin) {
// Skip if user already has a license for this plugin
$existingLicense = $user->pluginLicenses()
->where('plugin_id', $plugin->id)
->exists();
if ($existingLicense) {
continue;
}
// Create the plugin license
PluginLicense::create([
'user_id' => $user->id,
'plugin_id' => $plugin->id,
'price_paid' => 0,
'currency' => 'USD',
'is_grandfathered' => true,
'purchased_at' => now(),
]);
$claimedCount++;
}
if ($claimedCount === 0) {
return to_route('dashboard')
->with('message', 'You have already claimed all the free plugins.');
}
return to_route('dashboard')
->with('success', "Successfully claimed {$claimedCount} free plugin(s)! You can now install them via Composer.");
}
}