moonsflyer 5 месяцев назад
Родитель
Сommit
6cd35a16c4
1 измененных файлов с 76 добавлено и 28 удалено
  1. 76 28
      app/common/service/pay/MixedPayService.php

+ 76 - 28
app/common/service/pay/MixedPayService.php

@@ -23,8 +23,6 @@ use app\common\enum\AccountLogEnum;
 use app\common\enum\PayEnum;
 use app\common\logic\AccountLogLogic;
 use app\common\model\User;
-use app\common\service\pay\base\BasePayService;
-use app\common\service\pay\base\StatusTrait;
 use think\facade\Db;
 
 /**
@@ -34,19 +32,24 @@ use think\facade\Db;
  */
 class MixedPayService extends BasePayService
 {
-    use StatusTrait;
-
     protected $wechatPayService;
     protected $balancePayService;
 
     public function __construct($terminal, $userId = null)
     {
-        parent::__construct(['terminal' => $terminal]);
-        $this->wechatPayService = new WeChatPayService($terminal, $userId);
-        $this->balancePayService = new BalancePayService();
+        try {
+            $this->wechatPayService = new WeChatPayService($terminal, $userId);
+            $this->balancePayService = new BalancePayService();
+        } catch (\Exception $e) {
+            $this->setError('混合支付服务初始化失败:' . $e->getMessage());
+        }
     }
 
-   function realPay()
+    /**
+     * @notes 获取真实支付对象
+     * @return mixed
+     */
+    function realPay()
     {
         return $this->wechatPayService->realPay();
     }
@@ -67,15 +70,35 @@ class MixedPayService extends BasePayService
             if ($user->isEmpty()) {
                 throw new \Exception('用户不存在');
             }
-            outFileLog($user,'prepay','$user');
+
+            // 记录调试日志
+            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('余额支付失败');
+                    throw new \Exception('余额支付失败' . $this->balancePayService->getError());
                 }
                 Db::commit();
                 return $result;
@@ -85,6 +108,12 @@ class MixedPayService extends BasePayService
             $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([
@@ -100,16 +129,28 @@ class MixedPayService extends BasePayService
                     $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
+                );
             }
-            outFileLog(2,'prepay','2');
+
             // 创建微信支付订单(金额为剩余需要支付的金额)
             $wechatOrder = $order;
             $wechatOrder['order_amount'] = $wechatAmount;
             $wechatOrder['balance_amount'] = $balanceAmount; // 记录已使用的余额金额
-            outFileLog($wechatOrder,'prepay','$wechatOrder');
+
+            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
+            );
+
             $result = $this->wechatPayService->pay($from, $wechatOrder);
             if ($result === false) {
-                throw new \Exception('微信支付创建失败');
+                throw new \Exception('微信支付创建失败' . $this->wechatPayService->getError());
             }
 
             // 在返回结果中添加混合支付信息
@@ -118,12 +159,25 @@ class MixedPayService extends BasePayService
             $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();
-            $this->setStatus(false, $e->getMessage());
+
+            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;
         }
     }
@@ -162,25 +216,19 @@ class MixedPayService extends BasePayService
             // 微信退款
             if ($refundWechatAmount > 0) {
                 // 这里需要调用微信退款接口
-                // $this->wechatPayService->refund($order['sn'], $refundWechatAmount, $afterSaleId);
+                $refundData = [
+                    'out_trade_no' => $order['sn'],
+                    'out_refund_no' => 'refund_' . $order['sn'] . '_' . time(),
+                    'total_fee' => $wechatAmount * 100, // 原订单金额(分)
+                    'refund_fee' => $refundWechatAmount * 100, // 退款金额(分)
+                ];
+                $this->wechatPayService->refund($refundData);
             }
 
             return true;
         } catch (\Exception $e) {
-            $this->setStatus(false, $e->getMessage());
+            $this->setError($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(); }
-
 }