-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathDeveloperOnboardingController.php
More file actions
117 lines (95 loc) · 4.48 KB
/
DeveloperOnboardingController.php
File metadata and controls
117 lines (95 loc) · 4.48 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
<?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;
class DeveloperOnboardingController extends Controller
{
public function __construct(protected StripeConnectService $stripeConnectService) {}
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.');
}
}
}