From ff2d0b794e01a187b20e1099d021136c2126660c Mon Sep 17 00:00:00 2001 From: IceToast Date: Mon, 30 Jan 2023 22:34:06 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20handle=20free=20products?= 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]); }