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; }
/** * @param $name * @param $email * @param $phone * @param $password * @param $confirmPassword * @param string $role * @return mixed */ public function signup($name, $email, $phone, $password, $confirmPassword, $role = KACANA_AUTH_BUYER_NAME) { $userService = new userService(); $result['ok'] = 0; $permissions = [KACANA_AUTH_ADMIN_NAME, KACANA_AUTH_CUS_NAME, KACANA_AUTH_BUYER_NAME]; if ($this->_user) { return false; } if (!in_array($role, $permissions)) { $result['error_code'] = KACANA_AUTH_SIGNUP_ERROR_NOT_EXISTS_PERMISSION; $result['error_message'] = 'Không tồn tại role ' . $role . ' trong hệ thống !'; return $result; } if ($password != $confirmPassword) { $result['error_code'] = KACANA_AUTH_SIGNUP_ERROR_PASSWORD_NOT_MATCH; $result['error_message'] = 'password và confirm password không giống nhau !'; return $result; } if (!Validator::make(['email' => $email], ['email' => 'required|email'])) { $result['error_code'] = KACANA_AUTH_ERROR_BAD_EMAIL; $result['error_message'] = 'email không đúng định dạng !'; return $result; } if (!Validator::make(['name' => $name], ['name' => 'required|min:2'])) { $result['error_code'] = KACANA_AUTH_ERROR_BAD_NAME; $result['error_message'] = 'name không đúng định dạng !'; return $result; } //Validate phone $userModel = new userService(); $user = $userModel->getUserByEmail($email); if ($user && $user->status == KACANA_USER_STATUS_ACTIVE) { $result['error_code'] = KACANA_AUTH_SIGNUP_ERROR_EMAIL_EXISTS; $result['error_message'] = 'email "' . $email . '" đã tồn tại trong hệ thống !'; return $result; } else { if ($user && $user->status == KACANA_USER_STATUS_CREATE_BY_SYSTEM) { // user is created when user buy product on system - so update it when they sign up $userData = array(); $userData['name'] = $name; $userData['phone'] = $phone; $userData['password'] = Hash::make(md5($password)); $userData['role'] = $role; $userData['status'] = KACANA_USER_STATUS_ACTIVE; $user = $userService->updateItem($user->id, $userData); $result['ok'] = 1; $result['data'] = $user; return $result; } else { // create new user $userData = array(); $userData['name'] = $name; $userData['email'] = $email; $userData['phone'] = $phone; $userData['password'] = Hash::make(md5($password)); $userData['role'] = $role; $userData['status'] = KACANA_USER_STATUS_ACTIVE; $user = $userService->createUser($userData); if ($user) { $result['ok'] = 1; $result['data'] = $user; return $result; } } } }