/**
  * View: Customer shopping cart
  * @return Response
  */
 public function cart()
 {
     // Get sort conditions
     $orderColumn = Input::get('sort_up', Input::get('sort_down', 'created_at'));
     $direction = Input::get('sort_up') ? 'asc' : 'desc';
     // Get search conditions
     switch (Input::get('target')) {
         case 'title':
             $title = Input::get('like');
             break;
     }
     // Construct query statement
     $query = ShoppingCart::orderBy($orderColumn, $direction)->where('buyer_id', Auth::user()->id)->paginate(15);
     isset($title) and $query->where('title', 'like', "%{$title}%");
     $datas = $query;
     $payment = ShoppingCart::where('buyer_id', Auth::user()->id)->sum('payment');
     $resource = 'myproduct';
     $resourceName = '购物车';
     return View::make($this->resourceView . '.cart')->with(compact('datas', 'resource', 'resourceName', 'payment'));
 }
 /**
  * Action: Payment after add goods in shopping cart
  * @return Response
  */
 public function payment()
 {
     $resourceName = '订单';
     $resource = 'order';
     // Get all form data.
     $data = Input::all();
     $rules = array('product_id' => 'required|', 'customer_name' => 'required', 'customer_address' => 'required', 'customer_phone' => 'required|numeric');
     // Custom validation message
     $messages = array('customer_name.required' => '请填写收货人姓名', 'customer_address.required' => '请填写收货地址', 'customer_phone.required' => '请填写您的手机号码', 'customer_phone.numeric' => '请填写正确的手机号码');
     // Begin verification
     $validator = Validator::make($data, $rules, $messages);
     // Save user real name
     if (Auth::user()->username == NULL) {
         $user = Auth::user();
         $user->username = Input::get('customer_name');
         $user->save();
     }
     // Save user mobile phone number
     if (Auth::user()->phone == NULL) {
         $user = Auth::user();
         $user->phone = Input::get('customer_phone');
         $user->save();
     }
     $product_id = Input::input('product_id');
     $product = Product::where('id', $product_id)->first();
     $data = ShoppingCart::where('buyer_id', Auth::user()->id)->where('product_id', $product_id)->first();
     if ($product->quantity < $data->quantity) {
         return Redirect::back()->with('error', '商品剩余数量不足');
     } else {
         // Verification Success
         if ($validator->passes()) {
             $order_id = md5(date('his')) . $product_id . Auth::user()->id;
             $seller_id = $data->seller_id;
             $seller_alipay = User::where('id', $seller_id)->first()->alipay;
             $order_name = '时光碎片网购物-' . $product->title;
             $payment = $data->payment;
             $goods_show = 'http://www.timefragment.com/product/' . $product->slug;
             $customer_name = Input::input('customer_name');
             $customer_address = Input::input('customer_address');
             $customer_phone = Input::input('customer_phone');
             // Create product order
             $product_order = $this->model;
             $product_order->order_id = $order_id;
             $product_order->seller_id = $seller_id;
             $product_order->product_id = $product_id;
             $product_order->customer_id = Auth::user()->id;
             $product_order->customer_address = $customer_address;
             $product_order->quantity = $data->quantity;
             $product_order->price = $data->price;
             $product_order->payment = $payment;
             $product_order->save();
             // Destroy goods in shopping cart
             $data->delete();
             // Alipay API
             require_once app_path('api/alipay/alipay.config.php');
             require_once app_path('api/alipay/lib/alipay_submit.class.php');
             // Request parameters
             $payment_type = "1";
             // Payment type (required, don't modify)
             $notify_url = route('order.tradeNotify');
             // Server asynchronous notification page URL (start with http://, don't use http://localhost/ or add ?id=123)
             $return_url = route('order.tradeReturn');
             // Synchronization notification page URL (start with http://, don't use http://localhost/ or add ?id=123)
             $seller_email = $seller_alipay;
             // Saller Alipay ID (required)
             $out_trade_no = $order_id;
             // Order ID (required)
             $subject = $order_name;
             // Order name (required)
             $price = $payment;
             // Order payment (required)
             $quantity = "1";
             // Goods quantity (default is 1)
             $logistics_fee = "0.00";
             // Express payment (required)
             $logistics_type = "EXPRESS";
             // Express type: EXPRESS, POST or EMS
             $logistics_payment = "SELLER_PAY";
             // Express payment type (require:SELLER_PAY customer pay or BUYER_PAY saller pay)
             $body = $goods_show;
             // Order describe
             $show_url = $goods_show;
             // Goods show page (URL start with http://)
             $receive_name = $customer_name;
             // Customer name
             $receive_address = $customer_address;
             // Customer address
             $receive_zip = NULL;
             // Customer zip (code such as:123456)
             $receive_phone = NULL;
             // Custome telephone number (such as:0571-88158090)
             $receive_mobile = $customer_phone;
             // Customer mobile phone numer (such as:13312341234)
             // Constructs an array of arguments to request, no need to change
             $parameter = array("service" => "trade_create_by_buyer", "partner" => trim($alipay_config['partner']), "payment_type" => $payment_type, "notify_url" => $notify_url, "return_url" => $return_url, "seller_email" => $seller_email, "out_trade_no" => $out_trade_no, "subject" => $subject, "price" => $price, "quantity" => $quantity, "logistics_fee" => $logistics_fee, "logistics_type" => $logistics_type, "logistics_payment" => $logistics_payment, "body" => $body, "show_url" => $show_url, "receive_name" => $receive_name, "receive_address" => $receive_address, "receive_zip" => $receive_zip, "receive_phone" => $receive_phone, "receive_mobile" => $receive_mobile, "_input_charset" => trim(strtolower($alipay_config['input_charset'])));
             // Establishing request
             $alipaySubmit = new AlipaySubmit($alipay_config);
             $html_text = $alipaySubmit->buildRequestForm($parameter, "get", "确认付款");
             echo $html_text;
         } else {
             return Redirect::back()->withInput()->withErrors($validator);
         }
     }
 }