Merge pull request #94 from ControlPanel-gg/charge_credits
modified the charge_credits command
This commit is contained in:
commit
af19413060
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Classes\Pterodactyl;
|
use App\Models\Product;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use Carbon\Carbon;
|
use App\Models\User;
|
||||||
|
use App\Notifications\ServersSuspendedNotification;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
class ChargeCreditsCommand extends Command
|
class ChargeCreditsCommand extends Command
|
||||||
|
@ -23,6 +24,13 @@ class ChargeCreditsCommand extends Command
|
||||||
*/
|
*/
|
||||||
protected $description = 'Charge all users with active servers';
|
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.
|
* Create a new command instance.
|
||||||
*
|
*
|
||||||
|
@ -40,42 +48,54 @@ class ChargeCreditsCommand extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
Server::chunk(10, function ($servers) {
|
Server::whereNull('suspended')->chunk(10, function ($servers) {
|
||||||
/** @var Server $server */
|
/** @var Server $server */
|
||||||
foreach ($servers as $server) {
|
foreach ($servers as $server) {
|
||||||
|
/** @var Product $product */
|
||||||
//ignore suspended servers
|
$product = $server->product;
|
||||||
if ($server->isSuspended()) {
|
/** @var User $user */
|
||||||
echo Carbon::now()->isoFormat('LLL') . " Ignoring suspended server";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//vars
|
|
||||||
$user = $server->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("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$product->getHourlyPrice()}</>");
|
||||||
|
$user->decrement('credits', $product->getHourlyPrice());
|
||||||
} else {
|
} else {
|
||||||
$response = Pterodactyl::client()->post("/application/servers/{$server->pterodactyl_id}/suspend");
|
try {
|
||||||
|
#suspend server
|
||||||
|
$this->line("<fg=yellow>{$server->name}</> from user: <fg=blue>{$user->name}</> has been <fg=red>suspended!</>");
|
||||||
|
$server->suspend();
|
||||||
|
|
||||||
if ($response->successful()) {
|
#add user to notify list
|
||||||
echo Carbon::now()->isoFormat('LLL') . " [CREDIT DEDUCTION] Suspended server (" . $server->name . ") from user (" . $user->name . ")\n";
|
if (!in_array($user, $this->usersToNotify)) {
|
||||||
$server->update(['suspended' => now()]);
|
array_push($this->usersToNotify, $user);
|
||||||
} else {
|
|
||||||
echo Carbon::now()->isoFormat('LLL') . " [CREDIT DEDUCTION] CRITICAL ERROR! Unable to suspend server (" . $server->name . ") from user (" . $user->name . ")\n";
|
|
||||||
dump($response->json());
|
|
||||||
}
|
}
|
||||||
|
} 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("<fg=yellow>Notified user:</> <fg=blue>{$user->name}</>");
|
||||||
|
$user->notify(new ServersSuspendedNotification());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#reset array
|
||||||
|
$this->usersToNotify = array();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* @return BelongsTo
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -124,6 +124,7 @@ class Server extends Model
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return HasOne
|
* @return HasOne
|
||||||
*/
|
*/
|
||||||
|
|
70
app/Notifications/ServersSuspendedNotification.php
Normal file
70
app/Notifications/ServersSuspendedNotification.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications;
|
||||||
|
|
||||||
|
use App\Models\Configuration;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Notifications\Messages\MailMessage;
|
||||||
|
use Illuminate\Notifications\Notification;
|
||||||
|
|
||||||
|
class ServersSuspendedNotification extends Notification implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new notification instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the notification's delivery channels.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function via($notifiable)
|
||||||
|
{
|
||||||
|
return ['mail' , 'database'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mail representation of the notification.
|
||||||
|
*
|
||||||
|
* @param mixed $notifiable
|
||||||
|
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||||
|
*/
|
||||||
|
public function toMail($notifiable)
|
||||||
|
{
|
||||||
|
return (new MailMessage)
|
||||||
|
->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' => "
|
||||||
|
<h5>Your servers have been suspended!</h5>
|
||||||
|
<p>To automatically re-enable your server/s, you need to purchase more credits.</p>
|
||||||
|
<p>If you have any questions please let us know.</p>
|
||||||
|
<p>Regards,<br />" . config('app.name', 'Laravel') . "</p>
|
||||||
|
",
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -114,11 +114,6 @@ Route::middleware('auth')->group(function () {
|
||||||
Route::resource('api', ApplicationApiController::class)->parameters([
|
Route::resource('api', ApplicationApiController::class)->parameters([
|
||||||
'api' => 'applicationApi',
|
'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');
|
Route::get('/home', [HomeController::class, 'index'])->name('home');
|
||||||
|
|
Loading…
Reference in a new issue