balance($order, $money); } // 第三方支付退款 if (in_array($order['pay_method'], [PaymentMethodEnum::WECHAT, PaymentMethodEnum::ALIPAY, PaymentMethodEnum::HUIFU])) { return $this->payment($order, $money); } return false; } /** * 余额支付退款 * @param mixed $order 订单信息 * @param string $money 退款金额 * @return bool */ private function balance($order, string $money): bool { // 回退用户余额 UserModel::setIncBalance((int)$order['user_id'], (float)$money); // 记录余额明细 BalanceLogModel::add(SceneEnum::REFUND, [ 'user_id' => $order['user_id'], 'money' => $money, ], ['order_no' => $order['order_no']], $order['store_id']); return true; } /** * 第三方支付退款 * @param mixed $order 订单信息 * @param string $money 退款金额 * @return bool * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function payment($order, string $money): bool { // 获取第三方交易记录 $tradeInfo = $this->getTradeInfo($order['trade_id']); // 获取支付方式的配置信息 $options = $this->getPaymentConfig($order); // 构建支付模块 $Payment = PaymentFacade::store($order['pay_method'])->setOptions($options, $order['platform']); // 执行第三方支付下单API if (!$Payment->refund($tradeInfo['out_trade_no'], $money, ['totalFee' => (string)$order['pay_price'], 'trade_no' => $tradeInfo['trade_no']])) { throwError($Payment->getError() ?: '第三方支付退款API调用失败'); } // 将第三方交易记录更新为已退款状态 $this->updateTradeState($order['trade_id']); return true; } /** * 获取第三方交易记录 * @param int $tradeId 交易记录ID * @return PaymentTradeModel|null * @throws BaseException */ private function getTradeInfo(int $tradeId): ?PaymentTradeModel { $tradeInfo = PaymentTradeModel::detail($tradeId); empty($tradeInfo) && throwError('未找到第三方交易记录'); return $tradeInfo; } /** * 将第三方交易记录更新为已退款状态 * @param int $tradeId 交易记录ID */ private function updateTradeState(int $tradeId) { PaymentTradeModel::updateToRefund($tradeId); } /** * 获取支付方式的配置信息 * @param mixed $order 订单信息 * @return mixed * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function getPaymentConfig($order) { $PaymentModel = new PaymentModel; $templateInfo = $PaymentModel->getPaymentInfo($order['pay_method'], $order['platform'], $order['store_id']); return $templateInfo['template']['config'][$order['pay_method']]; } }