['app', 'mini']], [['pay_type'], 'in', 'range' => [1, 2, 3, 4]], // '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], ]; } $this->user->money = doubleval($this->user->money); 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 = AdoptCostOrder::findOne([ 'store_id' => $this->store_id, 'id' => $this->order_id, ]); 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' => '订单已取消', ]; } // 存储parent_id CommonOrder::saveParentId($this->parent_user_id); $goods_names = ''; if ($this->order->order_type == 0){ $goods_names = "代采订单"; } if ($this->order->order_type == 1){ $goods_names = "保管订单"; } if (!$this->order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单保存失败' ]; } // 余额抵扣支付 $balance_price = 0; if ($this->is_combine) { 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 = \app\models\Order::IS_COMBINE_PAY; $this->order->save(); } } else { $this->order->combine_money = 0; $this->order->is_combine_pay = \app\models\Order::NOT_COMBINE_PAY; $this->order->save(); } if ($this->pay_type == 1 || $this->pay_type == 4 || $this->pay_type == 5) { 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 OrderComplete(); $form->order_id = $this->order->id; $form->order_type = 0; $form->store_id = get_store_id(); $form->adoptOrderCostNotify(); $t->commit(); return [ 'code' => 0, 'msg' => '0元支付' ]; } if ($this->pay_type == 1) { if ($this->_from == self::PAY_FROM_APP) { if (is_profit_sharing()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, true, $balance_price); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, true, $balance_price); } } else if ($this->_from == self::PAY_FROM_H5) { if (is_profit_sharing()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true); } } else { if (is_profit_sharing()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price); } } if (isset($result['code']) && $result['code'] == 1) { 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 == 4) { if (\Yii::$app->params['paySwitch'] == 1) { $result = AlipayProfit::mini($this->order, $goods_names, $this->user, '', '', false, 0, $balance_price); } else { $result = Alipay::mini($this->order, $goods_names, $this->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 ]; } // 抖音小程序 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' => '支付失败', 'res' => $res ]; } $order->is_pay = 1; $order->pay_type = 3; if ($order->order_type == 1) { $order->trade_status = AdoptCostOrder::ORDER_FLOW_NO_SEND; } else{ $order->trade_status = AdoptCostOrder::ORDER_FLOW_CONFIRM; } $order->pay_time = time(); if ($order->user_id != get_user_id()) { $order->pay_user_id = get_user_id(); } if ($order->order_type == 0) { $adopt_order = AdoptOrderInfo::findOne(['order_id' => $order->order_id, 'store_id' => $order->store_id]); if ($adopt_order->pick_method == AdoptOrderInfo::ORDER_FLOW_NO_PICK) { $adopt_order->pick_method = AdoptOrderInfo::ORDER_FLOW_OTHER_PICK; $adopt_order->pick_treatment_method = AdoptOrderInfo::ORDER_FLOW_PICK_STORAGE; $adopt_order->pick_treatment_method_time = time(); $adopt_order->pick_time = time(); if (!$adopt_order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } } } if (!$order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } } // 同城配送订单下单 // if ($order->is_delivery == 1) { // $detail_goods = \app\models\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 = get_store_id(); $form->adoptOrderCostNotify(); // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); $t->commit(); return [ 'code' => 0, 'msg' => '支付成功', 'data' => '' ]; } } }