From 5ae631ee4f7fa2b7f8bc8e8ef18839b4801aacf6 Mon Sep 17 00:00:00 2001 From: IceToast Date: Sun, 29 Jan 2023 23:12:34 +0100 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20Move=20ref?= =?UTF-8?q?erral=5Fcode=20generation=20to=20trait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/MakeUserCommand.php | 17 +++--------- app/Http/Controllers/Api/UserController.php | 26 +++++-------------- .../Controllers/Auth/RegisterController.php | 18 ++----------- app/Traits/Referral.php | 23 ++++++++++++++++ .../2022_06_02_081655_referral_code.php | 18 ++++++------- 5 files changed, 44 insertions(+), 58 deletions(-) create mode 100644 app/Traits/Referral.php diff --git a/app/Console/Commands/MakeUserCommand.php b/app/Console/Commands/MakeUserCommand.php index f9b316f3..d579a877 100644 --- a/app/Console/Commands/MakeUserCommand.php +++ b/app/Console/Commands/MakeUserCommand.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use App\Classes\Pterodactyl; use App\Models\User; +use App\Traits\Referral; use Illuminate\Console\Command; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; @@ -11,6 +12,8 @@ use Illuminate\Support\Str; class MakeUserCommand extends Command { + use Referral; + /** * The name and signature of the console command. * @@ -38,18 +41,6 @@ class MakeUserCommand extends Command $this->pterodactyl = $pterodactyl; } - /** - * @param $userid - * @return string - */ - public function generateCode(){ - $random = STR::random(8); - if (User::where('referral_code', '=', $random)->doesntExist()) { - return $random; - } else { - $this->generateCode(); - } - } /** * Execute the console command. @@ -97,7 +88,7 @@ class MakeUserCommand extends Command 'email' => $response['email'], 'role' => 'admin', 'password' => Hash::make($password), - 'referral_code' => $this->generateCode(), + 'referral_code' => $this->createReferralCode(), 'pterodactyl_id' => $response['id'], ]); diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index dab72dba..68b0f948 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -8,6 +8,7 @@ use App\Http\Controllers\Controller; use App\Models\DiscordUser; use App\Models\User; use App\Notifications\ReferralNotification; +use App\Traits\Referral; use Carbon\Carbon; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Pagination\LengthAwarePaginator; @@ -27,6 +28,8 @@ use Spatie\QueryBuilder\QueryBuilder; class UserController extends Controller { + use Referral; + const ALLOWED_INCLUDES = ['servers', 'notifications', 'payments', 'vouchers', 'discordUser']; const ALLOWED_FILTERS = ['name', 'server_limit', 'email', 'pterodactyl_id', 'role', 'suspended']; @@ -92,7 +95,7 @@ class UserController extends Controller //Update Users Password on Pterodactyl //Username,Mail,First and Lastname are required aswell - $response = Pterodactyl::client()->patch('/application/users/'.$user->pterodactyl_id, [ + $response = Pterodactyl::client()->patch('/application/users/' . $user->pterodactyl_id, [ 'username' => $request->name, 'first_name' => $request->name, 'last_name' => $request->name, @@ -229,7 +232,7 @@ class UserController extends Controller $discordUser = DiscordUser::find($id); $user = $discordUser ? $discordUser->user : User::findOrFail($id); - if (! $user->isSuspended()) { + if (!$user->isSuspended()) { throw ValidationException::withMessages([ 'error' => 'You cannot unsuspend an User who is not suspended.', ]); @@ -240,21 +243,6 @@ class UserController extends Controller return $user; } - /** - * Create a unique Referral Code for User - * - * @return string - */ - protected function createReferralCode() - { - $referralcode = STR::random(8); - if (User::where('referral_code', '=', $referralcode)->exists()) { - $this->createReferralCode(); - } - - return $referralcode; - } - /** * @throws ValidationException */ @@ -267,7 +255,7 @@ class UserController extends Controller ]); // Prevent the creation of new users via API if this is enabled. - if (! config('SETTINGS::SYSTEM:CREATION_OF_NEW_USERS', 'true')) { + if (!config('SETTINGS::SYSTEM:CREATION_OF_NEW_USERS', 'true')) { throw ValidationException::withMessages([ 'error' => 'The creation of new users has been blocked by the system administrator.', ]); @@ -305,7 +293,7 @@ class UserController extends Controller 'pterodactyl_id' => $response->json()['attributes']['id'], ]); //INCREMENT REFERRAL-USER CREDITS - if (! empty($request->input('referral_code'))) { + if (!empty($request->input('referral_code'))) { $ref_code = $request->input('referral_code'); $new_user = $user->id; if ($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) { diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index edcc4960..ec678ace 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller; use App\Models\User; use App\Notifications\ReferralNotification; use App\Providers\RouteServiceProvider; +use App\Traits\Referral; use Carbon\Carbon; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\App; @@ -30,7 +31,7 @@ class RegisterController extends Controller | */ - use RegistersUsers; + use RegistersUsers, Referral; /** * Where to redirect users after registration. @@ -87,21 +88,6 @@ class RegisterController extends Controller return Validator::make($data, $validationRules); } - /** - * Create a unique Referral Code for User - * - * @return string - */ - protected function createReferralCode() - { - $referralcode = STR::random(8); - if (User::where('referral_code', '=', $referralcode)->exists()) { - $this->createReferralCode(); - } - - return $referralcode; - } - /** * Create a new user instance after a valid registration. * diff --git a/app/Traits/Referral.php b/app/Traits/Referral.php new file mode 100644 index 00000000..ad61e902 --- /dev/null +++ b/app/Traits/Referral.php @@ -0,0 +1,23 @@ +exists()) { + // if exists, generate another code + return $this->generateReferralCode(); + } + return $code; + } +} diff --git a/database/migrations/2022_06_02_081655_referral_code.php b/database/migrations/2022_06_02_081655_referral_code.php index 8c86d2ec..e75079cf 100644 --- a/database/migrations/2022_06_02_081655_referral_code.php +++ b/database/migrations/2022_06_02_081655_referral_code.php @@ -1,6 +1,7 @@ doesntExist()) { - DB::table('users') - ->where('id', '=', $userid) - ->update(['referral_code' => $random]); - } else { - $this->generateCode($userid); - } + $code = $this->createReferralCode(); + DB::table('users') + ->where('id', '=', $userid) + ->update(['referral_code' => $code]); } /** @@ -35,7 +33,7 @@ return new class extends Migration $existing_user = User::where('referral_code', '')->orWhere('referral_code', null)->get(); foreach ($existing_user as $user) { - $this->generateCode($user->id); + $this->setReferralCode($user->id); } } From ff2d0b794e01a187b20e1099d021136c2126660c Mon Sep 17 00:00:00 2001 From: IceToast Date: Mon, 30 Jan 2023 22:34:06 +0100 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20handle=20free=20produ?= =?UTF-8?q?cts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/PaymentController.php | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index b6d5b003..4fdaf2f9 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Events\PaymentEvent; use App\Events\UserUpdateCreditsEvent; use App\Http\Controllers\Controller; use App\Models\PartnerDiscount; @@ -71,29 +72,12 @@ class PaymentController extends Controller * @param ShopProduct $shopProduct * @return RedirectResponse */ - public function FreePay(ShopProduct $shopProduct) + public function handleFreeProduct(ShopProduct $shopProduct) { - //check if the product is really free or the discount is 100% - if ($shopProduct->getTotalPrice() > 0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.')); - - //give product /** @var User $user */ $user = Auth::user(); - //not updating server limit - - //update User with bought item - if ($shopProduct->type == "Credits") { - $user->increment('credits', $shopProduct->quantity); - } elseif ($shopProduct->type == "Server slots") { - $user->increment('server_limit', $shopProduct->quantity); - } - - //skipped the referral commission, because the user did not pay anything. - - //not giving client role - - //store payment + //create a payment $payment = Payment::create([ 'user_id' => $user->id, 'payment_id' => uniqid(), @@ -110,6 +94,7 @@ class PaymentController extends Controller ]); event(new UserUpdateCreditsEvent($user)); + event(new PaymentEvent($user, $payment, $shopProduct)); //not sending an invoice @@ -122,6 +107,12 @@ class PaymentController extends Controller $product = ShopProduct::find($request->product_id); $paymentGateway = $request->payment_method; + // on free products, we don't need to use a payment gateway + $realPrice = $product->price - ($product->price * PartnerDiscount::getDiscount() / 100); + if ($realPrice <= 0) { + return $this->handleFreeProduct($product); + } + return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]); } From a0770926315c9247f448d545fc8cafe0db82ba07 Mon Sep 17 00:00:00 2001 From: IceToast Date: Mon, 30 Jan 2023 22:44:48 +0100 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=E2=9C=A8=20Allow=20to=20buy=20a=20?= =?UTF-8?q?product=20for=20free?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/PaymentController.php | 29 ++++++++++++------- themes/default/views/store/checkout.blade.php | 10 +++++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index 4fdaf2f9..0cf2e399 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -39,21 +39,29 @@ class PaymentController extends Controller */ public function checkOut(ShopProduct $shopProduct) { - $extensions = ExtensionHelper::getAllExtensionsByNamespace('PaymentGateways'); + $discount = PartnerDiscount::getDiscount(); + $price = $shopProduct->price - ($shopProduct->price * $discount / 100); - // build a paymentgateways array that contains the routes for the payment gateways and the image path for the payment gateway which lays in public/images/Extensions/PaymentGateways with the extensionname in lowercase $paymentGateways = []; - foreach ($extensions as $extension) { - $extensionName = basename($extension); - if (!ExtensionHelper::getExtensionConfig($extensionName, 'enabled')) continue; // skip if not enabled + if ($price > 0) { + $extensions = ExtensionHelper::getAllExtensionsByNamespace('PaymentGateways'); - $payment = new \stdClass(); - $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name'); - $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png'); - $paymentGateways[] = $payment; + // build a paymentgateways array that contains the routes for the payment gateways and the image path for the payment gateway which lays in public/images/Extensions/PaymentGateways with the extensionname in lowercase + foreach ($extensions as $extension) { + $extensionName = basename($extension); + if (!ExtensionHelper::getExtensionConfig($extensionName, 'enabled')) continue; // skip if not enabled + + $payment = new \stdClass(); + $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name'); + $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png'); + $paymentGateways[] = $payment; + } } - $discount = PartnerDiscount::getDiscount(); + + + + return view('store.checkout')->with([ 'product' => $shopProduct, @@ -64,6 +72,7 @@ class PaymentController extends Controller 'taxpercent' => $shopProduct->getTaxPercent(), 'total' => $shopProduct->getTotalPrice(), 'paymentGateways' => $paymentGateways, + 'productIsFree' => $price <= 0, ]); } diff --git a/themes/default/views/store/checkout.blade.php b/themes/default/views/store/checkout.blade.php index 2b6cc796..4ce7c5ee 100644 --- a/themes/default/views/store/checkout.blade.php +++ b/themes/default/views/store/checkout.blade.php @@ -130,11 +130,15 @@
-
From 4449353f3ae7d4e157acc454667cc31997e9b581 Mon Sep 17 00:00:00 2001 From: IceToast Date: Mon, 30 Jan 2023 22:46:14 +0100 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Do=20not=20show=20"Pa?= =?UTF-8?q?yment=20Methods"=20if=20product=20is=20free?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/default/views/store/checkout.blade.php | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/themes/default/views/store/checkout.blade.php b/themes/default/views/store/checkout.blade.php index 4ce7c5ee..de61135d 100644 --- a/themes/default/views/store/checkout.blade.php +++ b/themes/default/views/store/checkout.blade.php @@ -77,22 +77,25 @@
-

{{ __('Payment Methods') }}:

+ @if (!$productIsFree) +

{{ __('Payment Methods') }}:

-
+
- @foreach ($paymentGateways as $gateway) -
- -
- @endforeach -
+ @foreach ($paymentGateways as $gateway) +
+ +
+ @endforeach +
+ @endif