Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function checkout(Request $request)
- {
- // Get the authenticated user ID
- $userId = Auth::id();
- $cartItems = Cart::where('user_id', $userId)->get();
- // Get the total amount from the form submission
- $totalAmount = $request->input('total_amount');
- // Get the cart items for the authenticated user
- $cartItems = Cart::where('user_id', $userId)->get();
- // Check if there are any cart items
- if (!$cartItems) {
- // Handle the case when there are no cart items
- // You may redirect back with an error message or perform other actions
- return redirect()->back()->with('error', 'Your cart is empty.');
- }
- // Step 1: Create an order
- $order = new orders();
- $order->user_id = $userId;
- $order->total_amount = $totalAmount;
- $order->save();
- // Step 2: Create order items
- foreach ($cartItems as $cartItem) {
- $orderItem = new order_items();
- $orderItem->product_id = $cartItem->product_id;
- $orderItem->order_id = $order->id;
- $orderItem->user_id = $userId;
- $orderItem->quantity = $cartItem->quantity;
- $orderItem->save();
- // Deduct product_stock for the ordered product
- $product = Product::find($cartItem->product_id);
- if ($product) {
- $product->product_stock -= $cartItem->quantity;
- $product->save();
- }
- }
- // Step 3: Store the transaction (payment receipt)
- $transaction = new transactions();
- $transaction->order_id = $order->id;
- $transaction->user_id = $userId;
- $image = $request->file('receipt');
- $imageName = $image->getClientOriginalName();
- $image->move(public_path('upload/payment_proof/'),$imageName);
- $transaction->payment_proof = $imageName;
- // Image upload logic
- // if ($request->hasFile('receipt')) {
- // $image = $request->file('receipt');
- // $name_gen = hexdec(uniqid()) . '.' . $image->getClientOriginalExtension();
- // Image::make($image)->resize(430, 327)->save('upload/payment_proof/' . $name_gen);
- // $save_url = 'upload/payment_proof/' . $name_gen;
- // $transaction->payment_proof = $save_url;
- // }
- // Handle the receipt file upload and store its path in the 'payment_proof' field.
- // Example: $transaction->payment_proof = $request->file('receipt')->store('receipts');
- // Image upload logic
- $transaction->save();
- // Clear the cart or perform any other required actions.
- // Clear the cart items
- Cart::where('user_id', $userId)->delete();
- // Redirect to a success page after successful checkout
- $notification = array(
- 'message' => 'Make payment successfully',
- 'alert-type' => 'success'
- );
- // Proceed with any other necessary steps
- return redirect()->back()->with($notification);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement