123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <?php
- namespace App\Services\Admin;
- use App\Models\WalletWithdrawRecord;
- use App\Models\WalletTransRecord;
- use App\Enums\WithdrawStatus;
- use App\Enums\WithdrawAuditStatus;
- use App\Enums\TransactionType;
- use App\Enums\TransactionStatus;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use App\Enums\WithdrawMethod;
- use EasyWeChat\Pay\Application;
- class WalletWithdrawRecordService
- {
- /**
- * 审核提现申请
- *
- * @param int $id 提现记录ID
- * @param int $status 审核状态(2:通过 3:拒绝)
- * @param string|null $remark 审核备注
- */
- public function audit(int $id, int $status, ?string $remark = null): void
- {
- DB::transaction(function () use ($id, $status, $remark) {
- // 获取提现记录
- $record = WalletWithdrawRecord::lockForUpdate()->findOrFail($id);
- // 验证记录状态
- abort_if($record->audit_state !== WithdrawAuditStatus::PENDING->value, 400, '提现申请状态异常');
- // 获取用户钱包
- $wallet = $record->wallet;
- abort_if(!$wallet, 404, '钱包信息不存在');
- // 验证冻结金额
- abort_if($wallet->frozen_amount < $record->amount, 400, '钱包冻结金额异常');
- if ($status === WithdrawAuditStatus::APPROVED->value) {
- // 审核通过,发起自动打款
- $this->processApproved($record, $remark);
- } else {
- // 审核拒绝,解冻余额
- $this->processRejected($record, $remark);
- }
- // 记录审核信息
- $record->auditor = auth()->user()->name;
- $record->audit_time = now();
- $record->audit_remark = $remark;
- $record->audit_state = $status;
- $record->save();
- // 记录日志
- Log::info('提现申请审核完成', [
- 'withdraw_id' => $record->id,
- 'external_no' => $record->external_no,
- 'status' => $status,
- 'remark' => $remark,
- 'auditor' => $record->auditor
- ]);
- });
- }
- /**
- * 处理审核通过
- */
- private function processApproved(WalletWithdrawRecord $record, ?string $remark): void
- {
- try {
- // 1. 更新提现记录状态为处理中
- $record->state = WithdrawStatus::PROCESSING->value;
- $record->save();
- // 获取钱包当前余额
- $wallet = $record->wallet;
- $currentBalance = $wallet->available_balance;
- $currentRechargeBalance = $wallet->recharge_amount;
- // 2. 创建转账记录
- $transRecord = WalletTransRecord::create([
- 'trans_no' => $this->generateTransNo(),
- 'wallet_id' => $record->wallet_id,
- 'owner_id' => $record->id,
- 'owner_type' => 'withdraw', // 业务类型:提现
- 'trans_type' => 2, // 交易类型:支出
- 'amount' => $record->amount,
- 'before_balance' => $currentBalance,
- 'after_balance' => $currentBalance - $record->amount,
- 'before_recharge_balance' => $currentRechargeBalance,
- 'after_recharge_balance' => $currentRechargeBalance,
- 'trans_time' => now(),
- 'remark' => '提现打款',
- 'state' => TransactionStatus::PROCESSING->value,
- 'province' => $record->area_code ? substr($record->area_code, 0, 2) . '0000' : null,
- 'city' => $record->area_code ? substr($record->area_code, 0, 4) . '00' : null,
- 'district' => $record->area_code
- ]);
- // 3. 调用支付服务发起转账
- // TODO: 对接实际的支付服务
- $paymentResult = $this->processPayment($record);
- if ($paymentResult['success']) {
- // 4. 打款成功
- // 4.1 更新提现记录状态
- $record->state = WithdrawStatus::SUCCESS->value;
- $record->withdraw_time = now();
- $record->trans_no = $paymentResult['trade_no'];
- $record->remark = '打款成功';
- $record->save();
- // 4.2 更新转账记录状态
- $transRecord->state = TransactionStatus::SUCCESS->value;
- $transRecord->trans_no = $paymentResult['trade_no'];
- $transRecord->save();
- // 4.3 扣除钱包冻结金额
- $record->wallet->decrement('frozen_amount', $record->amount);
- // 4.4 扣除钱包总金额
- $record->wallet->decrement('total_balance', $record->amount);
- } else {
- // 5. 打款失败
- throw new \Exception($paymentResult['message'] ?? '打款失败');
- }
- } catch (\Exception $e) {
- // 6. 处理异常
- Log::error('提现打款失败', [
- 'withdraw_id' => $record->id,
- 'error' => $e->getMessage()
- ]);
- // 6.1 更新提现记录状态
- $record->state = WithdrawStatus::FAILED->value;
- $record->remark = $e->getMessage();
- $record->save();
- // 6.2 更新转账记录状态
- if (isset($transRecord)) {
- $transRecord->state = TransactionStatus::FAILED->value;
- $transRecord->save();
- }
- // 6.3 解冻余额
- $this->unfreezeBalance($record);
- throw $e;
- }
- }
- /**
- * 处理审核拒绝
- */
- private function processRejected(WalletWithdrawRecord $record, ?string $remark): void
- {
- // 1. 更新提现记录状态
- $record->state = WithdrawStatus::FAILED->value;
- $record->save();
- // 2. 解冻余额
- $this->unfreezeBalance($record);
- // 3. 记录日志
- Log::info('提现申请已拒绝', [
- 'withdraw_id' => $record->id,
- 'external_no' => $record->external_no,
- 'amount' => $record->amount,
- 'remark' => $remark
- ]);
- }
- /**
- * 解冻余额
- */
- private function unfreezeBalance(WalletWithdrawRecord $record): void
- {
- $wallet = $record->wallet;
- // 获取当前余额
- $currentBalance = $wallet->available_balance;
- $currentRechargeBalance = $wallet->recharge_amount;
- // 从冻结金额中减少
- $wallet->decrement('frozen_amount', $record->amount);
- // 增加可用余额
- $wallet->increment('available_balance', $record->amount);
- // 记录解冻操作
- WalletTransRecord::create([
- 'trans_no' => $this->generateTransNo(),
- 'wallet_id' => $wallet->id,
- 'owner_id' => $record->id,
- 'owner_type' => 'withdraw',
- 'trans_type' => 1, // 交易类型:收入
- 'amount' => $record->amount,
- 'before_balance' => $currentBalance,
- 'after_balance' => $currentBalance + $record->amount,
- 'before_recharge_balance' => $currentRechargeBalance,
- 'after_recharge_balance' => $currentRechargeBalance,
- 'trans_time' => now(),
- 'remark' => '提现拒绝解冻',
- 'state' => TransactionStatus::SUCCESS->value
- ]);
- }
- /**
- * 生成交易流水号
- */
- private function generateTransNo(): string
- {
- return 'T' . date('YmdHis') . str_pad(random_int(1, 9999), 4, '0', STR_PAD_LEFT);
- }
- /**
- * 处理实际打款
- *
- * @param WalletWithdrawRecord $record 提现记录
- * @return array [
- * 'success' => bool 是否成功
- * 'trade_no' => string 交易流水号
- * 'message' => string 结果信息
- * ]
- * @throws \Exception
- */
- private function processPayment(WalletWithdrawRecord $record): array
- {
- try {
- Log::info('开始处理提现打款', [
- 'withdraw_id' => $record->id,
- 'withdraw_type' => $record->withdraw_type,
- 'amount' => $record->amount,
- 'account' => $record->withdraw_account
- ]);
- // 根据提现方式调用不同的转账接口
- if ($record->withdraw_type === WithdrawMethod::WECHAT->value) {
- Log::info('使用微信转账方式', [
- 'withdraw_id' => $record->id,
- 'config' => config('wechat.pay.default')
- ]);
- // 使用 EasyWeChat 支付实例
- $config = config('wechat.pay.default');
- $app = new \EasyWeChat\Pay\Application($config);
- // 生成商户订单号
- $outTradeNo = 'W' . date('YmdHis') . str_pad($record->id, 6, '0', STR_PAD_LEFT);
- $requestData = [
- 'partner_trade_no' => $outTradeNo,
- 'openid' => $record->withdraw_account,
- 'check_name' => 'NO_CHECK', // 不校验真实姓名
- 'amount' => (int)bcmul($record->amount, '100'), // 金额单位为分
- 'desc' => '提现到零钱',
- ];
- Log::info('微信转账请求参数', [
- 'withdraw_id' => $record->id,
- 'request_data' => $requestData
- ]);
- try {
- // 使用商家转账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,
- 'transfer_amount' => (int)bcmul($record->amount, '100'),
- 'transfer_remark' => '提现到零钱',
- 'transfer_scene_report_infos' => [
- [
- 'info_type' => '活动名称',
- 'info_content' => '用户提现'
- ],
- [
- 'info_type' => '奖励说明',
- 'info_content' => '余额提现'
- ]
- ]
- ]);
- $result = $response->toArray();
- Log::info('微信转账响应结果', [
- 'withdraw_id' => $record->id,
- 'result' => $result
- ]);
- // 返回需要前端处理的确认数据
- if (isset($result['transfer_id'])) {
- return [
- 'success' => true,
- 'trade_no' => $result['transfer_id'],
- 'message' => '请在微信中确认收款',
- 'need_confirm' => true,
- 'confirm_data' => [
- 'transfer_id' => $result['transfer_id'],
- 'out_bill_no' => $outTradeNo
- ]
- ];
- }
- throw new \Exception($result['err_code_des'] ?? '微信转账失败');
- } catch (\Exception $e) {
- Log::error('微信转账请求异常', [
- 'withdraw_id' => $record->id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- } elseif ($record->withdraw_type === WithdrawMethod::ALIPAY->value) {
- Log::info('使用支付宝转账方式', [
- 'withdraw_id' => $record->id,
- 'config' => [
- 'app_id' => config('alipay.app_id'),
- // 不记录敏感信息
- ]
- ]);
- // 使用支付宝 SDK 调用转账接口
- $config = [
- 'app_id' => config('alipay.app_id'),
- 'private_key' => config('alipay.private_key'),
- 'alipay_public_key' => config('alipay.alipay_public_key'),
- ];
- // 初始化支付宝 SDK
- \Alipay\EasySDK\Kernel\Factory::setOptions($config);
- $requestData = [
- 'out_biz_no' => $record->external_no,
- 'trans_amount' => $record->amount,
- 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
- 'biz_scene' => 'DIRECT_TRANSFER',
- 'payee_info' => [
- 'identity' => $record->withdraw_account,
- 'identity_type' => 'ALIPAY_LOGON_ID',
- 'name' => $record->withdraw_account_name,
- ],
- 'remark' => '用户提现',
- ];
- Log::info('支付宝转账请求参数', [
- 'withdraw_id' => $record->id,
- 'request_data' => $requestData
- ]);
- $result = \Alipay\EasySDK\Kernel\Factory::payment()->common()
- ->transfer($requestData);
- Log::info('支付宝转账响应结果', [
- 'withdraw_id' => $record->id,
- 'result' => json_encode($result)
- ]);
- if ($result->code === '10000') {
- Log::info('支付宝转账成功', [
- 'withdraw_id' => $record->id,
- 'trade_no' => $result->order_id
- ]);
- return [
- 'success' => true,
- 'trade_no' => $result->order_id,
- 'message' => '支付宝转账成功'
- ];
- }
- throw new \Exception($result->sub_msg ?? '支付宝转账失败');
- } elseif ($record->withdraw_type === WithdrawMethod::BANK->value) {
- Log::info('使用银行卡转账方式(模拟)', [
- 'withdraw_id' => $record->id
- ]);
- // 银行卡转账暂时使用模拟逻辑
- $tradeNo = 'BANK' . time() . random_int(1000, 9999);
- Log::info('模拟银行卡打款成功', [
- 'withdraw_id' => $record->id,
- 'trade_no' => $tradeNo
- ]);
- return [
- 'success' => true,
- 'trade_no' => $tradeNo,
- 'message' => '模拟银行卡打款成功'
- ];
- } else {
- Log::error('不支持的提现方式', [
- 'withdraw_id' => $record->id,
- 'withdraw_type' => $record->withdraw_type
- ]);
- throw new \Exception('不支持的提现方式');
- }
- } catch (\Exception $e) {
- Log::error('转账处理失败', [
- 'withdraw_id' => $record->id,
- 'withdraw_type' => $record->withdraw_type,
- 'error_message' => $e->getMessage(),
- 'error_trace' => $e->getTraceAsString()
- ]);
- return [
- 'success' => false,
- 'trade_no' => '',
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 审核通过提现申请
- *
- * @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小时内确认收款,逾期未确认将自动退回。'
- ]
- ]
- ]);
- }
- }
|