public function show()
 {
     $addresses = Useradd::where('user_id', '=', Auth::user()->id)->get();
     $orders = Order::where('orders.user_id', '=', Auth::user()->id)->leftJoin('user_adds as shipadd', 'orders.add_id', '=', 'shipadd.id')->leftJoin('user_adds as rcpadd', 'orders.rcp_id', '=', 'rcpadd.id')->select('orders.*', 'shipadd.title AS ship_title', 'shipadd.address AS ship_add', 'shipadd.address2 AS ship_add2', 'shipadd.postal AS ship_post', 'shipadd.city AS ship_city', 'shipadd.first_name AS ship_first', 'shipadd.last_name AS ship_last', 'rcpadd.title AS rcp_title', 'rcpadd.address AS rcp_add', 'rcpadd.address2 AS rcp_add2', 'rcpadd.postal AS rcp_post', 'rcpadd.city AS rcp_city', 'rcpadd.first_name AS rcp_first', 'rcpadd.last_name AS rcp_last')->orderBy('orders.date', 'DESC')->get();
     // on va récupérer les produits de chaque commande
     // commence par récupérer l'id de chaque commande
     $orders_id = Order::where('orders.user_id', '=', Auth::user()->id)->select('orders.id')->orderBy('orders.date', 'DESC')->get();
     $items = array();
     // rangés par commandes
     foreach ($orders_id->toArray() as $row => $ord) {
         $i = Orderitem::where('order_id', '=', $ord['id'])->leftJoin('products', 'order_items.item_id', '=', 'products.id')->select('order_items.*', 'products.name', 'products.picture')->get();
         $items[$ord['id']] = $i->toArray();
     }
     return View::make('user/show', array('addresses' => $addresses, 'orders' => $orders, 'items' => $items));
 }
 public function buy($id)
 {
     if (Useradd::where('user_id', '=', Auth::user()->id)->first()) {
         $product = Product::find($id);
         $quantity = Input::get("quantity");
         if (intval($quantity) < $product->stock) {
             $name = $product->name;
             $price = intval($product->price);
             $tax = intval($product->price) * 0.2;
             $fdp = 4.0;
             $total = (intval($product->price) + $tax) * intval($quantity) + $fdp;
             $user = "******";
             $password = "******";
             $signature = "AoGYbXCKniGwhG49iNKxRHnnmLrYAFTKM07RzfsOtBl3ppaYNM3k0CEj";
             $params = array('METHOD' => 'SetExpressCheckout', 'VERSION' => '93', 'USER' => $user, 'SIGNATURE' => $signature, 'PWD' => $password, 'RETURNURL' => "http://dcmstore.io/paypal/success", 'CANCELURL' => "http://dcmstore.io/paypal/error", 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', 'L_PAYMENTREQUEST_0_NAME0' => $name, 'L_PAYMENTREQUEST_0_AMT0' => strval($price), 'L_PAYMENTREQUEST_0_QTY0' => $quantity, 'PAYMENTREQUEST_0_ITEMAMT' => strval($price * intval($quantity)), 'PAYMENTREQUEST_0_TAXAMT' => strval($tax * $quantity), 'PAYMENTREQUEST_0_SHIPPINGAMT' => strval($fdp), 'PAYMENTREQUEST_0_HANDLINGAMT' => '0.00', 'PAYMENTREQUEST_0_SHIPDISCAMT' => '0.00', 'PAYMENTREQUEST_0_INSURANCEAMT' => '0.00', 'PAYMENTREQUEST_0_AMT' => strval($total), 'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR', 'ALLOWNOTE' => '1');
             $params = http_build_query($params);
             $endpoint = 'https://api-3T.sandbox.paypal.com/nvp';
             $curl = curl_init();
             curl_setopt_array($curl, array(CURLOPT_URL => $endpoint, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $params, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_VERBOSE => 1));
             $response = curl_exec($curl);
             $responseArray = array();
             parse_str($response, $responseArray);
             curl_close($curl);
             if ($responseArray['ACK'] == 'Success') {
                 $user = Useradd::where('user_id', '=', Auth::user()->id)->first();
                 $order_id = Order::insertGetId(array('user_id' => auth::user()->id, 'date' => \Carbon\Carbon::now(), 'price' => $total, 'add_id' => $user->id, 'rcp_id' => $user->id, 'note' => 'null', 'status' => 0, 'token' => $responseArray['TOKEN']));
                 $oders_items = Orderitem::create(array('order_id' => $order_id, 'item_id' => $id, 'item_qt' => $quantity, 'unit_price' => $price));
                 $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . $responseArray['TOKEN'];
                 return Redirect::to($url);
             }
         } else {
             return Redirect::to('product/' . $id)->with('message', 'Stock insuffisant');
         }
     } else {
         return Redirect::to('product/' . $id)->with('message', 'Veuillez renseigner une addresse dans le dashboard!');
     }
 }