public function getOrderReferNotifyUrl($f3, $orderRefer) { if (!('YIQIFACPS' == $orderRefer->utm_source || 'qqlogin' == $orderRefer->login_type && empty($orderRefer->utm_source))) { // 不是亿起发的订单,或者不是QQ登陆订单 return null; } $notifyUrlArray = array(); // 取得记录的 亿起发 参数 $referParamArray = json_decode($orderRefer->refer_param, true); // 取得订单信息 $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($orderRefer->order_id); // 取得订单商品详情 $orderGoodsArray = $orderBasicService->fetchOrderGoodsArray($orderRefer->order_id); // 计算佣金 $orderGoodsNameStr = ''; $coupon = $orderInfo->surplus + $orderInfo->bonus; // 商品总价 $orderAmount = $orderInfo->goods_amount - $orderInfo->discount - $orderInfo->extra_discount - $orderInfo->refund; // 商品总的额外退款金额 $orderExtraRefund = 0; // 对订单中每个商品单独计算 foreach ($orderGoodsArray as $orderGoodsItem) { $orderGoodsNameStr .= '{(' . $orderGoodsItem['goods_id'] . ')' . $orderGoodsItem['goods_name'] . '[' . $orderGoodsItem['goods_number'] . ' 件]},'; // 累计额外退款的总金额 $orderExtraRefund += $orderGoodsItem['extra_refund']; } // CPS 应付总价 $orderAmountOfCps = $orderAmount - $coupon - $orderExtraRefund; $orderAmountOfCps = $orderAmountOfCps > 0 ? $orderAmountOfCps : 0; $orderGoodsNameStr = str_replace('|', '_', $orderGoodsNameStr); $orderGoodsNameStr = mb_substr($orderGoodsNameStr, 0, 240); // 推送QQ登陆订单 if ('qqlogin' == $orderRefer->login_type) { // 取得QQ登陆用户的信息 $userBasicService = new UserBasicService(); $userInfo = $userBasicService->loadUserById($orderInfo->user_id); //取得 QQ 用户的 openId ,QQ登陆的用户 sns_login 例子 qq:476BA0B2332440759D485548637DFCDD $qqUserOpenId = $userInfo->sns_login; $qqUserOpenId = substr($qqUserOpenId, strpos($qqUserOpenId, ':') + 1); // QQ 登陆需要额外推单 $param = "cid=6406" . "&wid=435983" . "&qqoid=" . $qqUserOpenId . "&qqmid=bangzhufu" . "&ct=qqlogin003" . "&on=" . $orderInfo->order_id . "&ta=1" . "&dt=" . YiqifaCpsRefer::$notifyParamDt . "&pp=" . Money::toSmartyDisplay($orderAmountOfCps) . "&sd=" . urlencode(date("Y-m-d H:i:s", Time::gmTimeToLocalTime($orderInfo->add_time))) . "&os=pay" . "&ps=pay" . "&pw=alipay" . "&far=" . Money::toSmartyDisplay($orderInfo->shipping_fee) . "&fav=" . Money::toSmartyDisplay($coupon) . "&fac=0" . "&encoding=utf-8"; // QQ 登陆订单推送 $notifyUrlArray[] = YiqifaCpsRefer::yiqifaNotifyGateway . $param; } if ('YIQIFACPS' != $orderRefer->utm_source) { // 不是亿起发的订单 goto out; } // 亿起发 正常 CPS 推单 $param = "cid=" . $referParamArray['cid'] . "&wi=" . $referParamArray['wi'] . "&on=" . $orderInfo->order_id . "&ta=1" . "&pna=" . urlencode($orderGoodsNameStr) . "&dt=" . YiqifaCpsRefer::$notifyParamDt . "&pp=" . Money::toSmartyDisplay($orderAmountOfCps) . "&sd=" . urlencode(date("Y-m-d H:i:s", Time::gmTimeToLocalTime($orderInfo->add_time))) . "&os=pay" . "&ps=pay" . "&pw=alipay" . "&far=" . Money::toSmartyDisplay($orderInfo->shipping_fee) . "&fav=" . Money::toSmartyDisplay($coupon) . "&fac=0" . "&encoding=utf-8"; // 亿起发订单推送 $notifyUrlArray[] = YiqifaCpsRefer::yiqifaNotifyGateway . $param; out: return $notifyUrlArray; }
/** * 标记订单已经支付 * * @param int $orderId 订单数字ID * @param int $payId 支付类型,比如 4 代表财付通,5代表支付宝 * @param string $payType 支付类型,比如 alipay, tenpay * @param string $payNo 支付方返回的交易编号,比如支付宝返回的交易号 */ public function markOrderInfoPay($orderId, $payId, $payType, $payNo, $note = '', $username = '******') { global $logger; // 参数验证 $validator = new Validator(array('orderId' => $orderId, 'payId' => $payId)); $orderId = $validator->required()->digits()->min(1)->validate('orderId'); $payId = $validator->required()->digits()->min(1)->validate('payId'); $this->validate($validator); // 订单操作,需要保证事务 $dbEngine = DataMapper::getDbEngine(); try { $dbEngine->begin(); // 更新 order_info $orderBasicService = new Order(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); if ($orderInfo->isEmpty()) { $logger->addLogInfo(\Core\Log\Base::ERROR, 'PAYMENT', __CLASS__ . '-' . __FUNCTION__ . ' invalid order_id [' . $orderId . ']'); throw new \InvalidArgumentException('invalid order_id [' . $orderId . ']'); } $currentGmTime = Time::gmTime(); $orderInfo->order_status = Order::OS_CONFIRMED; $orderInfo->pay_status = Order::PS_PAYED; $orderInfo->update_time = $currentGmTime; $orderInfo->confirm_time = $currentGmTime; $orderInfo->pay_time = $currentGmTime; $orderInfo->pay_id = $payId; $orderInfo->pay_type = $payType; $orderInfo->pay_no = $payNo; $orderInfo->money_paid = $orderInfo->order_amount; $orderInfo->save(); // 更新 order_goods $orderGoodsService = new Goods(); $orderGoodsService->markOrderGoodsPay($orderInfo); if (empty($note)) { $note = '[' . $payType . ']付款确认'; } //记录订单操作日志 $orderActionService = new Action(); $orderActionService->logOrderAction($orderId, 0, Order::OS_CONFIRMED, Order::PS_PAYED, Goods::OGS_PAY, $note, $username, 0, $orderInfo['shipping_status']); // 提交事务 $dbEngine->commit(); // 记录成功日志 $logger->addLogInfo(\Core\Log\Base::INFO, 'PAYMENT', __CLASS__ . '-' . __FUNCTION__ . ' success order_id [' . $orderId . ']'); } catch (Exception $e) { // 记录异常日志 $logger->addLogInfo(\Core\Log\Base::ERROR, 'PAYMENT', print_r($e->getTrace(), true)); $dbEngine->rollback(); } // 由于商品库存发生变化,我们需要清除商品缓存,显示新的库存 // 注意: 这个操作绝对不能在前面的 Transaction 中操作,防止对数据库性能造成巨大影响 $orderGoodsArray = $orderBasicService->fetchOrderGoodsArray($orderId); $goodsIdArray = array(); foreach ($orderGoodsArray as $orderGoodsItem) { $goodsIdArray[] = $orderGoodsItem['goods_id']; } $goodsIdArray = array_unique($goodsIdArray); foreach ($goodsIdArray as $goodsId) { // 清除商品的缓存,确保库存数据显示是正确的 ClearHelper::clearGoodsCacheById($goodsId); } }
/** * 加载未支付的订单到购物车中,方便用于计算 * * @return boolean * * @param int $orderId 订单 ID 号 * */ public function loadFromOrderInfo($orderId) { // 参数验证 $validator = new Validator(array('orderId' => $orderId)); $orderId = $validator->required()->digits()->min(1)->validate('orderId'); $this->validate($validator); $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); if ($orderInfo->isEmpty() || OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status']) { return false; } $orderGoodsArray = $orderBasicService->fetchOrderGoodsArray($orderId); if (empty($orderGoodsArray)) { // 空订单,不加载 return false; } // 清除购物车中的数据 $this->cartContext->clear(); // 把订单商品加载到购物车中 foreach ($orderGoodsArray as $orderGoodsItem) { $orderGoods = new OrderGoods(); $orderGoods->orderGoods = $orderGoodsItem; $this->cartContext->orderGoodsArray[] = $orderGoods; } // 订单加载 $this->cartContext->orderInfo = $orderInfo->toArray(); return true; }
/** * 订单详情 */ public function Detail($f3) { // 权限检查 $this->requirePrivilege('manage_order_order_detail'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $order_id = $validator->required('订单ID非法')->digits('订单ID非法')->min(1)->validate('order_id'); if (!$this->validate($validator)) { goto out_fail; } // 查询订单 $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($order_id); if ($orderInfo->isEmpty()) { $this->addFlashMessage('订单ID非法'); goto out_fail; } $orderInfo['order_status_desc'] = OrderBasicService::$orderStatusDesc[$orderInfo['order_status']]; $orderInfo['pay_status_desc'] = OrderBasicService::$payStatusDesc[$orderInfo['pay_status']]; $orderGoodsArray = $orderBasicService->fetchOrderGoodsArray($order_id); // 转换状态显示 foreach ($orderGoodsArray as &$orderGoodsItem) { $orderGoodsItem['order_goods_status_desc'] = OrderGoodsService::$orderGoodsStatusDesc[$orderGoodsItem['order_goods_status']]; } unset($orderGoodsItem); // 取订单来源信息 $orderReferService = new OrderReferService(); $orderRefer = $orderReferService->loadOrderReferByOrderId($orderInfo['order_id'], 300); //缓存5分钟 // 取用户账户 $userBasicService = new UserBasicService(); $userInfo = $userBasicService->loadUserById($orderInfo['user_id']); // 取得订单的操作日志 $orderActionService = new OrderActionService(); $orderLogArray = $orderActionService->fetchOrderLogArray($order_id, 0); // 状态字段转换成可以显示的字符串 foreach ($orderLogArray as &$orderLog) { $orderLog['order_status'] = OrderBasicService::$orderStatusDesc[$orderLog['order_status']]; $orderLog['pay_status'] = OrderBasicService::$payStatusDesc[$orderLog['pay_status']]; $orderLog['order_goods_status'] = OrderGoodsService::$orderGoodsStatusDesc[$orderLog['order_goods_status']]; $orderLog['action_note'] = nl2br($orderLog['action_note']); } unset($orderLog); // 给模板赋值 $smarty->assign('orderInfo', $orderInfo); $smarty->assign('orderRefer', $orderRefer); $smarty->assign('userInfo', $userInfo); $smarty->assign('orderGoodsArray', $orderGoodsArray); $smarty->assign('orderLogArray', $orderLogArray); $smarty->display('order_order_detail.tpl'); return; out_fail: // 失败从这里退出 RouteHelper::reRoute($this, '/Order/Order/Search'); }
/** * 订单详情 */ public function Detail($f3) { global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $order_id = $validator->required('订单ID非法')->digits('订单ID非法')->min(1, true, '订单ID非法')->validate('order_id'); if (!$this->validate($validator)) { goto out_fail; } $userInfo = AuthHelper::getAuthUser(); // 查询订单 $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($order_id, 10); // 缓存 10 秒钟 if ($orderInfo->isEmpty() || $userInfo['user_id'] != $orderInfo['user_id'] || !$this->verifyOrderSystem($orderInfo)) { $this->addFlashMessage('订单ID非法'); goto out_fail; } $orderInfo['order_status_desc'] = OrderBasicService::$orderStatusDesc[$orderInfo['order_status']]; $orderInfo['pay_status_desc'] = OrderBasicService::$payStatusDesc[$orderInfo['pay_status']]; $orderGoodsArray = $orderBasicService->fetchOrderGoodsArray($order_id, 10); // 缓存 10 秒钟 // 转换商品状态显示 foreach ($orderGoodsArray as &$orderGoodsItem) { $orderGoodsItem['order_goods_status_desc'] = OrderGoodsService::$orderGoodsStatusDesc[$orderGoodsItem['order_goods_status']]; } unset($orderGoodsItem); // 给模板赋值 $smarty->assign('orderInfo', $orderInfo); $smarty->assign('orderGoodsArray', $orderGoodsArray); $smarty->display('my_order_detail.tpl'); return; out_fail: // 失败从这里退出 RouteHelper::reRoute($this, '/My/Order'); }
/** * 标记 order_goods 记录为已经支付状态 * * @param $orderId */ public function markOrderGoodsPay($orderInfo) { // 参数验证 $validator = new Validator(array('orderId' => $orderInfo['order_id'])); $orderId = $validator->required()->digits()->min(1)->validate('orderId'); $this->validate($validator); // 设置 order_goods 的状态 $sql = 'update ' . DataMapper::tableName('order_goods') . ' set order_goods_status = ' . Goods::OGS_PAY . ', update_time = ' . Time::gmTime() . ' where order_id = ? '; $dbEngine = DataMapper::getDbEngine(); $dbEngine->exec($sql, $orderId); $goodsCommentService = new GoodsCommentService(); $userBasicService = new UserBasicSerivce(); $userInfo = $userBasicService->loadUserById($orderInfo['user_id']); $orderBasicService = new OrderBasicService(); $orderGoodsArray = $orderBasicService->fetchOrderGoodsArray($orderId); foreach ($orderGoodsArray as $orderGoodsItem) { // 更新商品的销售数量 $this->updateGoodsUserPayCount($orderGoodsItem['goods_id'], $orderGoodsItem['goods_number']); // 更新商品的库存 $this->updateGoodsGoodsNumber($orderGoodsItem['goods_id'], $orderGoodsItem['goods_attr'], $orderGoodsItem['goods_number']); // 添加一个 goods_comment 记录 if (!$goodsCommentService->isOrderGoodsCommentExist($orderGoodsItem['rec_id'])) { $goodsComment = $goodsCommentService->loadGoodsCommentById(0); $goodsComment->create_time = Time::gmTime(); $goodsComment->rec_id = $orderGoodsItem['rec_id']; $goodsComment->goods_id = $orderGoodsItem['goods_id']; $goodsComment->goods_price = $orderGoodsItem['goods_price']; $goodsComment->goods_number = $orderGoodsItem['goods_number']; $goodsComment->goods_attr = $orderGoodsItem['goods_attr']; $goodsComment->comment_rate = 5; // 用户不评论,我们默认为好评,显示 $goodsComment->is_show = 1; // 用户不评论,我们默认为好评,显示 $goodsComment->user_id = $userInfo['user_id']; $goodsComment->user_name = $userInfo['user_name']; $goodsComment->save(); } } }