header('X-Cart-Session'); if (!$sessionId) { $sessionId = (string) Str::uuid(); } $cart = Cart::firstOrCreate( [ 'session_id' => $sessionId, 'sts' => 1, ] ); return [$cart, $sessionId]; } public function getCart(Request $request) { [$cart, $sessionId] = $this->getOrCreateCart($request); return response()->json([ 'cart' => $cart->load('items'), 'session_id' => $sessionId ]); } public function addItem(Request $request) { [$cart, $sessionId] = $this->getOrCreateCart($request); $product = Product::select('id', 'title', 'group_type')->where('id', $request->product_id)->first(); $item = CartItem::where('cart_id', $cart->id) ->where('product_id', $request->product_id) ->first(); if ($item) { $item->increment('quantity'); } else { CartItem::create([ 'cart_id' => $cart->id, 'product_id' => $product->id, 'title' => $product->title, 'group_type' => $product->group_type, 'quantity' => 1, ]); } return response()->json([ 'message' => 'محصول اضافه شد.', 'session_id' => $sessionId ]); } public function removeItem(Request $request) { $request->validate([ 'product_id' => 'required|integer', ]); $sessionId = $request->header('X-Cart-Session'); if (!$sessionId) { return response()->json([ 'message' => 'Cart session not found' ], 400); } $cart = Cart::where('session_id', $sessionId) ->where('sts', 'open') ->first(); if (!$cart) { return response()->json([ 'message' => 'Active cart not found' ], 404); } $item = CartItem::where('cart_id', $cart->id) ->where('product_id', $request->product_id) ->first(); if (!$item) { return response()->json([ 'message' => 'Item not found in cart' ], 404); } if ($item->quantity > 1) { $item->decrement('quantity'); } else { $item->delete(); } return response()->json([ 'message' => 'Item updated successfully' ]); } public function checkout(Request $request) { // return $request; $data = $request->json()->all(); $user= auth()->user(); if ($user->is_legal == 0 || !$user) { return response()->json(['massage' => 'شما باید اول خود را احراز هویت کنید'], 422); } // $data->validate([ // 'session_id' => 'required' // ]); //dd($data['products']); if(isset($data['gateway']) && $data['gateway'] !== 'wallet'){ return response()->json(['massage' => "Under construction👷🏻"], 501); } if( isset($data['session_id']) && ($data['session_id'] !== '' || $data['session_id'] !== null)){ $cart = Cart::where('session_id', $data['session_id']) ->where('sts', 1) ->first()->load('items'); if(!$cart){ return response()->json(['message' => 'سبد خرید پیدا نشد!'], 404); } // اتصال سبد به کاربر لاگین شده $cart->update([ 'customer_id' => auth('customer')->id(), // 'sts' => 2, ]); } else{ $items = []; [$cart, $sessionId] = $this->getOrCreateCart($request); $cart->customer_id = auth('customer')->id(); $cart->save(); $productIds = collect($data) ->pluck('product_id') ->unique() ->values(); $products = Product::select('id', 'title', 'group_type', 'inventory', 'amount', 'percent_fee_buy', 'cash_fee_buy', 'price_profit') ->whereIn('id', $productIds) ->get() ->keyBy('id'); $numbers = collect($data) ->mapWithKeys(function ($item) { return [ $item['product_id'] => $item['number'] ?? 1 ]; }); foreach ($products as $product) { if(($product->inventory - $numbers[$product->id]) < 0){ return response()->json(['message' => "موجودی محصول {$product->title} کافی نیست!"], 422); } $items[] = [ 'cart_id' => $cart->id, 'product_id' => $product->id, 'title' => $product->title, 'group_type' => $product->group_type, 'quantity' => $numbers[$product->id] ?? 1, 'created_at'=> now(), 'updated_at'=> now(), ]; } CartItem::insert($items); $cart = Cart::where('session_id', $cart->session_id) ->first() ->load('items'); } $wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id')->whereNull('product_id') ->first(); $priceRow = Price::latest()->first(); $goldPrice = $priceRow->price; $data['price'] = $goldPrice; $data['price_id'] = $priceRow->id; $data['price_original'] = $goldPrice; $data['customer_id'] = auth('customer')->id(); $total_price = 0; $total_amount = 0; $product_fees = 0; $wallet_interface = new WalletInterface($data['customer_id'], auth('customer')->user()->type); // قیمت‌گیری لحظه‌ای $data['amount'] = 0; $data['service_charge_percentage'] = 0; $data['fee'] = 0; foreach ($cart->items as $item) { $product = Product::find($item->product_id); // کارمزد درصدی هر واحد از محصول $feeAmount = $product->percent_fee_buy * $product->amount; //وزن نهایی هر واحد از محصول با احتساب کارمزد $finalAmount = $product->amount + $feeAmount; //وزن کل محصول با احتساب تعداد و کارمزد $total_amount += $product->amount * $item->quantity; //محاسبه مجموع وزن سبد با احتساب کارمزد $data['amount'] += $finalAmount * $item->quantity; //محاسبه مجموع درصد کارمزد سبد با احتساب تعداد $data['service_charge_percentage'] += $product->percent_fee_buy * $item->quantity; //محاسبه مجموع کارمزد ثابت سبد با احتساب تعداد $data['fee'] += $product->cash_fee_buy * $item->quantity; // قیمت نهایی هر گرم $priceWithFee = $goldPrice + $data['fee']; //محاسبه مجموع کارمزد ثابت سبد با احتساب تعداد $product_fees += $product->cash_fee_buy; // قیمت آیتم $itemTotalPrice = $finalAmount * $priceWithFee; // قیمت کل این آیتم با احتساب تعداد $itemTotalPrice = $item->quantity * $itemTotalPrice; //مبلغ سبد خرید با احتساب این آیتم $total_price += $itemTotalPrice; // ذخیره شفاف برای گزارش و فاکتور $item->update([ 'price' => $itemTotalPrice, 'gold_price' => $goldPrice ]); } //قیمت هر گرم با احتساب کارمزد ثابت $total_price_with_fees = $goldPrice + $product_fees; //مجموع کارمز ثابت و متغیر در سبد خرید $data['total_fee'] = $data['fee'] + ($total_price_with_fees * ($data['service_charge_percentage'] * $total_amount)); //قیمت هر گرم بدون احتساب کارمزدها $data['total_price_original'] = $total_amount * $goldPrice; //کارمزد طلایی $data['service_charge'] = $data['service_charge_percentage'] * $total_amount; //کارمزد ریالی $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; //قیمت کل برای مشتری $data['total_price'] = $total_price; //مقدار قابل پرداخت $data['payment_amount'] = $data['total_price']; if(!empty($data[0]['discount_code'])){ $discount = new GiftCardController(); $discount = $discount->useDiscount($data[0]['discount_code'], $data['customer_id']); $data['discount'] = true; if(!empty($discount['amount'])){ $data['payment_amount'] = $data['total_price'] = $data['total_price'] - ($data['total_price'] * (float)$discount['amount']); } } $data['cart_id'] = (int)$cart->id; $data['sts'] = 1; if ($wallet->cash < $data['total_price']){ throw new UnprocessableEntityHttpException('موجودی کیف پول شما کافی نیست.'); } $data['direction'] = 1; // خرید $data['pay_at'] = now(); $order = Order::create($data); $cart->order_id = $order->id; $cart->save(); $tr = new TransactionInterface($wallet_interface, $_inp = [ "cash" => $data['payment_amount'], 'expire_at' => now(), 'active' => true, 'order_id' => $order->id, 'start_at' => now(), 'title' => "سبد خرید" ], 12, false, false); foreach ($cart->items as $item) { $wallet_interface_product = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $item->product_id); $wallet_interface_product->wallet->gold = intval($wallet_interface_product->wallet->gold); $wallet_interface_product->wallet->save(); new TransactionInterface($wallet_interface_product, [ "gold" => $item->quantity, "product_id" => $item->product_id, 'expire_at' => now(), 'active' => true, 'order_id' => $order->id, 'start_at' => now(), ], 1, true, false); } $order->sts = 2; // پرداخت شده // $order->direction = 3; // سبد خرید $order->payment_sts = 4; // وضعیت پرداخت شده $order->save(); foreach ($cart->items as $item) { Product::where('id', $item->product_id)->decrement('inventory', $item->quantity); // $product = Product::find($item->product_id); // $product->decrement('inventory', $item->quantity); } return response()->json([ 'message' => 'true', ]); } }