Esempio n. 1
0
 public function postAdd()
 {
     $rules = ['firstname' => 'required|min:2', 'lastname' => 'required|min:2', 'address' => 'required|min:5', 'phone' => 'required|min:7'];
     if (!Auth::check()) {
         array_push($rules, ['email' => 'required|email|unique:users']);
     }
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to("checkout")->withErrors($validator)->withInput(Input::except(''));
     } else {
         if (Auth::check()) {
             $user = User::find(Auth::user()->id);
         } else {
             $user = new User();
             $user->email = Input::get('email');
             $password = str_random(10);
             $user->password = Hash::make($password);
         }
         $user->firstname = Input::get('firstname');
         $user->lastname = Input::get('lastname');
         $user->address = Input::get('address');
         $user->phone = Input::get('phone');
         if ($user->save()) {
             $role = Role::where('name', '=', 'Customer')->first();
             if (!$user->hasRole("Customer")) {
                 $user->roles()->attach($role->id);
             }
             $order = new Order();
             $order->user_id = $user->id;
             $order->status_id = OrderStatus::where('title', '=', 'Новый')->first()->id;
             $order->comment = 'Телефон: <b>' . $user->phone . '</b><br>Адрес: <b>' . $user->address . '</b><br>Комментарий покупателя: ' . '<i>' . Input::get('comment') . '</i>';
             if ($order->save()) {
                 $cart = Cart::content();
                 foreach ($cart as $product) {
                     $orderDetails = new OrderDetails();
                     $orderDetails->order_id = $order->id;
                     $orderDetails->product_id = $product->id;
                     $orderDetails->quantity = $product->qty;
                     $orderDetails->price = $product->price;
                     $orderDetails->save();
                 }
             }
             if (!Auth::check()) {
                 Mail::send('mail.registration', ['firstname' => $user->firstname, 'login' => $user->email, 'password' => $password, 'setting' => Config::get('setting')], function ($message) {
                     $message->to(Input::get('email'))->subject("Регистрация прошла успешно");
                 });
             }
             $orderId = $order->id;
             Mail::send('mail.order', ['cart' => $cart, 'order' => $order, 'phone' => $user->phone, 'user' => $user->firstname . ' ' . $user->lastname], function ($message) use($orderId) {
                 $message->to(Input::get('email'))->subject("Ваша заявка №{$orderId} принята");
             });
             Cart::destroy();
             return Redirect::to("checkout/thanks/spasibo-vash-zakaz-prinyat")->with('successcart', 'ok', ['cart' => $cart]);
         }
     }
 }
Esempio n. 2
0
 public function actionOrder()
 {
     $firstname = Yii::app()->request->getPost("firstname", null);
     $lastname = Yii::app()->request->getPost("lastname", null);
     $phone = Yii::app()->request->getPost("phone", null);
     $billing_addr1 = Yii::app()->request->getPost("billing_addr1", null);
     $billing_addr2 = Yii::app()->request->getPost('billing_addr2', null);
     $billing_city = Yii::app()->request->getPost('billing_city', null);
     $billing_region = Yii::app()->request->getPost('billing_region', null);
     $billing_postcode = Yii::app()->request->getPost('billing_postcode', null);
     $billing_country = Yii::app()->request->getPost('billing_country', null);
     $ship_to_billing = Yii::app()->request->getPost('ship_to_billing', null);
     if ($ship_to_billing === "true") {
         $shipping_addr1 = $billing_addr1;
         $shipping_addr2 = $billing_addr2;
         $shipping_city = $billing_city;
         $shipping_region = $billing_region;
         $shipping_postcode = $billing_postcode;
         $shipping_country = $billing_country;
     } else {
         $shipping_addr1 = Yii::app()->request->getPost("shipping_addr1", null);
         $shipping_addr2 = Yii::app()->request->getPost('shipping_addr2', null);
         $shipping_city = Yii::app()->request->getPost('shipping_city', null);
         $shipping_region = Yii::app()->request->getPost('shipping_region', null);
         $shipping_postcode = Yii::app()->request->getPost('shipping_postcode', null);
         $shipping_country = Yii::app()->request->getPost('shipping_country', null);
     }
     if (!$billing_addr1 || !$billing_city || !$billing_region || !$billing_postcode || !$billing_country || !$shipping_addr1 || !$shipping_city || !$shipping_region || !$shipping_postcode || !$shipping_country) {
         throw new CHttpException(400, 'Invalid request; missing parameters.');
     }
     if (!Yii::app()->user->user->firstname && !$firstname || !Yii::app()->user->user->lastname && !$lastname) {
         throw new CHttpException(400, 'Invalid request; missing parameters for basic user info.');
     }
     if ($firstname) {
         Yii::app()->user->user->firstname = $firstname;
         Yii::app()->user->user->save();
     }
     if ($lastname) {
         Yii::app()->user->user->lastname = $lastname;
         Yii::app()->user->user->save();
     }
     $cart = $this->getCart();
     $order_details = $cart->orderDetails;
     if ($order_details === null) {
         $order_details = new OrderDetails();
         $order_details->order_id = $cart->id;
     }
     // Save phone number
     Yii::app()->user->user->addUserPhone($phone);
     # TODO Replace systematic new addresses by saved addresses
     $billing_address = new Address();
     $billing_address->user_id = Yii::app()->user->user->id;
     $billing_address->street1 = $billing_addr1;
     $billing_address->street2 = $billing_addr2;
     $billing_address->city = $billing_city;
     $billing_address->postcode = $billing_postcode;
     $billing_address->region = $billing_region;
     $billing_address->country = $billing_country;
     $billing_address->save();
     if (!Yii::app()->user->user->prefered_billing_address_id) {
         Yii::app()->user->user->prefered_billing_address_id = $billing_address->id;
         Yii::app()->user->user->save();
     }
     $order_details->billing_address_id = $billing_address->id;
     if ($ship_to_billing === "true") {
         $shipping_address = $billing_address;
         $order_details->shipping_address_id = $billing_address->id;
     } else {
         $shipping_address = new Address();
         $shipping_address->user_id = Yii::app()->user->user->id;
         $shipping_address->street1 = $shipping_addr1;
         $shipping_address->street2 = $shipping_addr2;
         $shipping_address->city = $shipping_city;
         $shipping_address->postcode = $shipping_postcode;
         $shipping_address->region = $shipping_region;
         $shipping_address->country = $shipping_country;
         $shipping_address->save();
         $order_details->shipping_address_id = $shipping_address->id;
     }
     if (!Yii::app()->user->user->prefered_shipping_address_id) {
         Yii::app()->user->user->prefered_shipping_address_id = $shipping_address->id;
         Yii::app()->user->user->save();
     }
     // Save every detail for the order total price and taxes based on the provided shipping address
     $itemsInCart = new CActiveDataProvider('OrderHasProduct', array('criteria' => array('condition' => 'order_id=' . $cart->id, 'with' => array('product')), 'pagination' => false));
     $total_weight = 0.0;
     $total_value = 0.0;
     $total_item_qty = 0;
     foreach ($itemsInCart->getData() as $relationship) {
         $total_weight += $relationship->quantity * $relationship->product->weight;
         $total_value += $relationship->quantity * $relationship->price_paid;
         $total_item_qty += $relationship->quantity;
     }
     // Build the json request data we'll send to our server
     $weight = $total_weight;
     $handling = 0;
     $postal_code = $shipping_address->postcode;
     $country_code = $shipping_address->country;
     $value = $total_value;
     $qty = $total_item_qty;
     $orderData = array("weight" => $weight, "handling" => $handling, "postal_code" => $postal_code, "country_code" => $country_code, "value" => $value, "qty" => $qty);
     $jsonpayload = json_encode($orderData);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "https://kle-en-main.com/CloudServices/index.php/BoukemAPI/canadaPostEstimate/listServices?storeid=" . Yii::app()->params['outbound_api_user'] . "&storekey=" . Yii::app()->params['outbound_api_secret']);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonpayload);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
     $result = curl_exec($ch);
     curl_close($ch);
     $arr = json_decode($result);
     if (!isset($arr->services)) {
         throw new CHttpException(400, Yii::t("app", "Postes Canada ne peut fournir d'estimé en ce moment."));
     }
     $rates = $arr->services;
     if ($rates === null) {
         throw new CHttpException(400, Yii::t("app", 'Une erreur est survenue.'));
     }
     $preferred_method = Yii::app()->session['shipment_method'];
     $shiprate = null;
     foreach ($rates as $rate) {
         if ($rate->service_code === $preferred_method) {
             $shiprate = $rate->price_due;
             break;
         }
     }
     if ($shiprate === null) {
         throw new CHttpException(400, 'The delivery method specified in the cart is not available.');
     }
     // TAXES
     $order_details->shipping = $shiprate;
     $order_details->shipping_type = $preferred_method;
     $order_details->taxes = $this->taxesForProvinceCountryValue($shipping_address->region, $shipping_address->country, $total_value);
     $order_details->subtotal = $total_value;
     $order_details->total = floatval($order_details->subtotal) + floatval($order_details->taxes) + floatval($order_details->shipping);
     $order_details->balance = $order_details->total;
     $order_details->save();
     // Transform the cart in order to avoid deleting it.
     $cart->status = "pending";
     $cart->save();
     $order = $cart;
     $order->orderDetails = $order_details;
     // We need to manually set the association to avoid having a null returned
     //$outputArray = array("id"=>$order->id, , 'sid'=>Yii::app()->params['outbound_api_user']);
     $link = $order->getPaypalPaymentLink();
     $this->jsonout(array("payment_link" => $link));
 }
Esempio n. 3
0
     $date = $_GET['date'];
     $response['showShopProductDetailsResponse'] = $fetchPetRefreshListDetails->showingRefreshPetDetails($date);
     deliver_response($_GET['format'], $response, false);
 } else {
     if (strcasecmp($_GET['method'], 'showContactNo') == 0) {
         $response['code'] = 1;
         $response['status'] = $api_response_code[$response['code']]['HTTP Response'];
         $fetchContactDetails = new UsersDetails();
         $email = $_GET['email'];
         $response['showContactDetailsResponse'] = $fetchContactDetails->FetchingContactDetails($email);
         deliver_response($_GET['format'], $response, false);
     } else {
         if (strcasecmp($_GET['method'], 'showUserOrders') == 0) {
             $response['code'] = 1;
             $response['status'] = $api_response_code[$response['code']]['HTTP Response'];
             $fetchOrderDetails = new OrderDetails();
             $email = $_GET['email'];
             $currentPage = $_GET['currentPage'];
             $response['showOrderDetailsResponse'] = $fetchOrderDetails->FetchingOrderDetails($email, $currentPage);
             deliver_response($_GET['format'], $response, false);
         } else {
             if (strcasecmp($_GET['method'], 'showPetMateDetails') == 0) {
                 $response['code'] = 1;
                 $response['status'] = $api_response_code[$response['code']]['HTTP Response'];
                 $fetchPetMateDetails = new PetMateDetails();
                 $email = $_GET['email'];
                 $currentPage = $_GET['currentPage'];
                 if ($currentPage == 1) {
                     $response['showWishListResponse'] = $fetchPetMateDetails->showingUserWishListForPetMate($email);
                 }
                 $response['showPetMateDetailsResponse'] = $fetchPetMateDetails->showingPetMateDetails($currentPage, $email);