Fixed Upgrade/Downgrade Credit withdrawal

This commit is contained in:
IceToast 2022-11-22 13:42:46 +01:00 committed by IceToast
parent 56aad2a7cb
commit 94cbea528c
No known key found for this signature in database
GPG key ID: 1464353E063A5B97
2 changed files with 55 additions and 32 deletions

View file

@ -9,6 +9,8 @@ use App\Models\Nest;
use App\Models\Node;
use App\Models\Product;
use App\Models\Server;
use App\Models\User;
use App\Models\Settings;
use App\Notifications\ServerCreationError;
use Carbon\Carbon;
use Exception;
@ -231,8 +233,6 @@ class ServerController extends Controller
*/
private function serverCreationFailed(Response $response, Server $server)
{
$server->delete();
return redirect()->route('servers.index')->with('error', json_encode($response->json()));
}
@ -265,9 +265,9 @@ class ServerController extends Controller
/** Show Server Settings */
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);
$serverRelationships = $serverAttributes['relationships'];
$serverLocationAttributes = $serverRelationships['location']['attributes'];
@ -308,10 +308,9 @@ class ServerController extends Controller
public function upgrade(Server $server, Request $request)
{
if ($server->user_id != Auth::user()->id) {
return redirect()->route('servers.index');
}
if (! isset($request->product_upgrade)) {
if($server->user_id != Auth::user()->id || $server->suspended) return redirect()->route('servers.index');
if(!isset($request->product_upgrade))
{
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('this product is the only one'));
}
$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."));
}
$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()) {
$priceupgrade = 0;
}
if ($user->credits >= $priceupgrade && $user->credits >= $newProduct->minimum_credits) {
$server->product_id = $request->product_upgrade;
$server->update();
// Calculate the price for the time the user has been using the server
$overpayedCredits = $oldProduct->price - $oldProduct->price * ($timeDifference / $billingPeriodMultiplier);
if ($user->credits >= $newProduct->price && $user->credits >= $newProduct->minimum_credits)
{
$server->allocation = $serverAttributes['allocation'];
// Update the server on the panel
$response = Pterodactyl::updateServer($server, $newProduct);
if ($response->failed()) {
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']);
}
if ($response->failed()) return $this->serverCreationFailed($response, $server);
// 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'));
} else {
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('Not Enough Balance for Upgrade'));

View file

@ -243,10 +243,7 @@
</button>
</div>
<div class="modal-body card-body">
<strong>{{__("FOR DOWNGRADE PLEASE CHOOSE A PLAN BELOW YOUR PLAN")}}</strong>
<br>
<br>
<strong>{{__("YOUR PRODUCT")}} : </strong> {{ $server->product->name }}
<strong>{{__("Current Product")}}: </strong> {{ $server->product->name }}
<br>
<br>
@ -256,12 +253,13 @@
<option value="">{{__("Select the product")}}</option>
@foreach($products as $product)
@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>
@endif
@endforeach
</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")}}
</div>
<div class="modal-footer card-body">