public function checkout(Request $request) { $token = $request->input('stripeToken'); //Retriieve cart information $cart = Cart::where('user_id', Auth::user()->id)->first(); $items = $cart->cartItems; $total = 0; foreach ($items as $item) { $total += $item->product->price; } if (Auth::user()->charge($total * 100, ['source' => $token, 'receipt_email' => Auth::user()->email])) { $order = new Order(); $order->total_paid = $total; $order->user_id = Auth::user()->id; $order->save(); foreach ($items as $item) { $orderItem = new OrderItem(); $orderItem->order_id = $order->id; $orderItem->product_id = $item->product->id; $orderItem->file_id = $item->product->file->id; $orderItem->save(); CartItem::destroy($item->id); } return redirect('/order/' . $order->id); } else { return redirect('/cart'); } }
public function calculate() { $user = Auth::user(); $cart = Cart::where('user_id', '=', $user->id)->where('status', '=', 'pending')->first(); $invoice = new Invoice(); if (!empty($cart)) { $total = 0; foreach ($cart->Product as $order) { $product = $order->Product; if ($product->special_price > 0 && $product->special_price < $product->price) { $total += $product->special_price * $order->quanlity; } else { $total += $product->price * $order->quanlity; } } $invoice->cart_id = $cart->id; $invoice->total = $total; $invoice->user_id = Auth::user()->id; $invoice->status = 'pending'; $invoice->save(); $cart->status = 'confirm'; $cart->save(); return redirect('checkout/calculate/success'); } else { return redirect('/'); } }
public function postOrder() { $member_id = \Auth::user()->id; $address = \Request::get('address'); $books_in_cart = \App\Cart::with('books')->where('member_id', '=', $member_id)->get(); $cart_total = \App\Cart::with('books')->where('member_id', '=', $member_id)->sum('total'); $order = \App\Order::create(['member_id' => $member_id, 'address' => $address, 'total' => $cart_total, 'created_at' => \Carbon\Carbon::now()]); $message = []; foreach ($books_in_cart as $cart_item) { // dobavlenie v pivot $order->orderItems()->attach($cart_item->book_id, ['amount' => $cart_item->amount, 'price' => $cart_item->books->price, 'total' => $cart_item->books->price * $cart_item->amount]); array_push($message, $cart_item); } // dd($books_in_cart[0]->books->title); // $to = '*****@*****.**'; // $subject = 'Tema'; // mail($to, $subject, implode($message)); // dd($message); \Mail::send('emails.order', ['cart' => $message, 'order' => $order], function ($msg) { // and a copy to users $msg->to(['*****@*****.**', \Auth::user()->email])->subject('Order'); }); //Udalyaem iz korziny vse Posle uspeshnogo dobavleniya v book_order //To est sozdaniya zakaza i dobavleniya ego v PIVOT \App\Cart::where('member_id', '=', $member_id)->delete(); return \Redirect::to('index')->with('message', 'Your order successfully added'); }
public function addCart(Request $request) { $book = Book::find($request->id); if ($request->quantity <= $book->quantity) { $cart_old = Cart::where('book_id', '=', $request->id)->where(function ($query) use($request) { $query->where('user_id', '=', Auth::check() ? Auth::user()->id : 0)->orWhere('remember_token', '=', $request->header('X-CSRF-TOKEN')); }); if ($cart_old->count() > 0) { $check = $cart_old->firstOrFail()->update(['quantity' => $cart_old->firstOrFail()->quantity + $request->quantity]); } else { $cart = new Cart(); $cart->user_id = Auth::check() ? Auth::user()->id : null; $cart->book_id = $request->id; $cart->quantity = $request->quantity; $cart->remember_token = $request->header('X-CSRF-TOKEN'); $check = $cart->save(); } if ($check) { return "true"; } else { return "Lỗi thêm hàng vào giỏ. Vui lòng thử lại!"; } } else { return "Quá số hàng trong kho. Vui lòng thử lại!"; } }
public static function unsetCart() { $key = \Request::cookie('shoppingCart'); $cart = \App\Cart::where('key', $key)->first(); $cookie = \Cookie::forget('shoppingCart'); $cart->delete(); return $cookie; }
public function deleteFromCart() { $member_id = \Auth::user()->id; $book_id = \Request::get('book'); $cart_id = \Request::get('cart'); \App\Cart::where('member_id', '=', $member_id)->where('id', '=', $cart_id)->delete(); return \Redirect::route('cart'); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $products = \App\Cart::where('product_id', $id); for ($i = 0; $i <= count($products); $i++) { $product = \App\Product::where('product_id', $products[$i]['product_id'])->first(); $newstock = $product['stock'] - $products[$i]['amount']; DB::update('update tbl_products set stock = ? where product_id = ?', array($newstock, $product['product_id'])); } // return redirect('products')->with('message', 'payment succesfully'); }
public function index() { $token = Session('_token'); $products = Cart::where('_token', $token)->get(); $cartTotal = 0; foreach ($products as $product) { $cartTotal += $product->price * $product->quantity; } return view('front.cart', compact('products', 'cartTotal')); }
/** * @return Cart */ protected function getCart() { $cart = Cart::where('user_id', Auth::user()->id)->first(); if (!$cart) { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->save(); return $cart; } return $cart; }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // \App\Cart::where('customer_id', 'ChWvZc3Mm6GtY7EJCvKnr4XZqllJQZ8w033KRTVEUKOYj1kCR7')->get() // \Mail::send('emails.Newsale', array('cart' => $cart_id, 'customer' => \App\Shipping::where('cart_id', $cart_id)->first()), function($message){ // $checkoutAmt = \Session::get('checkoutAmt'); // $message->to(\App\Shipping::where('cart_id', \Session::get('cart_id'))->pluck('email'))->subject("Your Eternally Nocturnal Order"); // }); \Mail::send('emails.productshipped', array('cart' => \App\Cart::where('customer_id', 'AnVUUCPqtts5zelVNJS9i7hvs3YC4eju4ps5ydTP40SP3pbd23')->get(), 'customer' => \App\Shipping::where('id', 13)->first()), function ($message) { $message->to('*****@*****.**')->subject("Your Eternally Nocturnal Order"); }); return 'success'; // return $ship->buildLabel(); }
public function parseSale() { $total = number_format(\Session::get('checkoutAmt') / 100, 2); $messageitems = []; foreach (\App\Cart::where('customer_id', \Session::get('cart_id'))->get() as $cart) { $name = $cart->findItemProp('name'); $messageitems[] = ['title' => "Item {$name}"]; $messageitems[] = ['title' => "Size: {$cart->size}"]; $messageitems[] = ['title' => "Quantity: {$cart->quantity}"]; $messageitems[] = ['title' => "_________________________"]; } $slackcart = ['fallback' => 'It is all broken, man', 'color' => 'good', 'fields' => $messageitems]; return $slackcart; }
protected function _createCart($force = false) { $user = Sentinel::getUser(); $cart = Cart::where('user_id', $user->id)->where('closed', '!=', 1)->orderBy('id', 'DESC')->first(); if (!$cart) { if ($force == true) { return null; } $cart = new Cart(); $cart->user_id = $user->id; $cart->save(); } return $cart; }
public function storeCommand() { $user = Auth::user(); $token = Session('_token'); $command = Cart::where('_token', $token)->get(); foreach ($command as $item) { $product_id = $item->product_id; $price = $item->price; $quantity = $item->quantity; $customer = $user->customer; $history = History::create(['product_id' => $product_id, 'quantity' => $quantity, 'price' => $price, 'customer_id' => $customer->id, 'status' => 'finalized']); $item->delete(); } return redirect('/'); }
public function shopCart(Request $request) { $cart_list = Cart::where('uid', $this->user->getKey())->with('product_size')->get()->keyBy('id'); //刪除无效產品,修正數量 foreach ($cart_list as $key => $cart) { if (empty($cart->product_size)) { $cart->delete(); unset($cart_list[$key]); } elseif ($cart->product_size->count < $cart->count) { $cart->count = $cart->product_size->count; } } $this->_cart_list = $cart_list; return $this->view('m.shopcart'); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response */ public function store(Request $request) { $qtys = $request->get('qty'); $charge = Cart::where('user_id', Auth::user()->id)->get()->map(function ($c) { return $c->product; })->sum(function ($p) use($qtys) { return $p->price * $qtys[$p->id]; }); $order = Order::create(['user_id' => Auth::user()->id, 'note' => $request->get('note'), 'charge' => $charge, 'status' => 'Cooking', 'address' => $request->get('address')]); foreach ($qtys as $pid => $qty) { OrderItem::create(['product_id' => $pid, 'order_id' => $order->id, 'qty' => $qty]); } Cart::where('user_id', Auth::user()->id)->delete(); return redirect('/')->with('success', 'Your order will be delivered to ' . $request->get('address') . ' soon.'); }
public function showCart() { $cart = Cart::where('user_id', Auth::user()->id)->first(); if (!$cart) { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->save(); } $items = $cart->cartItems; $total = 0; foreach ($items as $item) { $total += $item->product->price; } return view('cart.view', ['items' => $items, 'total' => $total]); }
public function postOrder(Request $request) { $user = Auth::user(); if ($user->firstname == NULL || $user->lastname == NULL) { return back()->withErrors('Merci d\'enregistrer vos nom et prénom sur la page Mon Compte avant de passer commande.'); } $this->validate($request, ['address' => 'required']); if ($request->input('address') == 'saved') { $address = serialize(collect($user->Address)->only(['street', 'post_code', 'city', 'country'])); } else { $this->validate($request, ['street' => 'required', 'postcode' => 'required|numeric', 'city' => 'required', 'country' => 'required']); $address = serialize(['street' => $request->input('street'), 'post_code' => $request->input('postcode'), 'city' => $request->input('city'), 'country' => $request->input('country')]); if ($request->input('remember') == 'remember') { if (!$user->Address()->exists()) { $new_address = new Address(); } else { $new_address = $user->Address; } $new_address->street = $request->input('street'); $new_address->post_code = $request->input('postcode'); $new_address->city = $request->input('city'); $new_address->country = $request->input('country'); $user->Address()->save($new_address); } } $cart_items = Cart::with('Items')->where('user_id', $user->id)->get(); $cart_total = Cart::with('Items')->where('user_id', $user->id)->sum('total'); if ($cart_items->count() == 0) { return back()->withErrors('Une erreur est parvenue, vueillez recommencer l\'opération'); } $order = new Order(); $order->user_id = $user->id; $order->address = $address; $order->total = $cart_total; $order->fullname = serialize(['firstname' => $user->firstname, 'lastname' => $user->lastname]); $order->status = 1; $order->save(); foreach ($cart_items as $order_item) { $order->orderItems()->attach($order_item->item_id, ['amount' => $order_item->amount, 'price' => $order_item->Items->price, 'total' => $order_item->Items->price * $order_item->amount]); } Cart::where('user_id', $user->id)->delete(); Mail::send([], ['user' => $user], function ($mail) use($user) { $mail->to($user->email, $user->username)->subject('Commande enregistrée !')->setBody('Votre commande à été enregistrée'); }); return redirect('/')->with('status', 'Votre commande a été enregistrée.'); }
public function showCart() { // displays current items a user currently has in their cart $cart = Cart::where('user_id', Auth::user()->id)->first(); // A new cart is generated if a user has not got one, but accesses the showCart url. if (!$cart) { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->save(); } // Runs a loop that infers for every item bought, the total value will be increased by that price. $items = $cart->cartItems; $total = 0; foreach ($items as $item) { $total += $item->movie->price; } return view('cart.show', ['items' => $items, 'total' => $total]); }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $user_id = \Auth::user()->id; $orders = \App\Cart::where('user_id', $user_id)->groupby('id')->get(); $total = 0; $order = []; for ($i = 0; $i < sizeof($orders); $i++) { $order[$i] = \App\Cart::where('id', $orders[$i]['id']); } dd($order); foreach ($orders as $order) { $productInfo = \App\Product::where('product_id', $order['product_id'])->first(); $price = $productInfo['price']; $total += $price * $product->amount; dd($total); } $totalprice = $total * 1.21; return view('pages/orders/index', compact('order', 'totalprice')); }
public function removeAction($id) { $product = Product::find($id); if ($product) { $cart = Cart::where(['user_id' => Auth::user()->id, 'product_id' => $product->id])->get(); if (!$cart->isEmpty()) { $item = $cart->first(); $qte = $item->quantity - 1; if ($qte < 1) { Cart::Where(['user_id' => Auth::user()->id, 'product_id' => $id])->delete(); } else { $item->where(['user_id' => Auth::user()->id, 'product_id' => $product->id])->update(['quantity' => $qte]); } } } else { Session::flash('success', "Echec de l'ajout du produit au panier"); } return redirect()->route('user.cart'); }
public function checkout() { $cart = Cart::where('user_id', Auth::user()->id)->first(); $items = $cart->cartItems; foreach ($items as $item) { CartItem::destroy($item->id); $productID = $item['product_id']; $product = Product::where('id', $productID)->first(); if ($product->stock > 0) { $newStock = $product->stock; $newStock -= 1; Product::where('id', $productID)->update(['stock' => $newStock]); $purchase = new Purchase(); $purchase->user_id = Auth::user()->id; $purchase->product_id = $productID; $purchase->save(); } } $products = Product::all(); return view('main.index', ['products' => $products]); }
public function postCheckOut(CheckOutRequest $request) { try { if (Auth::check()) { $customer = Customer::find(Auth::user()->userable_id); if ($customer->name != $request->name) { $customer = new Customer(); $customer->name = $request->name; $customer->address = $request->address; $customer->phone = $request->phone; $customer->save(); } } else { $customer = Customer::where('phone', '=', $request->phone)->first(); if ($customer->count() == 0) { $customer = new Customer(); $customer->name = $request->name; $customer->address = $request->address; $customer->phone = $request->phone; $customer->save(); } } $_token = csrf_token(); $carts = Cart::where(function ($query) use($_token) { $query->where('user_id', '=', Auth::check() ? Auth::user()->id : 0)->orWhere('remember_token', '=', $_token); }); if ($carts->count() > 0) { $order = new Order(); $order->customer_id = $customer->id; $order->note = $request->note; $order->ship_time = $request->ship_time; $order->save(); foreach ($carts->get() as $cart) { $order_detail = new OrderDetail(); $order_detail->order_id = $order->id; $order_detail->book_id = $cart->book_id; $order_detail->quantity = $cart->quantity; $order_detail->save(); } $carts->delete(); return redirect('/')->with('message', 'Đặt hàng thành công. Chúng tôi sẽ sớm liên lạc với bạn!')->with('alert-class', 'alert-success')->with('fa-class', 'fa-check'); } else { return redirect('/checkout')->with('message', 'Không có hàng trong giỏ.')->with('alert-class', 'alert-warning')->with('fa-class', 'fa-warning'); } } catch (ValidationException $e) { return redirect('/checkout')->with('message', 'Xảy ra lỗi khi lưu đơn hàng, vui lòng thử lại!!!')->with('alert-class', 'alert-danger')->with('fa-class', 'fa-ban'); } }
public function completePayment(Request $request, SlackHandler $slacker) { // Set your secret key: remember to change this to your live secret key in production // See your keys here https://dashboard.stripe.com/account/apikeys Stripe::setApiKey(env('STRIPE_SK')); // Get the credit card details submitted by the form $token = $request->input('stripeToken'); // dd(\Session::get('cart_id')); if (!\Session::get('cart_id')) { return redirect()->route('alreadyPaid'); } if (\App\Shipping::where('cart_id', \Session::get('cart_id'))->pluck('payment_status') == 'Paid') { return redirect()->route('alreadyPaid'); } // $charge = 0; // $cart->checkoutPrice() // Create the charge on Stripe's servers - this will charge the user's card try { $charge = Charge::create(array("amount" => round($this->getCheckoutPrice()), "currency" => "usd", "source" => $token, "description" => \Session::get('cart_id'))); } catch (\Stripe\Error\Card $e) { // The card has been declined } $cart_id = \Session::get('cart_id'); $markPaid = \App\Shipping::where('cart_id', \Session::get('cart_id'))->first(); $markPaid->payment_status = 'Paid'; $markPaid->shipped_status = 'Not Shipped'; $markPaid->save(); $slacker->sendSaleMessage(); \App\Sale::create(array('customer_id' => $markPaid->email, 'cart_id' => \Session::get('cart_id'))); $purge = []; foreach (\App\Cart::where('customer_id', $cart_id)->get() as $purgeCarts) { $purge[] = $purgeCarts; $inventory = \App\Inventory::where('product_id', $purgeCarts->product_id)->pluck($purgeCarts->size); $newsize = $inventory - $purgeCarts->quantity; \DB::table('inventories')->where('product_id', $purgeCarts->product_id)->update(array($purgeCarts->size => $newsize)); } if (env('APP_ENV') == 'local') { \Mail::send('emails.productshipped', array('cart' => \App\Cart::where('customer_id', $cart_id)->get(), 'customer' => \App\Shipping::where('cart_id', $cart_id)->first()), function ($message) { $message->to(\App\Shipping::where('cart_id', \Session::get('cart_id'))->pluck('email'))->subject("Your Eternally Nocturnal Order"); }); } else { \Mail::send('emails.productshipped', array('cart' => \App\Cart::where('customer_id', $cart_id)->get(), 'customer' => \App\Shipping::where('cart_id', $cart_id)->first()), function ($message) { $message->to(\App\Shipping::where('cart_id', \Session::get('cart_id'))->pluck('email'))->subject("Your Eternally Nocturnal Order"); }); } \Session::forget('cart_id'); \Session::forget('checkoutAmt'); return redirect()->route('transSuccess'); }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('cart', ['products' => Cart::where('user_id', Auth::user()->id)->get()->map(function ($cart) { return $cart->product; })]); }
public function getCart() { return Cart::where(['user_id' => $this->id])->with('product')->get(); }
public function __construct() { $this->cart = Cart::where('customer_id', \Session::get('cart_id'))->with('product')->get(); }
public static function forget() { Cart::where("product_id", $_POST['ids'])->where("cust_id", \Auth::User()->id)->take(1)->forceDelete(); return redirect('pages/cart'); }
public function validCommand() { $user_id = Auth::user()->id; $customer_id = Customer::where('user_id', '=', $user_id)->value('id'); $token = session('_token'); if (empty($customer_id)) { return view('front.form_customer'); } else { $history = History::create(['customer_id' => $customer_id, 'token' => $token]); $history_id = $history->id; $carts = Cart::where('user_id', '=', $user_id)->get(); foreach ($carts as $cart) { History_detail::create(['history_id' => $history_id, 'product_id' => $cart->product_id, 'quantity' => $cart->quantity]); $number_products_commanded = DB::table('customers')->where('id', '=', $customer_id)->value('number_products_commanded'); $number_products_commanded += $cart->quantity; Customer::where('id', '=', $customer_id)->update(['number_products_commanded' => $number_products_commanded]); $cart->command_unf->delete(); $cart->delete(); } $total_price = 0; $history_details = History_detail::where('history_id', '=', $history_id)->get(); foreach ($history_details as $history_detail) { $quantity = $history_detail->quantity; $price = $history_detail->product->price; $total_price += $quantity * $price; } DB::table('histories')->where('id', '=', $history_id)->update(['total_price' => $total_price]); return redirect('/')->with(['validcommand' => "Votre commande a bien été enregistrée ! Nous vous en remercions.", 'alert' => 'success']); } }
public function removeFromCart(Request $request) { $item = $request->input('product'); $customer_id = \Session::get('cart_id'); \App\Cart::destroy($item); if (\App\Cart::where('customer_id', $customer_id)->first()) { return redirect()->back(); } return $this->emptyCart(); }