Fixed Upgrade/Downgrade Credit withdrawal
This commit is contained in:
parent
56aad2a7cb
commit
94cbea528c
|
@ -9,6 +9,8 @@ use App\Models\Nest;
|
||||||
use App\Models\Node;
|
use App\Models\Node;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\Settings;
|
||||||
use App\Notifications\ServerCreationError;
|
use App\Notifications\ServerCreationError;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
@ -231,8 +233,6 @@ class ServerController extends Controller
|
||||||
*/
|
*/
|
||||||
private function serverCreationFailed(Response $response, Server $server)
|
private function serverCreationFailed(Response $response, Server $server)
|
||||||
{
|
{
|
||||||
$server->delete();
|
|
||||||
|
|
||||||
return redirect()->route('servers.index')->with('error', json_encode($response->json()));
|
return redirect()->route('servers.index')->with('error', json_encode($response->json()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,9 +265,9 @@ class ServerController extends Controller
|
||||||
/** Show Server Settings */
|
/** Show Server Settings */
|
||||||
public function show(Server $server)
|
public function show(Server $server)
|
||||||
{
|
{
|
||||||
if ($server->user_id != Auth::user()->id) {
|
|
||||||
return back()->with('error', __('´This is not your Server!'));
|
|
||||||
}
|
if($server->user_id != Auth::user()->id){ return back()->with('error', __('This is not your Server!'));}
|
||||||
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
|
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
|
||||||
$serverRelationships = $serverAttributes['relationships'];
|
$serverRelationships = $serverAttributes['relationships'];
|
||||||
$serverLocationAttributes = $serverRelationships['location']['attributes'];
|
$serverLocationAttributes = $serverRelationships['location']['attributes'];
|
||||||
|
@ -308,10 +308,9 @@ class ServerController extends Controller
|
||||||
|
|
||||||
public function upgrade(Server $server, Request $request)
|
public function upgrade(Server $server, Request $request)
|
||||||
{
|
{
|
||||||
if ($server->user_id != Auth::user()->id) {
|
if($server->user_id != Auth::user()->id || $server->suspended) return redirect()->route('servers.index');
|
||||||
return redirect()->route('servers.index');
|
if(!isset($request->product_upgrade))
|
||||||
}
|
{
|
||||||
if (! isset($request->product_upgrade)) {
|
|
||||||
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('this product is the only one'));
|
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('this product is the only one'));
|
||||||
}
|
}
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
@ -333,27 +332,53 @@ class ServerController extends Controller
|
||||||
return redirect()->route('servers.index')->with('error', __("The node '".$nodeName."' doesn't have the required memory or disk left to upgrade the server."));
|
return redirect()->route('servers.index')->with('error', __("The node '".$nodeName."' doesn't have the required memory or disk left to upgrade the server."));
|
||||||
}
|
}
|
||||||
|
|
||||||
$priceupgrade = $newProduct->getHourlyPrice();
|
// 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);
|
||||||
|
|
||||||
if ($priceupgrade < $oldProduct->getHourlyPrice()) {
|
// Calculate the price for the time the user has been using the server
|
||||||
$priceupgrade = 0;
|
$overpayedCredits = $oldProduct->price - $oldProduct->price * ($timeDifference / $billingPeriodMultiplier);
|
||||||
}
|
|
||||||
if ($user->credits >= $priceupgrade && $user->credits >= $newProduct->minimum_credits) {
|
|
||||||
$server->product_id = $request->product_upgrade;
|
if ($user->credits >= $newProduct->price && $user->credits >= $newProduct->minimum_credits)
|
||||||
$server->update();
|
{
|
||||||
$server->allocation = $serverAttributes['allocation'];
|
$server->allocation = $serverAttributes['allocation'];
|
||||||
|
// Update the server on the panel
|
||||||
$response = Pterodactyl::updateServer($server, $newProduct);
|
$response = Pterodactyl::updateServer($server, $newProduct);
|
||||||
if ($response->failed()) {
|
if ($response->failed()) return $this->serverCreationFailed($response, $server);
|
||||||
return $this->serverCreationFailed($response, $server);
|
|
||||||
}
|
|
||||||
//update user balance
|
|
||||||
$user->decrement('credits', $priceupgrade);
|
|
||||||
//restart the server
|
|
||||||
$response = Pterodactyl::powerAction($server, 'restart');
|
|
||||||
if ($response->failed()) {
|
|
||||||
return redirect()->route('servers.index')->with('error', $response->json()['errors'][0]['detail']);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Refund the user the overpayed credits
|
||||||
|
if ($overpayedCredits > 0) $user->increment('credits', $overpayedCredits);
|
||||||
|
|
||||||
|
// Withdraw the credits for the new product
|
||||||
|
$user->decrement('credits', $newProduct->price);
|
||||||
|
|
||||||
|
//restart the server
|
||||||
|
$response = Pterodactyl::powerAction($server, "restart");
|
||||||
|
if ($response->failed()) return redirect()->route('servers.index')->with('error', 'Server upgraded successfully! Could not restart the server: '.$response->json()['errors'][0]['detail']);
|
||||||
return redirect()->route('servers.show', ['server' => $server->id])->with('success', __('Server Successfully Upgraded'));
|
return redirect()->route('servers.show', ['server' => $server->id])->with('success', __('Server Successfully Upgraded'));
|
||||||
} else {
|
} else {
|
||||||
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('Not Enough Balance for Upgrade'));
|
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('Not Enough Balance for Upgrade'));
|
||||||
|
|
|
@ -243,10 +243,7 @@
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body card-body">
|
<div class="modal-body card-body">
|
||||||
<strong>{{__("FOR DOWNGRADE PLEASE CHOOSE A PLAN BELOW YOUR PLAN")}}</strong>
|
<strong>{{__("Current Product")}}: </strong> {{ $server->product->name }}
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<strong>{{__("YOUR PRODUCT")}} : </strong> {{ $server->product->name }}
|
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
@ -256,12 +253,13 @@
|
||||||
<option value="">{{__("Select the product")}}</option>
|
<option value="">{{__("Select the product")}}</option>
|
||||||
@foreach($products as $product)
|
@foreach($products as $product)
|
||||||
@if(in_array($server->egg, $product->eggs) && $product->id != $server->product->id && $product->disabled == false)
|
@if(in_array($server->egg, $product->eggs) && $product->id != $server->product->id && $product->disabled == false)
|
||||||
<option value="{{ $product->id }}" @if($product->doesNotFit)disabled @endif>{{ $product->name }} [ {{ CREDITS_DISPLAY_NAME }} {{ $product->price }} @if($product->doesNotFit)] {{__('Server can´t fit on this node')}} @else @if($product->minimum_credits!=-1) /
|
<option value="{{ $product->id }}" @if($product->doesNotFit)disabled @endif>{{ $product->name }} [ {{ CREDITS_DISPLAY_NAME }} {{ $product->price }} @if($product->doesNotFit)] {{__('Server can\'t fit on this node')}} @else @if($product->minimum_credits!=-1) /
|
||||||
{{__("Required")}}: {{$product->minimum_credits}} {{ CREDITS_DISPLAY_NAME }}@endif ] @endif</option>
|
{{__("Required")}}: {{$product->minimum_credits}} {{ CREDITS_DISPLAY_NAME }}@endif ] @endif</option>
|
||||||
@endif
|
@endif
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
<br> {{__("Once the Upgrade button is pressed, we will automatically deduct the amount for the first hour according to the new product from your credits")}}. <br>
|
|
||||||
|
<br> <strong>{{__("Caution") }}:</strong> {{__("Upgrading/Downgrading your server will reset your billing cycle to now. Your overpayed Credits will be refunded. The price for the new billing cycle will be withdrawed")}}. <br>
|
||||||
<br> {{__("Server will be automatically restarted once upgraded")}}
|
<br> {{__("Server will be automatically restarted once upgraded")}}
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer card-body">
|
<div class="modal-footer card-body">
|
||||||
|
|
Loading…
Reference in a new issue