| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\common\service\pay;
- use app\common\enum\AccountLogEnum;
- use app\common\enum\PayEnum;
- use app\common\enum\AfterSaleEnum;
- use app\common\enum\AfterSaleLogEnum;
- use app\common\enum\NoticeEnum;
- use app\common\model\AfterSale;
- use app\common\service\after_sale\AfterSaleService;
- use app\common\logic\AccountLogLogic;
- use app\common\model\User;
- use think\facade\Db;
- /**
- * 混合支付服务(余额+微信)
- * Class MixedPayService
- * @package app\common\service\pay
- */
- class MixedPayService extends BasePayService
- {
- protected $wechatPayService;
- protected $balancePayService;
- public function __construct($terminal, $userId = null)
- {
- try {
- $this->wechatPayService = new WeChatPayService($terminal, $userId);
- $this->balancePayService = new BalancePayService();
- } catch (\Exception $e) {
- $this->setError('混合支付服务初始化失败:' . $e->getMessage());
- }
- }
- /**
- * @notes 获取真实支付对象
- * @return mixed
- */
- 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('用户不存在');
- }
- // 记录调试日志
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 混合支付开始 - 用户ID: {$order['user_id']}, 订单号: {$order['sn']}, 订单金额: {$order['order_amount']}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- $userBalance = $user['user_money']; // 用户余额
- $orderAmount = $order['order_amount']; // 订单金额
- // 记录余额信息
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 用户余额: {$userBalance}, 订单金额: {$orderAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- // 如果余额足够支付全部订单,直接使用余额支付
- if ($userBalance >= $orderAmount) {
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 余额充足,使用纯余额支付" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- $result = $this->balancePayService->pay($from, $order);
- if ($result === false) {
- throw new \Exception('余额支付失败:' . $this->balancePayService->getError());
- }
- Db::commit();
- return $result;
- }
- // 余额不足,使用混合支付
- $balanceAmount = $userBalance; // 使用全部余额
- $wechatAmount = $orderAmount - $balanceAmount; // 剩余金额用微信支付
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 使用混合支付 - 余额部分: {$balanceAmount}, 微信部分: {$wechatAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- // 先扣除用户余额
- 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'],
- '混合支付-余额部分'
- );
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 余额扣除成功,扣除金额: {$balanceAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- }
- // 创建微信支付订单(金额为剩余需要支付的金额)
- $wechatOrder = $order;
- $wechatOrder['order_amount'] = round($wechatAmount,2);
- $wechatOrder['balance_amount'] = $balanceAmount; // 记录已使用的余额金额
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 开始创建微信支付订单,金额: {$wechatAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- outFileLog($wechatOrder,'prepay','$wechatOrder');
- // Db::commit();
- // return $wechatOrder;
- $result = $this->wechatPayService->pay($from, $wechatOrder);
- if ($result === false) {
- throw new \Exception('微信支付创建失败:' . $this->wechatPayService->getError());
- }
- // 在返回结果中添加混合支付信息
- $result['pay_way'] = PayEnum::MIXED_PAY;
- $result['balance_amount'] = $balanceAmount;
- $result['wechat_amount'] = $wechatAmount;
- $result['total_amount'] = $orderAmount;
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 混合支付创建成功" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- Db::commit();
- return $result;
- } catch (\Exception $e) {
- Db::rollback();
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_pay_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 混合支付失败: " . $e->getMessage() . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- $this->setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 混合支付退款
- * @param $order 订单信息
- * @param $refundAmount 退款金额
- * @param $afterSaleId 售后ID
- * @return bool
- */
- public function refund($order, $refundAmount, $afterSaleId)
- {
- Db::startTrans();
- try {
- // 记录退款日志
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_refund_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 混合支付退款开始 - 订单号: {$order['sn']}, 退款金额: {$refundAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- // 获取订单的余额支付金额和微信支付金额
- $balanceAmount = $order['balance_amount'] ?? 0;
- $wechatAmount = $order['order_amount'] - $balanceAmount;
- // 计算退款比例
- $refundRatio = $refundAmount / $order['order_amount'];
- $refundBalanceAmount = round($balanceAmount * $refundRatio, 2);
- $refundWechatAmount = round($wechatAmount * $refundRatio, 2);
- // 确保退款金额精确
- $totalCalculated = $refundBalanceAmount + $refundWechatAmount;
- if ($totalCalculated != $refundAmount) {
- $diff = $refundAmount - $totalCalculated;
- if ($refundWechatAmount > 0) {
- $refundWechatAmount += $diff;
- } else {
- $refundBalanceAmount += $diff;
- }
- }
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_refund_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 退款分配 - 余额退款: {$refundBalanceAmount}, 微信退款: {$refundWechatAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- // 处理余额退款
- if ($refundBalanceAmount > 0) {
- // 返回余额
- User::update([
- 'user_money' => ['inc', $refundBalanceAmount]
- ], ['id' => $order['user_id']]);
- // 记录余额流水
- $afterSale = AfterSale::findOrEmpty($afterSaleId);
- AccountLogLogic::add(
- $order['user_id'],
- AccountLogEnum::BNW_INC_AFTER_SALE,
- AccountLogEnum::INC,
- $refundBalanceAmount,
- $afterSale->sn,
- '混合支付退款-余额部分'
- );
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_refund_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 余额退款完成: {$refundBalanceAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- }
- // 处理微信退款
- if ($refundWechatAmount > 0) {
- $refundData = [
- 'transaction_id' => $order['transaction_id'],
- 'refund_sn' => 'mixed_refund_' . $order['sn'] . '_' . time(),
- 'total_fee' => $wechatAmount,
- 'refund_fee' => $refundWechatAmount,
- ];
- $result = $this->wechatPayService->refund($refundData);
- if ($result !== true) {
- throw new \Exception('微信退款失败:' . $this->wechatPayService->getError());
- }
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_refund_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 微信退款完成: {$refundWechatAmount}" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- }
- // 更新售后状态
- $afterSale = AfterSale::findOrEmpty($afterSaleId);
- if (!$afterSale->isEmpty()) {
- // 判断退款状态
- $refundStatus = ($refundAmount >= $order['order_amount']) ?
- AfterSaleEnum::FULL_REFUND : AfterSaleEnum::PARTIAL_REFUND;
- $afterSale->status = AfterSaleEnum::STATUS_SUCCESS;
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUND_SUCCESS;
- $afterSale->refund_status = $refundStatus;
- $afterSale->save();
- // 添加售后日志
- AfterSaleService::createAfterLog(
- $afterSale->id,
- '系统已完成混合支付退款',
- 0,
- AfterSaleLogEnum::ROLE_SYS
- );
- // 发送退款成功通知
- event('Notice', [
- 'scene_id' => NoticeEnum::REFUND_SUCCESS_NOTICE,
- 'params' => [
- 'user_id' => $afterSale->user_id,
- 'order_sn' => $order['sn'],
- 'after_sale_sn' => $afterSale->sn,
- 'refund_type' => AfterSaleEnum::getRefundTypeDesc($afterSale->refund_type),
- 'refund_total_amount' => $afterSale->refund_total_amount,
- 'refund_time' => date('Y-m-d H:i:s'),
- ]
- ]);
- }
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_refund_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 混合支付退款成功" . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- file_put_contents(
- runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'mixed_refund_debug_' . date('Y-m-d') . '.log',
- "[" . date('Y-m-d H:i:s') . "] 混合支付退款失败: " . $e->getMessage() . PHP_EOL,
- FILE_APPEND | LOCK_EX
- );
- $this->setError($e->getMessage());
- return false;
- }
- }
- }
|