offlineCollection($data); $this->ajaxReturn($result); } /** * 2019-6-14 * 下单 * $goods_info 商品列表(json字符串) [{"id":1,"num":1,"goods_name":"测试","pricing_method":1,"goods_type":1,"sku":[{"id":1,"name":"红,冰,2L"}]}] */ public function placeOrderPay_v1() { $goods_info = $_POST['goods_info']; //所购买的商品 $vid = I('post.vid'); $card_id = I('post.card_id'); $card_type = I('post.card_type'); if (empty($goods_info)) { $result['status'] = 0; $result['msg'] = '缺少商品信息'; $this->ajaxReturn($result); } try { $goodsList = json_decode($goods_info, true); } catch (Exception $e) { echo $e->getMessage(); exit; } if (!is_array($goodsList)) { $result['status'] = 0; $result['msg'] = '商品信息有误'; $this->ajaxReturn($result); } // 判断是否为购买次卡 $is_buy_card = false; if ($card_id && in_array($card_type, array(1, 2))) { // 判断会员 if (!$vid) { $res = array('status' => 0, 'msg' => '请先登录会员'); $this->ajaxReturn($res); } $check_res = A('Api/SubCard')->checkCard(self::$merchant_id, self::$store_id, $vid, $card_id, $card_type, 5, 1); if ($check_res['status'] == 0) { $this->ajaxReturn($check_res); } else { $is_buy_card = true; } } $verifyGoods = $this->goodsVerify([ 'goods_list' => $goodsList, 'store_id' => self::$store_id ]); //验证商品 if ($verifyGoods['status'] == 0) { $result['status'] = 0; $result['msg'] = $verifyGoods['msg']; $this->ajaxReturn($result); } $BillModel = D('RetailBillOrder'); $billData = array( $BillModel::PAY_STATE => 0, //支付方式 $BillModel::TYPE => 2, //1商城 2店内 $BillModel::ADD_TIME => time(), //添加时间 $BillModel::STORE_ID => self::$store_id, //门店id $BillModel::MERCHANT_ID => self::$merchant_id, //商户id $BillModel::CASHIER_ID => self::$cashier_id, //收银员id $BillModel::ALL_MONEY => $verifyGoods['allMoney'], //总价 ); $bill = $BillModel->addBillOrder($billData); if ($bill) { $BillDisModel = D('RetailBillDiscount'); $billDis = array( $BillDisModel::BILL_ID => $bill[$BillModel::ID], $BillDisModel::USE_INTEGRAL => 0, $BillDisModel::VIP_DIS_MONEY => 0, $BillDisModel::COUPON_ID => 0, $BillDisModel::COUPON_TYPE => 0, $BillDisModel::SMALL_CHANGES_MONEY => 0, $BillDisModel::COUPON_MONEY => 0, $BillDisModel::DISCOUNT => 1, $BillDisModel::CASHIER_DIS_MONEY => 0, $BillDisModel::INTEGRAL_DIS_MONEY => 0 ); $addDis = $BillDisModel->addOrder($billDis); if (empty($addDis)) { $result['status'] = 0; $result['msg'] = '订单创建失败请重试'; } $num = 0; $SellModel = D('RetailSell'); $goods_ids = array(); $CardGoods = D('RetailCardGoods'); $SerialClass = new SerialNumberGoods(); foreach ($verifyGoods['data'] as $value) { // 判断是否为购买次卡 if ($is_buy_card == true) { // 判断无码商品 if ($value[$SellModel::GOODS_TYPE] == 2) { $result['status'] = 0; $result['msg'] = '购买次卡出现异常的无码商品,请刷新重试'; $this->ajaxReturn($res); } // 查询次卡商品 $cg_map = array( $CardGoods::MERCHANT_ID => self::$merchant_id, $CardGoods::STORE_ID => self::$store_id, $CardGoods::GOODS_ID => $value[$SellModel::GOODS_ID], $CardGoods::TYPE => $card_type == 1 ? 0 : 1, $CardGoods::C_ID => $card_id, $CardGoods::STATUS => 1, ); $card_goods = $CardGoods->getFind($cg_map); if (empty($card_goods)) { $res = array( 'status' => 0, 'msg' => '商品' . $value['goods_name'] . '设置的次卡商品异常' ); $this->ajaxReturn($res); break; } else if (floatval($value[$SellModel::NUM]) < floatval($card_goods[$CardGoods::NUMBER])) { $res = array( 'status' => 0, 'msg' => '商品' . $value['goods_name'] . '数量少于自定义购买数量' ); $this->ajaxReturn($res); break; } else if ($value[$SellModel::SKU_JSON_INFO] && $card_goods[$CardGoods::SKU_ID]) { $sku_id = json_decode($value[$SellModel::SKU_JSON_INFO], true)['id']; if ($sku_id != $card_goods[$CardGoods::SKU_ID]) { $res = array( 'status' => 0, 'msg' => '商品' . $value['goods_name'] . '非指定购买的次卡商品规格' ); $this->ajaxReturn($res); break; } } array_push($goods_ids, $value[$SellModel::GOODS_ID]); } //串号信息 if ($value['serial_number_state'] == 1) { $serial_list = $value['serial_list']; unset($value['serial_list']); } $sellData = $value; $sellData['sku_id'] = $sellData['sku_id'] ? $sellData['sku_id'] : 0; $sellData[$SellModel::BILL_ID] = $bill[$BillModel::ID]; $sellData[$SellModel::CASHIER_ID] = self::$cashier_id; $sellData['serial_number_state'] = $sellData['serial_number_state'] ? $sellData['serial_number_state'] : 0; $sell_id = $SellModel->addOrder($sellData); if ($sell_id) { //串号保存 if ($sellData['serial_number_state'] == 1) { $serialResult = $SerialClass->cashierSerialAdd([ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'goods_id' => $sellData['goods_id'], 'sku_id' => $sellData['sku_json_info'] ? json_decode($sellData['sku_json_info'], true)['id'] : 0, 'sell_list' => $serial_list, 'outside_id' => $sell_id ]); if ($serialResult['status'] != 1) { $this->ajaxReturn([ 'status' => 0, 'msg' => '商品' . $value['goods_name'] . '串号保存失败' ]); } } $num++; } } // 判断商品与商户设置的次卡商品是否匹配 if ($is_buy_card == true) { // 获取商户设置次卡商品 $cg_map2 = array( $CardGoods::MERCHANT_ID => self::$merchant_id, $CardGoods::STORE_ID => self::$store_id, $CardGoods::TYPE => $card_type == 1 ? 0 : 1, $CardGoods::C_ID => $card_id, $CardGoods::STATUS => 1, ); $card_goods_ids = $CardGoods->getFindField($cg_map2, $CardGoods::GOODS_ID, true); if (empty($card_goods_ids)) { $res = array( 'status' => 0, 'msg' => '指定次卡商品查询失败' ); $this->ajaxReturn($res); } else { $arr1 = array_diff($goods_ids, $card_goods_ids); $arr2 = array_diff($card_goods_ids, $goods_ids); if (!empty($arr1) || !empty($arr2)) { $res = array( 'status' => 0, 'msg' => '商品多余自定义数量,异常的购买' ); $this->ajaxReturn($res); } } // 创建次卡订单 $CardOrder = D('RetailCardOrder'); // 购买记录 $co_cr = array( $CardOrder::MERCHANT_ID => self::$merchant_id, $CardOrder::STORE_ID => self::$store_id, $CardOrder::VIP_ID => $vid, $CardOrder::TYPE => $card_type, $CardOrder::CARD_ID => $card_id, $CardOrder::STATE => 0, $CardOrder::BILL_ID => $bill[$BillModel::ID], ); // 下单 card_order $co_add = $CardOrder->addOrder($co_cr); if (!$co_add) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } } if ($num == count($verifyGoods['data'])) { $result['order_no'] = $bill[$BillModel::ORDER_NO]; $result['status'] = 1; $result['msg'] = '订单创建成功'; } else { $result['status'] = 0; $result['msg'] = '订单创建失败请重试1'; } } else { $result['status'] = 0; $result['msg'] = '订单创建失败请重试1'; } $this->ajaxReturn($result); } /** * 验证商品 * @param $param * @return mixed */ protected function goodsVerify($param) { $goodsList = $param['goods_list']; $store_id = $param['store_id']; $tempGoods = []; $tempId = []; $GoodsModel = new RetailGoodsModel(); $GoodsGiftsModel = new RetailGoodsGiftsModel(); //条码秤的扫码价格计算权限 $StoreAuthClass = new StoreAuthority(); $configResult = $StoreAuthClass->getStoreConfig([ 'store_id' => $store_id, 'fields' => 'code_price_state' ]); $code_price_state = $configResult['data']['code_price_state'] ? $configResult['data']['code_price_state'] : 0; foreach ($goodsList as $key => $val) { if ($val['goods_type'] == 2) { $tempGoods[] = $val; $tempId[] = 0; } else { $val_id = $val['id']; if (!empty($val['sku'])) { $val_id = $val['id'] . '_' . $val['sku'][0]['id']; } else if ($val['goods_gifts_id'] && $val['sku_id']) { $val_id = $val['id'] . '_' . $val['sku_id']; } $index = array_search($val_id, $tempId); if ($index !== false) { $tempGoods[$index]['num'] = round($tempGoods[$index]['num'] + $val['num'], 3); } else { $tempGoods[] = $val; $tempId[] = $val_id; } } } $serial_arr = []; $endGoods = array(); $allMoney = 0; $SkuGoodsModel = new RetailSkuGoodsModel(); $SkuListModel = new RetailSkuListModel(); $SerialClass = new SerialNumberGoods(); $SplitMakerModel = new GoodsSplit(); $InventoryApi = new InventoryController(); $time = time(); foreach ($goodsList as $k => $value) { if ($value['goods_type'] == 2) { //无码商品 if ($value['pricing_method'] != 1 && $value['pricing_method'] != 2) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个无码商品计量方式有误'; return $res; } if (!verifyWeight($value['num'], $value['pricing_method'])) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个无码商品数量格式有误'; return $res; } if (!verifyPrice($value['retail_price'])) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个无码商品价格有误'; return $res; } if (!empty($value['goods_name'])) { $temp['goods_name'] = $value['goods_name']; } else { $temp['goods_name'] = '无码商品'; } $temp['goods_id'] = 0; $temp['goods_type'] = 2; $temp['original_price'] = $value['retail_price']; $temp['purchase_price'] = $value['retail_price']; $temp['pricing_method'] = $value['pricing_method']; $sell_number = 1; } else { //赠品处理 if ($value['goods_gifts_id']) { // 赠品--只判断赠品库存 $gifts_map = [ 'id' => $value['goods_gifts_id'], 'status' => 1, 'is_del' => 1, ]; // 获取赠品库存 $remainInventory = $GoodsGiftsModel->getFind($gifts_map, 'remain_inventory'); if (!$remainInventory) { $res['status'] = 0; $res['msg'] = '赠品信息有误!'; return $res; } if ((float)$remainInventory['remain_inventory'] < (float)$value['num']) { $res['status'] = 0; $res['msg'] = '赠品库存不足'; return $res; } if ($value['sku_id']) { $value['sku'][0] = [ 'id' => $value['sku_id'], 'sku_num' => $value['sku_num'] ]; } } $value_id = $value['id']; if (!empty($value['sku'])) { $value_id = $value['id'] . '_' . $value['sku'][0]['id']; } $index = array_search($value_id, $tempId); if (empty($tempGoods[$index]['goods'])) { $map = array( 'store_id' => self::$store['id'], 'status' => 0, 'id' => $value['id'] ); $goods = $GoodsModel->getFind($map); if (empty($goods)) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个商品已下架或不存在'; return $res; } $goods['goods_num'] = $InventoryApi->goods_num(1, self::$store['id'], $value['id'], $value['sku'] ? $value['sku']['0']['id'] : ''); $tempGoods[$index]['goods'] = $goods; } else { $goods = $tempGoods[$index]['goods']; } if (!verifyWeight($value['num'], $goods['pricing_method'])) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个商品数量格式有误'; return $res; } $goods_empty_sell_status = self::$store['goods_empty_sell_status']; // 是否可以超库存出售 // 赠品库存 if ($value['goods_gifts_id']) { $goods_empty_sell_status = 1; // 赠品不进行此库存判断 } //自动拆分 $goods_map['goods_id'] = $value['id']; if (!empty($value['sku'])) { $goods_map['sku_id'] = $value['sku'][0]['id']; } $goods_num = round($goods['goods_num'] - $value['num'], 3); if ($goods_empty_sell_status == 0 && $goods_num < 0 && self::$store['is_resolution'] == 1) { $splitMakeData = array( 'supplier_type' => self::$store['supplier_type'], 'operation_type' => '1', 'split_type' => '2', 'operation_num' => abs($goods_num), 'store_id' => self::$store_id, 'merchant_id' => self::$merchant_id, 'small_goods_id' => $value['id'], 'small_sku_id' => $value['sku'][0]['id'], 'operator_type' => '2', 'operator_id' => self::$cashier_id, ); $split_res = $SplitMakerModel->goodsSplitMakeUp($splitMakeData); if ($split_res['status'] != 1) { $res['status'] = 0; $res['split_res'] = $split_res; $res['msg'] = '第' . ($k + 1) . '个商品' . $goods['goods_name'] . '库存不足' . $value['num']; return $res; } $goods['goods_num'] += abs($goods_num); } //串号验证 if ($value['serial_number_state'] == 1) { if (empty($value['serial_list'])) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个商品串号有误'; return $res; } $serialResult = $SerialClass->serialVerify([ 'serial_list' => $value['serial_list'], 'data' => $serial_arr ]); if ($serialResult['status'] != 1) { $serialResult['msg'] = '第' . ($k + 1) . '商品' . $goods['goods_name'] . $serialResult['message']; return $serialResult; } $serial_arr = $serialResult['data']; $temp['serial_list'] = $value['serial_list']; $temp['serial_number_state'] = $value['serial_number_state']; } if (!$value['goods_gifts_id'] && (float)$goods['goods_num'] < (float)$tempGoods[$index]['num'] && $goods_empty_sell_status == 0) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个商品' . $goods['goods_name'] . (!empty($value['sku']) ? ('(' . $value['sku'][0]['name'] . ')') : '') . '库存不足' . $value['num']; return $res; } else if (!$value['goods_gifts_id'] && !empty($value['sku']) && $goods['attribute_state'] == 1) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个商品' . $goods['goods_name'] . '规格已关闭'; return $res; } else if (!$value['goods_gifts_id'] && empty($value['sku']) && $goods['attribute_state'] == 2 && $goods['pricing_method'] == 1) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个商品' . $goods['goods_name'] . '开启规格,请刷新选择规格'; return $res; } else { if (!empty($value['sku'])) { $map = [ 'goods_id' => $value['id'], 'id' => $value['sku'][0]['id'], 'state' => 0 ]; $sku = $SkuGoodsModel->getFind($map); if (empty($sku)) { $res['status'] = 0; $res['msg'] = '第' . ($k + 1) . '个规格不存在,请刷新商品重试'; return $res; } $temp['goods_img_id'] = $sku['goods_img_id']; $temp['purchase_price'] = $sku['purchase_price']; $map = [ 'id' => ['in', $sku['sku_id_list']] ]; $name = $SkuListModel->getFindField($map, 'name', true); $sku['name'] = implode(',', $name); $temp['sku_id'] = $sku['id']; $temp['sku_json_info'] = json_encode($sku); $temp['attribute_state'] = 2; $temp['original_price'] = $sku['retail_price']; $sell_number = $sku['sku_num']; } else { $temp['goods_img_id'] = $goods['goods_img_id']; $temp['purchase_price'] = $goods['purchase_price']; $temp['original_price'] = $goods['retail_price']; $sell_number = 1; } $temp['goods_id'] = $value['id']; $temp['goods_name'] = $goods['goods_name']; $temp['goods_class_id'] = $goods['goods_class_id']; $temp['pricing_method'] = $goods['pricing_method']; $temp['goods_type'] = 1; } } //条码秤价格 if ($code_price_state == 1 && $value['code_price_state'] == 1) { $money = $value['original_price']; $temp['original_price'] = round($money / $value['num'], 2); } else { $money = round($value['num'] * $temp['original_price'], 2); } $money = $money > 0.01 ? $money : 0.01; if ($value['goods_gifts_id']) { $money = 0; $temp['original_price'] = 0; } $temp['type'] = 2; $temp['num'] = $value['num']; $temp['cashier_manual_discount'] = floatval($value['cashier_manual_discount']) ? $value['cashier_manual_discount'] : 1; $temp['add_time'] = $time; $temp['store_id'] = self::$store['id']; $temp['merchant_id'] = self::$store['merchantid']; $temp['all_money'] = $money; $temp['sell_number'] = round($sell_number * $value['num'], 3); $temp['company'] = $value['company'] ? $value['company'] : ''; $temp['goods_gifts_id'] = $value['goods_gifts_id']; $temp['goods_gifts_type'] = $value['goods_gifts_id'] ? 0 : null; $temp['serial_number_state'] = $value['serial_number_state'] ? $value['serial_number_state'] : 0; $temp['code_price_state'] = $value['code_price_state'] ? $value['code_price_state'] : 0; $endGoods[] = $temp; $allMoney += $money; unset($temp); } $allMoney = round($allMoney, 2); $res['status'] = 1; $res['msg'] = '验证成功'; $res['allMoney'] = $allMoney; $res['data'] = $endGoods; return $res; } /** * 2019-6-18 * 支付 */ public function orderPay_v1() { $order_no = I('post.order_no'); //订单编号 $out_trade_no = I('post.out_trade_no'); //微信刷脸支付订单内部流水号 $vid = I('post.vid'); //会员id $pay_type = I('post.pay_type'); //支付类型 1微信 2支付宝 3现金 5其他 6余额 $all_money = I('post.all_money'); //前台计算的总金额 $amount_received = I('post.amount_received'); //实收 $coupon_id = I('post.coupon_id'); //优惠券id $discount = I('post.discount'); //收银员手动打折折扣值 $small_changes_money = I('post.changes_money'); //抹零金额 $guide_id = I('post.guide_id'); //导购员id $pay_money = I('post.pay_money'); //支付金额 $vip_login_type = I('post.vip_login_type'); //会员登录方式 $card_code = I('post.card_code'); //会员动态码 $code = I('post.code'); //支付宝、微信付款码 $face_token = I('post.face_token'); //刷脸支付面部信息 $password = I('post.password'); //会员支付密码 $is_zero_storage = I('post.is_zero_storage'); //是否存零 1是 0否 $other_pay_id = I('post.other_pay_id'); //其他支付id $combine_pay = I('post.combine_pay'); //是否组合支付 1是 0否 $integral_cashing = I('post.integral_cashing'); //是否使用积分抵扣 1是 0否 $openid = I('post.openid'); //微信刷脸支付用户openid if (empty($order_no)) { $result['status'] = 0; $result['msg'] = '缺少必要参数order_no'; $this->ajaxReturn($result); } if (!in_array($pay_type, array('1', '2', '3', '5', '6'))) { $result['status'] = 0; $result['msg'] = '支付类型有误'; $this->ajaxReturn($result); } if (!in_array($combine_pay, array('0', '1'))) { $result['status'] = 0; $result['msg'] = '支付方式有误'; $this->ajaxReturn($result); } if (!in_array($integral_cashing, array('0', '1'))) { $result['status'] = 0; $result['msg'] = '积分使用方式有误'; $this->ajaxReturn($result); } $store_id = self::$store_id; $merchant_id = self::$merchant_id; //判断其他支付 if ($pay_type == 5) { $MerchantConfigModel = D('RetailMerchantConfig'); $merchantSet = $MerchantConfigModel->getFind(array($MerchantConfigModel::MERCHANT_ID => $merchant_id)); if (!empty($merchantSet) && $merchantSet[$MerchantConfigModel::OTHER_PAY_STATE] != 1) { $result['status'] = 0; $result['msg'] = '商家已关闭其他支付,请重新登录'; $this->ajaxReturn($result); } $OtherPayModel = D('RetailOtherPay'); if (!empty($other_pay_id)) { $oMap = array( $OtherPayModel::STATE => 1, $OtherPayModel::STATUS => 1, $OtherPayModel::ID => $other_pay_id, $OtherPayModel::MERCHANT_ID => self::$merchant_id ); $otherPay = $OtherPayModel->getFind($oMap); if (empty($otherPay)) { $result['status'] = 0; $result['msg'] = '该支付方式不存在'; $this->ajaxReturn($result); } } else { $oMap = array( $OtherPayModel::STATE => 1, $OtherPayModel::STATUS => 1, $OtherPayModel::MERCHANT_ID => self::$merchant_id ); $otherPay = $OtherPayModel->getCount($oMap); if (!empty($otherPay)) { $result['status'] = 0; $result['msg'] = '请选择其他支付'; $this->ajaxReturn($result); } } } //验证支付金额 if (!verifyPrice($pay_money) || !verifyPrice($amount_received) || floatval($amount_received) < floatval($pay_money) || !verifyPrice($all_money)) { $result['status'] = 0; $result['msg'] = '支付金额有误'; $this->ajaxReturn($result); } if ($combine_pay == 0 && floatval($all_money) > floatval($pay_money)) { $result['status'] = 0; $result['msg'] = '您需要一次付清'; $this->ajaxReturn($result); } if (!empty($guide_id)) { $GuideModel = D('RetailGuide'); $guide = $GuideModel->getFind(array($GuideModel::ID => $guide_id, $GuideModel::STORE_ID => $store_id)); if (empty($guide)) { $result['status'] = 0; $result['msg'] = '导购员信息有误'; $this->ajaxReturn($result); } } $config = array(); $PayOrderModel = D('RetailPayOrder'); $tempPayType = ''; switch ($pay_type) { case '1': case '2': //微信 //支付宝 if (!empty($code)) { //刷卡支付、付款码支付 $map = array( $PayOrderModel::STORE_ID => $store_id, $PayOrderModel::CODE => $code ); $historyOrder = $PayOrderModel->getFind($map); if (!empty($historyOrder)) { $result['status'] = 0; $result['msg'] = '请用户刷新付款码'; $this->ajaxReturn($result); } $text = substr($code, 0, 2); if ($text >= 25 && $text <= 30) { //支付宝付款码支付 $pay_type = 2; } else if ($text >= 10 && $text <= 15) { //微信的付款码支付 $pay_type = 1; } else { $result['status'] = 0; $result['msg'] = "请扫描正确的二维码"; $this->ajaxReturn($result); } $Order[$PayOrderModel::CODE] = $code; } else if (!empty($face_token)) { if ($pay_type == 1) { //微信刷脸支付 if (empty($openid)) { $result['status'] = 0; $result['msg'] = '缺少用户openid'; $this->ajaxReturn($result); } $Order[$PayOrderModel::OPENID] = $openid; } else { //支付宝刷脸支付 } $map = array( $PayOrderModel::STORE_ID => $store_id, $PayOrderModel::FACE_TOKEN => $face_token ); $historyOrder = $PayOrderModel->getFind($map); if (!empty($historyOrder)) { $result['status'] = 0; $result['msg'] = '请用户重新刷脸支付'; $this->ajaxReturn($result); } $Order[$PayOrderModel::FACE_TOKEN] = $face_token; } else { $result['status'] = 0; $result['msg'] = '缺少支付参数code或face_token'; $this->ajaxReturn($result); } if ($pay_type == 1) { $tempPayType = 3; } else { $tempPayType = 4; } $PaySetModel = D('RetailPaySet'); $paySet = $PaySetModel->getFindMerchantConfig($merchant_id, $pay_type); if ($paySet['status'] != 1) { $result['status'] = 0; $result['msg'] = $paySet['msg']; $this->ajaxReturn($result); } $config = $paySet['config']; $MerchantModel = D('RetailMerchant'); $StoreModel = D('RetailStore'); $config['body'] = self::$merchant[$MerchantModel::NAME] . '-' . self::$store[$StoreModel::STORE_NAME]; $Order[$PayOrderModel::M_PAY_TYPE] = $paySet['m_pay_type']; break; case '3': //现金 $tempPayType = 1; if (!verifyPrice($amount_received)) { $result['status'] = 0; $result['msg'] = '实付金额不正确'; $this->ajaxReturn($result); } if ($is_zero_storage && ($is_zero_storage != 1 || !$vid)) { $result['status'] = 0; $result['msg'] = '存零参数有误'; $this->ajaxReturn($result); } break; case '5': //其他支付 $tempPayType = 7; if (!empty($other_pay_id)) { $Order[$PayOrderModel::OTHER_PAY_ID] = $other_pay_id; } break; case '6': //会员卡余额 $tempPayType = 5; if (empty($vid) || empty($vip_login_type)) { $result['status'] = 0; $result['msg'] = '会员参数有误'; $this->ajaxReturn($result); } break; } $BillModel = D('RetailBillOrder'); $map = array( $BillModel::ORDER_NO => strval($order_no), $BillModel::STORE_ID => $store_id, $BillModel::PAY_STATE => array('in', '0,2'), //未支付或待支付状态 $BillModel::REFUND_MONEY => 0, ); $billOrder = $BillModel->getFind($map); if (empty($billOrder)) { $result['status'] = 0; $result['msg'] = '订单不存在'; $this->ajaxReturn($result); } $history = S('bill_order_no_' . $billOrder[$BillModel::ORDER_NO]); //获取缓存 if (!empty($history)) { $result['status'] = 0; $result['msg'] = '请勿频繁操作!'; $this->ajaxReturn($result); } S('bill_order_no_' . $billOrder[$BillModel::ORDER_NO], $billOrder[$BillModel::ORDER_NO], 2); //写入2秒的缓存,防止用户频繁点击 if ($billOrder[$BillModel::PAY_STATE] == 0) { //未计算钱并且金额发生改变 $item = array( 'bill_order' => $billOrder, 'vid' => $vid, //会员id 'coupon_id' => $coupon_id, //优惠券id 'discount' => $discount, //收银员手动打折折扣值 'small_changes_money' => $small_changes_money, //抹零金额 'allMoney' => $all_money, //前台计算的总价 'integral_cashing' => $integral_cashing, //积分抵现开启状态 ); $calculation = $this->calculationOrder($item); //重新计算该付的金额 if ($calculation['status'] == 1) { $billOrder = $calculation['order']; $billOrder[$BillModel::GUIDE_ID] = $guide_id; $billOrder[$BillModel::PAY_STATE] = 2; $billOrder[$BillModel::PAY_TYPE] = $tempPayType; //1现金 3微信支付 4支付宝支付 5会员卡余额支付 6小程序商城微信支付 7其他支付方式 8组合支付 $update = $BillModel->updateOrder(array($BillModel::ID => $billOrder[$BillModel::ID]), $billOrder); if (round($billOrder[$BillModel::ALL_MONEY] - $pay_money) < 0) { $result['all_money'] = $billOrder[$BillModel::ALL_MONEY]; $result['status'] = 0; $result['msg'] = '支付金额超出订单总额1'; $this->ajaxReturn($result); } if (!$update) { $result['status'] = 0; $result['msg'] = '订单更新失败,请重试'; $this->ajaxReturn($result); } } else { $this->ajaxReturn($calculation); } } else { if (round($billOrder[$BillModel::ALL_MONEY] - $all_money, 2) != 0) { $result['status'] = 0; $result['msg'] = 'bill-前后台计算价格不一致,请检查 ' . $billOrder[$BillModel::ALL_MONEY] . ' ' . $all_money; $this->ajaxReturn($result); } $toUp = false; if ($billOrder[$BillModel::PAY_STATE] == 0) { $toUp = true; $billOrder[$BillModel::PAY_STATE] = 2; } if (round($billOrder[$BillModel::ALL_MONEY] - $pay_money, 2) > 0) { $toUp = true; $billOrder[$BillModel::PAY_TYPE] = 8; } else if (round($billOrder[$BillModel::ALL_MONEY] - $pay_money - $billOrder[$BillModel::PAY_MONEY], 2) == 0) { $toUp = true; $billOrder[$BillModel::PAY_TYPE] = $tempPayType; //1现金 3微信支付 4支付宝支付 5会员卡余额支付 6小程序商城微信支付 7其他支付方式 8组合支付 } else { $result['status'] = 0; $result['msg'] = '支付金额超出订单总额2'; $this->ajaxReturn($result); } if ($toUp) { $BillModel->updateOrder(array($BillModel::ID => $billOrder[$BillModel::ID]), $billOrder); } } if (round($billOrder[$BillModel::ALL_MONEY] - $billOrder[$BillModel::PAY_MONEY] - $pay_money, 2) < 0) { $result['status'] = 0; $result['msg'] = '支付金额超出订单总额3'; $this->ajaxReturn($result); } $Order[$PayOrderModel::TRADE_AMOUNT] = $pay_money; $Order[$PayOrderModel::ORDER_TYPE] = 1; $Order[$PayOrderModel::PAY_TYPE] = $pay_type; $Order[$PayOrderModel::STORE_ID] = $store_id; $Order[$PayOrderModel::MERCHANT_ID] = $merchant_id; $Order[$PayOrderModel::CASHIER_ID] = self::$cashier_id; $Order[$PayOrderModel::VID] = $billOrder[$BillModel::VID]; $Order[$PayOrderModel::ORDER_STATE] = 'WAIT_BUYER_PAY'; $Order[$PayOrderModel::ADD_TIME] = time(); $Order[$PayOrderModel::BILL_ID] = $billOrder[$BillModel::ID]; if (!empty($out_trade_no)) { $pay_order = $Order; $pay_order_id = $PayOrderModel->getFindField(array($PayOrderModel::OUT_TRADE_NO => $out_trade_no), "id"); if (!$pay_order_id) { $res['status'] = 0; $res['msg'] = "未找到内部订单"; $this->ajaxReturn($res); } unset($Order[$PayOrderModel::ADD_TIME]); $pay_order_save = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $pay_order_id), $Order); if (!$pay_order_save) { $res['status'] = 0; $res['msg'] = "内部订单修改失败"; $this->ajaxReturn($res); } $pay_order[$PayOrderModel::ID] = $pay_order_id; $pay_order[$PayOrderModel::OUT_TRADE_NO] = $out_trade_no; } else { $pay_order = $PayOrderModel->addPayOrder($Order); } if (!$pay_order) { $result['status'] = 0; $result['msg'] = '创建订单失败,请重试'; $this->ajaxReturn($result); } $Order[$PayOrderModel::ID] = $pay_order[$PayOrderModel::ID]; $Order[$PayOrderModel::OUT_TRADE_NO] = $pay_order[$PayOrderModel::OUT_TRADE_NO]; switch ($pay_type) { case '1': case '2': //微信 //支付宝 if ($Order[$PayOrderModel::M_PAY_TYPE] == 3) { //新大陆支付 $res = A('Pay/PayContinent')->toContinentPay_v3($Order, $config['body']); } else if ($Order[$PayOrderModel::M_PAY_TYPE] == 4) { //新大陆支付 $payObj = new CloudPayController(); $res = $payObj->soltCard($Order, $config, $pay_type); } else { if ($pay_type == 1) { //微信 $res = A('Pay/NewWxPay')->toWxPay_v2($Order, $config); } else { //支付宝 $res = A('Pay/NewAliPay')->toAliPay_v2($Order, $config); } } if ($res['status'] == 1) { $item = array( 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder ); $result = $this->updateBillOrder($item); if ($res['status'] == 2) { $result['msg'] = $res['msg'] . ',' . $result['msg']; } } else { $result = $res; } break; case '3': case '5': //现金 //其他支付 $save = array( $PayOrderModel::ORDER_STATE => 'SUCCESS', $PayOrderModel::TRADE_DATE => time(), ); $up = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $pay_order[$PayOrderModel::ID]), $save); if (!$up) { $result['status'] = 0; $result['msg'] = '数据库发成故障,支付流水表更新失败,请重新支付'; } else { $itemData = array( 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder, 'is_zero_storage' => $is_zero_storage, 'pay_type' => 3 ); $result = $this->updateBillOrder($itemData); } break; case '6': //会员余额 $res = $this->toVipPay_v1($Order, $password, $vip_login_type, $card_code); if ($res['status'] == 1 || $res['status'] == 2) { $result = $this->updateBillOrder(array('pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder)); if ($res['status'] == 2) { $result['msg'] .= $res['msg']; } } else { $result = $res; } break; } $result['all_money'] = $billOrder[$BillModel::ALL_MONEY]; if ($result['status'] == 0 && $billOrder[$BillModel::PAY_MONEY] == 0) { $BillModel->updateOrder($map, array($BillModel::PAY_STATE => 0)); } $this->ajaxReturn($result); } /** * 会员卡支付 2019-6-3 会员免密支付 * @param $Order pay_order * @param $password 会员支付密码 * @param $vipLoginType 会员登录方式 1动态码 2手机号或实体卡号 * @param $cardCode 会员动态码或手机号实体卡号 * @return mixed */ protected function toVipPay_v1($Order, $password, $vipLoginType, $cardCode) { $VipModel = D('RetailVip'); $PayOrderModel = D('RetailPayOrder'); $vipMap = array( $VipModel::ID => $Order[$PayOrderModel::VID] ); $vipInfo = $VipModel->getFind($vipMap); if ($vipLoginType == 1) { $DynamicModel = D('RetailVipDynamicCode'); $map = array( $DynamicModel::CODE => $cardCode, $DynamicModel::STATUS => 1, $DynamicModel::STATE => 0, $DynamicModel::MERCHANT_ID => $Order[$PayOrderModel::MERCHANT_ID], $DynamicModel::USE_STORE_ID => $Order[$PayOrderModel::STORE_ID], $DynamicModel::USE_CASHIER_ID => $Order[$PayOrderModel::CASHIER_ID] ); $old = $DynamicModel->getFind($map); if (empty($old) || $old[$DynamicModel::FAILURE_TIME] <= time()) { $result['status'] = 0; $result['msg'] = '请会员刷新动态码'; return $result; } else { $DynamicModel->updateOrder($map, array($DynamicModel::STATUS => 2)); } } else { $store = self::$store; $StoreModel = D('RetailStore'); if ($vipInfo[$VipModel::DENSITY_FREE] != 1 && !$store[$StoreModel::DENSITY_FREE_STATE]) { //非免密 if (empty($password)) { $result['status'] = 3; $result['msg'] = '请输入会员支付密码'; return $result; } } else { $password = null; } } $result = A('PayApi/VipPay')->pay($Order[$PayOrderModel::BILL_ID], 1, 2, $password, $Order[$PayOrderModel::ID]); return $result; } /** * 计算总付款金额,拆单 * @param array $item * @return mixed * $item['bill_order'] bill表 id、all_money、pay_money、order_id * $item['vid'] 会员id * $item['coupon_id'] 优惠券id * $item['discount'] 收银员手动打折折扣值 * $item['small_changes_money'] 抹零金额 */ protected function calculationOrder($item = array()) { $BillModel = D('RetailBillOrder'); $discount = 1; //收银员手动折扣 $v_discount = 1; //会员等级折扣 $vid = 0; //会员id $coupon = array(); //优惠券 $small_changes_money = 0; //抹零金额 $lessMoney = 0; //存储最终支付的金额 $use_integral = 0; //会员消费该扣除的积分 $vipLessMoney = 0; //会员价减少的金额 $vipDisMoney = 0; //会员折扣的金额 $cashier_manual_discount = $item['cashier_manual_discount']; //收银员手动折扣--单品 $RetailCashierModel = D('RetailCashier'); if ($cashier_manual_discount < 1) { if (self::$cashier[$RetailCashierModel::MANUAL_DISCOUNT_STATE] != 1) { $res['status'] = 0; $res['msg'] = '您没有打折的权限'; return $res; } // 单品折扣 if (!verifyNumber($cashier_manual_discount, 2, 0.01, 1)) { $res['status'] = 0; $res['msg'] = '商品手动打折值有误!'; return $res; } } if (!empty($item['discount'])) { if (!verifyNumber($item['discount'], 3, 0.001, 1)) { $res['status'] = 0; $res['msg'] = '折扣值取值有误'; return $res; } if ($item['discount'] < 1) { if (self::$cashier[$RetailCashierModel::PREFERENTIAL_STATE] != 1) { $res['status'] = 0; $res['msg'] = '您没有打折的权限'; return $res; } } $discount = $item['discount']; } $merchant_id = self::$merchant_id; $store = self::$store; $StoreModel = D('RetailStore'); //查找会员 if (!empty($item['vid'])) { $VipModel = D('RetailVip'); $map = array( $VipModel::MERCHANT_ID => $merchant_id, $VipModel::STATUS => 1, $VipModel::ID => $item['vid'] ); $fields = array( $VipModel::ID, $VipModel::GRADE_INTEGRAL, $VipModel::INTEGRAL, $VipModel::PLUS_TYPE ); $vip = $VipModel->getFind($map, $fields); if (empty($vip)) { $res['status'] = 0; $res['msg'] = '会员信息有误'; return $res; } $vid = $item['vid']; //查找会员等级 if ($store[$StoreModel::PREFERENTIAL_STATE] == 1 || ($discount == 1 && $store[$StoreModel::PREFERENTIAL_STATE] != 1)) { //门店开启折上折 $VipIntegralModel = D('RetailVipIntegral'); $map = array( $VipIntegralModel::MERCHANT_ID => $merchant_id, $VipIntegralModel::DISCOUNT => array('gt', 0) ); if ($vip[$VipModel::PLUS_TYPE] == 2) { //plus会员 $grade = $VipIntegralModel->getOrderFind($map, '*', $VipIntegralModel::DISCOUNT . ' asc'); } else { //常规会员 $map[$VipIntegralModel::MIN_INTEGRAL] = array('ELT', $vip[$VipModel::GRADE_INTEGRAL]); $grade = $VipIntegralModel->getOrderFind($map, '*', $VipIntegralModel::MIN_INTEGRAL . ' desc'); } if (!empty($grade)) { $v_discount = $grade[$VipIntegralModel::DISCOUNT]; } if (!verifyNumber($v_discount, 3, 0.001, 1)) { $res['status'] = 0; $res['msg'] = '会员等级折扣值有误'; return $res; } } //判断优惠券 if (!empty($item['coupon_id'])) { if (empty($vip)) { $res['status'] = 0; $res['msg'] = '非会员请勿使用优惠券'; return $res; } $time = time(); $VipMCouponModel = D('RetailVipMCoupon'); $map = array( 'v.' . $VipMCouponModel::IS_USE => 1, 'v.' . $VipMCouponModel::VID => $vid, 'v.' . $VipMCouponModel::START_TIME => array("LT", $time), 'v.' . $VipMCouponModel::END_TIME => array("GT", $time), 'v.' . $VipMCouponModel::MERCHANT_ID => $merchant_id, 'v.' . $VipMCouponModel::ID => $item['coupon_id'], ); $fields = array( 'v.' . $VipMCouponModel::ID, 'v.' . $VipMCouponModel::NEED_MONEY, 'v.' . $VipMCouponModel::REDUCE_MONEY, 'v.' . $VipMCouponModel::ID, ); $couponRes = $VipMCouponModel->getFindUseCoupon($map, self::$store_id, $fields); if ($couponRes['status'] == 1) { $coupon = $couponRes['coupon']; } else { return $couponRes; } } } //判断抹零 if (!empty($item['small_changes_money'])) { if (!verifyPrice($item['small_changes_money'])) { $res['status'] = 0; $res['msg'] = '抹零金额有误'; return $res; } $small_changes_money = $item['small_changes_money']; } $SellModel = D('RetailSell'); $map = array( 's.' . $SellModel::BILL_ID => $item['bill_order'][$BillModel::ID] ); $GoodsModel = D('RetailGoods'); $fields = array( 's.*', 's.original_price as before_price', 'g.' . $GoodsModel::STORE_MEM_STATUS, 'g.' . $GoodsModel::STORE_MEM_PRICE, 'g.' . $GoodsModel::VIP_RETAIL_STATE, 'g.' . $GoodsModel::PLUS_PRICE, 'g.' . $GoodsModel::PLUS_MEM_STATUS, ); //获取sell详情 $sellOrder = $SellModel->getSelectAndGoods($map, $fields, 's.original_price asc'); if (empty($sellOrder)) { $res['status'] = 0; $res['msg'] = '订单信息异常'; return $res; } //会员打折或者把价格改为会员价 $SkuModel = D('RetailSkuGoods'); foreach ($sellOrder as &$val) { $before_money = round($val['original_price'] * $val[$SellModel::NUM], 2); $price = $val['original_price']; //原价 if (!empty($val[$SellModel::SKU_JSON_INFO])) { //存在商品规格 $sku = json_decode($val[$SellModel::SKU_JSON_INFO], true); if ($val[$GoodsModel::PLUS_MEM_STATUS] == 1) { //开启plus会员价 $val[$GoodsModel::PLUS_PRICE] = $sku[$SkuModel::PLUS_PRICE]; } if ($val[$GoodsModel::STORE_MEM_STATUS] == 1) { //开启普通会员价 $val[$GoodsModel::STORE_MEM_PRICE] = $sku[$SkuModel::STORE_MEM_PRICE]; } } if (!empty($vip) && $val[$GoodsModel::PLUS_MEM_STATUS] == 1 && round($val[$GoodsModel::PLUS_PRICE], 2) > 0 && $vip[$VipModel::PLUS_TYPE] == 2) { //开启plus会员价 //开启plus会员价并且价格设置正确并且用户是plus会员 $val[$SellModel::PLUS_VIP_PRICE] = $val[$GoodsModel::PLUS_PRICE]; //记录plus会员价 $price = $val[$SellModel::PLUS_VIP_PRICE]; } else if (!empty($vip) && $val[$GoodsModel::STORE_MEM_STATUS] == 1 && round($val[$GoodsModel::STORE_MEM_PRICE], 2) > 0) { //开启会员价并且价格设置正确 $val[$SellModel::VIP_PRICE] = $val[$GoodsModel::STORE_MEM_PRICE]; //记录会员价 $price = $val[$SellModel::VIP_PRICE]; } else { $val[$SellModel::PLUS_VIP_PRICE] = 0; $val[$SellModel::VIP_PRICE] = 0; } $center_money = round($price * $val[$SellModel::NUM], 2); //计算会员价优惠之后的价格 $val[$SellModel::VIP_LESS_MONEY] = round($before_money - $center_money, 2); //存储会员优惠价 $val[$SellModel::PRICE] = $price; if ($v_discount < 1 && $val[$GoodsModel::VIP_RETAIL_STATE] == 1) { //开启会员打折 $price = round($price * $v_discount, 2); //计算打折之后的价格 $price = $price > 0.01 ? $price : 0.01; //打折之后单价最低为0.01元 $val[$SellModel::VIP_DISCOUNT] = $v_discount; //存储折扣值 } $val[$SellModel::VID] = $vid; //存储会员id $money = round($price * $val[$SellModel::NUM], 2); //计算打折后总价 $val[$SellModel::VIP_DIS_MONEY] = round($center_money - $money, 2); //计算会员打折优惠的价格 // 收银员手动打折 if ($cashier_manual_discount < 1) { $price = round($price * $cashier_manual_discount, 2); //计算打折之后的价格 $price = $price > 0.01 ? $price : 0.01; //打折之后单价最低为0.01元 CASHIER_MANUAL_DIS $val[$SellModel::CASHIER_MANUAL_DIS] = $cashier_manual_discount; //存储折扣值 // 存储折扣值? } $val[$SellModel::ALL_MONEY] = $money; $val[$SellModel::GET_INTEGRAL] = 0; $val[$SellModel::CASHIER_DIS_MONEY] = 0; $val[$SellModel::COUPON_MONEY] = 0; $val[$SellModel::INTEGRAL_DIS_MONEY] = 0; $val[$SellModel::SMALL_CHANGES_MONEY] = 0; $lessMoney += $val[$SellModel::ALL_MONEY]; //订单总价累加 $vipDisMoney += $val[$SellModel::VIP_DIS_MONEY]; //总打折优惠累加 $vipLessMoney += $val[$SellModel::VIP_LESS_MONEY]; //总打折优惠累加 } $sellOrder = ascSortByFields($sellOrder, $SellModel::PRICE); //①减优惠券 if (!empty($coupon)) { if ($lessMoney < round($coupon[$VipMCouponModel::NEED_MONEY], 2)) { $res['status'] = 0; $res['msg'] = '当前订单未达到该优惠券门槛'; return $res; } $sellOrder = reduceOrderMoney($sellOrder, floatval($coupon[$VipMCouponModel::REDUCE_MONEY]), $lessMoney, $SellModel::COUPON_MONEY); $lessMoney = round($lessMoney - $coupon[$VipMCouponModel::REDUCE_MONEY], 2); //整单减掉优惠券后剩余金额 //按照单价重新排序 $sellOrder = ascSortByFields($sellOrder, $SellModel::PRICE); } //②积分抵扣 $integralDisMoney = 0; if (!empty($vip) && $item['integral_cashing'] == 1 && $vip[$VipModel::INTEGRAL] > 0) { $ConfigModel = D('RetailMerchantConfig'); $Config = $ConfigModel->getFind(array($ConfigModel::MERCHANT_ID => $merchant_id)); if ($Config[$ConfigModel::INTEGRAL_ARRIVE_CASH] == 1 && $Config[$ConfigModel::INTEGRAL_ARRIVE_CASH_RATE] > 0 && $Config[$ConfigModel::INTEGRAL_ARRIVE_CASH_DATA] > 0) { //执行积分抵扣 $integral_money_rate = $Config[$ConfigModel::INTEGRAL_ARRIVE_CASH_DATA] * 100; //由于设置的是 integral_arrive_cash_data积分=0.01元 所以要乘以100 $integralDisMoney = round($lessMoney * $Config[$ConfigModel::INTEGRAL_ARRIVE_CASH_RATE], 2); $needIntegral = round($integralDisMoney * $integral_money_rate); if ($vip[$VipModel::INTEGRAL] >= $needIntegral) { $use_integral = $needIntegral; } else { $use_integral = $vip[$VipModel::INTEGRAL]; $integralDisMoney = round($use_integral / $integral_money_rate, 2); } $sellOrder = reduceOrderMoney($sellOrder, $integralDisMoney, $lessMoney, $SellModel::INTEGRAL_DIS_MONEY, $use_integral, $integral_money_rate); $lessMoney = round($lessMoney - $integralDisMoney, 2); $sellOrder = ascSortByFields($sellOrder, $SellModel::PRICE); } } //③收银员打折 $cashierDiscountMoney = 0; if ($discount < 1) { $nowMoney = round($lessMoney * $discount, 2); $cashierDiscountMoney = round($lessMoney - $nowMoney, 2); $sellOrder = reduceOrderMoney($sellOrder, $cashierDiscountMoney, $lessMoney, $SellModel::CASHIER_DIS_MONEY); $lessMoney = round($lessMoney - $cashierDiscountMoney, 2); //整单减掉优惠券后剩余金额 $sellOrder = ascSortByFields($sellOrder, $SellModel::PRICE); } //④抹零 if ($small_changes_money > 0) { $sellOrder = reduceOrderMoney($sellOrder, $small_changes_money, $lessMoney, $SellModel::SMALL_CHANGES_MONEY); $lessMoney = round($lessMoney - $small_changes_money, 2); //整单减掉优惠券后剩余金额 } //修改sell表 if (round($lessMoney - $item['allMoney'], 2) != 0) { $res['status'] = 0; $res['msg'] = 'sell-前后台计算价格不一致,请检查 ' . $lessMoney . ' ' . $item['allMoney']; return $res; } foreach ($sellOrder as $valueEnd) { $temp = $valueEnd; unset($temp[$GoodsModel::STORE_MEM_STATUS]); unset($temp[$GoodsModel::STORE_MEM_PRICE]); unset($temp[$GoodsModel::PLUS_MEM_STATUS]); unset($temp[$GoodsModel::PLUS_PRICE]); unset($temp[$GoodsModel::VIP_RETAIL_STATE]); unset($temp[$SellModel::ID]); unset($temp['before_price']); if (!empty($valueEnd[$SellModel::ID])) { $up = $SellModel->updateOrder(array($SellModel::ID => $valueEnd[$SellModel::ID]), $temp); } else { $up = $SellModel->addOrder($temp); } if ($up) { } else if ($valueEnd['before_price'] != $valueEnd['original_price']) { $res['status'] = 0; $res['msg'] = '订单更新失败'; return $res; } } $newBill = $item['bill_order']; $newBill[$BillModel::ALL_MONEY] = $lessMoney; $newBill[$BillModel::VID] = $vid ? $vid : 0; $newBill[$BillModel::GUIDE_ID] = $item['guide_id'] ? $item['guide_id'] : 0; $BillDisModel = D('RetailBillDiscount'); $dMap = array( $BillDisModel::BILL_ID => $item['bill_order'][$BillModel::ID], ); $billDis = array( $BillDisModel::USE_INTEGRAL => $use_integral, $BillDisModel::VIP_DIS_MONEY => $vipDisMoney, $BillDisModel::VIP_LESS_MONEY => $vipLessMoney, $BillDisModel::COUPON_ID => $item['coupon_id'] ? $item['coupon_id'] : 0, $BillDisModel::COUPON_TYPE => $item['coupon_id'] ? 2 : 0, $BillDisModel::SMALL_CHANGES_MONEY => $small_changes_money, $BillDisModel::COUPON_MONEY => $coupon ? $coupon[$VipMCouponModel::REDUCE_MONEY] : 0, $BillDisModel::DISCOUNT => $discount, $BillDisModel::CASHIER_DIS_MONEY => $cashierDiscountMoney, $BillDisModel::INTEGRAL_DIS_MONEY => $integralDisMoney ); $BillDisModel->updateOrder($dMap, $billDis); $res['status'] = 1; $res['order'] = $newBill; return $res; } /** * 更新订单及库存 * @param $item * @return mixed */ protected function updateBillOrder($item) { $pay_money = $item['pay_money']; $amount_received = $item['amount_received']; $billOrder = $item['billOrder']; $pay_type = $item['pay_type']; $is_zero_storage = $item['is_zero_storage']; $pay_coupon_id_str = $item['pay_coupon_id_str']; //代金券id $pay_coupon_fee = $item['pay_coupon_fee']; //代金券金额 $BillModel = D('RetailBillOrder'); $save = array( $BillModel::PAY_MONEY => round($billOrder[$BillModel::PAY_MONEY] + $pay_money, 2), $BillModel::AMOUNT_RECEIVED => round($billOrder[$BillModel::AMOUNT_RECEIVED] + $amount_received, 2), ); if ($pay_type == 3 && $is_zero_storage && $amount_received > $pay_money) { $save[$BillModel::ZERO_STORAGE] = round($amount_received - $pay_money + $billOrder[$BillModel::ZERO_STORAGE], 2); $billOrder[$BillModel::ZERO_STORAGE] = $save[$BillModel::ZERO_STORAGE]; } else if ($pay_type == 3 && $amount_received > $pay_money) { $save[$BillModel::GIVE_CHANGE] = round($amount_received - $pay_money + $billOrder[$BillModel::GIVE_CHANGE], 2); } //微信代金券的id 与优惠总金额 $billOrderMoney = $billOrder['all_money']; if (!empty($pay_coupon_id_str) && !empty($pay_coupon_fee)) { $billOrderMoney = round($billOrder['all_money'] - $pay_coupon_fee, 2); $save['all_money'] = $billOrderMoney; $save['pay_money'] = round($save['pay_money'] - $pay_coupon_fee, 2); $save['amount_received'] = round($save['amount_received'] - $pay_coupon_fee, 2); } $upBill = $BillModel->updateOrder(array($BillModel::ID => $billOrder[$BillModel::ID]), $save); if ($upBill) { if (round($save[$BillModel::PAY_MONEY] - $billOrderMoney, 2) == 0) { //订单已经支付完成 //开始更新商品 $res = A('PayApi/Order')->paySuccess_v1($billOrder[$BillModel::ID]); } else { $res['status'] = 2; $res['msg'] = '请继续支付'; } } else { $res['status'] = 0; $res['msg'] = '数据库发生错误,bill更新失败,请联系系统人员进行退款'; } return $res; } /** * 取消未完成支付的订单 */ public function cancelPayOrder_v1() { $order_no = I('post.order_no'); if (empty($order_no)) { $result['status'] = 0; $result['msg'] = '缺少订单号'; $this->ajaxReturn($result); } $BillModel = D('RetailBillOrder'); $map = array( $BillModel::ORDER_NO => strval($order_no), $BillModel::TYPE => 2, $BillModel::PAY_STATE => 2, $BillModel::REFUND_MONEY => 0, $BillModel::ORDER_TYPE => 1, $BillModel::REFUND_STATUS => 0 ); $billOrder = $BillModel->getFind($map); if (empty($billOrder)) { $result['status'] = 0; $result['msg'] = '您的操作有误'; $this->ajaxReturn($result); } $PayOrderModel = D('RetailPayOrder'); $map = array( $PayOrderModel::BILL_ID => $billOrder[$BillModel::ID], $PayOrderModel::ORDER_TYPE => 1, $PayOrderModel::ORDER_STATE => 'SUCCESS' ); $payOrder = $PayOrderModel->getSelect($map); if (empty($payOrder)) { $result['status'] = 0; $result['msg'] = '您的操作有误'; $this->ajaxReturn($result); } $history = S('refund_order_no_' . $billOrder[$BillModel::ORDER_NO]); //获取缓存 if (!empty($history)) { $result['status'] = 0; $result['msg'] = '请勿频繁操作!'; $this->ajaxReturn($result); } S('refund_order_no_' . $billOrder[$BillModel::ORDER_NO], $billOrder[$BillModel::ORDER_NO], 2); //写入2秒的缓存,防止用户频繁点击 $paySet = D('RetailPaySet')->getFindMerchantPaySet(self::$merchant_id); $errorArr = array(); foreach ($payOrder as $value) { $item = array( $PayOrderModel::PAY_ORDER_ID => $value[$PayOrderModel::ID], //'退款原订单id', $PayOrderModel::ORDER_TYPE => 1, //'1购买商品 $PayOrderModel::SCENE_TYPE => 2, //'1付款订单 2退款订单', $PayOrderModel::PAY_TYPE => $value[$PayOrderModel::PAY_TYPE], //'1微信 2支付宝 3现金 4小程序微信 5其他支付方式 6会员余额', $PayOrderModel::TRADE_AMOUNT => $value[$PayOrderModel::TRADE_AMOUNT], //'订单金额', $PayOrderModel::STORE_ID => $value[$PayOrderModel::STORE_ID], //'门店id', $PayOrderModel::MERCHANT_ID => $value[$PayOrderModel::MERCHANT_ID], //'商户id', $PayOrderModel::CASHIER_ID => $value[$PayOrderModel::CASHIER_ID], //'收银员id', $PayOrderModel::VID => $value[$PayOrderModel::VID], //'会员id', $PayOrderModel::ADD_TIME => time(), //'订单创建时间', $PayOrderModel::M_PAY_TYPE => $value[$PayOrderModel::M_PAY_TYPE], //'订单支付类型 1普通商户模式 2服务商模式 3新大陆模式', $PayOrderModel::BILL_ID => $value[$PayOrderModel::BILL_ID], //'bill表id', $PayOrderModel::OTHER_PAY_ID => $value[$PayOrderModel::OTHER_PAY_ID], //'其他支付id', ); $add = $PayOrderModel->addPayOrder($item); if ($add) { $item = $add; $item['before_amount'] = $value[$PayOrderModel::TRADE_AMOUNT]; //原订单支付金额 $item[$PayOrderModel::TRADE_NO] = $value[$PayOrderModel::TRADE_NO]; //原订单支付金额 switch ($value[$PayOrderModel::PAY_TYPE]) { case '1': case '2': //微信 //支付宝 if ($value[$PayOrderModel::M_PAY_TYPE] == 3) { //新大陆 $res = A('Pay/PayContinent')->toOrderRefund_v3($item); } else if ($value[$PayOrderModel::M_PAY_TYPE] == 4) { //云闪付 $item['before_trade_no'] = $value['out_trade_no']; $config = $paySet['config']; $payObj = new CloudPayController(); $res = $payObj->cloudRefund($item, $config, $item[$PayOrderModel::PAY_TYPE]); unset($item['before_trade_no']); } else if ($value[$PayOrderModel::PAY_TYPE] == 1) { //微信 if ($paySet['status'] != 1) { $result['status'] = 0; $result['msg'] = $paySet['msg']; $this->ajaxReturn($result); } $config = $paySet['config']; $res = A('Pay/NewWxPay')->toWxRefund_v2($item, $config); } else { //支付宝 if ($paySet['status'] != 1) { $result['status'] = 0; $result['msg'] = $paySet['msg']; $this->ajaxReturn($result); } $config = $paySet['config']; $res = A('Pay/NewAliPay')->toAliRefund_v2($item, $config); } break; case '3': case '5': //现金 //其他支付 $res = array( 'status' => 1 ); break; case '6': //会员余额 $res = A('PayApi/VipPay')->refundForVip_v1($item, 1, 5); break; default: $res = array( 'status' => 0 ); } if ($res['status'] == 1) { $save = array( $PayOrderModel::OUT_TRADE_NO => $item[$PayOrderModel::OUT_TRADE_NO], $PayOrderModel::ORDER_STATE => 'REFUND', $PayOrderModel::EXTEND_CONTENT => $res['data'] ? json_encode($res['data']) : '', $PayOrderModel::TRADE_DATE => time(), $PayOrderModel::REFUND_MONEY => $value[$PayOrderModel::TRADE_AMOUNT], ); $re = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $item[$PayOrderModel::ID]), $save); $BillModel->toSetInc(array($BillModel::ID => $billOrder[$BillModel::ID]), $BillModel::REFUND_MONEY, $value[$PayOrderModel::TRADE_AMOUNT]); if (!$re) { $errorArr[] = $value['id'] . '更新失败'; } } else { $errorArr[] = $res['msg']; } } else { $res['status'] = 0; $res['msg'] = '数据库发生错误'; $this->ajaxReturn($res); } } if ($errorArr) { $result['status'] = 0; $result['msg'] = implode(',', $errorArr); } else { $BillModel->updateOrder(array($BillModel::ID => $billOrder[$BillModel::ID]), array($BillModel::REFUND_STATUS => 2)); $result['status'] = 1; $result['msg'] = '订单取消成功并已退款'; } $this->ajaxReturn($result); } /** * 默认抹零方式 */ public function smallChange() { $store_id = self::$store_id;; // 门店id if (!$store_id) { $res['status'] = 0; $res['msg'] = '登录超时,请重新登录'; $this->ajaxReturn($res); } $retailStore = D('RetailStore'); $where[$retailStore::ID] = $store_id; $data = $retailStore::SMALL_CHANGE_STATE; $storeInfo = $retailStore->getFind($where, $data); if ($storeInfo) { $res['status'] = 1; $res['small_change_state'] = $storeInfo[$retailStore::SMALL_CHANGE_STATE]; } else { $res['status'] = 0; $res['msg'] = '未找到该门店,请查看后台门店状态'; } $this->ajaxReturn($res); } /** * 获取支付宝刷脸支付初始配置 */ public function getFacePayInitializeData() { $PaySetModel = D('RetailPaySet'); $merchant_id = self::$merchant_id; $paySet = $PaySetModel->getFindMerchantConfig($merchant_id, 2); if ($paySet['status'] != 1) { $result['status'] = 0; $result['msg'] = $paySet['msg']; $this->ajaxReturn($result); } $config = $paySet['config']; //获取商户微信配置信息 if ($paySet['m_pay_type'] != 1 && $paySet['m_pay_type'] != 2) { if ($paySet['m_pay_type'] == 3) { $result['status'] = 0; $result['msg'] = '新大陆模式暂不支持刷脸支付'; $this->ajaxReturn($result); } else if ($paySet['m_pay_type'] == 4) { $result['status'] = 0; $result['msg'] = '云闪付模式暂不支持刷脸支付'; $this->ajaxReturn($result); } else { $result['status'] = 0; $result['msg'] = '当前支付模式暂不支持刷脸支付'; $this->ajaxReturn($result); } } if ($paySet['m_pay_type'] == 1) { $appId = $config[$PaySetModel::AL_APPID]; $pid = $config[$PaySetModel::AL_SELLER_ID]; } else { $set = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); $appId = $set[$SystemConfigModel::AL_APPID]; $pid = $config[$PaySetModel::AL_SELLER_ID]; } if (empty($appId) || empty($pid)) { $result['status'] = 0; $result['msg'] = '商户支付参数未配置'; } else { $result['status'] = 1; $result['msg'] = '查询成功'; $result['data'] = array( 'appId' => $appId, 'pid' => $pid ); } $this->ajaxReturn($result); } /** * 支付宝刷脸凭证 * @throws \Exception */ public function customerSmilePay() { $keys = I('post.keys'); //设备机器参数 if (!empty($keys)) { $merchant_id = self::$merchant_id; $PaySetModel = D('RetailPaySet'); $paySet = $PaySetModel->getFindMerchantConfig($merchant_id, 2); $config = $paySet['config']; vendor('AopSDK.AopClient'); $item = json_decode(urldecode($keys), true); $aop = new \AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($paySet['m_pay_type'] == 1) { if (empty($config[$PaySetModel::AL_APPID]) || empty($config[$PaySetModel::AL_PRI_KEY]) || empty($config[$PaySetModel::AL_PUB_KEY])) { $result['status'] = 0; $result['msg'] = '商家支付宝参数未配置'; $this->ajaxReturn($result); } $aop->appId = $config[$PaySetModel::AL_APPID]; $aop->rsaPrivateKey = $config[$PaySetModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $config[$PaySetModel::AL_PUB_KEY]; } else { $AliPay = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); if (!empty($AliPay[$SystemConfigModel::AL_PID])) { $item['extend_params'] = array('sys_service_provider_id' => $AliPay[$SystemConfigModel::AL_PID]); } if (empty($AliPay[$SystemConfigModel::AL_APPID]) || empty($AliPay[$SystemConfigModel::AL_PRI_KEY]) || empty($AliPay[$SystemConfigModel::AL_PUB_KEY])) { $result['status'] = 0; $result['msg'] = '商家支付宝参数未配置'; $this->ajaxReturn($result); } $aop->appId = $AliPay[$SystemConfigModel::AL_APPID]; $aop->rsaPrivateKey = $AliPay[$SystemConfigModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $AliPay[$SystemConfigModel::AL_PUB_KEY]; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.ZolozAuthenticationCustomerSmilepayInitializeRequest'); $request = new \ZolozAuthenticationCustomerSmilepayInitializeRequest(); $request->setBizContent(json_encode($item)); if ($paySet['m_pay_type'] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config[$PaySetModel::AL_TOKEN]); } } else { $result['status'] = 0; $result['msg'] = '缺少参数'; } $this->ajaxReturn($result); } /** * 获取微信调用刷脸支付凭证 */ public function getWxPayFaceInfo() { $data = I("post."); $merchant_id = self::$merchant_id; $PaySetModel = D('RetailPaySet'); $paySet = $PaySetModel->getFindMerchantConfig($merchant_id, 1); if ($paySet['status'] == 0) { $this->ajaxReturn($paySet); } $config = $paySet['config']; if ($paySet['m_pay_type'] == 1) { //普通商户 $post['appid'] = $config['wx_appid']; //商户公众号appid $post['mch_id'] = $config['wx_mch_id']; //商户号 } else { //服务商 $systemConfig = $config['systemConfig']; $post['appid'] = $systemConfig['wx_appid']; //服务商公众号appid $post['mch_id'] = $systemConfig['mch_id']; //服务商商户号 if (!empty($config['wx_sub_appid'])) { $post['sub_appid'] = $config['wx_sub_appid']; //子商户公众号appid } $post['sub_mch_id'] = $config['wx_sub_mchid']; //子商户号 } $post['store_id'] = self::$store_id; //门店编号 商户定义,唯一 $post['store_name'] = self::$store['storetag']; //门店名称 商户定义 $post['device_id'] = $data['device_id']; //终端设备号 商户定义 $post['rawdata'] = $data['rawdata']; $post['now'] = time(); //当前时间 $post['version'] = 1; //版本号 固定为1 $post['sign_type'] = "MD5"; //签名类型 支持HMAC-SHA256和MD5 $post['nonce_str'] = createKey(); //随机字符串 不长于32位 if ($paySet['m_pay_type'] == 1) { $sign = createSign($post, $config['wx_secret_key']); } else { $sign = createSign($post, $systemConfig['wx_key']); } $post['sign'] = $sign; //签名 $url = "https://payapp.weixin.qq.com/face/get_wxpayface_authinfo"; $params = arrToXml($post); //考虑到与后台人脸支付API的一致性, 统一采用xml格式 $result = http_request($url, $params); $result = xmlToArray($result); if ($result['return_code'] == 'SUCCESS') { //请求成功 $return['status'] = 1; $return['pay_type'] = $paySet['m_pay_type']; //1:普通商户 2:服务商 $return['data'] = array( "authinfo" => $result['authinfo'], //调用凭证 "expires_in" => $result['expires_in'], //authinfo的有效时间, 单位秒。 例如: 3600;在有效时间内, 对于同一台终端设备,相同的参数的前提下(如:相同的公众号、商户号、 门店编号等),可以用同一个authinfo "appid" => $post['appid'], "mch_id" => $post['mch_id'], "sub_appid" => $post['sub_appid'] ? $post['sub_appid'] : "", "sub_mch_id" => $post['sub_mch_id'] ? $post['sub_mch_id'] : "" ); } else { $return['status'] = 0; $return['message'] = $result['return_msg']; } $this->ajaxReturn($return); } /** * 生成微信订单号 */ public function addPayOrderNumber() { $PayOrderModel = D("RetailPayOrder"); $Order[$PayOrderModel::PAY_TYPE] = 1; $Order[$PayOrderModel::STORE_ID] = self::$store_id; $Order[$PayOrderModel::MERCHANT_ID] = self::$merchant_id; $Order[$PayOrderModel::CASHIER_ID] = self::$cashier_id; $Order[$PayOrderModel::ORDER_STATE] = 'WAIT_BUYER_PAY'; $Order[$PayOrderModel::ADD_TIME] = time(); $pay_order = $PayOrderModel->addPayOrder($Order); if (!$pay_order) { $res['status'] = 0; $res['msg'] = "订单内部流水号生成失败"; $this->ajaxReturn($res); } $res['status'] = 1; $res['out_trade_no'] = $pay_order[$PayOrderModel::OUT_TRADE_NO]; $this->ajaxReturn($res); } /** * 2019-9-23 * 支付 */ public function OrderPay_v2() { $order_no = I('post.order_no'); //订单编号 $out_trade_no = I('post.out_trade_no'); //微信刷脸支付订单内部流水号 $vid = I('post.vid'); //会员id $pay_type = I('post.pay_type'); //支付类型 1微信 2支付宝 3现金 5其他 6余额 $all_money = I('post.all_money'); //前台计算的总金额 $amount_received = I('post.amount_received'); //实收 $coupon_id = I('post.coupon_id'); //优惠券id $discount = I('post.discount'); //收银员手动打折折扣值 $small_changes_money = I('post.changes_money'); //抹零金额 $guide_id = I('post.guide_id'); //导购员id $pay_money = I('post.pay_money'); //支付金额 $vip_login_type = I('post.vip_login_type'); //会员登录方式 $card_code = I('post.card_code'); //会员动态码 $code = I('post.code'); //支付宝、微信付款码 $face_token = I('post.face_token'); //刷脸支付面部信息 $password = I('post.password'); //会员支付密码 $is_zero_storage = I('post.is_zero_storage'); //是否存零 1是 0否 $other_pay_id = I('post.other_pay_id'); //其他支付id $combine_pay = I('post.combine_pay'); //是否组合支付 1是 0否 $integral_cashing = I('post.integral_cashing'); //是否使用积分抵扣 1是 0否 $openid = I('post.openid'); //微信刷脸支付用户openid if (empty($order_no)) { $result['status'] = 0; $result['msg'] = '缺少必要参数order_no'; $this->ajaxReturn($result); } if (!in_array($pay_type, array('1', '2', '3', '5', '6'))) { $result['status'] = 0; $result['msg'] = '支付类型有误'; $this->ajaxReturn($result); } if (!in_array($combine_pay, array('0', '1'))) { $result['status'] = 0; $result['msg'] = '支付方式有误'; $this->ajaxReturn($result); } if (!in_array($integral_cashing, array('0', '1'))) { $result['status'] = 0; $result['msg'] = '积分使用方式有误'; $this->ajaxReturn($result); } $store_id = self::$store_id; $merchant_id = self::$merchant_id; //判断其他支付 if ($pay_type == 5) { $MerchantConfigModel = D('RetailMerchantConfig'); $merchantSet = $MerchantConfigModel->getFind(array($MerchantConfigModel::MERCHANT_ID => $merchant_id)); if (!empty($merchantSet) && $merchantSet[$MerchantConfigModel::OTHER_PAY_STATE] != 1) { $result['status'] = 0; $result['msg'] = '商家已关闭其他支付,请重新登录'; $this->ajaxReturn($result); } if (!empty($other_pay_id)) { $OtherPayModel = D('RetailOtherPay'); $oMap = array( $OtherPayModel::STATE => 1, $OtherPayModel::STATUS => 1, $OtherPayModel::ID => $other_pay_id, $OtherPayModel::MERCHANT_ID => self::$merchant_id ); $otherPay = $OtherPayModel->getFind($oMap); if (empty($otherPay)) { $result['status'] = 0; $result['msg'] = '该支付方式不存在'; $this->ajaxReturn($result); } } } //验证支付金额 if (!verifyPrice($pay_money) || !verifyPrice($amount_received) || floatval($amount_received) < floatval($pay_money) || !verifyPrice($all_money)) { $result['status'] = 0; $result['msg'] = '支付金额有误'; $this->ajaxReturn($result); } if ($combine_pay == 0 && floatval($all_money) > floatval($pay_money)) { $result['status'] = 0; $result['msg'] = '您需要一次付清'; $this->ajaxReturn($result); } if (!empty($guide_id)) { $GuideModel = D('RetailGuide'); $guide = $GuideModel->getFind(array($GuideModel::ID => $guide_id, $GuideModel::STORE_ID => $store_id)); if (empty($guide)) { $result['status'] = 0; $result['msg'] = '导购员信息有误'; $this->ajaxReturn($result); } } $config = array(); $PayOrderModel = D('RetailPayOrder'); $tempPayType = ''; $StoreModel = D('RetailStore'); switch ($pay_type) { case '1': case '2': //微信 //支付宝 if (!empty($code)) { //刷卡支付、付款码支付 $map = array( $PayOrderModel::STORE_ID => $store_id, $PayOrderModel::CODE => $code ); $historyOrder = $PayOrderModel->getFind($map); if (!empty($historyOrder)) { $result['status'] = 0; $result['msg'] = '请用户刷新付款码'; $this->ajaxReturn($result); } $text = substr($code, 0, 2); if ($text >= 25 && $text <= 30) { //支付宝付款码支付 $pay_type = 2; } else if ($text >= 10 && $text <= 15) { //微信的付款码支付 $pay_type = 1; } else { $result['status'] = 0; $result['msg'] = "请扫描正确的二维码"; $this->ajaxReturn($result); } $Order[$PayOrderModel::CODE] = $code; } else if (!empty($face_token)) { if ($pay_type == 1) { //微信刷脸支付 if (empty($openid)) { $result['status'] = 0; $result['msg'] = '缺少用户openid'; $this->ajaxReturn($result); } $Order[$PayOrderModel::OPENID] = $openid; } else { //支付宝刷脸支付 } $map = array( $PayOrderModel::STORE_ID => $store_id, $PayOrderModel::FACE_TOKEN => $face_token ); $historyOrder = $PayOrderModel->getFind($map); if (!empty($historyOrder)) { $result['status'] = 0; $result['msg'] = '请用户重新刷脸支付'; $this->ajaxReturn($result); } $Order[$PayOrderModel::FACE_TOKEN] = $face_token; } else { $result['status'] = 0; $result['msg'] = '缺少支付参数code或face_token'; $this->ajaxReturn($result); } if ($pay_type == 1) { $tempPayType = 3; } else { $tempPayType = 4; } $PaySetModel = D('RetailPaySet'); $paySet = $PaySetModel->getFindMerchantConfig($merchant_id, $pay_type); if ($paySet['status'] != 1) { $result['status'] = 0; $result['msg'] = $paySet['msg']; $this->ajaxReturn($result); } $config = $paySet['config']; $MerchantModel = D('RetailMerchant'); $config['body'] = self::$merchant[$MerchantModel::NAME] . '-' . self::$store[$StoreModel::STORE_NAME]; $Order[$PayOrderModel::M_PAY_TYPE] = $paySet['m_pay_type']; break; case '3': //现金 $tempPayType = 1; if (!verifyPrice($amount_received)) { $result['status'] = 0; $result['msg'] = '实付金额不正确'; $this->ajaxReturn($result); } if ($is_zero_storage && ($is_zero_storage != 1 || !$vid)) { $result['status'] = 0; $result['msg'] = '存零参数有误'; $this->ajaxReturn($result); } break; case '5': //其他支付 $tempPayType = 7; if (!empty($other_pay_id)) { $Order[$PayOrderModel::OTHER_PAY_ID] = $other_pay_id; } break; case '6': //会员卡余额 $tempPayType = 5; if (empty($vid) || empty($vip_login_type)) { $result['status'] = 0; $result['msg'] = '会员参数有误'; $this->ajaxReturn($result); } break; } $BillModel = D('RetailBillOrder'); $map = array( $BillModel::ORDER_NO => strval($order_no), $BillModel::STORE_ID => $store_id, $BillModel::PAY_STATE => array('in', '0,2'), //未支付或待支付状态 $BillModel::REFUND_MONEY => 0, ); $billOrder = $BillModel->getFind($map); if (empty($billOrder)) { $result['status'] = 0; $result['msg'] = '订单不存在'; $this->ajaxReturn($result); } $history = S('bill_order_no_' . $billOrder[$BillModel::ORDER_NO]); //获取缓存 if (!empty($history)) { $result['status'] = 0; $result['msg'] = '请勿频繁操作!'; $this->ajaxReturn($result); } S('bill_order_no_' . $billOrder[$BillModel::ORDER_NO], $billOrder[$BillModel::ORDER_NO], 2); //写入2秒的缓存,防止用户频繁点击 if ($billOrder[$BillModel::PAY_STATE] == 0) { //未计算钱并且金额发生改变 $CashierModel = D('RetailCashier'); $item = array( 'bill_order' => $billOrder, 'vid' => $vid, //会员id 'coupon_id' => $coupon_id, //优惠券id 'discount' => $discount, //收银员手动打折折扣值 'small_changes_money' => $small_changes_money, //抹零金额 'allMoney' => $all_money, //前台计算的总价 'integral_cashing' => $integral_cashing, //积分抵现开启状态 'guide_id' => $guide_id, //导购员id 'cashier_preferential_state' => self::$cashier[$CashierModel::PREFERENTIAL_STATE], //收银员打折权限 'store_preferential_state' => self::$store[$StoreModel::PREFERENTIAL_STATE], //门店打折权限 'guide_open_state' => self::$store[$StoreModel::GUIDE_OPEN_STATE], //导购员分佣开启状态 ); $calculation = A('PayApi/Order')->calculationOrder_v1($item); //重新计算该付的金额 if ($calculation['status'] == 1) { $billOrder = $calculation['order']; if (round($billOrder[$BillModel::ALL_MONEY] - $pay_money) < 0) { $result['all_money'] = $billOrder[$BillModel::ALL_MONEY]; $result['status'] = 0; $result['msg'] = '支付金额超出订单总额1'; $this->ajaxReturn($result); } $billOrder[$BillModel::GUIDE_ID] = $guide_id; $billOrder[$BillModel::PAY_STATE] = 2; $billOrder[$BillModel::PAY_TYPE] = $tempPayType; //1现金 3微信支付 4支付宝支付 5会员卡余额支付 6小程序商城微信支付 7其他支付方式 8组合支付 $update = $BillModel->updateOrder(array($BillModel::ID => $billOrder[$BillModel::ID]), $billOrder); if (!$update) { $result['status'] = 0; $result['msg'] = '订单更新失败,请重试'; $this->ajaxReturn($result); } } else { $this->ajaxReturn($calculation); } } else { if (round($billOrder[$BillModel::ALL_MONEY] - $all_money, 2) != 0) { $result['status'] = 0; $result['msg'] = '前后台计算价格不一致,请检查 ' . $billOrder[$BillModel::ALL_MONEY] . ' ' . $all_money; $this->ajaxReturn($result); } $toUp = false; if ($billOrder[$BillModel::PAY_STATE] == 0) { $toUp = true; $billOrder[$BillModel::PAY_STATE] = 2; } if (round($billOrder[$BillModel::ALL_MONEY] - $pay_money, 2) > 0) { $toUp = true; $billOrder[$BillModel::PAY_TYPE] = 8; } else if (round($billOrder[$BillModel::ALL_MONEY] - $pay_money - $billOrder[$BillModel::PAY_MONEY], 2) == 0) { $toUp = true; $billOrder[$BillModel::PAY_TYPE] = $tempPayType; //1现金 3微信支付 4支付宝支付 5会员卡余额支付 6小程序商城微信支付 7其他支付方式 8组合支付 } else { $result['status'] = 0; $result['msg'] = '支付金额超出订单总额2'; $this->ajaxReturn($result); } if ($toUp) { $BillModel->updateOrder(array($BillModel::ID => $billOrder[$BillModel::ID]), $billOrder); } } if (round($billOrder[$BillModel::ALL_MONEY] - $billOrder[$BillModel::PAY_MONEY] - $pay_money, 2) < 0) { $result['status'] = 0; $result['msg'] = '支付金额超出订单总额3'; $this->ajaxReturn($result); } $Order[$PayOrderModel::TRADE_AMOUNT] = $pay_money; $Order[$PayOrderModel::ORDER_TYPE] = 1; $Order[$PayOrderModel::PAY_TYPE] = $pay_type; $Order[$PayOrderModel::STORE_ID] = $store_id; $Order[$PayOrderModel::MERCHANT_ID] = $merchant_id; $Order[$PayOrderModel::CASHIER_ID] = self::$cashier_id; $Order[$PayOrderModel::VID] = $billOrder[$BillModel::VID]; $Order[$PayOrderModel::ORDER_STATE] = 'WAIT_BUYER_PAY'; $Order[$PayOrderModel::ADD_TIME] = time(); $Order[$PayOrderModel::BILL_ID] = $billOrder[$BillModel::ID]; if (!empty($out_trade_no)) { $pay_order = $Order; $pay_order_id = $PayOrderModel->getFindField(array($PayOrderModel::OUT_TRADE_NO => $out_trade_no), "id"); if (!$pay_order_id) { $res['status'] = 0; $res['msg'] = "未找到内部订单"; $this->ajaxReturn($res); } unset($Order[$PayOrderModel::ADD_TIME]); $pay_order_save = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $pay_order_id), $Order); if (!$pay_order_save) { $res['status'] = 0; $res['msg'] = "内部订单修改失败"; $this->ajaxReturn($res); } $pay_order[$PayOrderModel::ID] = $pay_order_id; $pay_order[$PayOrderModel::OUT_TRADE_NO] = $out_trade_no; } else { $pay_order = $PayOrderModel->addPayOrder($Order); } if (!$pay_order) { $result['status'] = 0; $result['msg'] = '创建订单失败,请重试'; $this->ajaxReturn($result); } $Order[$PayOrderModel::ID] = $pay_order[$PayOrderModel::ID]; $Order[$PayOrderModel::OUT_TRADE_NO] = $pay_order[$PayOrderModel::OUT_TRADE_NO]; switch ($pay_type) { case '1': case '2': //微信 //支付宝 if ($Order[$PayOrderModel::M_PAY_TYPE] == 3) { //新大陆支付 $res = A('Pay/PayContinent')->toContinentPay_v3($Order, $config['body']); } elseif ($Order[$PayOrderModel::M_PAY_TYPE] == 4) { //新大陆支付 $payObj = new CloudPayController(); $res = $payObj->soltCard($Order, $config, $pay_type); } else { if ($pay_type == 1) { //微信 $res = A('Pay/NewWxPay')->toWxPay_v2($Order, $config); } else { //支付宝 $res = A('Pay/NewAliPay')->toAliPay_v2($Order, $config); } } if ($res['status'] == 1) { $item = array( 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder ); $result = $this->updateBillOrder($item); if ($res['status'] == 2) { $result['msg'] = $res['msg'] . ',' . $result['msg']; } } else { $result = $res; } break; case '3': case '5': //现金 //其他支付 $save = array( $PayOrderModel::ORDER_STATE => 'SUCCESS', $PayOrderModel::TRADE_DATE => time(), ); $up = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $pay_order[$PayOrderModel::ID]), $save); if (!$up) { $result['status'] = 0; $result['msg'] = '数据库发成故障,支付流水表更新失败,请重新支付'; } else { $itemData = array( 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder, 'is_zero_storage' => $is_zero_storage, 'pay_type' => 3 ); $result = $this->updateBillOrder($itemData); } break; case '6': //会员余额 $res = $this->toVipPay_v1($Order, $password, $vip_login_type, $card_code); if ($res['status'] == 1 || $res['status'] == 2) { $result = $this->updateBillOrder(array('pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder)); if ($res['status'] == 2) { $result['msg'] .= $res['msg']; } } else { $result = $res; } break; } $result['all_money'] = $billOrder[$BillModel::ALL_MONEY]; if ($result['status'] == 0 && $billOrder[$BillModel::PAY_MONEY] == 0) { $BillModel->updateOrder($map, array($BillModel::PAY_STATE => 0)); } $this->ajaxReturn($result); } /** * 2020-5-19积分抵现 * 2020-8-17 新增商家券营销插件 * 支付 */ public function OrderPay_v3() { $order_no = I('post.order_no'); //订单编号 $out_trade_no = I('post.out_trade_no'); //微信刷脸支付订单内部流水号 $vid = I('post.vid'); //会员id $pay_type = I('post.pay_type'); //支付类型 1微信 2支付宝 3现金 5其他 6余额 7赊账 8德育币 $all_money = I('post.all_money'); //前台计算的总金额 $amount_received = I('post.amount_received'); //实收 $coupon_id = I('post.coupon_id'); //优惠券id $coupon_type = I('post.coupon_type'); //优惠券类型 2商户优惠券 3门店商家券 $discount = I('post.discount'); //收银员手动打折折扣值 $small_changes_money = I('post.changes_money'); //抹零金额 $guide_id = I('post.guide_id'); //导购员id $pay_money = I('post.pay_money'); //支付金额 $vip_login_type = I('post.vip_login_type'); //会员登录方式 $card_code = I('post.card_code'); //会员动态码 $code = I('post.code'); //支付宝、微信付款码 $face_token = I('post.face_token'); //刷脸支付面部信息 $password = I('post.password'); //会员支付密码 $is_zero_storage = I('post.is_zero_storage'); //是否存零 1是 0否 $other_pay_id = I('post.other_pay_id'); //其他支付id $combine_pay = I('post.combine_pay'); //是否组合支付 1是 0否 $integral_cashing = I('post.integral_cashing'); //是否使用积分抵扣 1是 0否 $openid = I('post.openid'); //微信刷脸支付用户openid] $integral_dis_num = I('post.integral_dis_num') ? I('post.integral_dis_num') : 0; //扣除的总积分 $integral_dis_money = I('post.integral_dis_money') ? I('post.integral_dis_money') : 0; //抵现积分的金额 $business_remark = I('post.business_remark') ? I('post.business_remark') : ''; //订单备注 $balance_pay = I('post.balance_pay') ? I('post.balance_pay') : 2; //赊销订单是否先用余额支付 1 是 2 否 $editor_type = I('post.editor_type') ? I('post.editor_type') : 1; //操作场景 1收银员 2后台 $third_vip_code = I('post.third_vip_code') ? I('post.third_vip_code') : ''; //德育币会员ID $third_vip_name = I('post.third_vip_name') ? I('post.third_vip_name') : ''; //德育币会员名称 if (empty($order_no)) { $result['status'] = 0; $result['msg'] = '缺少必要参数order_no'; $this->ajaxReturn($result); } if (!in_array($pay_type, ['1', '2', '3', '5', '6', '7', '8'])) { $result['status'] = 0; $result['msg'] = '支付类型有误'; $this->ajaxReturn($result); } if (!in_array($combine_pay, ['0', '1'])) { $result['status'] = 0; $result['msg'] = '支付方式有误'; $this->ajaxReturn($result); } if (!in_array($integral_cashing, ['0', '1'])) { $result['status'] = 0; $result['msg'] = '积分使用方式有误'; $this->ajaxReturn($result); } $store_id = self::$store_id; $merchant_id = self::$merchant_id; //判断其他支付 if ($pay_type == 5) { $MerchantConfigModel = D('RetailMerchantConfig'); $merchantSet = $MerchantConfigModel->getFind(['merchant_id' => $merchant_id]); if (!empty($merchantSet) && $merchantSet['other_pay'] != 1) { $result['status'] = 0; $result['msg'] = '商家已关闭其他支付,请重新登录'; $this->ajaxReturn($result); } if (!empty($other_pay_id)) { $OtherPayModel = D('RetailOtherPay'); $oMap = [ 'state' => 1, 'status' => 1, 'id' => $other_pay_id, 'merchant_id' => self::$merchant_id ]; $otherPay = $OtherPayModel->getFind($oMap); if (empty($otherPay)) { $result['status'] = 0; $result['msg'] = '该支付方式不存在'; $this->ajaxReturn($result); } } } //验证支付金额 if (!verifyPrice($pay_money) || !verifyPrice($amount_received) || floatval($amount_received) < floatval($pay_money) || !verifyPrice($all_money)) { $result['status'] = 0; $result['msg'] = '支付金额有误'; $this->ajaxReturn($result); } if ($combine_pay == 0 && floatval($all_money) > floatval($pay_money) && $pay_type != 7) { $result['status'] = 0; $result['msg'] = '您需要一次付清'; $this->ajaxReturn($result); } if (!empty($guide_id)) { $GuideModel = D('RetailGuide'); $guide = $GuideModel->getFind(['id' => $guide_id, 'storeid' => $store_id]); if (empty($guide)) { $result['status'] = 0; $result['msg'] = '导购员信息有误'; $this->ajaxReturn($result); } } $config = []; $PayOrderModel = new RetailPayOrderModel(); $tempPayType = ''; $Plug = new Plug(); $result = $Plug->PlugPermissions([ 'merchant_id' => self::$merchant_id, 'identification_arr' => [ 'wxpaycoupon', 'moraleducationcoin' ] ]); //查询是否有代金券插件 switch ($pay_type) { case '1': case '2': //微信 //支付宝 if (!empty($code)) { //刷卡支付、付款码支付 $map = [ 'store_id' => $store_id, 'code' => $code ]; $historyOrder = $PayOrderModel->getFind($map); if (!empty($historyOrder)) { $result['status'] = 0; $result['msg'] = '请用户刷新付款码'; $this->ajaxReturn($result); } $text = substr($code, 0, 2); if ($text >= 25 && $text <= 30) { //支付宝付款码支付 $pay_type = 2; } else if ($text >= 10 && $text <= 15) { //微信的付款码支付 $pay_type = 1; } else { $result['status'] = 0; $result['msg'] = "请扫描正确的二维码"; $this->ajaxReturn($result); } $Order['code'] = $code; } else if (!empty($face_token)) { if ($pay_type == 1) { //微信刷脸支付 if (empty($openid)) { $result['status'] = 0; $result['msg'] = '缺少用户openid'; $this->ajaxReturn($result); } $Order['openid'] = $openid; } else { //支付宝刷脸支付 } $map = array( 'store_id' => $store_id, 'face_token' => $face_token ); $historyOrder = $PayOrderModel->getFind($map); if (!empty($historyOrder)) { $result['status'] = 0; $result['msg'] = '请用户重新刷脸支付'; $this->ajaxReturn($result); } $Order['face_token'] = $face_token; } else { $result['status'] = 0; $result['msg'] = '缺少支付参数code或face_token'; $this->ajaxReturn($result); } if ($pay_type == 1) { $tempPayType = 3; } else { $tempPayType = 4; } $PaySetModel = D('RetailPaySet'); $paySet = $PaySetModel->getFindMerchantConfig($merchant_id, $pay_type); if ($paySet['status'] != 1) { $result['status'] = 0; $result['msg'] = $paySet['msg']; $this->ajaxReturn($result); } $config = $paySet['config']; $config['body'] = self::$merchant['name'] . '-' . self::$store['storetag']; $Order['m_pay_type'] = $paySet['m_pay_type']; break; case '3': //现金 $tempPayType = 1; if (!verifyPrice($amount_received)) { $result['status'] = 0; $result['msg'] = '实付金额不正确'; $this->ajaxReturn($result); } if ($is_zero_storage && ($is_zero_storage != 1 || !$vid)) { $result['status'] = 0; $result['msg'] = '存零参数有误'; $this->ajaxReturn($result); } break; case '5': //其他支付 $tempPayType = 7; if (!empty($other_pay_id)) { $Order['other_pay_id'] = $other_pay_id; } break; case '6': //会员卡余额 $tempPayType = 5; if (empty($vid) || empty($vip_login_type)) { $result['status'] = 0; $result['msg'] = '会员参数有误'; $this->ajaxReturn($result); } break; case '7': //赊账订单 if (!$vid) { $this->ajaxReturn(['status' => 0, 'msg' => '缺少会员参数!']); } break; case '8': //德育币支付 $tempPayType = 9; if (empty($result['data']['plug']['moraleducationcoin'])) { $result['status'] = 0; $result['msg'] = '该商户未拥有德育币支付插件'; $this->ajaxReturn($result); } if (empty($third_vip_code)) { $result['status'] = 0; $result['msg'] = '请先登录德育币会员'; $this->ajaxReturn($result); } break; } $BillModel = new RetailBillOrderModel(); $map = [ 'order_no' => strval($order_no), 'store_id' => $store_id, 'pay_state' => ['in', '0,2'], //未支付或待支付状态 'refund_money' => 0, ]; $billOrder = $BillModel->getFind($map); if (empty($billOrder)) { $result['status'] = 0; $result['msg'] = '订单不存在'; $this->ajaxReturn($result); } $history = S('bill_order_no_' . $billOrder['order_no']); //获取缓存 if (!empty($history)) { $result['status'] = 0; $result['msg'] = '请勿频繁操作!'; $this->ajaxReturn($result); } S('bill_order_no_' . $billOrder['order_no'], $billOrder['order_no'], 2); //写入2秒的缓存,防止用户频繁点击 if ($billOrder['pay_state'] == 0) { //未计算钱并且金额发生改变 //条码秤打印的条码价权限 $StoreAuthClass = new StoreAuthority(); $configResult = $StoreAuthClass->getStoreConfig([ 'store_id' => self::$store_id, 'fields' => 'code_price_state' ]); $code_price_state = $configResult['data']['code_price_state'] ? $configResult['data']['code_price_state'] : 0; $item = [ 'code_price_state' => $code_price_state, //条码秤打印的条码价权限 0关闭 1开启 'bill_order' => $billOrder, 'vid' => $vid, //会员id 'coupon_id' => $coupon_id, //优惠券id 'coupon_type' => $coupon_type, //优惠券类型 'discount' => $discount, //收银员手动打折折扣值 'small_changes_money' => $small_changes_money, //抹零金额 'allMoney' => $all_money, //前台计算的总价 'integral_cashing' => $integral_cashing, //积分抵现开启状态 'integral_dis_money' => $integral_dis_money, //积分抵现开启抵现的金额 'integral_dis_num' => $integral_dis_num, //积分抵现开启状态抵现的积分 'guide_id' => $guide_id, //导购员id 'cashier_preferential_state' => self::$cashier['preferential'], //收银员打折权限 'cashier_manual_discount_state' => self::$cashier['manual_discount'], // 收银员-单品折扣权限 'manual_min_discount_state' => self::$cashier['manual_min_discount'], // 收银员-手动折扣最低折扣 'manual_max_amount_state' => self::$cashier['manual_max_amount'], // 收银员-手动折扣最多优惠金额 'store_preferential_state' => self::$store['preferential'], //门店打折权限 'store_manual_discount_state' => self::$store['manual_discount'], // 门店折上折--收银员 'guide_open_state' => self::$store['openstate'], //导购员分佣开启状态 ]; $calculation = A('PayApi/Order')->calculationOrder_v2($item); //重新计算该付的金额 if ($calculation['status'] == 1) { $billOrder = $calculation['order']; if (round($billOrder['all_money'] - $pay_money) < 0) { $result['all_money'] = $billOrder['all_money']; $result['status'] = 0; $result['msg'] = '支付金额超出订单总额1'; $this->ajaxReturn($result); } $billOrder['guide_id'] = $guide_id; $billOrder['pay_state'] = 2; $billOrder['business_remark'] = $business_remark; $billOrder['pay_type'] = $tempPayType; //1现金 3微信支付 4支付宝支付 5会员卡余额支付 6小程序商城微信支付 7其他支付方式 8组合支付 9德育币 if ($tempPayType == 9) { $billOrder['third_vip_code'] = $third_vip_code; $billOrder['third_vip_name'] = $third_vip_name; } $update = $BillModel->updateOrder(['id' => $billOrder['id']], $billOrder); if (!$update) { $result['status'] = 0; $result['msg'] = '订单更新失败,请重试'; $this->ajaxReturn($result); } } else { $this->ajaxReturn($calculation); } } else { //组合 if (round($billOrder['all_money'] - $all_money, 2) != 0) { $result['status'] = 0; $result['msg'] = 'bill2-前后台计算价格不一致,请检查 ' . $billOrder['all_money'] . ' ' . $all_money; $this->ajaxReturn($result); } $toUp = false; if ($billOrder['pay_state'] == 0) { $toUp = true; $billOrder['pay_state'] = 2; } if (round($billOrder['all_money'] - $pay_money, 2) > 0) { $toUp = true; $billOrder['pay_type'] = 8; } else if (round($billOrder['all_money'] - $pay_money - $billOrder['pay_money'], 2) == 0) { $toUp = true; $billOrder['pay_type'] = $tempPayType; //1现金 3微信支付 4支付宝支付 5会员卡余额支付 6小程序商城微信支付 7其他支付方式 8组合支付 } else { $result['status'] = 0; $result['msg'] = '支付金额超出订单总额2'; $this->ajaxReturn($result); } if ($toUp) { $BillModel->updateOrder(['id' => $billOrder['id']], $billOrder); } } if (round($billOrder['all_money'] - $billOrder['pay_money'] - $pay_money, 2) < 0) { $result['status'] = 0; $result['msg'] = '支付金额超出订单总额3'; $this->ajaxReturn($result); } $Order['trade_amount'] = $pay_money; $Order['order_type'] = 1; $Order['pay_type'] = $pay_type; $Order['store_id'] = $store_id; $Order['merchant_id'] = $merchant_id; $Order['cashier_id'] = self::$cashier_id; $Order['vid'] = $billOrder['vid']; $Order['order_state'] = 'WAIT_BUYER_PAY'; $Order['add_time'] = time(); $Order['bill_id'] = $billOrder['id']; if (!empty($out_trade_no)) { $pay_order = $Order; $pay_order_id = $PayOrderModel->getFindField(['out_trade_no' => $out_trade_no], "id"); if (!$pay_order_id) { $res['status'] = 0; $res['msg'] = "未找到内部订单"; $this->ajaxReturn($res); } unset($Order['add_time']); $pay_order_save = $PayOrderModel->updateOrder(['id' => $pay_order_id], $Order); if (!$pay_order_save) { $res['status'] = 0; $res['msg'] = "内部订单修改失败"; $this->ajaxReturn($res); } $pay_order['id'] = $pay_order_id; $pay_order['out_trade_no'] = $out_trade_no; } else { $pay_order = $PayOrderModel->addPayOrder($Order); } if (!$pay_order) { $result['status'] = 0; $result['msg'] = '创建订单失败,请重试'; $this->ajaxReturn($result); } $Order['id'] = $pay_order['id']; $Order['out_trade_no'] = $pay_order['out_trade_no']; if (!empty($result['data']['plug']['wxpaycoupon'])) { $Order['plug']['wxpaycoupon'] = $result['data']['plug']['wxpaycoupon']; $SellClass = new Sell(); $result = $SellClass->getGoodsTag([ 'bill_id' => $billOrder['id'], 'store_id' => self::$store_id ]); //查询商品信息 if ($result['status'] == 1 && !empty($result['data']['goods_detail'])) { $Order['shop_coupon']['goods_tag'] = strval(self::$merchant_id); $Order['shop_coupon']['goods_detail'] = $result['data']['goods_detail']; } } if (C('IS_SET_REDIS') && !empty($item['coupon_id'])) { $ApiRedis = new \Addons\Api\RedisController(); $coupon_redis_key = 'retail_coupon_store_id_*_uid_*_coupon_type_' . $item['coupon_type'] . '_coupon_id_' . $item['coupon_id']; $user_shop_coupon = $ApiRedis::$redis->mget($ApiRedis::$redis->keys($coupon_redis_key)); if (!empty($user_shop_coupon)) { $res['status'] = 0; $res['msg'] = '该优惠券正在使用中,请勿重复使用'; $this->ajaxReturn($result); } if ($item['coupon_type'] == 3) { $ShopCouponClass = new ShopCoupon(); $shopResult = $ShopCouponClass->getUserShopCouponState([ 'coupon_id' => $item['coupon_id'] ]); if ($shopResult['status'] == 1 && $shopResult['data']['used_type'] == 2) { $res['status'] = 0; $res['msg'] = '该优惠券已使用,请刷新重试'; $this->ajaxReturn($result); } } $coupon_redis_key = 'retail_coupon_store_id_' . $item['bill_order']['store_id'] . '_uid_' . 0 . '_coupon_type_' . $item['coupon_type'] . '_coupon_id_' . $item['coupon_id']; $CouponModel = new RetailCouponModel(); $coupon_time = 602; $parameter = [ 'coupon_id' => $item['coupon_id'], 'coupon_type' => $item['coupon_type'], 'uid' => 0, 'coupon_redis_key' => $coupon_redis_key, 'coupon_time' => $coupon_time ]; $CouponModel->frozen($parameter); } switch ($pay_type) { case '1': case '2': //微信 //支付宝 if ($Order['m_pay_type'] == 3) { //新大陆支付 $res = A('Pay/PayContinent')->toContinentPay_v3($Order, $config['body']); } elseif ($Order['m_pay_type'] == 4) { //新大陆支付 $payObj = new CloudPayController(); $res = $payObj->soltCard($Order, $config, $pay_type); } elseif ($Order['m_pay_type'] == 7) { //钱客云 $moneyGuest = new \Terminalapi\Controller\BaseController(); //支付,查询 $res = $moneyGuest->paymentQuery($Order, $config, $pay_type); } else { if ($pay_type == 1) { //微信 $res = A('Pay/NewWxPay')->toWxPay_v2($Order, $config); } else { //支付宝 $res = A('Pay/NewAliPay')->toAliPay_v2($Order, $config); } } if ($res['status'] == 1) { $item = [ 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder, 'pay_coupon_id_str' => $res['pay_coupon_id_str'] ? $res['pay_coupon_id_str'] : '', 'pay_coupon_fee' => $res['pay_coupon_fee'] ? $res['pay_coupon_fee'] : 0 ]; if (!empty($res['pay_coupon_fee']) && !empty($res['pay_coupon_id_str'])) { //使用了代金券 $DiscountModel = new RetailBillDiscountModel(); $DiscountModel->updateOrder([ 'bill_id' => $billOrder['id'] ], [ 'pay_coupon_id_str' => $res['pay_coupon_id_str'], 'pay_coupon_fee' => $res['pay_coupon_fee'] ]); } $result = $this->updateBillOrder($item); if ($res['status'] == 2) { $result['msg'] = $res['msg'] . ',' . $result['msg']; } } else { $result = $res; } break; case '3': case '5': //现金 //其他支付 $save = [ 'order_state' => 'SUCCESS', 'trade_date' => time(), ]; $up = $PayOrderModel->updateOrder(['id' => $pay_order['id']], $save); if (!$up) { $result['status'] = 0; $result['msg'] = '数据库发成故障,支付流水表更新失败,请重新支付'; } else { $itemData = [ 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder, 'is_zero_storage' => $is_zero_storage, 'pay_type' => 3 ]; $result = $this->updateBillOrder($itemData); } break; case '6': //会员余额 $res = $this->toVipPay_v1($Order, $password, $vip_login_type, $card_code); if ($res['status'] == 1 || $res['status'] == 2) { $result = $this->updateBillOrder(['pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder]); if ($res['status'] == 2) { $result['msg'] .= $res['msg']; } } else { $result = $res; } break; case '7': //赊账 $StoreConfigModel = new StoreConfigModel(); if (in_array('Credit', C('MODULE_ALLOW_LIST'))) { $RetailCashierModel = new RetailCashierModel(); if ($editor_type == 1) { $setAuth = $RetailCashierModel->getFindField(['id' => self::$cashier_id], 'credit_money_manage'); //收银员是否有还款赊账权限 1否 2是 if ($setAuth != 2) { $this->ajaxReturn(['status' => 0, 'msg' => '该收银员没有进行赊账的权限!']); } } $is_open = $StoreConfigModel->getFindField(['store_id' => $store_id], 'is_credit'); if ($is_open == 1) { //赊销下单 $orderParam = [ 'merchant_id' => $merchant_id, 'vid' => $vid, 'store_id' => $store_id, 'all_money' => $all_money, 'cashier_id' => self::$cashier_id, ]; $CreditOrder = new \Credit\Controller\CreditOrderController(); $result = $CreditOrder->creditOrderAdd($billOrder, $orderParam, $balance_pay); } else { $this->ajaxReturn(['status' => 0, 'msg' => '门店尚未开启赊账功能!']); } } else { $this->ajaxReturn(['status' => 0, 'msg' => '商户尚未开启赊账功能!']); } break; case '8': //德育币支付 $MoralEducationCoinPay = new \MoralEducationCoin\Business\Pay(); $result = $MoralEducationCoinPay->pay([ 'store_third_code' => self::$store['third_code'], 'amount' => $Order['trade_amount'], 'third_vip_code' => $billOrder['third_vip_code'], ]); if ($result['status'] == 1) { $save = [ 'order_state' => 'SUCCESS', 'trade_date' => time(), ]; $up = $PayOrderModel->updateOrder(['id' => $pay_order['id']], $save); if (!$up) { $result['status'] = 0; $result['msg'] = '数据库发成故障,支付流水表更新失败,请重新支付'; } else { $item = [ 'pay_money' => $pay_money, 'amount_received' => $amount_received, 'billOrder' => $billOrder, ]; $result = $this->updateBillOrder($item); } } break; } if (C('IS_SET_REDIS') && !empty($item['coupon_id']) && $result['status'] != 1) { $ApiRedis::$redis->delete($coupon_redis_key); } $result['all_money'] = $billOrder['all_money']; if ($result['status'] == 0 && $billOrder['pay_money'] == 0) { $BillModel->updateOrder($map, ['pay_state' => 0]); } $this->ajaxReturn($result); } /** * 2020-10-14 新版支付流程 * 支付 */ public function orderPay_v4() { $data = I('post.'); $data['merchant_id'] = self::$merchant_id; $data['store_id'] = self::$store_id; $data['cashier_id'] = self::$cashier_id; $data['merchantInfo'] = self::$merchant; $data['storeInfo'] = self::$store; $data['cashierInfo'] = self::$cashier; $data['user_type'] = 1; $PayOrderClass = new PayOrder(); $result = $PayOrderClass->placeOrder($data); $this->ajaxReturn($result); } /** * 查询支付订单 */ public function searchOrder() { $data = json_decode(I('post.info'), true); $data['merchant_id'] = self::$merchant_id; $data['store_id'] = self::$store_id; $data['cashier_id'] = self::$cashier_id; $QueryPayOrderClass = new QueryPayOrder(); $result = $QueryPayOrderClass->queryOrder($data); $this->ajaxReturn($result); } /** * 异常处理 */ public function abnormal() { $data = I('post.'); $data['merchant_id'] = self::$merchant_id; $data['store_id'] = self::$store_id; $data['cashier_id'] = self::$cashier_id; $AbnormalClass = new Abnormal(); $result = $AbnormalClass->abnormalOrder($data); $this->ajaxReturn($result); } /** * 撤销支付订单 */ public function revokeOrder() { $data = I('post.'); $data['merchant_id'] = self::$merchant_id; $data['store_id'] = self::$store_id; $data['cashier_id'] = self::$cashier_id; $RevokeOrderClass = new RevokeOrder(); $result = $RevokeOrderClass->revokeOrder($data); $this->ajaxReturn($result); } }