['app', 'mini', 'h5', 'official','cashier']], [['pay_type'], 'in', 'range' => [1, 2, 3, 4, 5, 6]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY','现金' [['form_id', 'order_id_list', '_from','auth_code'], 'string'], ['is_combine', 'default', 'value' => 0], [['parent_user_id', 'condition', 'order_id', 'is_combine'], 'integer'], ]; } public function search() { $this->wechatPay = \Yii::$app->controller->wechatPay; if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } if ($this->order_id_list) { $order_id_list = json_decode($this->order_id_list, true); if (is_array($order_id_list) && count($order_id_list) == 1) { $this->order_id = $order_id_list[0]; $this->order_id_list = ''; } } $t = \Yii::$app->db->beginTransaction(); $this->order = Order::findOne([ 'store_id' => $this->store_id, 'id' => $this->order_id, ]); if ($this->order->pay_price == 0) { $this->order->pay_time = time(); $this->order->is_pay = 1; if($this->order->order_type == 7){ $this->order->pay_type = 7; //积分兑换 }else{ $this->order->pay_type = 3; } $this->order->trade_status = $this->order->is_offline == 1 ? Order::ORDER_FLOW_CONFIRM : Order::ORDER_FLOW_NO_SEND; $this->order->save(); if ($this->order->verify_card_id > 0) { VerifyCardSale::updateAll(['status' => 1], ['id' => $this->order->verify_card_id]); $verifylog = new VerifyCardLog(); $verifylog->store_id = get_store_id(); $verifylog->sale_id = $this->order->verify_card_id; $verifylog->shop_id = 0; $verifylog->use_time = time(); $verifylog->user_id = get_user_id(); $verifylog->type = VerifyCardLog::WRITE_TYPE_EXCHANGE; $verifylog->describe = '使用卡券礼品卡兑换商品'; $verifylog->save(); } $t->commit(); // 支付完成后,相关操作 (new NotifyForm())->videoGoodsShare($this->order); $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); return [ 'code' => 0, 'msg' => '支付成功' ]; } if (!$this->order) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单不存在', ]; } if ($this->order->is_delete == 1 || $this->order->trade_status == Order::ORDER_FLOW_CANCEL) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单已取消', ]; } if ($this->order->is_pay == 1 || $this->order->trade_status > -1) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单状态异常', ]; } try { $this->checkGoodsConfine($this->order); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } // 存储parent_id // if ($this->user && $this->user->parent_user_id) { // CommonOrder::saveParentId($this->user->parent_user_id); // } $goods_names = ''; $goods_list = OrderDetail::find()->alias('od')->leftJoin(['g' => Goods::tableName()], 'g.id=od.goods_id')->where(['od.order_id' => $this->order->id, 'od.is_delete' => 0, ])->select('g.name,g.verify_card_id')->asArray()->all(); foreach ($goods_list as $goods) { $goods_names .= $goods['name'] . ';'; } $goods_names = mb_substr($goods_names, 0, 32, 'utf-8'); $this->setReturnData($this->order); $this->order->order_union_id = 0; if (!$this->order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单保存失败' ]; } // 余额抵扣支付 $balance_price = 0; if ($this->is_combine) { $this->user->money = doubleval($this->user->money); if ($this->user->money >= $this->order->pay_price) { $this->pay_type = 3; } else { $balance_price = $this->user->money; $this->order->combine_money = $this->user->money; $this->order->is_combine_pay = Order::IS_COMBINE_PAY; } } else { $this->order->combine_money = 0; $this->order->is_combine_pay = Order::NOT_COMBINE_PAY; } $this->order->save(); if (in_array($this->pay_type, [1, 2, 3, 4, 6])) { if ($this->order->pay_price == 0) { $this->order->is_pay = 1; $this->order->pay_type = 1; $this->order->pay_time = time(); // 已发货状态 $this->order->trade_status = $this->order->is_offline == 1 ? ORDER::ORDER_FLOW_SEND : ORDER::ORDER_FLOW_NO_SEND; $this->order->send_time = $this->order->is_offline == 1 ? time() : 0; if (!$this->order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); // 支付完成后,相关操作 $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); $t->commit(); if($this->order->is_offline == 1){ $this->order->trade_status = Order::ORDER_FLOW_CONFIRM; $this->order->confirm_time = time(); } $this->order->save(); // if($this->order->hanging_order_id > 0){ // HangingOrder::updateAll(['is_delete'=>1],['id'=>$this->order->hanging_order_id]); // HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$this->order->hanging_order_id]); // } return [ 'code' => 0, 'msg' => '0元支付' ]; } // 微信支付 if ($this->pay_type == 1) { if (is_profit_pay()) { $result = WechatNewPay::micropay($this->order,OrderNo::ORDER_MALL, $goods_names, $this->order->pay_price, $balance_price, $this->auth_code); }else{ $result = WechatPay::micropay($this->order,OrderNo::ORDER_MALL, $goods_names, $this->order->pay_price, $balance_price, $this->auth_code); } if($result['return_code'] == 'SUCCESS' ){ if($result['result_code'] === 'FAIL'){ // 如果返回结果不是 等待用户输入密码 则直接返回 if($result['err_code'] == 'USERPAYING'){ $result = $this->checkOrderPay($this->order,0); }else{ return [ 'code' => 1, 'msg' => '微信支付参数错误', 'data' => $result ]; } } if($result['result_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS' && $result['trade_type'] == 'MICROPAY'){ $this->order->transaction_id = $result['transaction_id']; $this->order->is_pay = 1; $this->order->pay_time = time(); $this->order->pay_type = 1; // 已发货状态 $this->order->trade_status = $this->order->is_offline == 1 ? ORDER::ORDER_FLOW_SEND : ORDER::ORDER_FLOW_NO_SEND; $this->order->send_time = $this->order->is_offline == 1 ? time() : 0; $this->order->save(); CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); // 支付完成后,相关操作 $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); $t->commit(); if($this->order->is_offline == 1){ $this->order->trade_status = Order::ORDER_FLOW_CONFIRM; $this->order->confirm_time = time(); } $this->order->save(); // if($this->order->hanging_order_id > 0){ // HangingOrder::updateAll(['is_delete'=>1],['id'=>$this->order->hanging_order_id]); // HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$this->order->hanging_order_id]); // } //添加分账接收方 $notifyForm = new NotifyForm(); $notifyForm->addReceiver($this->order); return [ 'code' => 0, 'msg' => '支付成功', ]; }else{ return [ 'code' => 1, 'msg' => '支付失败', 'data' => $result ]; } }else{ return ['code'=>1,'msg'=>'支付失败','data'=>$result]; } } // 纯余额支付 if($this->pay_type == 3){ $user = User::findOne(['id' => $this->order->user_id]); if ($user->money < $this->order->pay_price) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败,余额不足', ]; } $res = AccountLog::saveLog($user->id, floatval($this->order->pay_price), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 1, $this->order->id, "商城余额支付,订单号为:{$this->order->order_no}。"); if (!$res) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } $this->order->is_pay = 1; $this->order->pay_type = 3; $this->order->pay_time = time(); $this->order->trade_status = $this->order->is_offline == 1 ? ORDER::ORDER_FLOW_SEND : ORDER::ORDER_FLOW_NO_SEND; $this->order->send_time = $this->order->is_offline == 1 ? time() : 0; $this->order->save(); CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); $t->commit(); if($this->order->is_offline == 1){ $this->order->trade_status = Order::ORDER_FLOW_CONFIRM; $this->order->confirm_time = time(); } $this->order->save(); // if($this->order->hanging_order_id > 0){ // HangingOrder::updateAll(['is_delete'=>1],['id'=>$this->order->hanging_order_id]); // HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$this->order->hanging_order_id]); // } return [ 'code' => 0, 'msg' => '订单支付成功', ]; } // 支付宝支付 if ($this->pay_type == 4) { if (is_profit_pay()) { $result = AlipayProfit::scanNew($this->order, $goods_names, '', $this->auth_code,$this->order->pay_price,$balance_price); }else{ $result = Alipay::scanNew($this->order, $goods_names, '', $this->auth_code,$this->order->pay_price,$balance_price); } // 支付成功 if($result['code'] == 0){ $this->order->alipay_trade_no = $result['data']['trade_no']; $this->order->is_pay = 1; $this->order->pay_time = time(); $this->order->pay_type = 4; // 已发货状态 $this->order->trade_status = $this->order->is_offline == 1 ? ORDER::ORDER_FLOW_SEND : ORDER::ORDER_FLOW_NO_SEND; $this->order->send_time = $this->order->is_offline == 1 ? time() : 0; $this->order->save(); // if($this->order->hanging_order_id > 0){ // HangingOrder::updateAll(['is_delete'=>1],['id'=>$this->order->hanging_order_id]); // HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$this->order->hanging_order_id]); // } CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); // 支付完成后,相关操作 $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); $t->commit(); // 确认收获 if($this->order->is_offline == 1){ $this->order->trade_status = Order::ORDER_FLOW_CONFIRM; $this->order->confirm_time = time(); } $this->order->save(); $notifyForm = new AlipayNotifyForm(); $notifyForm->addReceiver($this->order); return [ 'code' => 0, 'msg' => '订单支付成功', ]; }else{ // todo 再去查询是否支付成功 } } // 现金 if($this->pay_type == 6){ // 现金支付 直接完成订单 $this->order->is_pay = 1; $this->order->pay_time = time(); $this->order->pay_type = 6; // 已发货状态 $this->order->trade_status = $this->order->is_offline == 1 ? ORDER::ORDER_FLOW_SEND : ORDER::ORDER_FLOW_NO_SEND; $this->order->send_time = $this->order->is_offline == 1 ? time() : 0; if(!$this->order->save()){ $t->rollBack(); return ['code' =>1 ,' msg' => '订单支付失败']; } CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); if($this->order->is_offline == 1){ $this->order->trade_status = Order::ORDER_FLOW_CONFIRM; $this->order->confirm_time = time(); } $this->order->save(); $t->commit(); // 如果有挂单ID 将挂单信息删除 // if($this->order->hanging_order_id > 0){ // HangingOrder::updateAll(['is_delete'=>1],['id'=>$this->order->hanging_order_id]); // HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$this->order->hanging_order_id]); // } return ['code' => 0,'msg' => '订单支付成功']; } //货到付款 if ($this->pay_type == 2) { // 现金支付 直接完成订单 $this->order->pay_type = 2; // 已发货状态 $this->order->trade_status = ORDER::ORDER_FLOW_NO_SEND; $this->order->send_time = time(); if(!$this->order->save()){ $t->rollBack(); return ['code' =>1 ,' msg' => '订单支付失败']; } CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); $form = new OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->notify(); // $this->order->trade_status = Order::ORDER_FLOW_CONFIRM; // $this->order->confirm_time = time(); // $this->order->save(); $t->commit(); // 如果有挂单ID 将挂单信息删除 // if($this->order->hanging_order_id > 0){ // HangingOrder::updateAll(['is_delete'=>1],['id'=>$this->order->hanging_order_id]); // HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$this->order->hanging_order_id]); // } return ['code' => 0,'msg' => '订单支付成功']; } } } // 查询付款码支付后订单状态 private function checkOrderPay($order,$num = 0){ sleep(5); if($num >= 45){ return ['result_code'=>'FAIL','data' => '支付超时']; } $num += 5; if (is_profit_pay()) { $result = WechatNewPay::orderQuery($order); }else{ $result = WechatPay::orderQuery($order); } if($result['return_code'] == 'SUCCESS'){ if($result['trade_state'] == 'USERPAYING'){ return $this->checkOrderPay($order,$num); }else{ return $result; } } } /** * 设置佣金 * @param Order $order * @return mixed */ private function setReturnData($order) { $form = new ShareMoneyForm(); $form->order = $order; $form->order_type = 0; return $form->setData(); } /** * @param Order $order * @throws \Exception */ private function checkGoodsConfine($order) { foreach ($order->detail as $detail) { /* @var Goods $goods*/ /* @var OrderDetail $detail*/ $goods = $detail->goods; if ($goods->confine_count && $goods->confine_count > 0) { $goodsNum = Goods::getBuyNum($this->user, $goods->id); if ($goodsNum) { } else { $goodsNum = 0; } $goodsTotalNum = intval($goodsNum + $detail->num); if ($goodsTotalNum > $goods->confine_count) { throw new \Exception('商品:' . $goods->name . ' 超出购买数量', 1); } } if (in_array($order->order_type, [1, 2])) { $res = $this->bookCheckGoodsNum($goods, $detail, $order); if ($res['code'] > 0) { throw new \Exception($res['msg'], 1); } } } } /** * @param Goods $goods * @param OrderDetail $detail * @param Order $order */ private function bookCheckGoodsNum($goods, $detail, $order) { // 判断时间 if (time() - $order->created_at > 15 * 60) { return [ 'code' => 1, 'msg' => '订单已过支付时间,将自动取消' ]; } $attr = Json::decode($detail->attr); // 酒店预约 if ($order->order_type == 1) { // $date_book = GoodsBook::findOne(['goods_id' => $goods->id])->date_book; // $date_book = Json::decode($date_book); // if (!$date_book) { // return [ // 'code' => 1, // 'msg' => '数据异常' // ]; // } // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num')); // foreach ($attr as $value) { // if (isset($data_config[$value['date']])) { // if ($data_config[$value['date']] < $detail->num) { // return [ // 'code' => 1, // 'msg' => '选中日期' . $value['date'] . '内暂无房源' // ]; // } // } else { // return [ // 'code' => 1, // 'msg' => '未找到预约时间下相关商品数据' // ]; // } // } } // 服务预约 if ($order->order_type == 2) { $service_book = GoodsBook::findOne(['goods_id' => $goods->id])->service_book; $service_book = Json::decode($service_book); if (!$service_book) { return [ 'code' => 1, 'msg' => '数据异常' ]; } $date_data = $this->getDateByInterval(7); $count = 0; $new_arr = [ 'data' => [] ]; $order = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id') ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods['id']]) ->andWhere(['>', 'o.created_at', strtotime(date("Y-m-d"))]) ->select('od.attr, od.num')->asArray()->all(); foreach ($date_data as $index => $datum) { $new_arr['data'][$index]['date'] = $datum; $new_arr['data'][$index]['time'] = $service_book['data'][0]['time']; } if (!$new_arr) { return [ 'code' => 1, 'msg' => '数据异常' ]; } $service_book = $new_arr['data']; $service_book_date = array_column($service_book, NULL, 'date'); $service_book_time = $service_book_date[$attr['date']]['time']; $num = 0; $is = false; foreach ($order as $item) { $item['attr'] = json_decode($item['attr'], true); if ($item['attr']['date'] === $attr['date']) { if ($attr['time'] == $item['attr']['time']) { $num += $item['num']; } } } foreach ($service_book_time as $m) { foreach ($m['times'] as $n) { $is = true; if ($n['time'] == $attr['time']) { if (($detail->num + $num) > $n['num']) { return [ 'code' => 1, 'msg' => $n['time'] . '时间段内暂不可预约' ]; } } } } if ($is === false) { return [ 'code' => 1, 'msg' => '未找到预约时间下相关商品数据' ]; } } return [ 'code' => 0, 'msg' => 'success' ]; } public function getDateByInterval(int $num) :array { //var_dump($st, $et);die; $returnData = []; $i = 0; do { $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d')))); $returnData[] = $temp; $i++; } while ($i < $num); return $returnData; } }