public function processCart($order) { $addressService = new addressService(); $userService = new userService(); $orderService = new orderService(); $cart = $this->cartInformation(); if (!$cart) { throw new \Exception('bad Cart items'); } if (!$order['email']) { throw new \Exception('bad email'); } if (!$order['name']) { throw new \Exception('bad user name'); } if (!$order['street']) { throw new \Exception('bad street'); } if (!$order['city_id']) { throw new \Exception('bad cityId'); } if (!$order['district_id']) { throw new \Exception('bad districtId'); } if (!$order['ward_id']) { throw new \Exception('bad ward_id'); } if (!$order['phone']) { throw new \Exception('bad phone number'); } // check user if exists - we not create new user $user = $userService->getUserByEmail($order['email']); if (!$user) { $order['role'] = KACANA_USER_ROLE_BUYER; $order['status'] = KACANA_USER_STATUS_CREATE_BY_SYSTEM; $user = $userService->createUser($order); } $addressDefault = 1; if (count($user->userAddress)) { $addressDefault = 0; } // create new address for user $addressReceive = $addressService->createUserAddress($user->id, $order, $addressDefault); // create new order for user $order = $orderService->createOrder($user->id, $addressReceive->id, $cart->total, $cart->quantity, $cart->originTotal, $cart->discount); $items = $cart->items; foreach ($items as $item) { $orderService->createOrderDetail($order->id, $item); } // destroy CART Cart::destroy(); //send email for user $mailService = new mailService(); if ($mailService->sendEmailOrder($user->email, $order->id)) { return $order; } else { throw new \Exception('Bị lỗi trong quá trình gửi mail'); } // send zalo message for user return $order; }
public function createOrder(Request $request) { $addressService = new addressService(); $orderService = new orderService(); $deliveryId = $request->input('deliveryId', 0); $deliveryName = $request->input('deliveryName', ''); $deliveryPhone = $request->input('deliveryPhone', ''); $cityId = $request->input('cityId', ''); $districtId = $request->input('districtId', ''); $wardId = $request->input('wardId', ''); $deliveryStreet = $request->input('deliveryStreet', ''); $deliveryEmail = $request->input('deliveryEmail', ''); try { if (!intval($deliveryId)) { $deliveryAddress = []; $deliveryAddress['name'] = $deliveryName; $deliveryAddress['email'] = $deliveryEmail; $deliveryAddress['phone'] = $deliveryPhone; $deliveryAddress['street'] = $deliveryStreet; $deliveryAddress['city_id'] = $cityId; $deliveryAddress['district_id'] = $districtId; $deliveryAddress['ward_id'] = $wardId; $addressReceive = $addressService->createUserAddress($this->_user->id, $deliveryAddress); $deliveryId = $addressReceive->id; } $order = $orderService->createOrder($this->_user->id, $deliveryId, 0, 0, 0, 0, KACANA_ORDER_PARTNER_STATUS_NEW); return redirect('/order/edit/' . $order->order_code); } catch (\Exception $e) { // @codeCoverageIgnoreStart $return['error'] = $e->getMessage(); $return['errorMsg'] = $e->getMessage(); // @codeCoverageIgnoreEnd } return response()->json($return); }
public function addNewAddressReceive(Request $request) { $userService = new userService(); $addressService = new addressService(); try { $user = $userService->getUserByEmail($this->_user->email); $data['listCity'] = $addressService->getListCity(); $data['listDistrict'] = $addressService->getListDistrict(); $data['user'] = $user; if ($request->isMethod('post')) { $addressArray = array(); $addressArray['name'] = $request->get('name', false); $addressArray['street'] = $request->get('street', false); $addressArray['city_id'] = $request->get('cityId', false); $addressArray['district_id'] = $request->get('districtId', false); $addressArray['ward_id'] = $request->get('wardId', false); $addressArray['phone'] = $request->get('phone', false); $data['address'] = $addressService->createUserAddress($user->id, $addressArray); $data['ok'] = 1; $data['ok_message'] = 'thêm mới địa chỉ: ' . $addressArray['name'] . ' thành công!'; return redirect('/khach-hang/so-dia-chi'); } return view('client.customer.my-address-detail', $data); } catch (\Exception $e) { return view('errors.404', ['error_message' => $e->getMessage()]); } }