public function actionOrder() { $order = new Order(); /* @var $cart ShoppingCart */ $cart = \Yii::$app->cart; /* @var $products Product[] */ $products = $cart->getPositions(); $total = $cart->getCost(); // echo var_dump($order->errors). "<br>"; // echo var_dump($order->validate()). "<br>"; // die('fdf'); if ($total === 0) { \Yii::$app->session->addFlash('info', 'У вас корзина пуста.'); } if ($order->load(\Yii::$app->request->post()) && $order->validate() && $total) { $transaction = $order->getDb()->beginTransaction(); $order->total_price = $total; $order->save(false); foreach ($products as $product) { $orderItem = new OrderItem(); $orderItem->order_id = $order->id; $orderItem->title = $product->title; $orderItem->price = $product->getPrice(); $orderItem->product_id = $product->id; $orderItem->quantity = $product->getQuantity(); if (!$orderItem->save(false)) { $transaction->rollBack(); \Yii::$app->session->addFlash('error', 'Не можете оформить заказ. Пожалуйста свяжитесь с нами.'); return $this->redirect('catalog/list'); } } $transaction->commit(); \Yii::$app->cart->removeAll(); $order->sendEmail('Новый Заказ', 'order'); \Yii::$app->session->addFlash('success', 'Спасибо за ваш заказ. Мы свяжемся с Вами в ближайшее время.'); return $this->goHome(); // return $this->redirect('catalog/list'); } return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $total]); }
public function actionSave() { $request = Yii::$app->request; $user = $request->post('user'); $cart = $request->post('cart'); $deliveryAddress = $request->post('deliveryAddress'); if ($user != null && $cart != null) { $order = new Order(); $order->userId = $user['id']; $order->mobile = $user['mobile']; $order->email = $user['email']; $order->cart = $cart; $order->balancePoints = $user['balancePoints']; $order->createDate = time(); if ($deliveryAddress == 0) { $order->gender = $user['gender']; $order->name = $user['name']; $order->city = $user['city']; $order->address = $user['address']; $order->zipCode = $user['zipCode']; } else { $order->gender = $user['gender_backup']; $order->name = $user['name_backup']; $order->city = $user['city_backup']; $order->address = $user['address_backup']; $order->zipCode = $user['zip_backup']; } if ($order->validate()) { $order->save(); return $order->_id; } else { $error = $order->errors; } } $code = 500; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['code' => $code]; }
public function actionExchange() { $st = time(); $request = Yii::$app->request; $userId = $request->post('id'); $origin = $request->post('origin'); $mobile = $request->post('mobile'); // address $gender = $request->post('gender'); $name = $request->post('name'); // $city = $request->post('city'); $address = $request->post('address'); $zipCode = $request->post('zipCode'); $companyName = $request->post('companyName'); $cart = $request->post('cart'); $cart = array_values($cart); // For Mars $email = $request->post('email'); $isDefaultMail = $request->post('isDefaultMail'); $pickupCenter = $request->post('pickupCenter'); if ($userId != null && $mobile != null && $cart != null && $name != null && $address != null) { // tag app user if ($origin == 'app:android' || $origin == 'app:ios') { $url = QUN_CRM_API . '/members/tags?access_token=' . QUN_CRM_ACCESS_TOKEN; $body = ['member_ids' => [$userId], 'tags' => [TAG_APP_USER]]; $tagResp = Yii::$app->curl->postJson($url, json_encode($body)); } $products = array(); for ($i = 0; $i < count($cart); $i++) { $product = ["id" => $cart[$i]['id'], "count" => intval($cart[$i]['quantity'])]; array_push($products, $product); } $order_address = $isDefaultMail == "true" ? $address : $pickupCenter; $body = ["products" => $products, "member_origin" => ["member_id" => $userId, "origin" => $origin], "address" => $order_address, "postcode" => $zipCode, "receiving_mode" => $isDefaultMail == "true" ? "express" : "self"]; //return json_encode($body); $url = QUN_CRM_API . '/pointcatalog/products/exchange?access_token=' . QUN_CRM_ACCESS_TOKEN; $response = Yii::$app->curl->putJson($url, json_encode($body)); $response = json_decode($response, true); if (array_key_exists("user_balance", $response)) { $user_balance = $response['user_balance']; $exchange_log_id = $response['exchange_log_id']; } else { // 本次兑换失败,返回code 500,和错误信息 $code = 500; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['code' => $code, 'failedReason' => $response['message']]; } // 保存订单 if ($userId != null && $cart != null) { $order = new Order(); $order->userId = $userId; $order->mobile = $mobile; $order->cart = $cart; $order->orderId = $exchange_log_id; // $user_new = Yii::$app->userService->getUserByPhone($mobile); // $user_new = json_decode($user_new, true); // if (array_key_exists("email", $user_new)) { // $email = $user_new['email']; // } else { // $email = null; // } $order->email = $email; $order->balancePoints = $user_balance; // $user_new['score'] date_default_timezone_set(TIMEZONE); $order->createDate = date("Y-m-d H:i:s", time()); $order->gender = $gender; $order->name = $name; //$order->city = $city; $order->address = $address; $order->zipCode = $zipCode; $order->companyName = $companyName; $order->isDefaultMail = $isDefaultMail; $order->pickupCenter = $pickupCenter; if ($order->validate()) { $order->save(); $result = $order->_id . ''; /*Yii::getLogger()->log("before sms " . (time() - $st), 2, 'goods'); // send message. $cartPoints = 0; $content = ''; $redeemList = array(); for ($i = 0; $i < count($cart); $i++) { $cartPoints = $cartPoints + $cart[$i]['points'] * $cart[$i]['quantity']; $content = $content . $cart[$i]['name'] . ' x' . $cart[$i]['quantity'] . ' '; $item = array('quantity'=>$cart[$i]['quantity'], 'redeemInfo'=>$cart[$i]['name'], 'price'=>$cart[$i]['points'], 'totalPrice'=>$cart[$i]['points'] * $cart[$i]['quantity']); array_push($redeemList, $item); } $content = $name . "您好,您在康寶集點拿獎活動中換領以下獎品,總計花費" . $cartPoints . "點。\n" . $content; Yii::$app->sms->sendSms($mobile, $content); // send email. if ($email != null) { $params = array('name'=>$name, 'address'=>$address, 'postNo'=>$zipCode, 'city'=>$city, 'phoneNo'=>$mobile, 'email'=>$email, 'list'=>$redeemList); $response = Yii::$app->email->sendMailByTemplate(REDEEM_SUCCESS, $email, $params); } Yii::getLogger()->log("after email " . (time() - $st), 2, 'goods');*/ // update lastActiveDate Yii::$app->userService->update(['phone' => $mobile, 'id' => $userId, 'custom_properties' => [['property_id' => 'lastActiveDate', 'value' => date('c')]]]); } else { $result = 'error'; } } \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['balance' => $user_balance, 'orderId' => $result]; } $code = 500; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['code' => $code, 'failedReason' => "Information is not completed."]; }