request->post(); $customId = $this->customId; $post['customId'] = $customId; $post['payWay'] = $this->isWx == true ? 0 : 1; $lat2 = $post['receiveLat'];//收货人纬度 $lng2 = $post['long'];//收货人经度 $lat1 = $this->sj['lat'];//花店纬度 $lng1 = $this->sj['long'];//花店经度 $calcDistance = util::getDistance($lat1, $lng1, $lat2, $lng2);//防止别人改动,PHP计算的距离会比腾讯js算的小,但能保证有一定准确性 $post['sendDistance'] = $post['sendDistance'] >= $calcDistance ? $post['sendDistance'] : $calcDistance;//二地距离 $sendDistance = $post['sendDistance']; $freight = dict::getDict('freight'); $firstDistance = $freight['firstDistance']; $firstPrice = $freight['firstPrice']; $nextDistance = $freight['nextDistance']; $nextPrice = $freight['nextPrice']; if ($sendDistance <= $firstDistance && $post['isCharge'] == 1) { $post['sendCost'] = $firstPrice; } else if ($post['isCharge'] == 1) { $subDistance = $sendDistance - $firstDistance; $addPrice = intval($subDistance / $nextDistance) * $nextPrice; $post['sendCost'] = $firstPrice + $addPrice; } if (empty($this->sjExtend['payment'])) { util::fail('支付功能未开通,暂时无法购买哦~'); } $couponAmount = 0; $couponId = isset($post['couponId']) ? $post['couponId'] : 0;//代金劵 if (!empty($couponId)) { $coupon = xhCouponService::getById($couponId); $couponUserId = $coupon['userId']; if ($couponUserId != $customId) { util::fail('不是你的代金劵'); } if ($coupon['useStatus'] == 1 || $coupon['deadline'] < time()) { util::fail('代金劵已经失效了'); } $couponAmount = $coupon['amount']; } //不要将代金劵保存到订单表,付款成功后再保存进去!!! unset($post['couponId']); $post['sjId'] = $this->sjId; //店铺id $post['shopId'] = $this->sj['defaultShopId']; $order = xhOrderService::add($post);//创建订单 if ($order == false) { util::fail('创建订单失败'); } $orderId = $order['id']; $oData = []; $goodsInfo = $post['goodsInfo']; $totalFee = 0;//计算总价格 $goodsIdList = []; $multiPriceIdList = []; $orderName = ''; $goodsNum = 0; foreach ($goodsInfo as $key => $val) { $multiPriceId = 0;//多种价格表Id $goodsId = $val['goodsId']; $num = $val['num']; $goodsNum += $num; $goods = xhGoodsService::getById($goodsId); if (empty($orderName)) { $orderName = $goods['name']; } $price = $goods['price']; if ($val['multiPriceId'] != 0 && $goods['multiPrice'] != '') { $pos = strpos($goods['multiPrice'], $val['multiPriceId']); if ($pos === false) { util::fail('订单的商品不存在'); } $goodPrice = xhGoodsPriceService::getById($val['multiPriceId']); //$price = $goodPrice['price']; $price = xhGoodsSettingService::changePrice($goods, $goodPrice['price']);//统一对商品进行价格等方面设置 //多种价格规格 替换 默认(单价格) $goods['name'] = $goodPrice['title']; //$goods['cover'] = $goodPrice['picture'];//后台图片上传功能未开发,暂时使用默认商品图片 $multiPriceId = $val['multiPriceId']; } $totalFee += $price * $num; $goodsIdList[] = $goodsId; $multiPriceIdList[] = $multiPriceId; $data = [ 'orderId' => $orderId, 'goodsId' => $goodsId, 'customId' => $customId, 'sjId' => $this->sjId, 'title' => $goods['name'], 'cover' => $goods['cover'], 'unitPrice' => $price, 'num' => $num, 'multiPriceId' => $multiPriceId, 'createTime' => date("Y-m-d H:i:s"), ]; xhOrderGoodsService::add($data);//创建订单商品列表 } $sendCost = $post['sendCost'];//运费 $oData['goodsNum'] = $goodsNum;//订单的商品数量类型,单个还是多个的 $oData['prePrice'] = $totalFee + $sendCost; $oData['orderName'] = $goodsNum > 1 ? $orderName . '等' . $goodsNum . '件商品' : $orderName; if (isset($post['sourceType']) && $post['sourceType'] == 'cart') { if (!empty($goodsIdList)) { foreach ($goodsIdList as $key => $goodsId) { xhCartService::delByIds($customId, $goodsId, $multiPriceIdList[$key]);//删除购物车($userId, $goodsId, $goodsPriceId, $cartId) } } } $userAsset = xhUserAssetService::getByUserId($customId); $level = $userAsset['member']; if ($level > 0) { $gradeList = xhMerchantExtendService::getGradeList($this->sjExtend); $discount = $gradeList[$level]['discount']; $discount = strlen($discount) == 1 ? $discount * 10 : $discount; $totalFee = number_format($discount * $totalFee / 100, 2); } $oData['actPrice'] = $totalFee + $sendCost - $couponAmount; xhOrderService::updateById($orderId, $oData); util::success(['orderId' => $orderId, 'couponId' => $couponId], '订单创建成功'); } public function actionParams() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $payWay = isset($post['payWay']) ? $post['payWay'] : ''; $shopId = isset($post['shopId']) ? $post['shopId'] : 0; //来源 0自建商城 1门店 2微信朋友圈 3淘宝 $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0; $shopInfo = xhShop::find()->where(['id' => $shopId])->one(); if (empty($shopInfo) || $shopInfo->sjId != $this->sjId) { util::fail('门店无效'); } unset($post['payWay']); $post['userId'] = $this->userId; $prePrice = round($post['prePrice'], 2);//四舍五入,保留二位小数 $userAsset = xhUserAssetService::getByUserId($this->userId); $level = isset($userAsset['member']) ? $userAsset['member'] : 0; $sjExtend = xhMerchantExtendService::getBySjId($this->sjId); $memberIntegral = xhMerchantExtendService::getGradeList($sjExtend, 'desc'); Yii::info(json_encode($sjExtend)); Yii::info(json_encode($memberIntegral)); //$discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount']; $discount = 0; $actPrice = $prePrice; $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0; Yii::info($this->userId . ' ' . $level . ' ' . $discount); //没有优惠券才能使用会员折扣 ssh 2019.8.30 if (!empty($discount) && $prePrice >= 1 && empty($couponId)) { $currentPrice = ($prePrice * $discount) / 100; $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2); } //没有优惠卷,微信朋友圈付款才给随机优惠金额 ssh 2019.9.12 if ($sourceType == 2 && empty($couponId)) { $discountAmount = OrderService::getRandDiscount($actPrice); $discountAmount = 0.01; $actPrice = stringUtil::calcSub($actPrice, $discountAmount); $post['discountType'] = 2;//优惠类型是:付款随机优惠 $post['discountAmount'] = $discountAmount; } $post['actPrice'] = $actPrice; $post['payStyle'] = 0; $post['sjId'] = $this->sjId; $now = time(); $expireTime = $now + 1800;//订单30分钟后过期 $post['createTime'] = date("Y-m-d H:i:s", $now); $post['deadline'] = $expireTime; if (!empty($couponId)) { $time = time(); $coupon = CouponService::getById($couponId); if ($coupon['deadline'] <= $time) { util::fail('优惠卷已经过期'); } if ($coupon['status'] == 1) { util::fail('优惠卷已经使用'); } if ($coupon['meetAmount'] > $post['prePrice']) { util::fail('消费金额不足' . $coupon['meetAmount'] . '元'); } $post['actPrice'] = stringUtil::calcSub($post['prePrice'], $coupon['amount']);//浮点相减 $post['discountAmount'] = $coupon['amount']; $post['discountType'] = 0; } if ($post['actPrice'] <= 0) { util::fail('消费金额太低'); } if ($this->isWx) { $post['modPriceStatus'] = 1;//微信已经提交不能再修改价格 } $post['sourceType'] = $sourceType; $post['id'] = stringUtil::generateOrderNo($this->sjId, $this->userId); $payment = xhOrderService::add($post); $orderId = $payment['id']; if ($payWay == 'balance') {//余额支付 util::success(['orderId' => $orderId]); } $name = '购买商品'; $totalFee = $post['actPrice']; if ($this->isWx) { $userId = $post['userId']; $user = xhUserService::getById($userId); if ($user['sjId'] != $this->sjId) { util::fail('非法用户'); } $openId = $user['openId']; $capitalType = dict::getDict('capitalType','xhOrder','id'); $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去 $weixin = Yii::getAlias("@vendor/weixin"); require_once($weixin . '/lib/WxPay.Api.php'); require_once($weixin . '/example/WxPay.JsApiPay.php'); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetOut_trade_no($orderId); $input->SetTotal_fee($totalFee * 100); $input->SetTime_start(date("YmdHis", $now)); $input->SetAttach($attach); $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟 $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/'); $input->SetTrade_type("JSAPI"); //服务商 代设置微信支付,要添加的参数设置 if ($this->sjExtend['wxPayApply'] == 1) { //$input->SetOpenid($openId); $input->SetSub_openid($openId); //自设置 微信支付 } else { $input->SetOpenid($openId); } $wxOrder = \WxPayApi::unifiedOrder($input, 6, $this->sjExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $this->sjExtend); $newParams = json_decode($jsApiParameters, true); $newParams['orderId'] = $orderId; util::success($newParams); } util::success(['orderId' => $orderId]); } }