['app', 'mini', 'h5', 'official', 'yunst']], [['pay_type'], 'in', 'range' => [1, 2, 3, 4, 5, Order::PAY_TYPE_YUNST_WECHAT_PAY]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY', [['form_id', 'order_id_list', '_from'], '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], ]; } $t = \Yii::$app->db->beginTransaction(); if ($this->order_id) { //单个订单付款 $this->order = Order::findOne([ 'id' => $this->order_id, ]); if (intval($this->order->is_pay)) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单已支付', ]; } // 供应链系统下单时是否使用平台商户号,1使用,0未使用 if (!Store::hasIncoming($this->order->store_id)) { $this->order->is_use_platform_mch = 1; $this->order->save(); } //0元订单开始 if ($this->order->pay_price == 0) { $this->order->pay_time = time(); $this->order->is_pay = 1; $this->order->pay_type = 3; $this->order->trade_status = Order::ORDER_FLOW_NO_SEND; $this->order->save(); $form = new AllianceOrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = $this->order->store_id; $form->notify(); $t->commit(); return [ 'code' => 0, 'msg' => '支付成功' ]; } //0元订单结束 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() ]; } $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; $this->order->combine_money = 0; $this->order->is_combine_pay = Order::NOT_COMBINE_PAY; $this->order->save(); if ($this->pay_type == 1 || $this->pay_type == Order::PAY_TYPE_YUNST_WECHAT_PAY || $this->pay_type == 4 || $this->pay_type == 5) { //0元支付开始 if ($this->order->pay_price == 0) { $this->order->is_pay = 1; $this->order->pay_type = 1; if ($this->pay_type == 4) { $this->order->pay_type = 4; } $this->order->pay_time = time(); if (!$this->order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } // 支付完成后,相关操作 $form = new AllianceOrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; //$form->store_id = get_store_id(); $form->store_id = $this->order->store_id; $form->notify(); $t->commit(); return [ 'code' => 0, 'msg' => '0元支付' ]; } //0元支付结束 if ($this->pay_type == 1) { // \Yii::error($this->_from); if ($this->_from == self::PAY_FROM_APP) { if (!Store::hasIncoming($this->order->store_id)) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_ALLIANCE, $goods_names, 0, true, $balance_price); } else { $result = WechatAlliancePay::orderUnify($this->order,OrderNo::ORDER_ALLIANCE, $goods_names, 0, true, $balance_price); } } else if ($this->_from == self::PAY_FROM_H5) { if (!Store::hasIncoming($this->order->store_id)) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true); } else { $result = WechatAlliancePay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true); } } else if ($this->_from == self::PAY_FROM_OFFICIAL) { if (!Store::hasIncoming($this->order->store_id)) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, false, true); } else { $result = WechatAlliancePay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, false, true); } } else { if (!Store::hasIncoming($this->order->store_id)) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_ALLIANCE, $goods_names, 0, false, $balance_price); } else { $result = WechatAlliancePay::orderUnify($this->order,OrderNo::ORDER_ALLIANCE, $goods_names, 0, false, $balance_price); } } if (isset($result['code']) && $result['code'] == 1) { if ($this->_from == self::PAY_FROM_OFFICIAL) { $result['wechat_platform_open_id'] = get_user()->wechat_platform_open_id; $result['h5_auth_link'] = LoginForm::getAuthLink(); } return $result; } CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); $t->commit(); return [ 'code' => 0, 'msg' => 'success', 'data' => (object)$result['data'], 'res' => $result['res'], 'body' => $goods_names ]; } if ($this->pay_type == Order::PAY_TYPE_YUNST_WECHAT_PAY) { // if (!Store::hasIncoming($this->order->store_id, 'yunst')) { // return [ // 'code' => 1, // 'msg' => '商家未配置通联支付' // ]; // } //是非独立部署 进件流程//通联支付 start $store = Store::findOne(['id' => $this->order->store_id]); $OrderYunst = new OrderYunst(); $result2 = $OrderYunst->payYunst($this->order->store_id, $this->order->order_no, $store->cusid, $this->order->pay_price * 100); if ($result2['subCode'] == 'SUCCESS') { $payInfo = json_decode($result2['data']['payinfo'], true); if (!empty($payInfo)) { if (is_array($payInfo)) { return [ 'code' => 0, 'data' => $payInfo, 'msg' => 'success', 'res' => $payInfo ]; } return [ 'code' => 1, 'msg' => $result2['data']['errmsg'] ]; } return [ 'code' => 1, 'msg' => $result2['subMsg'] ]; } return $result2; //通联支付 end } if ($this->pay_type == 4) { $user = ($this->user ?: get_saas_user()); if ($this->_from == self::PAY_FROM_MINI) { //TODO 兼容 //AlipayUnion if (is_profit_pay('ali')) { $result = AlipayUnion::mini($this->order, $goods_names, $user, '', '', false, 0, $balance_price); } else { $result = Alipay::mini($this->order, $goods_names, $user, '', '', 0, $balance_price); } if (isset($result['code']) && $result['code'] == 1) { return $result; } //$t->commit(); return [ 'code' => 0, 'msg' => 'success', 'data' => $result['data']['trade_no'], 'body' => $goods_names ]; } else { if (is_profit_pay('ali')) { if ($this->order->store_id <= 0) { $result = AlipayUnion::app($this->order, $goods_names, $this->user, $balance_price); } else { $result = AlipayProfit::app($this->order, $goods_names, $this->user, $balance_price); } } else { $result = Alipay::app($this->order, $goods_names, $this->user, $balance_price); } if (isset($result['code']) && $result['code'] == 1) { //$t->commit(); return $result; } //$t->commit(); return [ 'code' => 0, 'msg' => 'success', 'data' => $result['data'], 'body' => $goods_names ]; } } // // 抖音小程序 // if ($this->pay_type == 5) { // $result = ByteDance::pay($this->order, '', $goods_names, 0 , $balance_price); // if (isset($result['code']) && $result['code'] == 1) { // return $result; // } // //$t->commit(); // return [ // 'code' => 0, // 'msg' => 'success', // 'data' => $result['data'], // ]; // } } // //货到付款和余额支付数据处理 // if ($this->pay_type == 2 || $this->pay_type == 3) { // $order = $this->order; // // 中间页选择货到付款,需要存入支付类型 // if ($this->pay_type == 2) { // $order->is_pay = 0; // $order->pay_type = 2; // $order->save(); // } // // //余额支付 用户余额变动 // // if ($this->pay_type == 3) { // // $user = User::findOne(['id' => $order->user_id]); // // if ($user->money < $order->pay_price) { // // $t->rollBack(); // // return [ // // 'code' => 1, // // 'msg' => '支付失败,余额不足', // // ]; // // } // // // $user->money -= floatval($order->pay_price); // // $res = AccountLog::saveLog($user->id, floatval($order->pay_price), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 1, $order->id, "商城余额支付,订单号为:{$order->order_no}。"); // // if (!$res) { // // $t->rollBack(); // // return [ // // 'code' => 1, // // 'msg' => '支付失败' // // ]; // // } // // $order->is_pay = 1; // // $order->pay_type = 3; // // $order->trade_status = Order::ORDER_FLOW_NO_SEND; // // $order->pay_time = time(); // // if ($order->user_id != get_user_id()) { // // $order->pay_user_id = get_user_id(); // // } // // if (!$order->save()) { // // $t->rollBack(); // // return [ // // 'code' => 1, // // 'msg' => '支付失败' // // ]; // // } // // } // // 同城配送订单下单 // if ($order->is_delivery == 1) { // $detail_goods = OrderDetail::find()->where(['order_id' => $order->id])->select('goods_name as name, pic, num, total_price as price')->asArray()->all(); // Delivery::addOrder($detail_goods, $order->order_no); // } // // 支付完成后,相关操作 // $form = new OrderComplete(); // $form->order_id = $order->id; // $form->order_type = 0; // $form->store_id = $this->order->store_id; // $form->notify(); // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); // //$t->commit(); // return [ // 'code' => 0, // 'msg' => '支付成功', // 'data' => '' // ]; // } } } /** * 设置佣金 * @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) { $saas_user = get_saas_user(); if ($order->user_id > 0) { $user = User::findOne($order->user_id); } else { if ($order->saas_id > 0) { $saas_user = SaasUser::findOne($order->saas_id); $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => get_store_id(), 'is_delete' => 0]); } } foreach ($order->detail as $detail) { /* @var Goods $goods*/ /* @var OrderDetail $detail*/ $goods = $detail->goods; if ($goods->confine_count && $goods->confine_count > 0) { $goodsNum = 0; if (!empty($user)) { $goodsNum = Goods::getBuyNum($user, $goods->id); } $goodsTotalNum = intval($goodsNum + $detail->num); if ($goodsTotalNum > $goods->confine_count) { throw new \Exception('商品:' . $goods->name . ' 超出购买数量', 1); } } //起订数量 if (($goods['order_min_count'] && $goods['order_min_count'] > 0)) { if ($detail->num < $goods['order_min_count']) { throw new \Exception('商品:' . $goods->name . ' 起订数量' . $goods['order_min_count'], 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; } }