_model = new OrderModel(); parent::initController($request, $response, $logger); $this->_viewDatas['title'] = lang($this->_model->getClassName() . '.title'); $this->_viewPath .= strtolower($this->_model->getClassName()); helper($this->_model->getClassName()); helper('cookie'); } //쿠키에 장바구니에 담긴 Order UID를 추가해준다. private function setOrderCookie(OrderEntity $entity) { try { $order_uids = array(); if (has_cookie($this->_cart_cookie_name)) { $order_uids = explode($this->_cart_cookie_delimeter, get_cookie($this->_cart_cookie_name)); } // delete_cookie($this->_cart_cookie_name); set_cookie([ 'name' => $this->_cart_cookie_name, 'value' => implode($this->_cart_cookie_delimeter, [...$order_uids, $entity->getPrimaryKey()]), 'expire' => time() + $this->_cart_cookie_expire, 'domain' => ".localhost", 'path' => '/', ]); } catch (CookieException $e) { throw new \Exception(__FUNCTION__ . "에서 오류발생:\n" . $e->getMessage()); } } // private function setOrderCookie_ORG(OrderEntity $entity) // { // $order_uids = array(); // if (isset($_COOKIE[$this->_cart_cookie_name])) { // $order_uids = explode($this->_cart_cookie_delimeter, $_COOKIE[$this->_cart_cookie_name]); // } // // setcookie($this->_cart_cookie_name, time() - $this->_cart_cookie_expire); // if (setcookie( // $this->_cart_cookie_name, // implode($this->_cart_cookie_delimeter, [...$order_uids, $entity->getPrimaryKey()]), // time() + $this->_cart_cookie_expire, // '/', // '.localhost' // )) { // throw new \Exception(__FUNCTION__ . "에서 SetCookie 오류발생"); // } // } //장바구니에 담기 protected function insert_process() { //fieldData Rule 검사및 fieldData 적용 parent::insert_process(); //상품재고 줄이기 $productModel = new ProductModel(); $product = $productModel->getEntity([$this->_model->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]); //구매 금액 비교 $price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity']; if ($price != $this->_viewDatas['fieldDatas']['price']) { throw new \Exception("실 상품금액{$price} 와 구매금액{$this->_viewDatas['fieldDatas']['price']}이 서로 다릅니다."); } $productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']); } public function insert() { $msg = ""; try { $this->_viewDatas['fields'] = $this->_model->getFields(__FUNCTION__); $this->_viewDatas['fieldRules'] = $this->_model->getFieldRules($this->_viewDatas['fields'], __FUNCTION__); $this->insert_process(); //Transaction 시작 $this->_model->transStart(); $entity = $this->_model->create($this->_viewDatas['fieldDatas']); $this->setOrderCookie($entity); //Transaction Commit $this->_model->transComplete(); $msg = sprintf( "%s에서 해당 상품 %s개를 장바구니에 담았습니다.", $this->_viewDatas['title'], $this->_viewDatas['fieldDatas']['quantity'] ); return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } catch (\Exception $e) { //Transaction Rollback $this->_model->transRollback(); log_message("error", $e->getMessage()); $msg = sprintf( "%s에서 다음 오류로 인해 장바구니에 담기를 실패하였습니다.\n%s", $this->_viewDatas['title'], $e->getMessage() ); return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } finally { $this->_session->setFlashdata("return_message", $msg); } } }