['app', 'mini', 'official', self::PAY_FROM_H5]], [['pay_type'], 'in', 'range' => [1, 2, 3, 4, 5, Order::PAY_TYPE_WX_B2B]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY', [['_from'], 'string'], ['is_combine', 'default', 'value' => 0], [[ 'order_id', 'is_combine'], 'integer'], [['session_key'], 'safe'] ]; } 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); $t = \Yii::$app->db->beginTransaction(); $this->order = LevelOrder::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->is_pay == 1) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单已取消或已支付', ]; } $goods_names = '会员购买'; // 余额抵扣支付 $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 = 1; $this->order->save(); } } else { $this->order->combine_money = 0; $this->order->is_combine_pay = 0; $this->order->save(); } // 供应链系统下单时是否使用平台商户号,1使用,0未使用 if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->store_id)) { $this->order->is_use_platform_mch = 1; $this->order->save(); } if ($this->pay_type == 1 || $this->pay_type == 4) { 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' => '支付失败' ]; } $t->commit(); return [ 'code' => 0, 'msg' => '0元支付' ]; } if ($this->pay_type == 1) { // if ($this->_from == self::PAY_FROM_APP) { // if (is_profit_pay()) { // $result = WechatNewPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, true, $balance_price); // } else { // $result = WechatPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, true, $balance_price); // } // } else { // if (is_profit_pay()) { // $result = WechatNewPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, false, $balance_price); // } else { // $result = WechatPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, false, $balance_price); // } // } if ($this->_from == self::PAY_FROM_APP) { $self_mini = intval(\app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']); if (\Yii::$app->prod_is_dandianpu() && !$self_mini) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price); } else { if (is_profit_pay()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price); } } } else if ($this->_from == self::PAY_FROM_H5) { if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && self_mini() === false) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true); } else { if (is_profit_pay()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true); } } } else if ($this->_from == self::PAY_FROM_OFFICIAL) { $self_mini = intval(\app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']); if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && !$self_mini) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true); } else { if (is_profit_pay()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true); } } } else { if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && self_mini() === false) { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price); } else { if (is_profit_pay() && Store::hasIncoming($this->order->store_id)) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $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 (is_profit_pay('ali')) { $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 == Order::PAY_TYPE_WX_B2B) { return \app\modules\admin\models\UserAuditForm::pay($this->session_key, $this->order,OrderNo::ORDER_RECHARGE, $goods_names); } //货到付款和余额支付数据处理 if ($this->pay_type == 2 || $this->pay_type == 3) { $order = $this->order; // 中间页选择货到付款,需要存入支付类型 if ($this->pay_type == 2) { $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->pay_time = time(); if (!$order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } else { //会员升级 $user = User::findOne($order->user_id); $user->level = $order->after_level; $user->save(); $level = Level::findOne(['level' => $user->level, 'store_id' => $order->store_id, 'is_delete' => 0]); if ($level) { $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); $user_name = $user->nickname; if ($saas_user) { $user_name = $saas_user->name; } if ($user->old_parent_id) { $parent = User::findOne($user->old_parent_id);//上级 //检测一级是否存在且佣金是否设置 if ($parent && $level->firstProfit) { //发放一级佣金 //... $parent->total_price += $level->firstProfit; $parent->price += $level->firstProfit; $parent->save(); UserShareMoney::set($level->firstProfit, $user->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放一级佣金'); if ($parent->old_parent_id) { $parent_1 = User::findOne($parent->old_parent_id);//上级 //检测二级是否存在且佣金是否设置 if ($parent_1 && $level->secondProfit) { //发放二级佣金 $parent_1->total_price += $level->secondProfit; $parent_1->price += $level->secondProfit; $parent_1->save(); UserShareMoney::set($level->secondProfit, $parent->old_parent_id, $order->id, 0, 2, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放二级佣金'); //检测三级是否存在且佣金是否设置 $parent_2 = User::findOne($parent_1->old_parent_id);//上级 if ($parent_2 && $level->thirdProfit) { //发放三级佣金 $parent_2->total_price += $level->thirdProfit; $parent_2->price += $level->thirdProfit; $parent_2->save(); UserShareMoney::set($level->thirdProfit, $parent_1->old_parent_id, $order->id, 0, 3, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放三级佣金'); } } } } } } } } $t->commit(); return [ 'code' => 0, 'msg' => '下单成功', 'data' => '' ]; } } }