Browse Source

feat: 更新提现申请审核逻辑以支持用户确认收款

在WalletWithdrawRecordService中,新增approve方法以审核通过提现申请,并在processPayment方法中更新了微信转账请求的接口路径为'v3/merchant-transfer/transfer-bills'。此更改引入了用户确认收款的机制,确保用户在微信中确认收款后再完成提现操作,同时记录了相关日志以便追踪。这一改进旨在提升提现流程的安全性和用户体验。
刘学玺 2 months ago
parent
commit
6c3ab83eeb
1 changed files with 97 additions and 13 deletions
  1. 97 13
      app/Services/Admin/WalletWithdrawRecordService.php

+ 97 - 13
app/Services/Admin/WalletWithdrawRecordService.php

@@ -264,13 +264,12 @@ class WalletWithdrawRecordService
                 ]);
 
                 try {
-                    // 使用商家转账接口
-                    $response = $app->getClient()->postJson('v3/fund-app/mch-transfer/transfer-bills', [
+                    // 使用商家转账API(需要用户确认)
+                    $response = $app->getClient()->postJson('v3/merchant-transfer/transfer-bills', [
                         'appid' => $config['app_id'],
                         'out_bill_no' => $outTradeNo,
                         'transfer_scene_id' => '1001', // 现金营销场景
                         'openid' => $record->withdraw_account,
-                        // 'user_name' => $record->withdraw_account_name,
                         'transfer_amount' => (int)bcmul($record->amount, '100'),
                         'transfer_remark' => '提现到零钱',
                         'transfer_scene_report_infos' => [
@@ -292,18 +291,17 @@ class WalletWithdrawRecordService
                         'result' => $result
                     ]);
 
-                    // 检查转账状态
-                    if (isset($result['bill_id'])) {
-                        Log::info('微信转账成功', [
-                            'withdraw_id' => $record->id,
-                            'bill_id' => $result['bill_id'],
-                            'out_bill_no' => $outTradeNo
-                        ]);
-
+                    // 返回需要前端处理的确认数据
+                    if (isset($result['transfer_id'])) {
                         return [
                             'success' => true,
-                            'trade_no' => $result['bill_id'],
-                            'message' => '微信转账成功'
+                            'trade_no' => $result['transfer_id'],
+                            'message' => '请在微信中确认收款',
+                            'need_confirm' => true,
+                            'confirm_data' => [
+                                'transfer_id' => $result['transfer_id'],
+                                'out_bill_no' => $outTradeNo
+                            ]
                         ];
                     }
 
@@ -416,4 +414,90 @@ class WalletWithdrawRecordService
             ];
         }
     }
+
+    /**
+     * 审核通过提现申请
+     *
+     * @param WalletWithdrawRecord $record
+     * @return array
+     * @throws \Exception
+     */
+    public function approve(WalletWithdrawRecord $record): array
+    {
+        // 1. 检查状态
+        if ($record->state !== WithdrawAuditStatus::PENDING->value) {
+            throw new \Exception('提现记录状态不正确');
+        }
+
+        // 2. 处理打款
+        $result = $this->processPayment($record);
+
+        if ($result['success']) {
+            // 3. 更新提现记录状态
+            $record->update([
+                'state' => WithdrawAuditStatus::APPROVED->value,  // 更新为处理中状态
+                'external_no' => $result['trade_no'],         // 保存交易流水号
+                'audit_time' => now(),                        // 审核时间
+                'audit_user_id' => auth()->id(),             // 审核人ID
+            ]);
+
+            // 4. 发送模板消息通知用户确认收款
+            if (isset($result['need_confirm']) && $result['need_confirm']) {
+                try {
+                    $this->sendConfirmNotification($record, $result['confirm_data']);
+                } catch (\Exception $e) {
+                    Log::error('发送确认收款通知失败', [
+                        'withdraw_id' => $record->id,
+                        'error' => $e->getMessage()
+                    ]);
+                }
+            }
+
+            // 5. 记录日志
+            Log::info('提现申请已审核通过', [
+                'withdraw_id' => $record->id,
+                'external_no' => $result['trade_no'],
+                'amount' => $record->amount,
+                'need_confirm' => $result['need_confirm'] ?? false
+            ]);
+
+            return [
+                'success' => true,
+                'message' => $result['message']
+            ];
+        }
+
+        throw new \Exception($result['message']);
+    }
+
+    /**
+     * 发送确认收款通知
+     */
+    private function sendConfirmNotification(WalletWithdrawRecord $record, array $confirmData): void
+    {
+        // 获取微信配置
+        $config = config('wechat.official_account.default');
+        $app = new \EasyWeChat\OfficialAccount\Application($config);
+
+        // 发送模板消息
+        $app->getClient()->postJson('cgi-bin/message/template/send', [
+            'touser' => $record->withdraw_account,
+            'template_id' => config('wechat.template_ids.withdraw_confirm'),
+            'url' => config('app.h5_url') . '/pages/wallet/withdraw-confirm',  // 确认收款页面
+            'data' => [
+                'first' => [
+                    'value' => '您的提现申请已审核通过,请确认收款'
+                ],
+                'keyword1' => [
+                    'value' => $record->amount . '元'
+                ],
+                'keyword2' => [
+                    'value' => $record->external_no
+                ],
+                'remark' => [
+                    'value' => '请在24小时内确认收款,逾期未确认将自动退回。'
+                ]
+            ]
+        ]);
+    }
 }