-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathProcessPluginCheckoutJob.php
More file actions
318 lines (255 loc) · 11.3 KB
/
ProcessPluginCheckoutJob.php
File metadata and controls
318 lines (255 loc) · 11.3 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace App\Jobs;
use App\Enums\PayoutStatus;
use App\Models\Plugin;
use App\Models\PluginBundle;
use App\Models\PluginLicense;
use App\Models\PluginPayout;
use App\Models\PluginPrice;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class ProcessPluginCheckoutJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
public string $checkoutSessionId,
public array $metadata,
public int $amountTotal,
public string $currency,
public ?string $paymentIntentId = null
) {}
public function handle(): void
{
$userId = $this->metadata['user_id'] ?? null;
$cartId = $this->metadata['cart_id'] ?? null;
if (! $userId) {
Log::error('No user_id in checkout session metadata', ['session_id' => $this->checkoutSessionId]);
return;
}
$user = User::find($userId);
if (! $user) {
Log::error('User not found for checkout session', ['session_id' => $this->checkoutSessionId, 'user_id' => $userId]);
return;
}
// Handle bundle checkout
if (isset($this->metadata['bundle_ids']) && ! empty($this->metadata['bundle_ids'])) {
$this->processBundleCheckout($user);
}
// Handle cart checkout (individual plugins)
if ($cartId && isset($this->metadata['plugin_ids']) && ! empty($this->metadata['plugin_ids'])) {
$this->processCartCheckout($user);
return;
}
// Handle single plugin checkout
if (isset($this->metadata['plugin_id'])) {
// Check if single plugin already processed
if (PluginLicense::where('stripe_checkout_session_id', $this->checkoutSessionId)->exists()) {
Log::info('Single plugin checkout already processed', ['session_id' => $this->checkoutSessionId]);
return;
}
$this->processSinglePluginCheckout($user);
return;
}
Log::error('Unknown checkout session format', ['session_id' => $this->checkoutSessionId, 'metadata' => $this->metadata]);
}
protected function processCartCheckout(User $user): void
{
Log::info('Starting cart checkout processing', [
'session_id' => $this->checkoutSessionId,
'metadata' => $this->metadata,
]);
$pluginIds = explode(',', $this->metadata['plugin_ids']);
$priceIds = explode(',', $this->metadata['price_ids'] ?? '');
Log::info('Parsed plugin IDs from metadata', [
'session_id' => $this->checkoutSessionId,
'raw_plugin_ids' => $this->metadata['plugin_ids'],
'parsed_plugin_ids' => $pluginIds,
'plugin_count' => count($pluginIds),
]);
// Get already processed plugin IDs for this session
$alreadyProcessedPluginIds = PluginLicense::where('stripe_checkout_session_id', $this->checkoutSessionId)
->pluck('plugin_id')
->toArray();
$processedCount = 0;
$skippedCount = count($alreadyProcessedPluginIds);
foreach ($pluginIds as $index => $pluginId) {
Log::info('Processing plugin in cart', [
'session_id' => $this->checkoutSessionId,
'index' => $index,
'plugin_id' => $pluginId,
'already_processed' => in_array((int) $pluginId, $alreadyProcessedPluginIds),
]);
// Skip if this plugin was already processed for this session
if (in_array((int) $pluginId, $alreadyProcessedPluginIds)) {
continue;
}
$plugin = Plugin::find($pluginId);
if (! $plugin) {
Log::warning('Plugin not found during checkout processing', ['plugin_id' => $pluginId]);
continue;
}
$priceId = $priceIds[$index] ?? null;
$price = $priceId ? PluginPrice::find($priceId) : $plugin->activePrice;
$amount = $price ? $price->amount : 0;
try {
$this->createLicense($user, $plugin, $amount);
$processedCount++;
Log::info('Successfully created license for plugin', [
'session_id' => $this->checkoutSessionId,
'plugin_id' => $pluginId,
'plugin_name' => $plugin->name,
]);
} catch (\Exception $e) {
Log::error('Failed to create license for plugin', [
'session_id' => $this->checkoutSessionId,
'plugin_id' => $pluginId,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw $e;
}
}
// Ensure user has a plugin license key
$user->getPluginLicenseKey();
Log::info('Processed cart checkout', [
'session_id' => $this->checkoutSessionId,
'user_id' => $user->id,
'total_plugins' => count($pluginIds),
'processed_now' => $processedCount,
'already_processed' => $skippedCount,
]);
}
protected function processBundleCheckout(User $user): void
{
$bundleIds = array_filter(explode(',', $this->metadata['bundle_ids']));
$bundlePluginIds = json_decode($this->metadata['bundle_plugin_ids'] ?? '{}', true);
Log::info('Processing bundle checkout', [
'session_id' => $this->checkoutSessionId,
'bundle_ids' => $bundleIds,
]);
foreach ($bundleIds as $bundleId) {
$bundle = PluginBundle::with(['plugins.developerAccount', 'plugins.activePrice'])
->find($bundleId);
if (! $bundle) {
Log::warning('Bundle not found during checkout processing', ['bundle_id' => $bundleId]);
continue;
}
// Check how many plugins are already processed for this bundle in this session
$existingLicenseCount = PluginLicense::where('stripe_checkout_session_id', $this->checkoutSessionId)
->where('plugin_bundle_id', $bundleId)
->count();
if ($existingLicenseCount === $bundle->plugins->count()) {
Log::info('Bundle already fully processed', [
'session_id' => $this->checkoutSessionId,
'bundle_id' => $bundleId,
]);
continue;
}
// Calculate proportional allocation for developer payouts
$allocations = $bundle->calculateProportionalAllocation();
foreach ($bundle->plugins as $plugin) {
// Skip if license already exists for this plugin in this session
if (PluginLicense::where('stripe_checkout_session_id', $this->checkoutSessionId)
->where('plugin_id', $plugin->id)
->where('plugin_bundle_id', $bundleId)
->exists()) {
continue;
}
$allocatedAmount = $allocations[$plugin->id] ?? 0;
$this->createBundleLicense($user, $plugin, $bundle, $allocatedAmount);
}
Log::info('Processed bundle checkout', [
'session_id' => $this->checkoutSessionId,
'bundle_id' => $bundleId,
'bundle_name' => $bundle->name,
'plugin_count' => $bundle->plugins->count(),
]);
}
$user->getPluginLicenseKey();
}
protected function createBundleLicense(User $user, Plugin $plugin, PluginBundle $bundle, int $allocatedAmount): PluginLicense
{
$license = PluginLicense::create([
'user_id' => $user->id,
'plugin_id' => $plugin->id,
'plugin_bundle_id' => $bundle->id,
'stripe_checkout_session_id' => $this->checkoutSessionId,
'stripe_payment_intent_id' => $this->paymentIntentId,
'price_paid' => $allocatedAmount,
'currency' => strtoupper($this->currency),
'is_grandfathered' => false,
'purchased_at' => now(),
]);
// Create proportional payout for developer
if ($plugin->developerAccount && $plugin->developerAccount->canReceivePayouts() && $allocatedAmount > 0) {
$split = PluginPayout::calculateSplit($allocatedAmount);
PluginPayout::create([
'plugin_license_id' => $license->id,
'developer_account_id' => $plugin->developerAccount->id,
'gross_amount' => $allocatedAmount,
'platform_fee' => $split['platform_fee'],
'developer_amount' => $split['developer_amount'],
'status' => PayoutStatus::Pending,
'eligible_for_payout_at' => now()->addDays(15),
]);
}
Log::info('Created bundle license', [
'session_id' => $this->checkoutSessionId,
'bundle_id' => $bundle->id,
'plugin_id' => $plugin->id,
'allocated_amount' => $allocatedAmount,
]);
return $license;
}
protected function processSinglePluginCheckout(User $user): void
{
$pluginId = $this->metadata['plugin_id'];
$priceId = $this->metadata['price_id'] ?? null;
$plugin = Plugin::find($pluginId);
if (! $plugin) {
Log::error('Plugin not found for single checkout', ['plugin_id' => $pluginId]);
return;
}
$price = $priceId ? PluginPrice::find($priceId) : $plugin->activePrice;
$amount = $price ? $price->amount : $this->amountTotal;
$this->createLicense($user, $plugin, $amount);
$user->getPluginLicenseKey();
Log::info('Processed single plugin checkout', [
'session_id' => $this->checkoutSessionId,
'user_id' => $user->id,
'plugin_id' => $pluginId,
]);
}
protected function createLicense(User $user, Plugin $plugin, int $amount): PluginLicense
{
$license = PluginLicense::create([
'user_id' => $user->id,
'plugin_id' => $plugin->id,
'stripe_checkout_session_id' => $this->checkoutSessionId,
'stripe_payment_intent_id' => $this->paymentIntentId,
'price_paid' => $amount,
'currency' => strtoupper($this->currency),
'is_grandfathered' => false,
'purchased_at' => now(),
]);
// Create payout record for developer if applicable
if ($plugin->developerAccount && $plugin->developerAccount->canReceivePayouts()) {
$split = PluginPayout::calculateSplit($amount);
PluginPayout::create([
'plugin_license_id' => $license->id,
'developer_account_id' => $plugin->developerAccount->id,
'gross_amount' => $amount,
'platform_fee' => $split['platform_fee'],
'developer_amount' => $split['developer_amount'],
'status' => PayoutStatus::Pending,
'eligible_for_payout_at' => now()->addDays(15),
]);
}
return $license;
}
}