request->post(); $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0; $goodsStyleId = isset($post['goodsStyleId']) ? $post['goodsStyleId'] : 0; $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1; $needSend = isset($post['needSend']) && is_numeric($post['needSend']) ? $post['needSend'] : 1; if (empty($goodsId)) { util::fail('请选择商品'); } $goodsInfo = GoodsService::getGoodsInfo($goodsId); if (empty($goodsInfo)) { util::fail('没有找到商品'); } if (isset($goodsInfo['stock']) == false || $goodsInfo['stock'] == 0) { util::fail('已经卖完了'); } //运费计算方式 $freightType = isset($goodsInfo['freightType']) ? $goodsInfo['freightType'] : 0; //事务处理 $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $post['customId'] = $this->customId; $user = $this->user; $post['bookName'] = isset($user['userName']) ? $user['userName'] : ''; $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : ''; $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile; $post['bookMobile'] = $bookMobile; $post['payWay'] = $this->isWx == true ? 0 : 1; $defaultShopId = MerchantService::getDefaultShopId($this->sj); if (empty($defaultShopId)) { util::fail('没有找到门店'); } //计算运费 $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0; $sendCost = 0; //按距离计算运费并且客户要求送的 if ($freightType == 0 && $needSend == 1) { $lat = $post['latitude'];//收货人纬度 $lng = $post['longitude'];//收货人经度 $respond = ExpressService::getUserDistance($lng, $lat, $defaultShopId, $this->sjExtend); $sendCost = isset($respond['fee']) ? $respond['fee'] : 0; } $post['sendCost'] = $sendCost; $post['merchantId'] = $this->sjId; $post['shopId'] = $defaultShopId; //计算总金额 $unitPrice = $goodsInfo['price']; $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : []; if (!empty($goodsStyleId)) { if (empty($goodsStyleList)) { util::fail('商品款式没有找到'); } $styleIdList = array_keys($goodsStyleList); if (in_array($goodsStyleId, $styleIdList) == false) { util::fail('商品款式没有找到!'); } $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999; } $goodsPrice = $unitPrice * $goodsNum; $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice); //非门店订单 $store = 0; $post['store'] = $store; //来源 0微信 1支付宝 2小程序 3朋友圈 4美团 $sourceType = $this->isWx ? 0 : 1; if (httpUtil::isMiniProgram()) { $sourceType = 2; } $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0; $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]); $actPrice = $discountData['price']; $post['discountType'] = $discountData['discountType']; $post['discountAmount'] = $discountData['discountAmount']; $now = time(); $expireTime = $now + 1800;//订单30分钟后过期 $post['deadline'] = $expireTime; $post['fromType'] = 1;//商城 $post['prePrice'] = $prePrice; $post['actPrice'] = $actPrice; $order = OrderClass::addOrder($post); $orderId = $order['id']; $orderSn = $order['orderSn']; $data = [ 'orderId' => $orderId, 'goodsId' => $goodsId, 'userId' => $this->userId, 'merchantId' => $this->sjId, 'title' => $goodsInfo['goodsName'], 'cover' => isset($goodsInfo['shortImgList']) && !empty($goodsInfo['shortImgList']) ? current($goodsInfo['shortImgList']) : '', 'unitPrice' => $unitPrice, 'num' => $goodsNum, 'goodsStyleName' => isset($goodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $goodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '', 'goodsStyleId' => $goodsStyleId, 'createTime' => date("Y-m-d H:i:s"), ]; OrderGoodsService::add($data);//创建订单商品列表 $transaction->commit(); util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId]); } catch (Exception $e) { $transaction->rollBack(); Yii::info("下单失败原因:" . $e->getMessage()); util::fail('下单失败'); } } //下单要用到的相关信息 ssh 2019.12.6 public function actionOrderRelate() { $regionTree = RegionService::tree(); $shop = $this->shop; $freight = ['first' => 5, 'add' => 2]; $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH'; $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj]; util::success($out); } //余额支付 ssh 2019.12.6 public function actionBalancePay() { $post = Yii::$app->request->post(); $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0; $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0; $couponId = isset($post['couponId']) ? $post['couponId'] : 0; //验证优惠券是否还有效 if (!empty($couponId)) { CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']); } $order = OrderService::getByOrderSn($orderSn); $userId = $this->userId; $user = UserService::getUserInfo($userId, false); //验证支付密码是否正确 UserService::validPayPassword($payPassword, $user); //支付前验证订单有效性 OrderService::checkBeforePay($order, $this->userId); $typeList = dict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; $totalFee = $order['prePrice']; $sourceType = 0; $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]); $actPrice = $discountData['price']; $callbackParams = []; $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId); $balance = isset($respond['balance']) ? $respond['balance'] : 0; util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]); } //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3 public function actionWxPay() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $couponId = isset($post['couponId']) ? $post['couponId'] : 0; $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0; $order = OrderService::getByOrderSn($orderSn); $orderId = $order['id']; //验证优惠券是否还有效 if (!empty($couponId)) { CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']); } //支付前验证订单有效性 OrderService::checkBeforePay($order, $this->userId); $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品'; $totalFee = $order['actPrice']; //小程序使用miniOpenId if (httpUtil::isMiniProgram()) { $openId = $this->user['miniOpenId']; } else { $openId = $this->user['openId']; } $typeList = dict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调 $wxPayType = 0; if (httpUtil::isMiniProgram()) { $wxPayType = 1; } $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType; //订单30分钟后过期 $now = time(); $expireTime = $now + 1800; $wx = Yii::getAlias("@vendor/weixin"); require_once($wx . '/lib/WxPay.Api.php'); require_once($wx . '/example/WxPay.JsApiPay.php'); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetOut_trade_no($orderSn); $input->SetTotal_fee($totalFee * 100); $input->SetTime_start(date("YmdHis", $now)); $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传 $input->SetTime_expire(date("YmdHis", $expireTime)); $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/'); $input->SetTrade_type("JSAPI"); //花卉宝代为申请的微信支付 $sjExtend = $this->sjExtend; if ($sjExtend['wxPayApply'] == 1) { $input->SetSub_openid($openId); } else { $input->SetOpenid($openId); } //小程序使用miniAppId if (httpUtil::isMiniProgram()) { $sjExtend['wxAppId'] = $sjExtend['miniAppId']; } $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend); $newParams = json_decode($jsApiParameters, true); //已请求微信不能修改价格 $updateData = []; $updateData['modPrice'] = 0; if (empty($order['deadline'])) { $updateData['deadline'] = $expireTime; } OrderService::updateById($orderId, $updateData); util::success($newParams); } //快捷付款订单 public function actionFastPay() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $payWay = isset($post['payWay']) ? $post['payWay'] : 0; $shopId = $this->shopId; $post['shopId'] = $shopId; $post['merchantId'] = $this->sjId; //默认门店订单 $store = isset($post['store']) ? $post['store'] : 1; $post['store'] = $store; //来源 0微信 1支付宝 2小程序 3朋友圈 4美团 $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 2; //兼容旧系统sourceType=3表示朋友圈来源 $fromType = $sourceType == 3 ? 2 : 0; $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType; $post['customId'] = $this->customId; $user = $this->user; $post['bookName'] = isset($user['userName']) ? $user['userName'] : ''; $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : ''; $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile; $post['bookMobile'] = $bookMobile; $prePrice = round($post['prePrice'], 2); $couponId = isset($post['couponId']) ? $post['couponId'] : 0; $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]); $actPrice = $discountData['price']; $post['discountType'] = $discountData['discountType']; $post['discountAmount'] = $discountData['discountAmount']; $post['actPrice'] = $actPrice; $post['payStyle'] = 0; $post['merchantId'] = $this->sjId; $now = time(); $expireTime = $now + 300;//订单5分钟后过期 $post['createTime'] = date("Y-m-d H:i:s", $now); $post['deadline'] = $expireTime; if ($actPrice <= 0) { util::fail('请填写正确的金额'); } //微信支付订单提交不能修改价格 $post['modPrice'] = $this->isWx ? 0 : 1; $post['sourceType'] = $sourceType; $post['goodsNum'] = 1; $post['fromType'] = $fromType; $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00'; $order = OrderClass::addOrder($post); $orderId = $order['id']; $orderSn = $order['orderSn']; $sjId = $this->sjId; if ($payWay == 0) { $orderId = $order['id']; $name = '购买商品'; $totalFee = $actPrice; $user = UserService::getById($this->userId); //小程序使用miniOpenId $openId = $user['miniOpenId']; if (empty($openId)) { util::fail('没有找您的微信ID'); } $typeList = dict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调 $wxPayType = 0; if (httpUtil::isMiniProgram()) { $wxPayType = 1; } $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType; $wx = Yii::getAlias("@vendor/weixin"); require_once($wx . '/lib/WxPay.Api.php'); require_once($wx . '/example/WxPay.JsApiPay.php'); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetOut_trade_no($orderSn); $input->SetTotal_fee($totalFee * 100); $input->SetTime_start(date("YmdHis", $now)); $input->SetAttach($attach); //设置订单有效期5分钟 $input->SetTime_expire(date("YmdHis", $expireTime)); $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/'); $input->SetTrade_type("JSAPI"); $sjExtend = WxOpenClass::getMallWxInfo(); $input->SetOpenid($openId); //小程序使用miniAppId $sjExtend['wxAppId'] = $sjExtend['miniAppId']; $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend); $newParams = json_decode($jsApiParameters, true); $newParams['orderId'] = $orderId; $newParams['orderSn'] = $orderSn; $newParams['totalPrice'] = $actPrice; util::success($newParams); } elseif ($payWay == 1) { $extend = MerchantExtendService::getByMerchantId($sjId); if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) { util::fail('支付宝付款即将开通...'); } $config = [ //应用ID,您的APPID。 'app_id' => $extend['alipayPId'], //商户私钥,您的原始格式RSA私钥 'merchant_private_key' => $extend['alipayKey'], //异步通知地址 'notify_url' => Yii::$app->params['mallHost'] . "/notice/alipay-callback", //同步跳转 'return_url' => "", //编码格式 'charset' => "UTF-8", //签名方式 'sign_type' => "RSA2", //支付宝网关 'gatewayUrl' => "https://openapi.alipay.com/gateway.do", //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 'alipay_public_key' => $extend['alipayPublicKey'], ]; Yii::info(json_encode($config) . ' aplipay config'); $alipayWap = Yii::getAlias("@vendor/alipayWap"); require_once($alipayWap . '/wappay/service/AlipayTradeService.php'); require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php'); $totalFee = $order['actPrice']; $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填 $subject = '购买商品';//订单名称,必填 $total_amount = $totalFee;//付款金额,必填 $body = '';//商品描述,可空 $timeout_express = "1m";//超时时间 $typeList = dict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $sjId;//将流水类型、优惠卷传过去 $passbackParams = urlencode($passbackParams); $payRequestBuilder = new \AlipayTradeWapPayContentBuilder(); $payRequestBuilder->setBody($body); $payRequestBuilder->setSubject($subject); $payRequestBuilder->setOutTradeNo($out_trade_no); $payRequestBuilder->setTotalAmount($total_amount); $payRequestBuilder->setTimeExpress($timeout_express); //在异步通知时将该参数原样返回 $payRequestBuilder->setPassbackParams($passbackParams); //同步通知 $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0"; //异步通知 $notifyUrl = Yii::$app->params['mallHost'] . "/notice/alipay-callback"; $payResponse = new \AlipayTradeService($config); $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl); //直接将支付宝的html返回给前端 echo $result; } elseif ($payWay == 2) { //余额支付,验证支付密码是否正确 $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0; $user = UserService::getUserInfo($this->userId, false); UserService::validPayPassword($payPassword, $user); $typeList = dict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; $callbackParams = []; $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId); $balance = isset($respond['balance']) ? $respond['balance'] : 0; util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]); } else { util::fail('无效的支付方式'); } } //订单列表 ssh 2019.12.12 public function actionList() { $where = ['customId' => $this->customId]; $list = OrderService::getOrderList($where); util::success($list); } //订单评价 public function actionComment() { $post = Yii::$app->request->post(); $id = $post['id']; $order = OrderService::getById($id); OrderService::valid($order, $this->shopId); if ($order['grade'] > 0) { util::fail('您已经评过了'); } OrderService::comment($post); util::complete('提交成功'); } //订单详情 ssh 2019.12.16 public function actionDetail() { $id = Yii::$app->request->get('id', 0); $orderSn = Yii::$app->request->get('orderSn', ''); $detail = []; if (!empty($id)) { $detail = OrderService::getOrderById($id); } if (!empty($orderSn)) { $detail = OrderService::getOrderBySn($orderSn); } OrderService::valid($detail, $this->shopId); util::success($detail); } }