Skip to content
13 changes: 6 additions & 7 deletions app/Extensions/PaymentGateways/Stripe/StripeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public static function getConfig(): array
];
}

public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, string $totalPriceString): string
public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, int $totalPrice): string
{
// check if the total price is valid for stripe
$totalPriceNumber = floatval($totalPriceString);
if (!self::checkPriceAmount($totalPriceNumber, strtoupper($shopProduct->currency_code), 'stripe')) {
if (!self::checkPriceAmount(floatval($totalPrice), strtoupper($shopProduct->currency_code), 'stripe')) {
throw new Exception('Invalid price amount');
}

Expand All @@ -61,7 +60,7 @@ public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct
'name' => $shopProduct->display,
'description' => $shopProduct->description,
],
'unit_amount_decimal' => self::convertAmount($totalPriceString, $shopProduct->currency_code),
'unit_amount_decimal' => self::convertAmount($totalPrice, $shopProduct->currency_code),
],
'quantity' => 1,
],
Expand Down Expand Up @@ -377,13 +376,13 @@ public static function checkPriceAmount(float $amount, string $currencyCode, st
protected static function convertAmount(float $amount, string $currency): int
{
if (in_array($currency, self::ZERO_DECIMAL_CURRENCIES, true)) {
return $amount;
return $amount / 1000;
}

if (in_array($currency, self::THREE_DECIMAL_CURRENCIES, true)) {
return $amount * 1000;
return $amount;
}

return $amount * 100;
return $amount / 10;
}
}
13 changes: 13 additions & 0 deletions app/Facades/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Currency extends Facade
{
protected static function getFacadeAccessor()
{
return 'currency';
}
}
26 changes: 26 additions & 0 deletions app/Helpers/CurrencyHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Helpers;

class CurrencyHelper
{
public function convertForDisplay($amount)
{
return $amount / 1000;
}

public function formatForDisplay($amount, $decimals = 2)
{
return number_format($this->convertForDisplay($amount), $decimals, '.', ',');
}

public function formatForForm($amount, $decimals = 2)
{
return number_format($this->convertForDisplay($amount), $decimals, '.', '');
}

public function prepareForDatabase($amount)
{
return (int)($amount * 1000);
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/Admin/OverViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin;

use App\Classes\PterodactylClient;
use App\Helpers\CurrencyHelper;
use App\Settings\PterodactylSettings;
use App\Settings\GeneralSettings;
use App\Http\Controllers\Controller;
Expand Down Expand Up @@ -30,7 +31,7 @@ public function __construct(PterodactylSettings $ptero_settings)
$this->pterodactyl = new PterodactylClient($ptero_settings);
}

public function index(GeneralSettings $general_settings)
public function index(GeneralSettings $general_settings, CurrencyHelper $currencyHelper)
{
$this->checkAnyPermission([self::READ_PERMISSION,self::SYNC_PERMISSION]);

Expand Down
30 changes: 14 additions & 16 deletions app/Http/Controllers/Admin/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Events\CouponUsedEvent;
use App\Events\PaymentEvent;
use App\Events\UserUpdateCreditsEvent;
use App\Helpers\CurrencyHelper;
use App\Http\Controllers\Controller;
use App\Models\Invoice;
use App\Models\PartnerDiscount;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function index(LocaleSettings $locale_settings)
* @param ShopProduct $shopProduct
* @return Application|Factory|View
*/
public function checkOut(ShopProduct $shopProduct, GeneralSettings $general_settings, CouponSettings $coupon_settings)
public function checkOut(ShopProduct $shopProduct, GeneralSettings $general_settings, CouponSettings $coupon_settings, CurrencyHelper $currencyHelper)
{
$this->checkPermission(self::BUY_PERMISSION);

Expand Down Expand Up @@ -85,7 +86,7 @@ public function checkOut(ShopProduct $shopProduct, GeneralSettings $general_sett
'discountpercent' => $discount,
'discountvalue' => $discount * $shopProduct->price / 100,
'discountedprice' => $shopProduct->getPriceAfterDiscount(),
'taxvalue' => $shopProduct->getTaxValue(),
'taxvalue' => $currencyHelper->formatForDisplay($shopProduct->getTaxValue()),
'taxpercent' => $shopProduct->getTaxPercent(),
'total' => $shopProduct->getTotalPrice(),
'paymentGateways' => $paymentGateways,
Expand Down Expand Up @@ -156,11 +157,6 @@ public function pay(Request $request)
return $this->handleFreeProduct($shopProduct);
}

// Format the total price to a readable string
$totalPriceString = number_format($subtotal, 2, '.', '');
//reset the price after coupon use
$shopProduct->price = $totalPriceString;

// create a new payment
$payment = Payment::create([
'user_id' => $user->id,
Expand All @@ -172,13 +168,13 @@ public function pay(Request $request)
'price' => $shopProduct->price,
'tax_value' => $shopProduct->getTaxValue(),
'tax_percent' => $shopProduct->getTaxPercent(),
'total_price' => $totalPriceString,
'total_price' => $subtotal,
'currency_code' => $shopProduct->currency_code,
'shop_item_product_id' => $shopProduct->id,
]);

$paymentGatewayExtension = ExtensionHelper::getExtensionClass($paymentGateway);
$redirectUrl = $paymentGatewayExtension::getRedirectUrl($payment, $shopProduct, $totalPriceString);
$redirectUrl = $paymentGatewayExtension::getRedirectUrl($payment, $shopProduct, $subtotal);

} catch (Exception $e) {
Log::error($e->getMessage());
Expand Down Expand Up @@ -210,19 +206,21 @@ public function dataTable()
->addColumn('user', function (Payment $payment) {
return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
})
->editColumn('price', function (Payment $payment) {
return $payment->formatToCurrency($payment->price);
->editColumn('amount', function (Payment $payment, CurrencyHelper $currencyHelper) {
return $payment->type == 'Credits' ? $currencyHelper->formatForDisplay($payment->amount) : $payment->amount;
})
->editColumn('price', function (Payment $payment, CurrencyHelper $currencyHelper) {
return $payment->formatToCurrency($currencyHelper->formatForDisplay($payment->price));
})
->editColumn('tax_value', function (Payment $payment) {
return $payment->formatToCurrency($payment->tax_value);
->editColumn('tax_value', function (Payment $payment, CurrencyHelper $currencyHelper) {
return $payment->formatToCurrency($currencyHelper->formatForDisplay($payment->tax_value));
})
->editColumn('tax_percent', function (Payment $payment) {
return $payment->tax_percent . ' %';
})
->editColumn('total_price', function (Payment $payment) {
return $payment->formatToCurrency($payment->total_price);
->editColumn('total_price', function (Payment $payment, CurrencyHelper $currencyHelper) {
return $payment->formatToCurrency($currencyHelper->formatForDisplay($payment->total_price));
})

->editColumn('created_at', function (Payment $payment) {
return [
'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Admin/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Admin;

use App\Helpers\CurrencyHelper;
use App\Http\Controllers\Controller;
use App\Models\Pterodactyl\Location;
use App\Models\Pterodactyl\Nest;
Expand Down Expand Up @@ -266,6 +267,9 @@ public function dataTable()
</form>
';
})
->editColumn('price', function (Product $product, CurrencyHelper $currencyHelper) {
return $currencyHelper->formatForDisplay($product->price);
})
->editColumn('minimum_credits', function (Product $product, UserSettings $user_settings) {
return $product->minimum_credits==-1 ? $user_settings->min_credits_to_make_server : $product->minimum_credits;
})
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Admin/ShopProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Admin;

use App\Helpers\CurrencyHelper;
use App\Http\Controllers\Controller;
use App\Models\ShopProduct;
use App\Settings\GeneralSettings;
Expand Down Expand Up @@ -182,8 +183,8 @@ public function dataTable(Request $request)
->editColumn('created_at', function (ShopProduct $shopProduct) {
return $shopProduct->created_at ? $shopProduct->created_at->diffForHumans() : '';
})
->editColumn('price', function (ShopProduct $shopProduct) {
return $shopProduct->formatToCurrency($shopProduct->price);
->editColumn('price', function (ShopProduct $shopProduct, CurrencyHelper $currencyHelper) {
return $shopProduct->formatToCurrency($currencyHelper->formatForDisplay($shopProduct->price));
})
->rawColumns(['actions', 'disabled'])
->make();
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Settings\LocaleSettings;
use App\Settings\PterodactylSettings;
use App\Classes\PterodactylClient;
use App\Helpers\CurrencyHelper;
use App\Settings\GeneralSettings;
use Exception;
use Illuminate\Contracts\Foundation\Application;
Expand Down Expand Up @@ -409,8 +410,8 @@ public function dataTable(Request $request)
->addColumn('avatar', function (User $user) {
return '<img width="28px" height="28px" class="ml-1 rounded-circle" src="' . $user->getAvatar() . '">';
})
->addColumn('credits', function (User $user) {
return '<i class="mr-2 fas fa-coins"></i> ' . $user->credits();
->addColumn('credits', function (User $user, CurrencyHelper $currencyHelper) {
return '<i class="mr-2 fas fa-coins"></i> ' . $currencyHelper->formatForDisplay($user->credits);
})
->addColumn('verified', function (User $user) {
return $user->getVerifiedStatus();
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Controllers/Admin/VoucherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin;

use App\Events\UserUpdateCreditsEvent;
use App\Helpers\CurrencyHelper;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Voucher;
Expand Down Expand Up @@ -202,8 +203,8 @@ public function usersDataTable(Voucher $voucher)
->editColumn('name', function (User $user) {
return '<a class="text-info" target="_blank" href="'.route('admin.users.show', $user->id).'">'.$user->name.'</a>';
})
->addColumn('credits', function (User $user) {
return '<i class="mr-2 fas fa-coins"></i> '.$user->credits();
->addColumn('credits', function (User $user, CurrencyHelper $currencyHelper) {
return '<i class="mr-2 fas fa-coins"></i> '. $currencyHelper->formatForDisplay($user->credits);
})
->addColumn('last_seen', function (User $user) {
return $user->last_seen ? $user->last_seen->diffForHumans() : '';
Expand Down Expand Up @@ -245,8 +246,8 @@ public function dataTable()
->editColumn('uses', function (Voucher $voucher) {
return "{$voucher->used} / {$voucher->uses}";
})
->editColumn('credits', function (Voucher $voucher) {
return number_format($voucher->credits, 2, '.', '');
->editColumn('credits', function (Voucher $voucher, CurrencyHelper $currencyHelper) {
return $currencyHelper->formatForDisplay($voucher->credits);
})
->editColumn('expires_at', function (Voucher $voucher) {
if (! $voucher->expires_at) {
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public function getTimeLeftBoxText(float $daysLeft, float $hoursLeft)
public function index(GeneralSettings $general_settings, WebsiteSettings $website_settings, ReferralSettings $referral_settings)
{
$usage = Auth::user()->creditUsage();
$credits = Auth::user()->Credits();
$credits = Auth::user()->credits;
$bg = '';
$boxText = '';
$unit = '';

/** Build our Time-Left-Box */
if ($credits > 0.01 and $usage > 0) {
if ($credits > 10 && $usage > 0) {
$daysLeft = number_format($credits / ($usage / 30), 2, '.', '');
$hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ private function createServer(Request $request): ?Server

private function handlePostCreation(User $user, Server $server): void
{
logger('Product Price: ' . $server->product->price);

$user->decrement('credits', $server->product->price);

try {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/StoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function index(UserSettings $user_settings, GeneralSettings $general_sett
}

return view('store.index')->with([
'products' => ShopProduct::where('disabled', '=', false)->orderBy('type', 'asc')->orderBy('price', 'asc')->get(),
'shopProducts' => ShopProduct::where('disabled', '=', false)->orderBy('type', 'asc')->orderBy('price', 'asc')->get(),
'isStoreEnabled' => $isStoreEnabled,
'credits_display_name' => $general_settings->credits_display_name
]);
Expand Down
11 changes: 8 additions & 3 deletions app/Listeners/UserPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\PaymentStatus;
use App\Events\PaymentEvent;
use App\Facades\Currency;
use App\Models\User;
use App\Settings\DiscordSettings;
use App\Models\PartnerDiscount;
Expand Down Expand Up @@ -58,15 +59,16 @@ public function __construct(UserSettings $user_settings, ReferralSettings $refer
public function handle(PaymentEvent $event)
{
$user = $event->user;
$payment = $event->payment;
$shopProduct = $event->shopProduct;

// only update user if payment is paid
if ($event->payment->status != PaymentStatus::PAID) {
if ($payment->status != PaymentStatus::PAID) {
return;
}

//update server limit
if (!$user->email_verified_reward && $this->server_limit_increment_after_irl_purchase !== 0) {
if (!$user->email_verified_reward && $this->server_limit_increment_after_irl_purchase !== 0 && $user->server_limit < $this->server_limit_increment_after_irl_purchase) {
$user->increment('server_limit', $this->server_limit_increment_after_irl_purchase);
}

Expand Down Expand Up @@ -130,6 +132,9 @@ public function handle(PaymentEvent $event)
activity()
->performedOn($user)
->causedBy($user)
->log('bought ' . $shopProduct->quantity . ' ' . $shopProduct->type . ' for ' . $shopProduct->price . $shopProduct->currency_code);
->log($payment->type == 'Credits'
? 'bought ' . Currency::formatForDisplay($payment->amount) . ' ' . $payment->type . ' for ' . Currency::formatForDisplay($payment->total_price) . $payment->currency_code
: 'bought ' . $payment->amount . ' ' . $shopProduct->type . ' for ' . Currency::formatForDisplay($payment->total_price) . $payment->currency_code
);
}
}
Loading