validate()) return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; try { $mchListData = $this->getMchListData(true); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]; } $order_id_list = []; $t = \Yii::$app->db->beginTransaction(); foreach ($mchListData as &$mch) { if (isset($mch['use_integral']) && !$mch['use_integral']) { $mch['integral'] = ['forehead' => 0, 'forehead_integral' => 0]; $mch['give'] = 0; } $mch_goods = $mch['goods_list']; if ($mch_goods) { $checkMchData = $this->checkMchData($mch); if ($checkMchData['code'] == 1) { return $checkMchData; } $payPrice = $this->getPayPrice($mch); $order = new Order(); $order->store_id = $this->store_id; $order->user_id = $this->user_id; $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SCAN_CODE_PAY); $order->pay_price = $payPrice; if (isset($mch['picker_coupon']) && !empty($mch['picker_coupon'])) { $order->user_coupon_id = $mch['picker_coupon']['user_coupon_id']; $order->coupon_sub_price = $mch['picker_coupon']['sub_price']; // 查找优惠券优惠的商品 $this->pickerGoods($mch); } // 是否是平台小程序订单 if (is_platform()) { $order->is_platform = 1; } $order->created_at = time(); $order->first_price = 0; $order->second_price = 0; $order->third_price = 0; $order->content = $mch['content']; $order->is_offline = 1; if (isset($mch['integral'])) { $order->integral = json_encode($mch['integral'], JSON_UNESCAPED_UNICODE); } $order->version = cyy_version(); $order->mch_id = $mch['mch_id']; $order->discount = 10; // 当面付订单不享受会员折扣 $order->remark = isset($mch['remark']) ?: '' ; if ($this->payment == 2) { $order->pay_type = 2; $order->is_pay = 0; } if ($this->payment == 3) { $order->pay_type = 3; $order->is_pay = 0; } $order->total_price = $mch['total_price']; $order->express_price = 0; if ($order->save()) { // 处理订单生成之后其他相关数据 $orderRes = $this->insertData($mch, $order); if ($orderRes['code'] == 1) { $t->rollBack(); return $orderRes; } // 订单提交完成发送消息 NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name'], 0, $order->store_id); $order_id_list[] = $order->id; } else { $t->rollBack(); return $this->getErrorResponse($order); } } } if (count($order_id_list) > 0) { $t->commit(); if (count($order_id_list) > 1) {//多个订单合并 return [ 'code' => 0, 'msg' => '订单提交成功', 'data' => (object)[ 'order_id_list' => $order_id_list, 'add_time' => $order->created_at ], ]; } else {//单个订单 $order_id = $order_id_list[0]; // CloudPrint::doPrint($order_id, 0, $this->store_id); return [ 'code' => 0, 'msg' => '订单提交成功', 'data' => (object)[ 'order_id' => $order_id_list[0], 'add_time' => $order->created_at ], ]; } } else { $t->rollBack(); return [ 'order_list' => $order_id_list ]; } } // 获得实际支付金额 private function getPayPrice($mch) { $payPrice = $mch['total_price']; if (isset($mch['use_integral']) && $mch['use_integral']) { $payPrice -= $mch['integral']['forehead']; } if (isset($mch['picker_coupon'])) { $payPrice -= $mch['picker_coupon']['sub_price']; } return $payPrice > 0 ? $payPrice : 0.01; } // 检查数据 private function checkMchData($mch) { if (empty($mch['goods_list'])) { return [ 'code' => 1, 'msg' => '商品不存在或已删除' ]; } $checkCoupon = $this->checkCoupon($mch); if ($checkCoupon['code'] == 1) { return $checkCoupon; } return ['code' => 0, 'msg' => 'success']; } // 检测优惠券是否可使用 private function checkCoupon($mch) { if (empty($mch['picker_coupon'])) { return [ 'code' => 0, 'msg' => '' ]; } $ok = false; foreach ($mch['coupon_list'] as $item) { if ($item['user_coupon_id'] == $mch['picker_coupon']['user_coupon_id']) { $ok = true; } } if (!$ok) { return [ 'code' => 1, 'msg' => '该优惠券已过期' ]; } else { return [ 'code' => 0, 'msg' => '' ]; } } // 优惠券可优惠的商品总额计算 private function pickerGoods(&$mch) { $pickerCoupon = $mch['picker_coupon']; if (empty($pickerCoupon)) { return; } $mch['picker_coupon']['total_price'] = $mch['total_price']; } private function insertData($mch, $order) { // 存入下单表单 if (isset($mch['form']) && $mch['form'] && $mch['form']['is_form'] == 1) { foreach ($mch['form']['list'] as $index => $value) { $order_form = new \app\models\OrderForm(); $order_form->store_id = $this->store_id; $order_form->order_id = $order->id; $order_form->key = $value['name']; $order_form->value = $value['default']; $order_form->type = $value['type']; $order_form->is_delete = 0; $order_form->save(); } } // 减去当前用户账户积分 if (isset($mch['integral']) && $mch['integral'] && $mch['integral']['forehead_integral'] > 0) { $this->user->integral -= $mch['integral']['forehead_integral']; if ($this->user->integral < 0) { return [ 'code' => 1, 'msg' => '积分不足' ]; } $this->user->save(); } // 计算商品相关 $payPrice = $mch['total_price']; $goods_list = $mch['goods_list']; foreach ($goods_list as $goods) { // 删除购物车 if (isset($goods['cart_id'])) { Cart::updateAll(['is_delete' => 1], ['id' => $goods['cart_id']]); } $order_detail = new OrderDetail(); $order_detail->order_id = $order->id; $order_detail->goods_id = $goods['goods_id']; $order_detail->num = $goods['num']; // if ($goods['is_level'] && $goods['is_level'] == 1) { // $order_detail->is_level = (int)$goods['is_level']; // } else { $order_detail->is_level = 1; // } if (isset($mch['picker_coupon']) && $mch['picker_coupon'] && !empty($mch['picker_coupon'])) { if ($mch['picker_coupon']['total_price'] > 0) { $pickerCouponSubPrice = doubleval($mch['picker_coupon']['sub_price'] * doubleval($goods['price']) / $mch['picker_coupon']['total_price']); $payPrice -= $pickerCouponSubPrice; } // 删除优惠券 UserCoupon::updateAll(['is_use' => 1], ['id' => $mch['picker_coupon']['user_coupon_id']]); } if (isset($mch['use_integral']) && $mch['use_integral']) { if ($goods['resIntegral'] && $goods['resIntegral']['forehead'] > 0) { $payPrice -= $goods['resIntegral']['forehead']; } } $payPrice = $payPrice >= 0 ? sprintf('%.2f', $payPrice) : 0; // $order_detail->supplier_price = $goods['supplier_price']; $order_detail->total_price = $payPrice; $order_detail->is_delete = 0; $order_detail->attr = json_encode($goods['attr_list'], JSON_UNESCAPED_UNICODE); $goods_info = Goods::findOne($goods['goods_id']); $order_detail->pic = $goods['goods_pic'] ? $goods['goods_pic'] : $goods_info->cover_pic; $order_detail->goods_name = $goods_info ? $goods_info->name : $goods['goods_name']; $order_detail->goods_info = $goods_info ? Json::encode($goods_info->toArray()) : json::encode([]); // $order_detail->delivery_type = $goods['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : OrderDetail::GOODS_DELIVERY_SHOP; // if ($goods['send'] == 'shop') { // $order_detail->shop_id = $goods['shop_id']; // } if (isset($goods['give']) && $goods['give'] > 0) { $order_detail->integral = $goods['give']; } else { $order_detail->integral = 0; } $attr_id_list = []; foreach ($goods['attr_list'] as $item) { array_push($attr_id_list, $item['attr_id']); } // $order_detail->batch_price_tips = $goods['current_batch_price_tips']; if (!$order_detail->save()) { return [ 'code' => 1, 'msg' => '订单提交失败,请稍后再重试', 'error' => $order_detail->errors[0] ]; } } return [ 'code' => 0, 'msg' => '' ]; } }