public function run()
 {
     $faker = Factory::create();
     $orders = Order::lists('id');
     $products = Product::sell()->lists('id');
     foreach (range(1, 45) as $index) {
         if ($index > 29) {
             $index -= 29;
         }
         OrderProduct::create(['order_id' => $orders[$index], 'product_id' => $faker->randomElement($products), 'quantity' => $faker->numberBetween(1, 3)]);
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param $order_id
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, $order_id)
 {
     $order = Order::find($order_id);
     if (!$order) {
         return $this->respondNotFound('Order does not exist.');
     }
     if (Auth::user()->id != $order->user->id) {
         return $this->respondInsufficientPermissions('User not authorized for this request.');
     }
     $request['order_id'] = $order_id;
     $validator = Validator::make($request->all(), ['order_id' => 'required|integer', 'name' => 'required|string', 'type' => 'required|integer', 'image_path' => 'required|string', 'option_strength' => 'required|integer', 'option_milk' => 'required|integer', 'option_sugar' => 'required|integer', 'option_mug' => 'required|boolean']);
     if ($validator->fails()) {
         return $this->respondValidationError($validator->errors(), "Parameters failed validation for a product");
     }
     $orderProduct = OrderProduct::create($request->all());
     return $this->respondCreated($this->productTransformer->transform($orderProduct));
 }
 public function payment(Request $request)
 {
     $userCart = Auth::user()->cart;
     $total = 0;
     foreach ($userCart as $item) {
         $total += $item->product->price * $item->amount;
     }
     if (Session::has('coupon')) {
         $total = $total - $total * Session::get('coupon.discount') / 100;
     }
     $billing = App::make('Ecommerce\\Billing\\BillingInterface');
     $billing->charge(['email' => Auth::user()->email, 'token' => $request->stripeToken, 'amount' => $total]);
     $order = Order::create(['user_id' => Auth::user()->id, 'amount' => $total, 'status' => 'Processing', 'firstname' => Session::get('shipping.firstname'), 'lastname' => Session::get('shipping.lastname'), 'shipping_address' => Session::get('shipping.address'), 'shipping_city' => Session::get('shipping.city'), 'shipping_zipcode' => Session::get('shipping.zipcode'), 'shipping_country' => Session::get('shipping.country'), 'payment_method' => 'Credit Card', 'phone' => Session::get('shipping.phone'), 'coupon_id' => Session::get('coupon.id')]);
     Session::forget('coupon');
     foreach ($userCart as $item) {
         OrderProduct::create(['order_id' => $order->id, 'product_id' => $item->product_id, 'amount' => $item->amount, 'options' => $item->options]);
         $item->product->quantity -= $item->amount;
         $item->product->save();
     }
     $this->clear();
     return \Redirect('/dashboard')->with(['alert-success' => 'Payment success']);
 }
 public function run()
 {
     $faker = Faker::create();
     $orderIds = Order::lists('id')->all();
     foreach (range(1, 10) as $index) {
         OrderProduct::create(['order_id' => $faker->randomElement($orderIds), 'name' => $faker->word(), 'type' => $faker->numberBetween(0, 4), 'image_path' => $faker->word(), 'option_strength' => $faker->numberBetween(0, 4), 'option_milk' => $faker->numberBetween(0, 4), 'option_sugar' => $faker->numberBetween(0, 4), 'option_mug' => $faker->boolean()]);
     }
 }
 public function getPaymentStatus(Request $request)
 {
     // Get the payment ID before session clear
     $payment_id = Session::get('paypal_payment_id');
     // clear the session payment ID
     Session::forget('paypal_payment_id');
     if (!empty($request->PayerID) || !empty($request->token)) {
         return Redirect('/')->with('error', 'Payment failed');
     }
     $payment = Payment::get($payment_id, $this->_api_context);
     // PaymentExecution object includes information necessary
     // to execute a PayPal account payment.
     // The payer_id is added to the request query parameters
     // when the user is redirected from paypal back to your site
     $execution = new PaymentExecution();
     $execution->setPayerId($request->PayerID);
     //Execute the payment
     $result = $payment->execute($execution, $this->_api_context);
     // echo '<pre>';print_r($result);echo '</pre>';exit; // DEBUG RESULT, remove it later
     if ($result->getState() == 'approved') {
         // payment made
         $userCart = Auth::user()->cart;
         $total = 0;
         foreach ($userCart as $item) {
             $total += $item->product->price * $item->amount;
         }
         if (Session::has('coupon')) {
             $total = $total - $total * Session::get('coupon.discount') / 100;
         }
         $order = Order::create(['user_id' => Auth::user()->id, 'amount' => $total, 'status' => 'Processing', 'firstname' => Session::get('shipping.firstname'), 'lastname' => Session::get('shipping.lastname'), 'shipping_address' => Session::get('shipping.address'), 'shipping_city' => Session::get('shipping.city'), 'shipping_zipcode' => Session::get('shipping.zipcode'), 'shipping_country' => Session::get('shipping.country'), 'payment_method' => 'Paypal', 'phone' => Session::get('shipping.phone'), 'coupon_id' => Session::get('coupon.id')]);
         Session::forget('coupon');
         foreach ($userCart as $item) {
             OrderProduct::create(['order_id' => $order->id, 'product_id' => $item->product_id, 'options' => $item->options, 'amount' => $item->amount]);
             $item->product->quantity -= $item->amount;
             $item->product->save();
         }
         Auth::user()->cart()->delete();
         return Redirect('/dashboard')->with(['alert-success' => 'Paypal Payment success']);
     }
     return Redirect()->back()->with('alert-error', 'Payment failed');
 }