public function doNotifyUrl($f3) { if (empty($this->orderId)) { throw new \InvalidArgumentException('orderId invalid'); } // 记录所有的参数,方便调试查找 printLog('CreditPay Notify orderId [' . $this->orderId . ']', 'PAYMENT', Base::INFO); $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($this->orderId); //判断金额是否一致 if ($orderInfo->isEmpty() || 0 != $orderInfo['order_amount']) { printLog('CreditPay total_fee error, order_amount :' . Money::storageToCent($orderInfo['order_amount']) . ' total_fee : 0 ', 'PAYMENT', Base::ERROR); goto out_fail; } //检查订单状态 if (OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status']) { printLog('CreditPay order_status is not OS_UNCONFIRMED, order_status[' . $orderInfo['order_status'] . '] orderId[' . $this->orderId . ']', 'PAYMENT', Base::WARN); goto out_succ; } // 把订单设置为已付款状态 $orderPaymentService = new OrderPaymentService(); $orderPaymentService->markOrderInfoPay($this->orderId, $this->payId, $this->getGatewayType(), '0'); printLog('CreditPay orderId[' . $this->orderId . '] notifyUrl success', 'PAYMENT', Base::INFO); //------------------------------ //处理业务完毕 //------------------------------ out_succ: return true; out_fail: // 失败从这里返回 return false; }
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; }
public function doNotifyUrl($f3) { // 记录所有的参数,方便调试查找 printLog('Tenpay Notify Paramters : ' . print_r($_REQUEST, true), 'PAYMENT', Base::INFO); /* 创建支付应答对象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($this->partnerKey); //判断签名 if (!$resHandler->isTenpaySign()) { //签名错误 printLog('Tenpay Notify sign error:' . $resHandler->getDebugInfo(), 'PAYMENT', Base::ERROR); goto out_fail; } //通知id $notify_id = $resHandler->getParameter("notify_id"); //通过通知ID查询,确保通知来至财付通 //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($this->partnerKey); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml"); $queryReq->setParameter("partner", $this->partnerId); $queryReq->setParameter("notify_id", $notify_id); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(10); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if (!$httpClient->call()) { //通信失败 后台调用通信失败,写日志,方便定位问题 printLog('Tenpay verify notify_id connect error :responseCode[' . $httpClient->getResponseCode() . ']' . $httpClient->getErrInfo(), 'PAYMENT', Base::ERROR); goto out_fail; } //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($this->partnerKey); if ($resHandler->getParameter("trade_mode") == "1") { //判断签名及结果(即时到帐) //只有签名正确,retcode为0,trade_state为0才是支付成功 if (!($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $resHandler->getParameter("trade_state") == "0")) { $logMsg = "Tenpay sign error or trade_state error : trade_state=" . $resHandler->getParameter("trade_state") . ",retcode=" . $queryRes->getParameter("retcode") . ",retmsg=" . $queryRes->getParameter("retmsg"); printLog($logMsg, 'PAYMENT', Base::ERROR); //更多的错误信息方便调试 printLog('Tenpay QueryRequestUrl:' . $queryReq->getRequestURL(), 'PAYMENT', Base::ERROR); printLog('Tenpay QueryRequestDebugInfo:' . $queryReq->getDebugInfo(), 'PAYMENT', Base::ERROR); printLog('Tenpay QueryResponseContent:' . $queryRes->getContent(), 'PAYMENT', Base::ERROR); printLog('Tenpay QueryResponseDebugInfo:' . $queryRes->getDebugInfo(), 'PAYMENT', Base::ERROR); goto out_fail; } //取结果参数做业务处理 $out_trade_no = $resHandler->getParameter("out_trade_no"); //财付通订单号 $transaction_id = $resHandler->getParameter("transaction_id"); //金额,以分为单位 $total_fee = $resHandler->getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee $discount = $resHandler->getParameter("discount"); //------------------------------ //处理业务开始 //------------------------------ if (empty($out_trade_no)) { printLog('Tenpay bad out_trade_no', 'PAYMENT', Base::ERROR); goto out_fail; } $paramArray = explode('_', $out_trade_no); if (empty($paramArray) || count($paramArray) < 2 || !ctype_digit($paramArray[count($paramArray) - 1])) { printLog('Tenpay bad out_trade_no', 'PAYMENT', Base::ERROR); goto out_fail; } // 最后一个应该是订单 ID 号 $orderId = intval($paramArray[count($paramArray) - 1]); if (empty($orderId)) { printLog('Tenpay bad out_trade_no', 'PAYMENT', Base::ERROR); goto out_fail; } // 设置订单 ID $this->orderId = $orderId; $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); // 判断金额是否一致,使用 分 做单位来比较 if ($orderInfo->isEmpty() || Money::storageToCent($orderInfo['order_amount']) != intval($total_fee)) { printLog('Tenpay total_fee error, order_amount :{' . Money::storageToCent($orderInfo['order_amount']) . '} total_fee :{' . $total_fee . '}', 'PAYMENT', Base::ERROR); goto out_fail; } //检查订单状态 if (OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status']) { printLog('Tenpay order_status is not OS_UNCONFIRMED, order_status[' . $orderInfo['order_status'] . '] orderId[' . $orderId . ']', 'PAYMENT', Base::WARN); goto out_succ; } // 把订单设置为已付款状态 $orderPaymentService = new OrderPaymentService(); $orderPaymentService->markOrderInfoPay($orderId, $this->payId, $this->getGatewayType(), $transaction_id); printLog('Tenpay orderId[' . $orderId . '] notify success', 'PAYMENT', Base::INFO); //------------------------------ //处理业务完毕 //------------------------------ } else { printLog('Tenpay trade_mode is not 1', 'PAYMENT', Base::ERROR); } out_succ: // 成功从这里返回 echo "success"; return true; out_fail: // 错误从这里返回 echo "fail"; return false; }
/** * 设置订单的信息,注意:这个方法里面糅合了很多功能,通过 action="xxxx" 来区分 * * @param $f3 */ public function Update($f3) { // 验证 action $validator = new Validator($f3->get('GET')); $action = $validator->required()->oneOf(array('set_extra_discount', 'set_suppliers_price', 'set_shipping_no', 'set_memo', 'set_refund', 'set_extra_refund'), '非法操作')->validate('action'); if (!$this->validate($validator)) { goto out; } // 验证提交上来的参数 $validator = new Validator($f3->get('POST')); $updateValueArray = array(); $rec_id = $validator->required()->digits()->min(1)->validate('rec_id'); // 针对不同的 action 做不同的验证 switch ($action) { case 'set_extra_discount': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_extra_discount'); $updateValueArray['extra_discount'] = Money::toStorage($validator->validate('extra_discount')); $updateValueArray['extra_discount_note'] = $validator->required()->validate('extra_discount_note'); break; case 'set_suppliers_price': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_suppliers_price'); $updateValueArray['suppliers_price'] = Money::toStorage($validator->validate('suppliers_price')); $updateValueArray['suppliers_shipping_fee'] = Money::toStorage($validator->validate('suppliers_shipping_fee')); break; case 'set_shipping_no': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_shipping_no'); $updateValueArray['shipping_id'] = $validator->digits()->min(1)->validate('shipping_id'); $updateValueArray['shipping_no'] = $validator->validate('shipping_no'); break; case 'set_memo': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_memo'); $updateValueArray['memo'] = $validator->validate('memo'); break; case 'set_refund': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_refund'); $updateValueArray['refund'] = Money::toStorage($validator->validate('refund')); $updateValueArray['refund_note'] = $validator->required()->validate('refund_note'); $updateValueArray['refund_time'] = Time::gmTime(); $updateValueArray['suppliers_refund'] = Money::toStorage($validator->validate('suppliers_refund')); $updateValueArray['suppliers_refund_note'] = $validator->required()->validate('suppliers_refund_note'); break; case 'set_extra_refund': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_extra_refund'); $updateValueArray['extra_refund'] = Money::toStorage($validator->validate('extra_refund')); $updateValueArray['extra_refund_note'] = $validator->required()->validate('extra_refund_note'); $updateValueArray['extra_refund_time'] = Time::gmTime(); break; default: // 非法的 action goto out; } if (!$this->validate($validator)) { goto out; } // 取 order_goods $orderBasicService = new OrderBasicService(); $orderGoods = $orderBasicService->loadOrderGoodsById($rec_id); if ($orderGoods->isEmpty()) { $this->addFlashMessage('非法订单'); goto out_fail; } // 取得 orderInfo $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id']); // 针对不同的 action 做额外不同的工作 $action_note = ''; switch ($action) { case 'set_extra_discount': // 商品只有是未付款状态才可以设置额外优惠 if (OrderGoodsService::OGS_UNPAY != $orderGoods['order_goods_status']) { $this->addFlashMessage('只有未付款订单才可以给予额外优惠'); goto out; } // 额外优惠允许的最大金额 $allowExtraDiscount = $orderGoods['goods_price'] + $orderGoods['shipping_fee'] - $orderGoods['discount']; $maxExtraDiscount = intval($allowExtraDiscount * $f3->get('sysConfig[max_order_goods_extra_discount_rate]')); $maxExtraDiscount = max($maxExtraDiscount, $f3->get('sysConfig[max_order_goods_extra_discount_value]')); $maxExtraDiscount = min($maxExtraDiscount, $allowExtraDiscount); // 额外优惠不能超过商品本身的金额 if ($updateValueArray['extra_discount'] > $maxExtraDiscount) { $this->addFlashMessage('额外优惠不能超过商品总金额 ' . $maxExtraDiscount); goto out; } // 设置额外余额,需要重新计算 order_info 中的值 $diffDiscount = 0; if ($orderGoods->extra_discount != $updateValueArray['extra_discount']) { $diffDiscount = $updateValueArray['extra_discount'] - $orderGoods->extra_discount; } $orderInfo->extra_discount += $diffDiscount; $orderInfo->order_amount -= $diffDiscount; $action_note .= '额外优惠:' . Money::toSmartyDisplay($updateValueArray['extra_discount']) . ","; $action_note .= '优惠说明:' . $updateValueArray['extra_discount_note'] . "\n"; break; case 'set_suppliers_price': $action_note .= '供货价:' . Money::toSmartyDisplay($updateValueArray['suppliers_price']) . ","; $action_note .= '供货快递费:' . Money::toSmartyDisplay($updateValueArray['suppliers_shipping_fee']) . "\n"; break; case 'set_shipping_no': if ($updateValueArray['shipping_id'] > 0) { //取得快递信息 $expressService = new ExpressService(); $expressInfo = $expressService->loadMetaById($updateValueArray['shipping_id']); if ($expressInfo->isEmpty() || ExpressService::META_TYPE != $expressInfo['meta_type']) { $this->addFlashMessage('快递ID非法'); goto out; } $updateValueArray['shipping_name'] = $expressInfo['meta_name']; } else { $updateValueArray['shipping_name'] = null; } $action_note .= '快递公司:' . $updateValueArray['shipping_name'] . "\n"; $action_note .= '快递单号:' . $updateValueArray['shipping_no'] . "\n"; break; case 'set_memo': $action_note .= '客服备注:' . $updateValueArray['memo'] . "\n"; break; case 'set_refund': // 检查订单状态 if (!in_array($orderGoods['order_goods_status'], array(OrderGoodsService::OGS_PAY, OrderGoodsService::OGS_ASKREFUND))) { $this->addFlashMessage('订单状态非法,不能退款'); goto out; } if ($orderGoods['settle_id'] > 0) { $this->addFlashMessage('已经结算的订单不能退款'); goto out; } // 订单设置为 申请退款 $updateValueArray['order_goods_status'] = OrderGoodsService::OGS_ASKREFUND; // 同步更新 order_info 中的 refund 字段 $diffRefund = 0; if ($orderGoods->refund != $updateValueArray['refund']) { $diffRefund = $updateValueArray['refund'] - $orderGoods->refund; } $orderInfo->refund += $diffRefund; // 检查金额,对一些常见错误提出警告 if (0 == $updateValueArray['refund']) { $this->addFlashMessage('警告:你确定给顾客退款金额设置为 ' . Money::toSmartyDisplay($updateValueArray['refund']) . ' ?'); } if (0 == $updateValueArray['suppliers_refund']) { $this->addFlashMessage('警告:你确定供货商给我们退款金额为 ' . Money::toSmartyDisplay($updateValueArray['refund']) . ' ?'); } if ($updateValueArray['refund'] <= $updateValueArray['suppliers_refund']) { $this->addFlashMessage('警告:给顾客退款金额 <= 供货商给我们的退款金额'); } // 日志信息记录 $action_note .= '申请退款' . "\n"; $action_note .= '顾客金额:' . Money::toSmartyDisplay($updateValueArray['refund']) . ","; $action_note .= '顾客说明:' . $updateValueArray['refund_note'] . "\n"; $action_note .= '供货商金额:' . Money::toSmartyDisplay($updateValueArray['suppliers_refund']) . ","; $action_note .= '供货商说明:' . $updateValueArray['suppliers_refund_note'] . "\n"; break; case 'set_extra_refund': // 检查订单状态 if (OrderGoodsService::OGS_UNPAY == $orderGoods['order_goods_status']) { $this->addFlashMessage('订单状态非法,不能退款'); goto out; } $action_note .= '额外退款:' . Money::toSmartyDisplay($updateValueArray['extra_refund']) . ","; $action_note .= '退款说明:' . $updateValueArray['extra_refund_note'] . "\n"; break; default: // 非法的 action goto out; } // 更新订单信息 $orderGoods->copyFrom($updateValueArray); $orderGoods->update_time = Time::gmTime(); $orderGoods->save(); // 更新 order_info 的 update_time 字段 $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); // 添加订单操作日志 $authAdminUser = AuthHelper::getAuthUser(); $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, $authAdminUser['user_name'], 0, $orderInfo['shipping_status']); $this->addFlashMessage('订单信息保存成功'); out: RouteHelper::reRoute($this, RouteHelper::makeUrl('/Order/Goods/Detail', array('rec_id' => $rec_id), true)); return; out_fail: // 失败从这里退出 RouteHelper::reRoute($this, '/Order/Goods/Search', false); }
/** * 批量上传快递单号,必须上传配货单 * * @param $f3 */ public function Upload($f3) { $recIdColumnIndex = 5; // 子订单 ID 列号 $shippingIdColumnIndex = 23; // 快递公司 ID 列号 $shippingNoColumnIndex = 24; // 快递单号列 if (empty($_FILES) || !array_key_exists('uploadfile', $_FILES)) { $this->addFlashMessage('没有上传文件'); goto out; } if ($_FILES['uploadfile']['error'] > 0) { $this->addFlashMessage('上传文件错误:' . $_FILES['uploadfile']['error']); goto out; } // 解析上传的文件名 $pathInfoArray = pathinfo($_FILES['uploadfile']['name']); $fileExt = strtolower($pathInfoArray['extension']); if ('xls' != $fileExt) { $this->addFlashMessage('文件格式错误,必须是 Excel xls 文件'); goto out; } $targetFile = $f3->get('TEMP') . time() . $fileExt; move_uploaded_file($_FILES['uploadfile']['tmp_name'], $targetFile); require_once PROTECTED_PATH . '/Vendor/PHPExcel/Settings.php'; // 设置Excel缓存,防止数据太多拖死了程序 \PHPExcel_Settings::setCacheStorageMethod(\PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp); try { $objPHPExcel = \PHPExcel_IOFactory::load($targetFile); } catch (\Exception $e) { $this->addFlashMessage('上传的文件格式错误,请注意不要修改批量下载订单文件的格式'); goto out; } // 取得快递公司列表 $expressService = new ExpressService(); $expressArray = $expressService->fetchExpressArray(); // 构建 shipping_id --> express 的反查表 $shippingIdExpressArray = array(); foreach ($expressArray as $expressItem) { $shippingIdExpressArray[$expressItem['meta_id']] = $expressItem; } unset($expressArray); unset($expressService); // 释放内存 $activeSheet = $objPHPExcel->setActiveSheetIndex(0); $maxRow = $activeSheet->getHighestRow(); $expressSetCount = 0; // 成功设置计数 // 当前登录用户 $authSupplierUser = AuthHelper::getAuthUser(); $orderBasicService = new OrderBasicService(); // 一行一行的读取数据 for ($currentRow = 1; $currentRow <= $maxRow; $currentRow++) { // 取得子订单 ID $recIdStr = trim($activeSheet->getCellByColumnAndRow($recIdColumnIndex, $currentRow)->getValue()); if (!ctype_digit($recIdStr)) { // 如果不全是数字,说明这列不对 continue; } $orderGoods = $orderBasicService->loadOrderGoodsById(intval($recIdStr)); if ($orderGoods->isEmpty() || OrderGoodsService::OGS_PAY != $orderGoods->order_goods_status || $orderGoods['suppliers_id'] != $authSupplierUser['suppliers_id']) { $this->addFlashMessage('子订单[' . $recIdStr . ']非法'); continue; } //取得快递公司 ID 设置 $shippingIdStr = trim($activeSheet->getCellByColumnAndRow($shippingIdColumnIndex, $currentRow)->getValue()); if (!ctype_digit($shippingIdStr) || intval($shippingIdStr) <= 0) { $this->addFlashMessage('子订单[' . $recIdStr . '] 对应的 快递ID 错误'); continue; } $shipping_id = intval($shippingIdStr); if (!isset($shippingIdExpressArray[$shipping_id])) { $this->addFlashMessage('子订单[' . $recIdStr . '] 对应的 快递ID[' . $shipping_id . '] 非法'); continue; } if ($orderGoods->shipping_id > 0) { $this->addFlashMessage('子订单[' . $recIdStr . '] 覆盖了之前已有的快递信息 [' . $orderGoods->shipping_name . ':' . $orderGoods->shipping_no . ']'); } //取得快递单号 $shippingNoStr = trim($activeSheet->getCellByColumnAndRow($shippingNoColumnIndex, $currentRow)->getValue()); //设置快递信息 $orderGoods->shipping_id = $shipping_id; $orderGoods->shipping_name = $shippingIdExpressArray[$shipping_id]['meta_name']; $orderGoods->shipping_no = $shippingNoStr; $orderGoods->save(); $expressSetCount++; // 更新 order_info 的 update_time 字段 $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id'], 1); //缓存1秒 $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); // 添加订单操作日志 $action_note = '' . $shipping_id . ',' . $shippingIdExpressArray[$shipping_id]['meta_name'] . ',' . $shippingNoStr; $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, '供货商:[' . $authSupplierUser['suppliers_id'] . ']' . $authSupplierUser['suppliers_name'], 0, $orderInfo['shipping_status']); } $this->addFlashMessage('一共更新了 ' . $expressSetCount . ' 个快递信息'); out: // 删除上传文件 if (!empty($targetFile)) { @unlink($targetFile); } // 回到批量下载界面 RouteHelper::reRoute($this, '/Order/Excel'); }
/** * 取消还没有支付的订单,订单状态必须是 OS_UNCONFIRMED 才可以取消 * * @return boolean * * @param int $orderid 订单ID */ public function cancelUnpayOrderInfo($orderId) { // 参数验证 $validator = new Validator(array('orderId' => $orderId)); $orderId = $validator->required()->digits()->min(1)->validate('orderId'); $this->validate($validator); $orderBasicService = new Order(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); if ($orderInfo->isEmpty() || Order::OS_UNCONFIRMED != $orderInfo['order_status']) { return false; } if ($orderInfo->surplus > 0) { // 如果使用了余额支付,退还余额 $accountLog = new AccountLog(); $accountLog->logChange($orderInfo['user_id'], $orderInfo->surplus, 0, 0, 0, '退还余额,订单:' . $orderInfo['order_id'], AccountLog::ACT_OTHER); $orderInfo->order_amount += $orderInfo->surplus; // 修复 order_amount 的值 $orderInfo->surplus = 0; } if ($orderInfo->bonus_id > 0) { // 使用了红包支付,退还红包 $bonusService = new Bonus(); $bonusService->unUseBonus($orderInfo->bonus_id); $orderInfo->order_amount += $orderInfo->bonus; // 修复 order_amount 的值 $orderInfo->bonus_id = 0; $orderInfo->bonus = 0; } $orderInfo->order_status = Order::OS_CANCELED; // 设置为取消订单 $orderInfo->save(); return true; }
/** * 加载未支付的订单到购物车中,方便用于计算 * * @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; }
/** * 设置订单为 退款中 状态 * * 即:确认已经收到用户的退货了,告知财务这个订单可以给用户退款了 * * @param $f3 */ public function SetRefund($f3) { // 参数验证 $validator = new Validator($f3->get('GET')); $rec_id = $validator->required()->digits()->min(1)->validate('rec_id'); if (!$this->validate($validator)) { goto out; } // 取得当前供货商 $authSupplierUser = AuthHelper::getAuthUser(); $orderBasicService = new OrderBasicService(); $orderGoods = $orderBasicService->loadOrderGoodsById($rec_id); if ($orderGoods->isEmpty() || OrderGoodsService::OGS_ASKREFUND != $orderGoods->order_goods_status || $orderGoods['suppliers_id'] != $authSupplierUser['suppliers_id']) { $this->addFlashMessage('订单ID非法'); goto out; } $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id']); if ($orderInfo->isEmpty() || $orderInfo->pay_status != OrderBasicService::PS_PAYED) { $this->addFlashMessage('订单ID非法'); goto out; } // 设置订单状态为 退款中 $orderGoods->order_goods_status = OrderGoodsService::OGS_REFUNDING; $orderGoods->save(); // 更新 order_info 的 update_time 字段 $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); $action_note = '设置为退款中' . "\n"; $action_note .= '操作人:[' . $authSupplierUser['suppliers_id'] . ']' . $authSupplierUser['suppliers_name'] . "\n"; // 添加订单操作日志 $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, $authSupplierUser['suppliers_name'], 0, $orderInfo['shipping_status']); $this->addFlashMessage('订单状态设置为[退款中]'); out: RouteHelper::reRoute($this, RouteHelper::getRefer(), false); }
public function doReturnUrl($f3) { printLog($this->getGatewayType() . ' Return Paramters : ' . print_r($_REQUEST, true), 'PAYMENT', Base::INFO); // GET 特殊处理 $out_trade_no = $_GET['out_trade_no']; if (empty($out_trade_no)) { goto out_fail; } $paramArray = explode('_', $out_trade_no); if (empty($paramArray) || count($paramArray) < 2 || !ctype_digit($paramArray[count($paramArray) - 1])) { goto out_fail; } // 最后一个应该是订单 ID 号 $orderId = intval($paramArray[count($paramArray) - 1]); if (empty($orderId)) { goto out_fail; } // 设置订单 ID , WAP 页面目前没有 /My/Order/Detail 页面,所以不能跳转到 detail 页面 // $this->orderId = $orderId; $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); if (OrderBasicService::PS_PAYED == $orderInfo['pay_status']) { printLog($this->getGatewayType() . 'return order [' . $orderInfo['order_id'] . '] already pay', 'PAYMENT', Base::INFO); } else { printLog($this->getGatewayType() . 'return order [' . $orderInfo['order_id'] . '] does not pay', 'PAYMENT', Base::INFO); } return OrderBasicService::PS_PAYED == $orderInfo['pay_status']; out_fail: return false; }
public function post($f3) { // 首先做参数合法性验证 $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; } $validator = new Validator($f3->get('POST')); $payGatewayType = $validator->required('必须选择一种支付方式')->validate('pay_gateway_type'); $surplus = Money::toStorage($validator->float('余额格式错误')->min(0, true, '余额格式错误')->validate('surplus')); $bonusSn = $validator->validate('bonus_sn'); // 客服信息 $orderInfoKefuInfo = array(); $orderInfoKefuInfo['kefu_user_id'] = abs(intval($validator->digits()->validate('kefu_user_id'))); $orderInfoKefuInfo['kefu_user_rate'] = abs(intval($validator->digits()->validate('kefu_user_rate'))); $orderInfoKefuInfo['kefu_user_comment'] = $validator->validate('kefu_user_comment'); if (!$this->validate($validator)) { goto out_fail; } // 取得用户信息 $userInfo = AuthHelper::getAuthUser(); $userBasicService = new UserBasicService(); $userInfo = $userBasicService->loadUserById($userInfo['user_id']); // 支付某一个特定的订单需要把订单加载到临时购物车里面 $orderBasicService = new OrderBasicService(); // 检查权限 $orderInfo = $orderBasicService->loadOrderInfoById($order_id); if ($orderInfo->isEmpty() || $userInfo['user_id'] != $orderInfo['user_id'] || OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status']) { $this->addFlashMessage('订单ID非法'); goto out_fail; } // 更新客服信息 if ($orderInfoKefuInfo['kefu_user_id'] > 0) { $adminUserService = new AdminUserService(); $adminUser = $adminUserService->loadAdminById($orderInfoKefuInfo['kefu_user_id']); if (!$adminUser->isEmpty()) { $orderInfoKefuInfo['kefu_user_name'] = $adminUser['user_name']; } else { $orderInfoKefuInfo['kefu_user_id'] = 0; $orderInfoKefuInfo['kefu_user_name'] = null; } unset($adminUser); unset($adminUserService); } else { $orderInfoKefuInfo['kefu_user_id'] = 0; $orderInfoKefuInfo['kefu_user_name'] = null; } $orderInfo->copyFrom($orderInfoKefuInfo); $orderInfo->save(); $cartBasicService = new CartBasicService(); // 加载订单到购物车里 if (!$cartBasicService->loadFromOrderInfo($order_id)) { $this->addFlashMessage('订单加载失败'); goto out_fail; } $cartContext =& $cartBasicService->getCartContextRef(); if ($cartContext->isEmpty()) { $this->addFlashMessage('订单为空,不能支付'); goto out_fail; } // 做第一次购物车计算,需要计算原始订单的金额,后面红包使用的时候有最低订单金额限制 $cartBasicService->calcOrderPrice(); if (!empty($surplus) || !empty($bonusSn)) { if (null != $surplus && $surplus > 0 && $surplus <= $userInfo['user_money']) { // 设置余额支付金额,余额不能超过用户已经有的钱 $cartContext->setValue('surplus', $surplus); } // 设置红包支付 if (!empty($bonusSn)) { $bonusService = new Bonus(); //检查红包是否可以使用 $bonus = $bonusService->fetchUsableBonusBySn($userInfo['user_id'], $cartContext->getValue('order_amount'), $bonusSn); if (empty($bonus)) { $this->addFlashMessage('红包' . $bonusSn . '不能使用'); goto out_fail; } // 设置红包的使用 $cartContext->setValue('bonus_id', $bonus['bonus_id']); $cartContext->setValue('bonus', $bonus['type_money']); } } // 做第二次购物车计算,需要计算使用了余额或者红包 $cartBasicService->calcOrderPayment(); // 更新订单信息 $orderInfo = $cartBasicService->saveOrder($userInfo['user_id'], '买家:' . $userInfo['user_name']); if (!$orderInfo || $orderInfo->isEmpty()) { //订单创建失败,报错 $this->addFlashMessage('更新订单信息失败,请联系客服'); goto out_my_order_detail; } // 如果购物车里面有错误消息,我们需要显示它 if ($cartContext->hasError()) { $this->addFlashMessageArray($cartContext->getAndClearErrorMessageArray()); goto out_my_order_cart; } // 如果订单金额为 0 ,使用 credit 支付网关 if ($orderInfo['order_amount'] <= 0) { $payGatewayType = 'credit'; } $order_id = $orderInfo['order_id']; // 解析参数,我们允许写成 tenpay_cmbchina 代表财付通、招商银行 $payGatewayParamArray = explode('_', $payGatewayType); // 获取支付网关 $payGateway = PaymentGatewayHelper::getPaymentGateway($payGatewayParamArray[0]); // 根据参数做初始化 if (!$payGateway->init($payGatewayParamArray)) { $this->addFlashMessage('支付网关' . $payGatewayType . '初始化失败'); goto out_my_order_detail; } $payRequestUrl = $payGateway->getRequestUrl($order_id, RouteHelper::makeUrl('/Payment/PaymentReturn/' . $payGateway->getGatewayType(), null, false, true), RouteHelper::makeUrl('/Payment/PaymentNotify/' . $payGateway->getGatewayType(), null, false, true)); //notifyUrl if (empty($payRequestUrl)) { $this->addFlashMessage('系统错误:无法生成支付链接'); goto out_my_order_detail; } // 记录支付日志 printLog('[orderId:' . $order_id . ']' . $payRequestUrl, 'PAYMENT', Base::INFO); // 跳转支付 RouteHelper::reRoute($this, $payRequestUrl); return; out_my_order_cart: //失败从这里退出 RouteHelper::reRoute($this, RouteHelper::makeUrl('/My/Order/Cart', array('order_id' => $order_id), true)); return; out_my_order_detail: //失败从这里退出 RouteHelper::reRoute($this, RouteHelper::makeUrl('/My/Order/Detail', array('order_id' => $order_id), true)); return; out_fail: //失败从这里退出 RouteHelper::reRoute($this, '/My/Order'); }
/** * 把订单手动设置成已经付款 * * @param $f3 */ public function MarkPay($f3) { // 权限检查 $this->requirePrivilege('manage_order_order_markpay'); // 参数验证 $validator = new Validator($f3->get('GET')); $order_id = $validator->required('订单ID非法')->digits('订单ID非法')->min(1)->validate('order_id'); if (!$this->validate($validator)) { goto out; } $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($order_id); if ($orderInfo->isEmpty()) { $this->addFlashMessage('订单不存在'); goto out; } if ($orderBasicService::PS_PAYED == $orderInfo['pay_status']) { $this->addFlashMessage('订单已经是支付状态,不允许重复设置'); goto out; } $authAdminUser = AuthHelper::getAuthUser(); $orderPaymentService = new OrderPaymentService(); $orderPaymentService->markOrderInfoPay($order_id, 1, 'admin_set', '0', '手动设置为已经支付', $authAdminUser['user_name']); $this->addFlashMessage('订单成功设置为支付状态'); out: RouteHelper::reRoute($this, RouteHelper::makeUrl('/Order/Order/Detail', array('order_id' => $order_id), true)); }
public function doNotifyUrl($f3) { // 记录所有的参数,方便调试查找 printLog('Alipay Notify Paramters : ' . print_r($_REQUEST, true), 'PAYMENT', Base::INFO); //计算得出通知验证结果 $alipayNotify = new AlipayNotify($this->alipayConfig); $verifyResult = $alipayNotify->verifyNotify(); if (!$verifyResult) { printLog('Alipay verifyNotify Error ', 'PAYMENT', Base::ERROR); goto out_fail; } //验证成功 //商户订单号 $out_trade_no = $_POST['out_trade_no']; //支付宝交易号 $trade_no = $_POST['trade_no']; //交易状态 $trade_status = $_POST['trade_status']; //总金额 $total_fee = $_POST['total_fee']; if ($_POST['trade_status'] != 'TRADE_FINISHED' && $_POST['trade_status'] != 'TRADE_SUCCESS') { printLog('Alipay trade_status Error', 'PAYMENT', Base::ERROR); goto out_fail; } //$_POST['trade_status'] == 'TRADE_FINISHED' //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在两种情况下出现 //1、开通了普通即时到账,买家付款成功后。 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。 //$_POST['trade_status'] == 'TRADE_SUCCESS' //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。 //------------------------------ //处理业务开始 //------------------------------ if (empty($out_trade_no)) { goto out_bad_trade_no; } $paramArray = explode('_', $out_trade_no); if (empty($paramArray) || count($paramArray) < 2 || !ctype_digit($paramArray[count($paramArray) - 1])) { goto out_bad_trade_no; } // 最后一个应该是订单 ID 号 $orderId = intval($paramArray[count($paramArray) - 1]); if (empty($orderId)) { goto out_bad_trade_no; } // 设置订单 ID $this->orderId = $orderId; $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); //判断金额是否一致,我们用 分 做单位来比较 if ($orderInfo->isEmpty() || Money::storageToCent($orderInfo['order_amount']) != Money::displayToCent($total_fee)) { printLog('Alipay total_fee error, order_amount :{' . Money::storageToCent($orderInfo['order_amount']) . '} total_fee : {' . Money::displayToCent($total_fee) . '}', 'PAYMENT', Base::ERROR); goto out_fail; } //检查订单状态 if (OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status']) { printLog('Alipay order_status is not OS_UNCONFIRMED, order_status[' . $orderInfo['order_status'] . '] orderId[' . $orderId . ']', 'PAYMENT', Base::WARN); goto out_succ; } // 把订单设置为已付款状态 $orderPaymentService = new OrderPaymentService(); $orderPaymentService->markOrderInfoPay($orderId, $this->payId, $this->getGatewayType(), $trade_no); printLog('Alipay orderId[' . $orderId . '] notifyUrl success', 'PAYMENT', Base::INFO); //------------------------------ //处理业务完毕 //------------------------------ out_succ: echo "success"; //成功返回 return true; out_bad_trade_no: printLog('Alipay bad out_trade_no', 'PAYMENT', Base::ERROR); out_fail: // 失败从这里返回 echo "fail"; return false; }
/** * 把订单加载到购物车 */ public function Cart($f3) { // 参数验证 $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'] || OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status'] || !$this->verifyOrderSystem($orderInfo)) { $this->addFlashMessage('订单ID非法'); goto out_fail; } //加载订单 $cartBasicService = new CartBasicService(); // 加载订单到购物车里 if (!$cartBasicService->loadFromOrderInfo($order_id)) { $this->addFlashMessage('订单加载失败'); goto out_fail; } $cartContext =& $cartBasicService->getCartContextRef(); if ($cartContext->isEmpty()) { $this->addFlashMessage('订单为空,不能支付'); goto out_fail; } // 保存购物车 $cartBasicService->syncStorage(); RouteHelper::reRoute($this, '/Cart/Show'); return; // 成功从这里返回 out_fail: // 从这里退出 RouteHelper::reRoute($this, '/My/Order'); }
/** * 设置订单的快递信息 * * @param $f3 */ public function ajaxUpdate($f3) { // 参数验证 $validator = new Validator($f3->get('POST')); $rec_id = $validator->required('子订单ID不能为空')->digits('子订单ID必须是数字')->min(1)->validate('rec_id'); $shipping_id = $validator->digits('快递ID必须是数字')->min(1)->validate('shipping_id'); $shipping_no = $validator->validate('shipping_no'); if (!$this->validate($validator)) { goto out_fail; } // 取 order_goods $orderBasicService = new OrderBasicService(); $orderGoods = $orderBasicService->loadOrderGoodsById($rec_id); if ($orderGoods->isEmpty()) { $this->addFlashMessage('非法订单'); goto out_fail; } // 权限检查 $authSupplierUser = AuthHelper::getAuthUser(); if ($orderGoods['suppliers_id'] !== $authSupplierUser['suppliers_id']) { $this->addFlashMessage('非法订单'); goto out_fail; } // 取得快递名 if ($shipping_id > 0) { //取得快递信息 $expressService = new ExpressService(); $expressInfo = $expressService->loadMetaById($shipping_id); if ($expressInfo->isEmpty() || ExpressService::META_TYPE != $expressInfo['meta_type']) { $this->addFlashMessage('快递ID非法'); goto out_fail; } $shipping_name = $expressInfo['meta_name']; } else { $shipping_name = null; } // 更新快递信息 $orderGoods->shipping_id = $shipping_id; $orderGoods->shipping_name = $shipping_name; $orderGoods->shipping_no = $shipping_no; $orderGoods->save(); // 更新 order_info 的 update_time 字段 $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id']); $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); // 添加订单操作日志 if ($shipping_id > 0) { $action_note = '' . $shipping_id . ',' . $shipping_name . ',' . $shipping_no; } else { $action_note = '删除快递信息'; } $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, '供货商:[' . $authSupplierUser['suppliers_id'] . ']' . $authSupplierUser['suppliers_name'], 0, $orderInfo['shipping_status']); Ajax::header(); echo Ajax::buildResult(null, null, null); return; // 成功从这里返回 out_fail: // 失败从这里退出 Ajax::header(); $errorMessage = ''; foreach ($this->flashMessageArray as $messageItem) { $errorMessage .= $messageItem . ','; } echo Ajax::buildResult(-1, $errorMessage, null); }
/** * 拒绝退款请求 * * @param $f3 */ public function Refuse($f3) { // 权限检查 $this->requirePrivilege('manage_order_refund_confirm'); // 参数验证 $validator = new Validator($f3->get('POST')); $rec_id = $validator->required()->digits()->min(1)->validate('rec_id'); $refund_finish_note = $validator->required()->validate('refund_finish_note'); if (!$this->validate($validator)) { goto out; } $orderBasicService = new OrderBasicService(); $orderGoods = $orderBasicService->loadOrderGoodsById($rec_id); if ($orderGoods->isEmpty() || OrderGoodsService::OGS_REFUNDING != $orderGoods->order_goods_status) { $this->addFlashMessage('订单ID非法'); goto out; } $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id']); if ($orderInfo->isEmpty() || $orderInfo->pay_status != OrderBasicService::PS_PAYED) { $this->addFlashMessage('订单ID非法'); goto out; } // 标记订单为 付款 $orderGoods->order_goods_status = OrderGoodsService::OGS_PAY; // 清除退款记录 $orderGoods->refund = 0; // 我们给顾客退款 $orderGoods->refund_time = 0; $orderGoods->refund_finish_time = Time::gmTime(); $orderGoods->refund_finish_note = $refund_finish_note; $orderGoods->suppliers_refund = 0; // 供货商给我们退款 $orderGoods->save(); // 更新 order_info 的 update_time 字段 $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); $authAdminUser = AuthHelper::getAuthUser(); $action_note = '拒绝退款' . "\n"; $action_note .= '操作人:[' . $authAdminUser['user_id'] . ']' . $authAdminUser['user_name'] . "\n"; $action_note .= '备注:' . $refund_finish_note . "\n"; // 添加订单操作日志 $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, $authAdminUser['user_name'], 0, $orderInfo['shipping_status']); $this->addFlashMessage('拒绝退款成功'); out: RouteHelper::reRoute($this, RouteHelper::getRefer(), false); }
public function post($f3) { // 首先做参数合法性验证 $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; } $validator = new Validator($f3->get('POST')); $payGatewayType = $validator->required('必须选择一种支付方式')->validate('pay_gateway_type'); if (!$this->validate($validator)) { goto out_fail; } // 取得用户信息 $userInfo = AuthHelper::getAuthUser(); $userBasicService = new UserBasicService(); $userInfo = $userBasicService->loadUserById($userInfo['user_id']); // 支付某一个特定的订单需要把订单加载到临时购物车里面 $orderBasicService = new OrderBasicService(); // 检查权限 $orderInfo = $orderBasicService->loadOrderInfoById($order_id); if ($orderInfo->isEmpty() || $userInfo['user_id'] != $orderInfo['user_id'] || OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status']) { $this->addFlashMessage('订单ID非法'); goto out_fail; } $cartBasicService = new CartBasicService(); // 加载订单到购物车里 if (!$cartBasicService->loadFromOrderInfo($order_id)) { $this->addFlashMessage('订单加载失败'); goto out_fail; } $cartContext =& $cartBasicService->getCartContextRef(); if ($cartContext->isEmpty()) { $this->addFlashMessage('订单为空,不能支付'); goto out_fail; } // 做第一次购物车计算,需要计算原始订单的金额,后面红包使用的时候有最低订单金额限制 $cartBasicService->calcOrderPrice(); // 计算支付金额 $cartBasicService->calcOrderPayment(); // 如果购物车里面有错误消息,我们需要显示它 if ($cartContext->hasError()) { $this->addFlashMessageArray($cartContext->getAndClearErrorMessageArray()); goto out_fail; } // 更新订单信息 $orderInfo = $cartBasicService->saveOrder($userInfo['user_id'], '买家:' . $userInfo['user_name']); if (!$orderInfo || $orderInfo->isEmpty()) { //订单创建失败,报错 $this->addFlashMessage('更新订单信息失败,请联系客服'); goto out_fail; } // 如果订单金额为 0 ,使用 credit 支付网关 if ($orderInfo['order_amount'] <= 0) { $payGatewayType = 'credit'; } $order_id = $orderInfo['order_id']; // 解析参数,我们允许写成 tenpay_cmbchina 代表财付通、招商银行 $payGatewayParamArray = explode('_', $payGatewayType); // 获取支付网关 $payGateway = PaymentGatewayHelper::getPaymentGateway($payGatewayParamArray[0]); // 根据参数做初始化 if (!$payGateway->init($payGatewayParamArray)) { $this->addFlashMessage('支付网关' . $payGatewayType . '初始化失败'); goto out_fail; } $payRequestUrl = $payGateway->getRequestUrl($order_id, RouteHelper::makeUrl('/Payment/PaymentReturn/' . $payGateway->getGatewayType(), null, false, true), RouteHelper::makeUrl('/Payment/PaymentNotify/' . $payGateway->getGatewayType(), null, false, true)); //notifyUrl if (empty($payRequestUrl)) { $this->addFlashMessage('系统错误:无法生成支付链接'); goto out_fail; } // 记录支付日志 printLog('[orderId:' . $order_id . ']' . $payRequestUrl, 'PAYMENT', Base::INFO); // 跳转去支付 header('Location:' . $payRequestUrl); return; out_fail: //失败从这里退出 RouteHelper::reRoute($this, '/My/Order'); }