2021-06-05 09:26:32 +00:00
< ? php
namespace App\Http\Controllers ;
2023-02-03 18:03:57 +00:00
use App\Models\Pterodactyl\Egg ;
use App\Models\Pterodactyl\Location ;
use App\Models\Pterodactyl\Nest ;
use App\Models\Pterodactyl\Node ;
2021-06-05 09:26:32 +00:00
use App\Models\Product ;
use App\Models\Server ;
2022-11-22 12:42:46 +00:00
use App\Models\User ;
use App\Models\Settings ;
2021-06-05 09:26:32 +00:00
use App\Notifications\ServerCreationError ;
2022-06-16 14:15:57 +00:00
use Carbon\Carbon ;
2023-02-03 18:03:57 +00:00
use App\Settings\UserSettings ;
use App\Settings\ServerSettings ;
2023-02-06 20:16:54 +00:00
use App\Settings\PterodactylSettings ;
use App\Classes\PterodactylClient ;
use App\Settings\GeneralSettings ;
2021-06-05 09:26:32 +00:00
use Exception ;
2023-05-03 07:53:41 +00:00
use GuzzleHttp\Promise\Create ;
2021-11-07 11:07:30 +00:00
use Illuminate\Database\Eloquent\Builder ;
2021-06-30 21:53:28 +00:00
use Illuminate\Http\Client\Response ;
2021-06-26 21:56:54 +00:00
use Illuminate\Http\RedirectResponse ;
2021-06-05 09:26:32 +00:00
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Auth ;
2021-10-01 21:21:49 +00:00
use Illuminate\Support\Facades\Request as FacadesRequest ;
2021-06-05 09:26:32 +00:00
class ServerController extends Controller
{
2023-05-03 07:53:41 +00:00
const CREATE_PERMISSION = 'user.server.create' ;
const UPGRADE_PERMISSION = 'user.server.upgrade' ;
2023-02-06 20:16:54 +00:00
private $pterodactyl ;
public function __construct ( PterodactylSettings $ptero_settings )
{
$this -> pterodactyl = new PterodactylClient ( $ptero_settings );
}
2021-06-06 18:17:52 +00:00
/** Display a listing of the resource. */
2023-02-06 20:16:54 +00:00
public function index ( GeneralSettings $general_settings , PterodactylSettings $ptero_settings )
2021-06-05 09:26:32 +00:00
{
2021-12-21 18:41:04 +00:00
$servers = Auth :: user () -> servers ;
//Get and set server infos each server
foreach ( $servers as $server ) {
//Get server infos from ptero
2023-04-12 18:51:08 +00:00
$serverAttributes = $this -> pterodactyl -> getServerAttributes ( $server -> pterodactyl_id );
2023-05-08 09:26:47 +00:00
if ( ! $serverAttributes ) {
2023-01-05 17:01:42 +00:00
continue ;
}
2021-12-21 18:41:04 +00:00
$serverRelationships = $serverAttributes [ 'relationships' ];
$serverLocationAttributes = $serverRelationships [ 'location' ][ 'attributes' ];
//Set server infos
$server -> location = $serverLocationAttributes [ 'long' ] ?
$serverLocationAttributes [ 'long' ] :
$serverLocationAttributes [ 'short' ];
$server -> egg = $serverRelationships [ 'egg' ][ 'attributes' ][ 'name' ];
$server -> nest = $serverRelationships [ 'nest' ][ 'attributes' ][ 'name' ];
2021-12-23 13:48:04 +00:00
$server -> node = $serverRelationships [ 'node' ][ 'attributes' ][ 'name' ];
2022-08-19 19:50:04 +00:00
//Check if a server got renamed on Pterodactyl
$savedServer = Server :: query () -> where ( 'id' , $server -> id ) -> first ();
2023-01-05 17:01:42 +00:00
if ( $savedServer -> name != $serverAttributes [ 'name' ]) {
2022-08-19 19:50:04 +00:00
$savedServer -> name = $serverAttributes [ 'name' ];
$server -> name = $serverAttributes [ 'name' ];
$savedServer -> save ();
}
2021-12-21 18:41:04 +00:00
//get productname by product_id for server
2021-12-22 19:18:48 +00:00
$product = Product :: find ( $server -> product_id );
2021-12-21 18:41:04 +00:00
2021-12-22 19:18:48 +00:00
$server -> product = $product ;
2021-12-21 18:41:04 +00:00
}
2021-06-05 09:26:32 +00:00
return view ( 'servers.index' ) -> with ([
2023-01-05 17:01:42 +00:00
'servers' => $servers ,
2023-02-06 20:16:54 +00:00
'credits_display_name' => $general_settings -> credits_display_name ,
'pterodactyl_url' => $ptero_settings -> panel_url ,
'phpmyadmin_url' => $general_settings -> phpmyadmin_url
2021-06-05 09:26:32 +00:00
]);
}
2021-06-06 18:17:52 +00:00
/** Show the form for creating a new resource. */
2023-02-06 21:01:20 +00:00
public function create ( UserSettings $user_settings , ServerSettings $server_settings , GeneralSettings $general_settings )
2021-06-05 09:26:32 +00:00
{
2023-05-03 07:53:41 +00:00
$this -> checkPermission ( self :: CREATE_PERMISSION );
2023-05-08 09:26:47 +00:00
$validate_configuration = $this -> validateConfigurationRules ( $user_settings , $server_settings , $general_settings );
2023-02-03 18:03:57 +00:00
if ( ! is_null ( $validate_configuration )) {
return $validate_configuration ;
2023-01-05 17:01:42 +00:00
}
2021-06-05 09:26:32 +00:00
2021-11-07 11:07:30 +00:00
$productCount = Product :: query () -> where ( 'disabled' , '=' , false ) -> count ();
2021-11-07 15:29:18 +00:00
$locations = Location :: all ();
2021-11-12 19:01:35 +00:00
$nodeCount = Node :: query ()
-> whereHas ( 'products' , function ( Builder $builder ) {
$builder -> where ( 'disabled' , '=' , false );
}) -> count ();
$eggs = Egg :: query ()
-> whereHas ( 'products' , function ( Builder $builder ) {
$builder -> where ( 'disabled' , '=' , false );
}) -> get ();
$nests = Nest :: query ()
-> whereHas ( 'eggs' , function ( Builder $builder ) {
$builder -> whereHas ( 'products' , function ( Builder $builder ) {
$builder -> where ( 'disabled' , '=' , false );
});
}) -> get ();
2021-11-07 11:07:30 +00:00
2021-06-05 09:26:32 +00:00
return view ( 'servers.create' ) -> with ([
2021-11-07 15:29:18 +00:00
'productCount' => $productCount ,
2023-01-05 17:01:42 +00:00
'nodeCount' => $nodeCount ,
'nests' => $nests ,
'locations' => $locations ,
'eggs' => $eggs ,
'user' => Auth :: user (),
2023-02-06 20:16:54 +00:00
'server_creation_enabled' => $server_settings -> creation_enabled ,
2023-02-06 21:01:20 +00:00
'min_credits_to_make_server' => $user_settings -> min_credits_to_make_server ,
'credits_display_name' => $general_settings -> credits_display_name
2021-06-05 09:26:32 +00:00
]);
}
2021-06-26 21:56:54 +00:00
/**
2021-06-26 22:02:27 +00:00
* @ return null | RedirectResponse
2021-06-26 21:56:54 +00:00
*/
2023-05-08 09:26:47 +00:00
private function validateConfigurationRules ( UserSettings $user_settings , ServerSettings $server_settings , GeneralSettings $generalSettings )
2021-10-01 21:21:49 +00:00
{
2021-06-26 21:56:54 +00:00
//limit validation
if ( Auth :: user () -> servers () -> count () >= Auth :: user () -> server_limit ) {
2021-12-13 10:47:35 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( 'Server limit reached!' ));
2021-06-26 21:56:54 +00:00
}
2022-08-11 06:10:24 +00:00
// minimum credits && Check for Allocation
2023-01-05 17:01:42 +00:00
if ( FacadesRequest :: has ( 'product' )) {
$product = Product :: findOrFail ( FacadesRequest :: input ( 'product' ));
2022-08-11 14:55:24 +00:00
2022-08-13 20:21:55 +00:00
// Get node resource allocation info
2022-08-11 14:55:24 +00:00
$node = $product -> nodes () -> findOrFail ( FacadesRequest :: input ( 'node' ));
$nodeName = $node -> name ;
// Check if node has enough memory and disk space
2023-02-04 16:40:42 +00:00
$checkResponse = $this -> pterodactyl -> checkNodeResources ( $node , $product -> memory , $product -> disk );
2023-01-05 17:01:42 +00:00
if ( $checkResponse == false ) {
2023-02-06 13:45:44 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( " The node ' " . $nodeName . " ' doesn't have the required memory or disk left to allocate this product. " ));
2023-01-05 17:01:42 +00:00
}
2022-08-13 20:21:55 +00:00
2022-08-11 06:10:24 +00:00
// Min. Credits
2023-02-04 16:40:42 +00:00
if ( Auth :: user () -> credits < ( $product -> minimum_credits == - 1
? $user_settings -> min_credits_to_make_server
: $product -> minimum_credits )) {
2023-05-08 09:26:47 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , 'You do not have the required amount of ' . $generalSettings -> credits_display_name . ' to use this product!' );
2021-10-01 21:21:49 +00:00
}
2021-06-26 21:56:54 +00:00
}
//Required Verification for creating an server
2023-02-03 18:03:57 +00:00
if ( $user_settings -> force_email_verification && ! Auth :: user () -> hasVerifiedEmail ()) {
2023-01-05 17:01:42 +00:00
return redirect () -> route ( 'profile.index' ) -> with ( 'error' , __ ( 'You are required to verify your email address before you can create a server.' ));
2021-06-26 21:56:54 +00:00
}
2023-01-05 17:01:42 +00:00
2022-11-04 19:15:48 +00:00
//Required Verification for creating an server
2023-05-07 17:49:05 +00:00
if ( ! $server_settings -> creation_enabled && Auth :: user () -> cannot ( " admin.servers.bypass_creation_enabled " )) {
2023-01-05 17:01:42 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( 'The system administrator has blocked the creation of new servers.' ));
2022-11-03 22:15:06 +00:00
}
2021-06-26 21:56:54 +00:00
//Required Verification for creating an server
2023-02-03 18:03:57 +00:00
if ( $user_settings -> force_discord_verification && ! Auth :: user () -> discordUser ) {
2023-01-05 17:01:42 +00:00
return redirect () -> route ( 'profile.index' ) -> with ( 'error' , __ ( 'You are required to link your discord account before you can create a server.' ));
2021-06-26 21:56:54 +00:00
}
return null ;
}
2021-11-06 00:56:57 +00:00
/** Store a newly created resource in storage. */
2023-05-08 09:26:47 +00:00
public function store ( Request $request , UserSettings $user_settings , ServerSettings $server_settings , GeneralSettings $generalSettings )
2021-06-05 09:26:32 +00:00
{
2021-11-07 11:07:30 +00:00
/** @var Node $node */
/** @var Egg $egg */
/** @var Product $product */
2023-05-08 09:26:47 +00:00
$validate_configuration = $this -> validateConfigurationRules ( $user_settings , $server_settings , $generalSettings );
2023-02-04 16:40:42 +00:00
if ( ! is_null ( $validate_configuration )) {
return $validate_configuration ;
2023-01-05 17:01:42 +00:00
}
2021-11-06 00:56:57 +00:00
$request -> validate ([
2023-01-05 17:01:42 +00:00
'name' => 'required|max:191' ,
'node' => 'required|exists:nodes,id' ,
'egg' => 'required|exists:eggs,id' ,
'product' => 'required|exists:products,id' ,
2021-11-06 00:56:57 +00:00
]);
//get required resources
2021-11-07 11:07:30 +00:00
$product = Product :: query () -> findOrFail ( $request -> input ( 'product' ));
$egg = $product -> eggs () -> findOrFail ( $request -> input ( 'egg' ));
$node = $product -> nodes () -> findOrFail ( $request -> input ( 'node' ));
2021-11-07 00:47:16 +00:00
$server = $request -> user () -> servers () -> create ([
2023-01-05 17:01:42 +00:00
'name' => $request -> input ( 'name' ),
2021-11-07 00:47:16 +00:00
'product_id' => $request -> input ( 'product' ),
2022-06-16 14:15:57 +00:00
'last_billed' => Carbon :: now () -> toDateTimeString (),
2021-11-07 00:47:16 +00:00
]);
2021-11-06 00:56:57 +00:00
//get free allocation ID
2023-02-04 16:40:42 +00:00
$allocationId = $this -> pterodactyl -> getFreeAllocationId ( $node );
2023-05-08 09:26:47 +00:00
if ( ! $allocationId ) {
2023-01-05 17:01:42 +00:00
return $this -> noAllocationsError ( $server );
}
2021-11-06 00:56:57 +00:00
//create server on pterodactyl
2023-02-04 16:40:42 +00:00
$response = $this -> pterodactyl -> createServer ( $server , $egg , $allocationId );
2023-01-05 17:01:42 +00:00
if ( $response -> failed ()) {
return $this -> serverCreationFailed ( $response , $server );
}
2021-11-06 00:56:57 +00:00
2021-12-21 18:52:30 +00:00
$serverAttributes = $response -> json ()[ 'attributes' ];
2021-11-06 00:56:57 +00:00
//update server with pterodactyl_id
$server -> update ([
2021-12-21 18:52:30 +00:00
'pterodactyl_id' => $serverAttributes [ 'id' ],
2023-02-01 20:03:20 +00:00
'identifier' => $serverAttributes [ 'identifier' ],
2021-11-06 00:56:57 +00:00
]);
2022-06-16 14:15:57 +00:00
// Charge first billing cycle
$request -> user () -> decrement ( 'credits' , $server -> product -> price );
2021-06-30 21:53:28 +00:00
2021-12-13 10:47:35 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'success' , __ ( 'Server created' ));
2021-11-06 00:56:57 +00:00
}
2021-06-30 21:53:28 +00:00
/**
* return redirect with error
2023-01-05 17:01:42 +00:00
*
* @ param Server $server
2021-06-30 21:53:28 +00:00
* @ return RedirectResponse
*/
private function noAllocationsError ( Server $server )
{
$server -> delete ();
Auth :: user () -> notify ( new ServerCreationError ( $server ));
2023-01-05 17:01:42 +00:00
2021-12-13 10:47:35 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( 'No allocations satisfying the requirements for automatic deployment on this node were found.' ));
2021-06-30 21:53:28 +00:00
}
/**
* return redirect with error
2023-01-05 17:01:42 +00:00
*
* @ param Response $response
* @ param Server $server
2021-06-30 21:53:28 +00:00
* @ return RedirectResponse
*/
2021-10-01 21:21:49 +00:00
private function serverCreationFailed ( Response $response , Server $server )
2021-06-30 21:53:28 +00:00
{
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , json_encode ( $response -> json ()));
}
2021-11-06 00:56:57 +00:00
/** Remove the specified resource from storage. */
public function destroy ( Server $server )
{
try {
$server -> delete ();
2023-01-05 17:01:42 +00:00
2021-12-13 10:47:35 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'success' , __ ( 'Server removed' ));
2021-11-06 00:56:57 +00:00
} catch ( Exception $e ) {
2022-07-22 02:15:40 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( 'An exception has occurred while trying to remove a resource"' ) . $e -> getMessage () . '"' );
2022-07-22 01:52:49 +00:00
}
}
/** Cancel Server */
2023-02-06 13:45:44 +00:00
public function cancel ( Server $server )
2022-07-22 01:52:49 +00:00
{
2023-05-08 09:39:21 +00:00
if ( $server -> user_id != Auth :: user () -> id ) {
return back () -> with ( 'error' , __ ( 'This is not your Server!' ));
}
2022-07-22 01:52:49 +00:00
try {
2023-05-08 09:39:21 +00:00
$server -> update ([
'cancelled' => now (),
]);
2022-07-22 01:52:49 +00:00
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 () . '"' );
2021-11-06 00:56:57 +00:00
}
}
2022-08-03 12:34:00 +00:00
/** Show Server Settings */
2023-02-06 21:01:20 +00:00
public function show ( Server $server , ServerSettings $server_settings , GeneralSettings $general_settings )
2022-08-03 12:34:00 +00:00
{
2023-01-05 17:01:42 +00:00
if ( $server -> user_id != Auth :: user () -> id ) {
2023-02-04 16:40:42 +00:00
return back () -> with ( 'error' , __ ( 'This is not your Server!' ));
2023-01-05 17:01:42 +00:00
}
2023-02-04 16:40:42 +00:00
$serverAttributes = $this -> pterodactyl -> getServerAttributes ( $server -> pterodactyl_id );
2022-08-03 12:34:00 +00:00
$serverRelationships = $serverAttributes [ 'relationships' ];
$serverLocationAttributes = $serverRelationships [ 'location' ][ 'attributes' ];
2022-08-24 15:52:51 +00:00
//Get current product
$currentProduct = Product :: where ( 'id' , $server -> product_id ) -> first ();
2022-08-03 12:34:00 +00:00
//Set server infos
$server -> location = $serverLocationAttributes [ 'long' ] ?
$serverLocationAttributes [ 'long' ] :
$serverLocationAttributes [ 'short' ];
$server -> node = $serverRelationships [ 'node' ][ 'attributes' ][ 'name' ];
$server -> name = $serverAttributes [ 'name' ];
2022-08-03 14:40:57 +00:00
$server -> egg = $serverRelationships [ 'egg' ][ 'attributes' ][ 'name' ];
2022-08-24 15:52:51 +00:00
2023-02-04 16:40:42 +00:00
$pteroNode = $this -> pterodactyl -> getNode ( $serverRelationships [ 'node' ][ 'attributes' ][ 'id' ]);
2022-08-24 15:52:51 +00:00
2023-01-05 17:01:42 +00:00
$products = Product :: orderBy ( 'created_at' )
2023-02-06 13:45:44 +00:00
-> whereHas ( 'nodes' , function ( Builder $builder ) use ( $serverRelationships ) { //Only show products for that node
$builder -> where ( 'id' , '=' , $serverRelationships [ 'node' ][ 'attributes' ][ 'id' ]);
})
-> get ();
2022-08-03 14:40:57 +00:00
// Set the each product eggs array to just contain the eggs name
foreach ( $products as $product ) {
$product -> eggs = $product -> eggs -> pluck ( 'name' ) -> toArray ();
2023-01-05 17:01:42 +00:00
if ( $product -> memory - $currentProduct -> memory > ( $pteroNode [ 'memory' ] * ( $pteroNode [ 'memory_overallocate' ] + 100 ) / 100 ) - $pteroNode [ 'allocated_resources' ][ 'memory' ] || $product -> disk - $currentProduct -> disk > ( $pteroNode [ 'disk' ] * ( $pteroNode [ 'disk_overallocate' ] + 100 ) / 100 ) - $pteroNode [ 'allocated_resources' ][ 'disk' ]) {
$product -> doesNotFit = true ;
}
2022-08-03 14:40:57 +00:00
}
return view ( 'servers.settings' ) -> with ([
2022-08-03 12:34:00 +00:00
'server' => $server ,
2023-01-05 17:01:42 +00:00
'products' => $products ,
2023-02-06 21:01:20 +00:00
'server_enable_upgrade' => $server_settings -> enable_upgrade ,
'credits_display_name' => $general_settings -> credits_display_name
2022-08-03 12:34:00 +00:00
]);
}
public function upgrade ( Server $server , Request $request )
{
2023-05-03 07:53:41 +00:00
$this -> checkPermission ( self :: UPGRADE_PERMISSION );
2023-02-01 20:03:20 +00:00
if ( $server -> user_id != Auth :: user () -> id ) {
return redirect () -> route ( 'servers.index' );
}
2023-05-08 09:26:47 +00:00
if ( ! isset ( $request -> product_upgrade )) {
2022-08-03 12:34:00 +00:00
return redirect () -> route ( 'servers.show' , [ 'server' => $server -> id ]) -> with ( 'error' , __ ( 'this product is the only one' ));
}
$user = Auth :: user ();
$oldProduct = Product :: where ( 'id' , $server -> product -> id ) -> first ();
$newProduct = Product :: where ( 'id' , $request -> product_upgrade ) -> first ();
2023-02-04 16:40:42 +00:00
$serverAttributes = $this -> pterodactyl -> getServerAttributes ( $server -> pterodactyl_id );
2022-08-15 13:36:14 +00:00
$serverRelationships = $serverAttributes [ 'relationships' ];
2022-08-16 20:21:14 +00:00
// Get node resource allocation info
2022-08-15 13:36:14 +00:00
$nodeId = $serverRelationships [ 'node' ][ 'attributes' ][ 'id' ];
$node = Node :: where ( 'id' , $nodeId ) -> firstOrFail ();
$nodeName = $node -> name ;
// Check if node has enough memory and disk space
$requireMemory = $newProduct -> memory - $oldProduct -> memory ;
2023-01-05 17:01:42 +00:00
$requiredisk = $newProduct -> disk - $oldProduct -> disk ;
2023-02-04 16:40:42 +00:00
$checkResponse = $this -> pterodactyl -> checkNodeResources ( $node , $requireMemory , $requiredisk );
2023-01-05 17:01:42 +00:00
if ( $checkResponse == false ) {
2023-02-06 13:45:44 +00:00
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( " The node ' " . $nodeName . " ' doesn't have the required memory or disk left to upgrade the server. " ));
2023-01-05 17:01:42 +00:00
}
2022-08-15 13:36:14 +00:00
2022-11-22 12:42:46 +00:00
// calculate the amount of credits that the user overpayed for the old product when canceling the server right now
// billing periods are hourly, daily, weekly, monthly, quarterly, half-annually, annually
$billingPeriod = $oldProduct -> billing_period ;
// seconds
$billingPeriods = [
'hourly' => 3600 ,
'daily' => 86400 ,
'weekly' => 604800 ,
'monthly' => 2592000 ,
'quarterly' => 7776000 ,
'half-annually' => 15552000 ,
'annually' => 31104000
];
// Get the amount of hours the user has been using the server
$billingPeriodMultiplier = $billingPeriods [ $billingPeriod ];
$timeDifference = now () -> diffInSeconds ( $server -> last_billed );
// Calculate the price for the time the user has been using the server
$overpayedCredits = $oldProduct -> price - $oldProduct -> price * ( $timeDifference / $billingPeriodMultiplier );
2023-02-06 13:45:44 +00:00
if ( $user -> credits >= $newProduct -> price && $user -> credits >= $newProduct -> minimum_credits ) {
2022-08-03 12:34:00 +00:00
$server -> allocation = $serverAttributes [ 'allocation' ];
2023-02-04 16:40:42 +00:00
$response = $this -> pterodactyl -> updateServer ( $server , $newProduct );
2023-04-12 18:51:08 +00:00
if ( $response -> failed ()) return redirect () -> route ( 'servers.index' ) -> with ( 'error' , __ ( " The system was unable to update your server product. Please try again later or contact support. " ));
2023-02-01 20:03:20 +00:00
//update user balance
$user -> decrement ( 'credits' , $priceupgrade );
2023-01-21 14:04:12 +00:00
//restart the server
2023-02-04 16:40:42 +00:00
$response = $this -> pterodactyl -> powerAction ( $server , 'restart' );
2023-02-01 20:03:20 +00:00
if ( $response -> failed ()) {
return redirect () -> route ( 'servers.index' ) -> with ( 'error' , $response -> json ()[ 'errors' ][ 0 ][ 'detail' ]);
}
2022-11-22 12:42:46 +00:00
// Remove the allocation property from the server object as it is not a column in the database
unset ( $server -> allocation );
// Update the server on controlpanel
$server -> update ([
'product_id' => $newProduct -> id ,
'updated_at' => now (),
'last_billed' => now (),
'cancelled' => null ,
]);
2023-02-01 20:03:20 +00:00
2022-11-22 12:42:46 +00:00
// Refund the user the overpayed credits
if ( $overpayedCredits > 0 ) $user -> increment ( 'credits' , $overpayedCredits );
// Withdraw the credits for the new product
2023-02-06 13:45:44 +00:00
$user -> decrement ( 'credits' , $newProduct -> price );
2022-11-22 12:42:46 +00:00
//restart the server
$response = Pterodactyl :: powerAction ( $server , " restart " );
2023-02-06 13:45:44 +00:00
if ( $response -> failed ()) return redirect () -> route ( 'servers.index' ) -> with ( 'error' , 'Server upgraded successfully! Could not restart the server: ' . $response -> json ()[ 'errors' ][ 0 ][ 'detail' ]);
2022-08-03 12:34:00 +00:00
return redirect () -> route ( 'servers.show' , [ 'server' => $server -> id ]) -> with ( 'success' , __ ( 'Server Successfully Upgraded' ));
2023-01-05 17:01:42 +00:00
} else {
2022-08-03 12:34:00 +00:00
return redirect () -> route ( 'servers.show' , [ 'server' => $server -> id ]) -> with ( 'error' , __ ( 'Not Enough Balance for Upgrade' ));
}
}
2021-06-05 09:26:32 +00:00
}