feat: Added Server Cancelation route method and charging

This commit is contained in:
IceToast 2022-07-22 03:52:49 +02:00 committed by IceToast
parent f8c33d43be
commit 7fa9bf2062
No known key found for this signature in database
GPG key ID: 1464353E063A5B97
6 changed files with 44 additions and 9 deletions

View file

@ -81,14 +81,14 @@ class ChargeServers extends Command
continue;
}
// check if user has enough credits to charge the server
if ($user->credits < $product->price) {
// check if the server is canceled or if user has enough credits to charge the server or
if ( $server->cancelled || $user->credits < $product->price) {
try {
#suspend server
// suspend server
$this->line("<fg=yellow>{$server->name}</> from user: <fg=blue>{$user->name}</> has been <fg=red>suspended!</>");
$server->suspend();
#add user to notify list
// add user to notify list
if (!in_array($user, $this->usersToNotify)) {
array_push($this->usersToNotify, $user);
}

View file

@ -133,7 +133,25 @@ class ServerController extends Controller
}
/**
* @param Server $server
* Cancel the Server billing cycle.
*
* @param Server $server
* @return RedirectResponse|Response
*/
public function cancel (Server $server)
{
try {
error_log($server->update([
'cancelled' => now(),
]));
return redirect()->route('servers.index')->with('success', __('Server cancelled'));
} catch (Exception $e) {
return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to cancel the server"') . $e->getMessage() . '"');
}
}
/**
* @param Server $server
* @return RedirectResponse
*/
public function toggleSuspended(Server $server)

View file

@ -246,7 +246,21 @@ class ServerController extends Controller
return redirect()->route('servers.index')->with('success', __('Server removed'));
} catch (Exception $e) {
return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to remove a resource "').$e->getMessage().'"');
return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to delete the server"') . $e->getMessage() . '"');
}
}
/** Cancel Server */
public function cancel (Server $server)
{
try {
error_log($server->update([
'cancelled' => now(),
]));
return redirect()->route('servers.index')->with('success', __('Server cancelled'));
} catch (Exception $e) {
return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to cancel the server"') . $e->getMessage() . '"');
}
}

View file

@ -54,7 +54,8 @@ class Server extends Model
"identifier",
"product_id",
"pterodactyl_id",
"last_billed"
"last_billed",
"cancelled"
];
/**

View file

@ -219,8 +219,8 @@
}).then((result) => {
if (result.value) {
// Delete server
fetch("{{ route('servers.destroy', '') }}" + '/' + serverId, {
method: 'DELETE',
fetch("{{ route('servers.cancel', '') }}" + '/' + serverId, {
method: 'PATCH',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}

View file

@ -72,6 +72,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
//normal routes
Route::get('notifications/readAll', [NotificationController::class, 'readAll'])->name('notifications.readAll');
Route::resource('notifications', NotificationController::class);
Route::patch('/servers/cancel/{server}', [ServerController::class, 'cancel'])->name('servers.cancel');
Route::resource('servers', ServerController::class);
if (config('SETTINGS::SYSTEM:ENABLE_UPGRADE')) {
Route::post('servers/{server}/upgrade', [ServerController::class, 'upgrade'])->name('servers.upgrade');
@ -140,6 +141,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
//servers
Route::get('servers/datatable', [AdminServerController::class, 'datatable'])->name('servers.datatable');
Route::post('servers/togglesuspend/{server}', [AdminServerController::class, 'toggleSuspended'])->name('servers.togglesuspend');
Route::patch('/servers/cancel/{server}', [AdminServerController::class, 'cancel'])->name('servers.cancel');
Route::get('servers/sync', [AdminServerController::class, 'syncServers'])->name('servers.sync');
Route::resource('servers', AdminServerController::class);