-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathDeveloperOnboardingController.php
More file actions
165 lines (133 loc) · 6.24 KB
/
DeveloperOnboardingController.php
File metadata and controls
165 lines (133 loc) · 6.24 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
<?php
namespace App\Http\Controllers;
use App\Models\DeveloperAccount;
use App\Services\StripeConnectService;
use App\Support\StripeConnectCountries;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
class DeveloperOnboardingController extends Controller
{
public function __construct(protected StripeConnectService $stripeConnectService) {}
public function show(Request $request): View|RedirectResponse
{
$user = $request->user();
$developerAccount = $user->developerAccount;
if ($developerAccount && $developerAccount->hasCompletedOnboarding() && $developerAccount->hasAcceptedCurrentTerms()) {
return to_route('customer.developer.dashboard')
->with('message', 'Your developer account is already set up.');
}
return view('customer.developer.onboarding', [
'developerAccount' => $developerAccount,
'hasExistingAccount' => $developerAccount !== null,
]);
}
public function start(Request $request): RedirectResponse
{
$request->validate([
'accepted_plugin_terms' => ['required', 'accepted'],
'country' => ['required', 'string', 'size:2', Rule::in(StripeConnectCountries::supportedCountryCodes())],
'payout_currency' => ['required', 'string', 'size:3'],
], [
'accepted_plugin_terms.required' => 'You must accept the Plugin Developer Terms and Conditions.',
'accepted_plugin_terms.accepted' => 'You must accept the Plugin Developer Terms and Conditions.',
'country.required' => 'Please select your country.',
'country.in' => 'The selected country is not supported for Stripe Connect.',
'payout_currency.required' => 'Please select a payout currency.',
]);
$country = strtoupper($request->input('country'));
$payoutCurrency = strtoupper($request->input('payout_currency'));
if (! StripeConnectCountries::isValidCurrencyForCountry($country, $payoutCurrency)) {
return back()->withErrors(['payout_currency' => 'The selected currency is not available for your country.']);
}
$user = $request->user();
$developerAccount = $user->developerAccount;
if (! $developerAccount) {
$developerAccount = $this->stripeConnectService->createConnectAccount($user, $country, $payoutCurrency);
} else {
$developerAccount->update([
'country' => $country,
'payout_currency' => $payoutCurrency,
]);
}
if (! $developerAccount->hasAcceptedCurrentTerms()) {
$developerAccount->update([
'accepted_plugin_terms_at' => now(),
'plugin_terms_version' => DeveloperAccount::CURRENT_PLUGIN_TERMS_VERSION,
]);
}
// If Stripe onboarding is already complete, skip the Stripe redirect
if ($developerAccount->hasCompletedOnboarding()) {
return to_route('customer.plugins.create')
->with('success', 'Terms accepted! You can now submit plugins.');
}
try {
$onboardingUrl = $this->stripeConnectService->createOnboardingLink($developerAccount);
return redirect($onboardingUrl);
} catch (\Exception $e) {
return to_route('customer.developer.onboarding')
->with('error', 'Unable to start onboarding. Please try again.');
}
}
public function return(Request $request): RedirectResponse
{
$user = $request->user();
$developerAccount = $user->developerAccount;
if (! $developerAccount) {
return to_route('customer.developer.onboarding')
->with('error', 'Developer account not found.');
}
$this->stripeConnectService->refreshAccountStatus($developerAccount);
if ($developerAccount->hasCompletedOnboarding()) {
// Link any existing paid plugins that don't have a developer account
$user->plugins()
->where('type', 'paid')
->whereNull('developer_account_id')
->update(['developer_account_id' => $developerAccount->id]);
return to_route('customer.developer.dashboard')
->with('success', 'Your developer account is now active!');
}
return to_route('customer.developer.onboarding')
->with('message', 'Onboarding is not complete. Please finish the remaining steps.');
}
public function refresh(Request $request): RedirectResponse
{
$user = $request->user();
$developerAccount = $user->developerAccount;
if (! $developerAccount) {
return to_route('customer.developer.onboarding');
}
try {
$onboardingUrl = $this->stripeConnectService->createOnboardingLink($developerAccount);
return redirect($onboardingUrl);
} catch (\Exception $e) {
return to_route('customer.developer.onboarding')
->with('error', 'Unable to refresh onboarding. Please try again.');
}
}
public function dashboard(Request $request): View|RedirectResponse
{
$user = $request->user();
$developerAccount = $user->developerAccount;
if (! $developerAccount || ! $developerAccount->hasCompletedOnboarding()) {
return to_route('customer.developer.onboarding');
}
$this->stripeConnectService->refreshAccountStatus($developerAccount);
$plugins = $user->plugins()->withCount('licenses')->get();
$payouts = $developerAccount->payouts()->with('pluginLicense.plugin')->latest()->limit(10)->get();
$totalEarnings = $developerAccount->payouts()
->where('status', 'transferred')
->sum('developer_amount');
$pendingEarnings = $developerAccount->payouts()
->where('status', 'pending')
->sum('developer_amount');
return view('customer.developer.dashboard', [
'developerAccount' => $developerAccount,
'plugins' => $plugins,
'payouts' => $payouts,
'totalEarnings' => $totalEarnings,
'pendingEarnings' => $pendingEarnings,
]);
}
}