getUser(); // 获取请求参数 $params = $this->request->param(); // 验证参数 if (!isset($params['business_card_id']) || !isset($params['grade_id'])) { return $this->renderError('缺少必要参数'); } // 获取名片信息 $businessCard = CardModel::detail($params['business_card_id']); if (!$businessCard) { return $this->renderError('名片信息不存在'); } // 获取名片等级信息 $grade = CardGradeModel::detail($params['grade_id']); if (!$grade || $grade['grade_id'] != $params['grade_id']) { return $this->renderError('名片等级信息不存在'); } // 如果价格为0,直接标记为已支付 if ($grade['price'] == 0) { return $this->renderError('免费购买,无需支付'); } // 检查是否已经购买过 $orderModel = new BaseOrder(); if ($orderModel->checkCardPurchase($user['user_id'], $params['business_card_id'])) { return $this->renderError('您已购买过该名片'); } $order_no = $orderModel->orderNo(); // 创建订单 $orderData = [ 'user_id' => $user['user_id'], 'business_card_id' => $params['business_card_id'], 'grade_id' => $params['grade_id'], 'order_no' => $order_no, 'pay_price' => $grade['price'], 'order_source' => isset($params['order_source']) ? $params['order_source'] : 10, // 默认查看名片 'app_id' => $user['app_id'] ]; // 保存订单 if (!$orderModel->save($orderData)) { return $this->renderError('订单创建失败'); } $orderModel = $orderModel->where('order_no', $order_no)->find(); // 需要支付,构建支付参数 $payType = isset($params['pay_type']) ? $params['pay_type'] : OrderPayTypeEnum::WECHAT; return $this->renderSuccess('订单创建成功', [ 'order_id' => $orderModel['order_id'], 'order_no' => $orderModel['order_no'], 'pay_type' => $payType, 'order_type' => OrderTypeEnum::BUSINESS_CARD, 'pay_price' => $grade['price'] ]); } /** * 订单支付 */ public function pay() { // 获取用户信息 $user = $this->getUser(); // 获取请求参数 $params = $this->request->param(); $orderId = $params['order_id']; // 获取订单详情 $orderModel = BaseOrder::getDetail($orderId); if (!$orderModel) { return $this->renderError('订单不存在'); } // 验证订单是否属于当前用户 if ($orderModel['user_id'] != $user['user_id']) { return $this->renderError('订单不属于当前用户'); } // 订单已支付 if ($orderModel['pay_status'] == 20) { return $this->renderError('订单已支付'); } if ($this->request->isGet()) { // 开启的支付类型 $payTypes = AppModel::getPayType($orderModel['app_id'], $params['pay_source']); // 支付金额 $payPrice = $orderModel['pay_price']; $balance = $this->getUser()['balance']; return $this->renderSuccess('', compact('payTypes', 'payPrice', 'balance')); } $payType = $params['payType']; $paySource = $params['pay_source']; // 构建支付参数 $payment = $orderModel->orderPayment($user, $orderModel, $payType,$params); return $this->renderSuccess('', [ 'order_id' => $orderId, 'business_card_id' => $orderModel['business_card_id'], 'payPrice' => $orderModel['pay_price'], 'pay_type' => $payment['payType'], // 支付方式 'payment' => $payment['payment'], // 微信支付参数 'order_type' => OrderTypeEnum::BUSINESS_CARD, //订单类型 'use_balance' => $params['use_balance'],// 是否使用余额 'return_Url' => $params['pay_source'] == 'h5' ? urlencode(base_url() . "h5/pages/order/myorder") : '', //h5支付跳转地址 ]); } }