/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $cookieCart = json_decode($_COOKIE['cart'], true);
     if (empty($cookieCart)) {
         $cookieCart = json_decode($_COOKIE['cart'], true);
         $cart = [];
         $sum = 0;
         $i = 0;
         foreach ($cookieCart as $cartItem) {
             $cart[] = \App\Product::where('id', $cartItem[0])->first()->toArray();
             $cart[$i]['quantity'] = $cartItem[1];
             if ($cart[$i]['sale'] == 1) {
                 $cart[$i]['total'] = $cartItem[1] * $cart[$i]['sale_price'];
             } else {
                 $cart[$i]['total'] = $cartItem[1] * $cart[$i]['price'];
             }
             $cart[$i]['color'] = $cartItem[2];
             $cart[$i]['size'] = $cartItem[3];
             $i++;
         }
         foreach ($cart as $num) {
             if ($num['sale'] == 1) {
                 $sum += $num['quantity'] * $num['sale_price'];
             } else {
                 $sum += $num['quantity'] * $num['price'];
             }
         }
         $error = true;
         return view('cart.index', compact('cart', 'sum', 'error'));
     }
     $cart = [];
     $sum = 0;
     $i = 0;
     foreach ($cookieCart as $cartItem) {
         $cart[] = \App\Product::where('id', $cartItem[0])->first()->toArray();
         $cart[$i]['quantity'] = $cartItem[1];
         if ($cart[$i]['sale'] == 1) {
             $cart[$i]['total'] = $cartItem[1] * $cart[$i]['sale_price'];
         } else {
             $cart[$i]['total'] = $cartItem[1] * $cart[$i]['price'];
         }
         $cart[$i]['color'] = $cartItem[2];
         $cart[$i]['size'] = $cartItem[3];
         $i++;
     }
     foreach ($cart as $num) {
         if ($num['sale'] == 1) {
             $sum += $num['quantity'] * $num['sale_price'];
         } else {
             $sum += $num['quantity'] * $num['price'];
         }
     }
     //Paypal check out
     //Our request parameters
     $requestParams = array('RETURNURL' => 'http://vlambeer.dev/shop/paid', 'CANCELURL' => 'http://vlambeer.dev/shop/payment_failed');
     $items = array();
     $i = 0;
     foreach ($cart as $id => $value) {
         $product = Product::find($value['id']);
         if (!empty($product['color']) || !empty($product['size'])) {
             $product_name = $product['name'] . ' ' . $product['color'] . ' / ' . $product['size'];
         } else {
             $product_name = $product['name'];
         }
         if ($product['sale'] == 1) {
             $temp = array('L_PAYMENTREQUEST_0_NAME' . $i => $product_name, 'L_PAYMENTREQUEST_0_AMT' . $i => $product['sale_price'], 'L_PAYMENTREQUEST_0_QTY' . $i => $value['quantity']);
         } else {
             $temp = array('L_PAYMENTREQUEST_0_NAME' . $i => $product_name, 'L_PAYMENTREQUEST_0_AMT' . $i => $product['price'], 'L_PAYMENTREQUEST_0_QTY' . $i => $value['quantity']);
         }
         $items = array_merge($items, $temp);
         $i = $i + 1;
         if ($product['sale'] == 1) {
             $total[] = $product['sale_price'] * $value['quantity'];
         } else {
             $total[] = $product['price'] * $value['quantity'];
         }
     }
     $orderParams = array('PAYMENTREQUEST_0_AMT' => array_sum($total), 'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR', 'PAYMENTREQUEST_0_ITEMAMT' => array_sum($total));
     $paypal = new Paypal();
     $response = $paypal->request('SetExpressCheckout', $requestParams + $orderParams + $items);
     $order = new Order();
     $order->user_id = Auth::user()->id;
     $order->order_date = date('Y-m-d H:i:s');
     $order->status = 0;
     $order->paypal_token = $response['TOKEN'];
     $order->save();
     foreach ($cart as $id => $value) {
         $product = Product::find($value['id']);
         $order_product = new Order_Product();
         $order_product->order_id = $order->id;
         $order_product->product_id = $value['id'];
         $order_product->quantity = $value['quantity'];
         if (!empty($value['color'])) {
             $order_product->color = $value['color'];
         }
         if (!empty($value['size'])) {
             $order_product->size = $value['size'];
         }
         if ($product['sale'] == 1) {
             $order_product->price = $product->sale_price;
         } else {
             $order_product->price = $product->price;
         }
         $order_product->save();
     }
     if (is_array($response) && $response['ACK'] == 'Success') {
         //Request successful
         $token = $response['TOKEN'];
         return redirect('https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=' . urlencode($token));
     }
 }