Пример #1
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     //
     $menuOptions = [0 => ["data-letters" => "", "id" => "home", "text" => "Home", "href" => "/"]];
     if (Auth::check()) {
         $user = Auth::user();
         $verifyAdmin = Admin::find($user->id);
         if (isset($verifyAdmin->id)) {
             $users = User::all();
             $fabrics = Fabric::all();
             $data = array("latestOrders" => Order::getOrders(0), "latestUsers" => User::where("confirmed", "=", "0")->orderBy("created_at", "")->take(5)->get(), "user" => $user, "users" => $users, "fabrics" => $fabrics, "orders" => Order::getOrders(0), "confirmed-users" => User::where("confirmed", "=", "1")->orderBy("created_at", "")->take(5)->get(), "admin" => true);
             /*Session::put('paginate', [
                   "confirmed-users"      => 5,
                   "not-confirmed-users"  => 5,
                   "not-confirmed-orders" => 5,
                   "confirmed-orders"     => 5
               ]);*/
             return view("panel.admin-panel")->with("data", $data)->with("user", $user);
         } else {
             $stats = ["orders" => Order::where("user_id", "=", $user->id)->count(), "unconfirmed" => Order::where("user_id", "=", $user->id)->where("status", "=", "not-confirmed")->count(), "confirmed" => Order::where("user_id", "=", $user->id)->where("status", "=", "confirmed")->count(), "latest" => Order::where("user_id", "=", $user->id)->max("created_at")];
             //dd(Order::getOrdersUser($user->id));
             $data = array("orders" => Order::getOrdersUser($user->id, 0), "stats" => $stats, "fabrics" => Fabric::all(), "admin" => false);
             /*Session::put('paginate', [
                   "confirmed-orders"      => 5,
                   "not-confirmed-orders"  => 5, 
               ]);*/
             return view("panel.normal-panel")->with("user", $user)->with("data", $data);
         }
     } else {
         return redirect("/")->with("menuOptions", $menuOptions);
     }
 }
Пример #2
0
 public static function searchFabric($name)
 {
     return Fabric::where("name", "like", "%" . $name . "%")->orderBy("created_at", "desc")->get();
 }
Пример #3
0
 public function getAjaxFabric($id)
 {
     if ($id == "any") {
         $fabric = Fabric::first();
         $specs = Spec::getSpecFabric($fabric->id);
         $imgs = Image::getImageFabric($fabric->id);
         return ["result" => true, "msg" => "Fabric found", "fabric" => $fabric, "specs" => $specs, "images" => $imgs];
     } else {
         $fabric = Fabric::find($id);
         $specs = Spec::getSpecFabric($fabric->id);
         $imgs = Image::getImageFabric($id);
         return ["result" => true, "msg" => "Fabric found", "fabric" => $fabric, "specs" => $specs, "images" => $imgs];
     }
 }
Пример #4
0
 public function store(Request $request)
 {
     //stores a new order for confirmation
     $result = false;
     $msg = "";
     $info = $request->all();
     //return dd($request->all());
     //return var_dump( $info );
     if (Auth::check()) {
         if (User::profileComplete(Auth::user())) {
             $paypal_conf = config('paypal');
             $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
             $this->_api_context->setConfig($paypal_conf['settings']);
             $fabric = Fabric::find($info["fabric"]);
             $user = Auth::user();
             $order = new Order();
             if ($fabric->stock >= $info["totalAmount"] && $info["totalAmount"] > 0) {
                 //create new order with from the given data.
                 $order->user_id = $user->id;
                 $order->fabric_id = $fabric->id;
                 $order->fabric_name = $fabric->name;
                 $order->amount = $info["totalAmount"];
                 $weight = $info["totalAmount"] / 5 * (2.5 * 1.2);
                 $order->shipping = $weight;
                 $order->type_sample = $info["type-sample"];
                 $order->carrier = $info["carrier"];
                 $order->sub_total = $fabric->priceYard * $info["totalAmount"];
                 $order->total = $order->sub_total + $order->shipping;
                 $order->status = "not-confirmed";
                 $fabric->stock -= $order->amount;
                 $result = true;
                 $dataMail["subTotal"] = $order->sub_total;
                 $dataMail["shipping"] = $order->shipping;
                 $dataMail["carrier"] = $order->carrier;
                 $dataMail["type-sample"] = $order->type_sample;
                 $dataMail["total"] = $order->total;
                 $dataMail["fabric-name"] = $fabric->name;
                 $dataMail["name"] = $user->name;
                 $dataMail["city"] = $user->city;
                 $dataMail["country"] = $user->country;
                 $dataMail["zp"] = $user->zp_code;
                 $dataMail["address"] = $user->address;
                 $dataMail["email"] = $user->email;
                 $dataMail["totalAmount"] = $order->amount;
                 // paypal logic starts here
                 $payer = new Payer();
                 $payer->setPaymentMethod('paypal');
                 $item = new Item();
                 $item->setName($order->fabric_name . $order->created_at);
                 $item->setCurrency('USD');
                 $item->setQuantity($order->amount);
                 $item->setPrice($fabric->priceYard);
                 $itemCarrier = new Item();
                 $itemCarrier->setName("Shipping Price");
                 $itemCarrier->setCurrency('USD');
                 $itemCarrier->setQuantity(1);
                 $itemCarrier->setPrice($weight);
                 // add item to list
                 $item_list = new ItemList();
                 $item_list->setItems([$itemCarrier, $item]);
                 $amount = new Amount();
                 $amount->setCurrency('USD');
                 $amount->setTotal($order->sub_total + $weight);
                 $transaction = new Transaction();
                 $transaction->setAmount($amount);
                 $transaction->setItemList($item_list);
                 $transaction->setDescription('New Fabric Order');
                 $redirect_urls = new RedirectUrls();
                 $redirect_urls->setReturnUrl(url('successPay/orders'));
                 $redirect_urls->setCancelUrl(url("cancelPay/orders"));
                 $payment = new Payment();
                 $payment->setIntent('Sale');
                 $payment->setPayer($payer);
                 $payment->setRedirectUrls($redirect_urls);
                 $payment->setTransactions(array($transaction));
                 try {
                     PPHttpConfig::$DEFAULT_CURL_OPTS[CURLOPT_SSLVERSION] = 4;
                     $payment->create($this->_api_context);
                 } catch (\PayPal\Exception\PPConnectionException $ex) {
                     if (config('app.debug')) {
                         echo "Exception: " . $ex->getMessage() . PHP_EOL;
                         $err_data = json_decode($ex->getData(), true);
                         echo '<pre>';
                         print_r(json_decode($pce->getData()));
                         exit;
                         exit;
                     } else {
                         die('Some error occur, sorry for inconvenient');
                     }
                 }
                 foreach ($payment->getLinks() as $link) {
                     if ($link->getRel() == 'approval_url') {
                         $redirect_url = $link->getHref();
                         break;
                     }
                 }
                 // add payment ID to session
                 $paypal_data = ['paypal_payment_id' => $payment->getId(), "fabric" => $fabric, "order" => $order, "data_mail" => $dataMail, "paypal_context" => $this->_api_context];
                 Session::put("paypal_data", $paypal_data);
                 $order_data = ["msg" => "Error: could not connect to paypal", "result" => false];
                 if (isset($redirect_url)) {
                     // redirect to paypal
                     return redirect($redirect_url);
                 } else {
                     return redirect('/fabrics')->with('order_data', $order_data);
                 }
             } else {
                 $msg = "Error: The order value exceeds the stock or its 0";
                 $result = false;
             }
         } else {
             $msg = "Error: To use this option you need to fill your profile first";
             $result = false;
         }
     } else {
         $msg = "Error: You need to create an account first";
     }
     $order_data = ["msg" => $msg, "result" => $result];
     return redirect("/fabrics")->with("order_data", $order_data);
 }