$terminal]); $this->wechatPayService = new WeChatPayService($terminal, $userId); $this->balancePayService = new BalancePayService(); } function realPay() { return $this->wechatPayService->realPay(); } /** * @notes 混合支付(余额+微信) * @param $from 订单类型 * @param $order 订单信息 * @return array|false * @author 系统 * @date 2024/12/19 */ public function pay($from, $order) { Db::startTrans(); try { $user = User::findOrEmpty($order['user_id']); if ($user->isEmpty()) { throw new \Exception('用户不存在'); } $userBalance = $user['user_money']; // 用户余额 $orderAmount = $order['order_amount']; // 订单金额 // 如果余额足够支付全部订单,直接使用余额支付 if ($userBalance >= $orderAmount) { $result = $this->balancePayService->pay($from, $order); if ($result === false) { throw new \Exception('余额支付失败'); } Db::commit(); return $result; } // 余额不足,使用混合支付 $balanceAmount = $userBalance; // 使用全部余额 $wechatAmount = $orderAmount - $balanceAmount; // 剩余金额用微信支付 // 先扣除用户余额 if ($balanceAmount > 0) { User::update([ 'user_money' => ['dec', $balanceAmount] ], ['id' => $order['user_id']]); // 记录余额流水 AccountLogLogic::add( $order['user_id'], AccountLogEnum::BNW_DEC_ORDER, AccountLogEnum::DEC, $balanceAmount, $order['sn'], '混合支付-余额部分' ); } // 创建微信支付订单(金额为剩余需要支付的金额) $wechatOrder = $order; $wechatOrder['order_amount'] = $wechatAmount; $wechatOrder['balance_amount'] = $balanceAmount; // 记录已使用的余额金额 $result = $this->wechatPayService->pay($from, $wechatOrder); if ($result === false) { throw new \Exception('微信支付创建失败'); } // 在返回结果中添加混合支付信息 $result['pay_way'] = PayEnum::MIXED_PAY; $result['balance_amount'] = $balanceAmount; $result['wechat_amount'] = $wechatAmount; $result['total_amount'] = $orderAmount; Db::commit(); return $result; } catch (\Exception $e) { Db::rollback(); $this->setStatus(false, $e->getMessage()); return false; } } /** * @notes 混合支付退款 * @param $order 订单信息 * @param $refundAmount 退款金额 * @param $afterSaleId 售后ID * @return bool */ public function refund($order, $refundAmount, $afterSaleId) { try { // 获取订单的余额支付金额和微信支付金额 $balanceAmount = $order['balance_amount'] ?? 0; $wechatAmount = $order['order_amount'] - $balanceAmount; // 按比例退款 if ($refundAmount >= $order['order_amount']) { // 全额退款 $refundBalanceAmount = $balanceAmount; $refundWechatAmount = $wechatAmount; } else { // 部分退款,按比例分配 $refundRatio = $refundAmount / $order['order_amount']; $refundBalanceAmount = $balanceAmount * $refundRatio; $refundWechatAmount = $wechatAmount * $refundRatio; } // 退回余额 if ($refundBalanceAmount > 0) { $this->balancePayService->refund($order, $refundBalanceAmount, $afterSaleId); } // 微信退款 if ($refundWechatAmount > 0) { // 这里需要调用微信退款接口 // $this->wechatPayService->refund($order['sn'], $refundWechatAmount, $afterSaleId); } return true; } catch (\Exception $e) { $this->setStatus(false, $e->getMessage()); return false; } } // 实现抽象方法 public function payMnp() { return $this->wechatPayService->payMnp(); } public function payOa() { return $this->wechatPayService->payOa(); } public function payIos() { return $this->wechatPayService->payIos(); } public function payAndroid() { return $this->wechatPayService->payAndroid(); } public function payH5() { return $this->wechatPayService->payH5(); } public function payPc() { return $this->wechatPayService->payPc(); } public function payNotify() { return $this->wechatPayService->payNotify(); } public function queryPay() { return $this->wechatPayService->queryPay(); } public function queryRefund() { return $this->wechatPayService->queryRefund(); } }