-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathCompUltraSubscription.php
More file actions
59 lines (40 loc) · 1.67 KB
/
CompUltraSubscription.php
File metadata and controls
59 lines (40 loc) · 1.67 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
<?php
namespace App\Console\Commands;
use App\Enums\Subscription;
use App\Models\User;
use Illuminate\Console\Command;
class CompUltraSubscription extends Command
{
protected $signature = 'ultra:comp {email : The email address of the user to comp}';
protected $description = 'Create a comped Ultra subscription for a user using the dedicated $0 Stripe price';
public function handle(): int
{
$compedPriceId = config('subscriptions.plans.max.stripe_price_id_comped');
if (! $compedPriceId) {
$this->error('STRIPE_ULTRA_COMP_PRICE_ID is not configured.');
return self::FAILURE;
}
$email = $this->argument('email');
$user = User::where('email', $email)->first();
if (! $user) {
$this->error("User not found: {$email}");
return self::FAILURE;
}
$existingSubscription = $user->subscription('default');
if ($existingSubscription && $existingSubscription->active()) {
$currentPlan = 'unknown';
try {
$currentPlan = Subscription::fromStripePriceId(
$existingSubscription->items->first()?->stripe_price ?? $existingSubscription->stripe_price
)->name();
} catch (\Exception) {
}
$this->error("User already has an active {$currentPlan} subscription. Cancel it first or use swap.");
return self::FAILURE;
}
$user->createOrGetStripeCustomer();
$user->newSubscription('default', $compedPriceId)->create();
$this->info("Comped Ultra subscription created for {$email}.");
return self::SUCCESS;
}
}