diff --git a/app/Console/Commands/ChargeCreditsCommand.php b/app/Console/Commands/ChargeCreditsCommand.php index 7deefc3f..35bfad0a 100644 --- a/app/Console/Commands/ChargeCreditsCommand.php +++ b/app/Console/Commands/ChargeCreditsCommand.php @@ -2,9 +2,10 @@ namespace App\Console\Commands; -use App\Classes\Pterodactyl; +use App\Models\Product; use App\Models\Server; -use Carbon\Carbon; +use App\Models\User; +use App\Notifications\ServersSuspendedNotification; use Illuminate\Console\Command; class ChargeCreditsCommand extends Command @@ -23,6 +24,13 @@ class ChargeCreditsCommand extends Command */ protected $description = 'Charge all users with active servers'; + + /** + * A list of users that have to be notified + * @var array + */ + protected $usersToNotify = []; + /** * Create a new command instance. * @@ -40,42 +48,54 @@ class ChargeCreditsCommand extends Command */ public function handle() { - Server::chunk(10, function ($servers) { + Server::whereNull('suspended')->chunk(10, function ($servers) { /** @var Server $server */ foreach ($servers as $server) { - - //ignore suspended servers - if ($server->isSuspended()) { - echo Carbon::now()->isoFormat('LLL') . " Ignoring suspended server"; - continue; - } - - //vars + /** @var Product $product */ + $product = $server->product; + /** @var User $user */ $user = $server->user; - $price = ($server->product->price / 30) / 24; - - //remove credits or suspend server - if ($user->credits >= $price) { - $user->decrement('credits', $price); - - //log - echo Carbon::now()->isoFormat('LLL') . " [CREDIT DEDUCTION] Removed " . number_format($price, 2, '.', '') . " from user (" . $user->name . ") for server (" . $server->name . ")\n"; + #charge credits / suspend server + if ($user->credits >= $product->getHourlyPrice()) { + $this->line("{$user->name} Current credits: {$user->credits} Credits to be removed: {$product->getHourlyPrice()}"); + $user->decrement('credits', $product->getHourlyPrice()); } else { - $response = Pterodactyl::client()->post("/application/servers/{$server->pterodactyl_id}/suspend"); + try { + #suspend server + $this->line("{$server->name} from user: {$user->name} has been suspended!"); + $server->suspend(); - if ($response->successful()) { - echo Carbon::now()->isoFormat('LLL') . " [CREDIT DEDUCTION] Suspended server (" . $server->name . ") from user (" . $user->name . ")\n"; - $server->update(['suspended' => now()]); - } else { - echo Carbon::now()->isoFormat('LLL') . " [CREDIT DEDUCTION] CRITICAL ERROR! Unable to suspend server (" . $server->name . ") from user (" . $user->name . ")\n"; - dump($response->json()); + #add user to notify list + if (!in_array($user, $this->usersToNotify)) { + array_push($this->usersToNotify, $user); + } + } catch (\Exception $exception) { + $this->error($exception->getMessage()); } - } + } } }); - return 'Charged credits for existing servers!\n'; + return $this->notifyUsers(); + } + + /** + * @return bool + */ + public function notifyUsers() + { + if (!empty($this->usersToNotify)) { + /** @var User $user */ + foreach ($this->usersToNotify as $user) { + $this->line("Notified user: {$user->name}"); + $user->notify(new ServersSuspendedNotification()); + } + } + + #reset array + $this->usersToNotify = array(); + return true; } } diff --git a/app/Models/Product.php b/app/Models/Product.php index e63bf4d8..68dc8f74 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -27,6 +27,21 @@ class Product extends Model }); } + public function getHourlyPrice() + { + return ($this->price / 30) / 24; + } + + public function getDailyPrice() + { + return ($this->price / 30); + } + + public function getWeeklyPrice() + { + return ($this->price / 4); + } + /** * @return BelongsTo */ diff --git a/app/Models/Server.php b/app/Models/Server.php index d6f5e178..e48dc8a0 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -124,6 +124,7 @@ class Server extends Model return $this; } + /** * @return HasOne */ diff --git a/app/Notifications/ServersSuspendedNotification.php b/app/Notifications/ServersSuspendedNotification.php new file mode 100644 index 00000000..7a399cf2 --- /dev/null +++ b/app/Notifications/ServersSuspendedNotification.php @@ -0,0 +1,70 @@ +subject('Your servers have been suspended!') + ->greeting('Your servers have been suspended!') + ->line("To automatically re-enable your server/s, you need to purchase more credits.") + ->action('Purchase credits', route('store.index')) + ->line('If you have any questions please let us know.'); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'title' => "Servers suspended!", + 'content' => " +
Your servers have been suspended!
+

To automatically re-enable your server/s, you need to purchase more credits.

+

If you have any questions please let us know.

+

Regards,
" . config('app.name', 'Laravel') . "

+ ", + ]; + } +} diff --git a/routes/web.php b/routes/web.php index e0184fab..ecf59ce2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -114,11 +114,6 @@ Route::middleware('auth')->group(function () { Route::resource('api', ApplicationApiController::class)->parameters([ 'api' => 'applicationApi', ]); - - #Testing route to preview new ConfirmedPaymentNotification email - Route::get('test' , function(Request $request){ - return (new \App\Notifications\ConfirmPaymentNotification(\App\Models\Payment::factory()->create()))->toMail($request->user()); - }); }); Route::get('/home', [HomeController::class, 'index'])->name('home');