[self::SUBMIT_ORDER]], [['store_id', 'user_id', 'pay_type', 'recharge_id'], 'integer', 'on' => [self::SUBMIT_ORDER]], [['pay_price'], 'number', 'on' => [self::SUBMIT_ORDER]], [['order_no', 'pay_code'], 'string', 'on' => [self::PAY_ORDER]], [['order_no'], 'required', 'on' => [self::PAY_ORDER]], ]; } public function attributeLabels(): array { return [ 'store_id' => '商城ID', 'user_id' => '用户ID', 'pay_price' => '充值支付金额', 'pay_type' => '支付方式', 'order_no' => '支付的订单号', 'pay_code' => '付款码信息' ]; } public function recharge(): array { try { if (!$this->validate()) { return ['code' => 1, 'msg' => $this->getErrorSummary(FALSE)[0]]; } $user = User::findOne($this->user_id); if (empty($user)) { throw new \Exception('充值用户不存在'); } // 验证是否开启了自定义余额充值金额 $name = [ 'recharge_custom_status', ]; $data = Option::get($name, get_store_id(), 'recharge'); $data = array_column($data, NULL, 'name'); $customStatus = $data['recharge_custom_status']['value'];// 是否开启自定义充值金额 if ($customStatus == 0 && $this->recharge_id == 0) { throw new \Exception('请选择充值方案'); } if ($customStatus == 1 && $this->recharge_id == 0 && empty($this->pay_price)) { throw new \Exception('请输入充值金额'); } if ($this->recharge_id > 0) { $rechargePlan = Recharge::find()->where(['id' => $this->recharge_id, 'store_id' => $this->store_id, 'is_delete' => Recharge::IS_DELETE_NO])->one(); if (empty($rechargePlan)) { throw new \Exception('充值方案不存在'); } // 充值赠送金额计算 $this->pay_price = $rechargePlan->pay_price; $sendPrice = $rechargePlan->send_price; // 是否可以充值条件判断 if (!empty($rechargePlan->assign_member)) { $condition = json_decode($rechargePlan->assign_member, TRUE); $numeric_array = array_map('intval', $condition); $levelType = Level::find()->where(['store_id' => get_store_id(), 'is_delete' => Level::NOT_DELETE])->andWhere(['level' => $numeric_array])->asArray()->all(); if (in_array(0, $numeric_array)) { } else if (isset($user->level) && in_array($user->level, $numeric_array)) { } else { $names = ArrayHelper::getColumn( $levelType, function ($item) { return is_array($item['name']) ? implode(', ', $item['name']) : $item['name']; } ); $levelNames = implode(', ', $names); if (!empty($levelNames)) { throw new \Exception("仅限 {$levelNames} 购买"); } } } // 充值赠送积分计算 $sendIntegral = $rechargePlan->send_integral; // 充值升级会员 $levelUp = $rechargePlan->level_up; // 充值方案ID $rechargePlanId = $rechargePlan->id; } else { $sendPrice = 0; $sendIntegral = 0; $levelUp = 0; $rechargePlanId = 0; } $t = \Yii::$app->db->beginTransaction(); // 创建充值订单 $reOrder = new ReOrder(); $reOrder->store_id = $this->store_id; $reOrder->order_no = OrderNo::getOrderNo(OrderNo::ORDER_RECHARGE); $reOrder->user_id = $user->id; $reOrder->pay_price = $this->pay_price; $reOrder->send_price = $sendPrice; $reOrder->send_integral = $sendIntegral; $reOrder->pay_type = $this->pay_type; $reOrder->is_delete = 0; $reOrder->created_at = time(); $reOrder->create_user_id = get_user_id(); if ($levelUp && $rechargePlanId) { $reOrder->level_up = $levelUp; $reOrder->recharge_id = $rechargePlanId;// 2024-12-19 这里的支付金额不对 } // 订单生成失败 返回业务逻辑错误 if (!$reOrder->save()) { $t->rollBack(); throw new \Exception('订单生成失败'); } if ($this->pay_type == self::CASH_PAY) { self::payConfirm($user, $reOrder); return [ 'code' => 0, 'msg' => '余额充值成功', 'data' => [ 'need_pay' => 0, 'order_no' => $reOrder->order_no ] ]; } $t->commit(); return [ 'code' => 0, 'msg' => '充值订单创建成功', 'data' => [ 'need_pay' => 1, 'order_no' => $reOrder->order_no ] ]; } catch (\Exception $e) { return ['code' => 1, 'msg' => $e->getMessage()]; } } public function pay(): array { try { if (!$this->validate()) { throw new \Exception($this->getErrorSummary(FALSE)[0]); } $order = ReOrder::find()->where(['order_no' => $this->order_no, 'is_pay' => ReOrder::NOT_PAY, 'is_delete' => ReOrder::NOT_DELETE])->one(); if (empty($order)) { throw new \Exception('订单不存在'); } $user = User::findOne($order->user_id); if ($order->pay_type == self::WECHAT_PAY) { if (is_profit_pay()) { $result = WechatNewPay::micropay($order, OrderNo::ORDER_MALL, '余额充值', $order->pay_price, 0, $this->pay_code); } else { $result = WechatPay::micropay($order, OrderNo::ORDER_MALL, '余额充值', $order->pay_price, 0, $this->pay_code); } if ($result['return_code'] == 'SUCCESS') { if ($result['result_code'] === 'FAIL') { // 如果返回结果不是 等待用户输入密码 则直接返回 if ($result['err_code'] == 'USERPAYING') { $result = $this->checkOrderPay($order, 0); } else { return [ 'code' => 1, 'msg' => '微信支付参数错误', 'data' => $result ]; } } if ($result['result_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS' && $result['trade_type'] == 'MICROPAY') { $t = \Yii::$app->db->beginTransaction(); $order->transaction_id = $result['transaction_id']; $order->is_pay = 1; $order->pay_time = time(); if (!$order->save()) { $t->rollBack(); throw new \Exception('订单状态更新失败'); } self::payConfirm($user, $order); $t->commit(); $notifyForm = new NotifyForm(); $notifyForm->addRechargeReceiver($order); queue_push(new ReOrderWechatProfitSharingJob(['store_id' => $this->store_id, 'order' => $order]), 60); return ['code' => 0, 'msg' => '余额充值成功']; } return [ 'code' => 1, 'msg' => '微信支付错误', 'data' => $result ]; } else { throw new \Exception('微信支付失败'.$result['return_msg']); } } else { throw new \Exception('暂不支持该支付方式'); } } catch (\Exception $e) { return ['code' => 1, 'msg' => $e->getMessage()]; } } public 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; } } } private static function payConfirm($user, $order): void { try { try { // 用户充值成为股东 BonusPool::generalUserRecharge($order->user_id, $order->store_id); } catch (\Exception $e) { } $order->pay_time = time(); $order->pay_type = self::CASH_PAY; $before = $user->money; $user->money += floatval($order->pay_price) + floatval($order->send_price); if ($order->send_integral > 0) { $old_integral = $user->integral; $user->integral += $order->send_integral; $user->total_integral += $order->send_integral; $log = new AccountLog(); $log->store_id = get_store_id(); $log->user_id = $user->id; $log->type = AccountLog::TYPE_INTEGRAL; $log->log_type = AccountLog::LOG_TYPE_INCOME; $log->amount = $order->send_integral; $log->desc = "充值赠送, 订单号:{$order->order_no}"; $log->before = $old_integral; $log->after = $user->integral; $log->operator = 'system'; $log->operator_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $log->order_id = $order->id; $log->order_type = AccountLog::TYPE_PLATFORM_ORDER; $log->save(); } $user->save(); $log = new AccountLog(); $log->store_id = $order->store_id; $log->user_id = $user->id; $log->type = AccountLog::TYPE_BALANCE; $log->log_type = AccountLog::LOG_TYPE_INCOME; $log->amount = floatval($order->pay_price) + floatval($order->send_price); $log->desc = "充值余额微信在线支付,付款金额:{$order->pay_price}元,赠送金额:{$order->send_price}元。"; $log->before = $before; $log->after = $user->money; $log->operator = ''; $log->operator_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $log->order_id = $order->id; $log->order_type = AccountLog::TYPE_RECHARGE_ORDER; $log->save(); // 生成操作记录 $user = User::findOne($order->user_id); CashierActionLog::setLog($order->store_id,get_user_id(),CashierActionLog::RECHARGE_MONEY,'给'.$user->nickname.'用户充值余额'); } catch (\Exception $e) { } } }