fix: 🐛 handle free products

This commit is contained in:
IceToast 2023-01-30 22:34:06 +01:00
parent cfdfaf2fb1
commit ff2d0b794e
No known key found for this signature in database
GPG key ID: 1464353E063A5B97

View file

@ -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]);
}