Browse Source

fix: 🐛 handle free products

IceToast 2 years ago
parent
commit
ff2d0b794e
1 changed files with 10 additions and 19 deletions
  1. 10 19
      app/Http/Controllers/Admin/PaymentController.php

+ 10 - 19
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]);
     }