/
dimamir
/
coolify
Обзор
Документация
Войти
/
dimamir
/
coolify
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
app/Console/Commands/CloudCleanupSubscriptions.php
98 строк
4 KB
Lucas Michot
Get rid of many useless blank lines
31 окт 2024, 19:44
31 окт 2024, 19:44
8e1444e
Код
Авторство
О чём код?
<?php namespace App\Console\Commands; use App\Models\Team; use Illuminate\Console\Command; class CloudCleanupSubscriptions extends Command { protected $signature = 'cloud:cleanup-subs'; protected $description = 'Cleanup subcriptions teams'; public function handle() { try { if (! isCloud()) { $this->error('This command can only be run on cloud'); return; } $this->info('Cleaning up subcriptions teams'); $stripe = new \Stripe\StripeClient(config('subscription.stripe_api_key')); $teams = Team::all()->filter(function ($team) { return $team->id !== 0; })->sortBy('id'); foreach ($teams as $team) { if ($team) { $this->info("Checking team {$team->id}"); } if (! data_get($team, 'subscription')) { $this->disableServers($team); continue; } // If the team has no subscription id and the invoice is paid, we need to reset the invoice paid status if (! (data_get($team, 'subscription.stripe_subscription_id'))) { $this->info("Resetting invoice paid status for team {$team->id} {$team->name}"); $team->subscription->update([ 'stripe_invoice_paid' => false, 'stripe_trial_already_ended' => false, 'stripe_subscription_id' => null, ]); $this->disableServers($team); continue; } else { $subscription = $stripe->subscriptions->retrieve(data_get($team, 'subscription.stripe_subscription_id'), []); $status = data_get($subscription, 'status'); if ($status === 'active' || $status === 'past_due') { $team->subscription->update([ 'stripe_invoice_paid' => true, 'stripe_trial_already_ended' => false, ]); continue; } $this->info('Subscription status: '.$status); $this->info('Subscription id: '.data_get($team, 'subscription.stripe_subscription_id')); $confirm = $this->confirm('Do you want to cancel the subscription?', true); if (! $confirm) { $this->info("Skipping team {$team->id} {$team->name}"); } else { $this->info("Cancelling subscription for team {$team->id} {$team->name}"); $team->subscription->update([ 'stripe_invoice_paid' => false, 'stripe_trial_already_ended' => false, 'stripe_subscription_id' => null, ]); $this->disableServers($team); } } } } catch (\Exception $e) { $this->error($e->getMessage()); return; } } private function disableServers(Team $team) { foreach ($team->servers as $server) { if ($server->settings->is_usable === true || $server->settings->is_reachable === true || $server->ip !== '1.2.3.4') { $this->info("Disabling server {$server->id} {$server->name}"); $server->settings()->update([ 'is_usable' => false, 'is_reachable' => false, ]); $server->update([ 'ip' => '1.2.3.4', ]); } } } }