WalletWithdrawRecordService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace App\Services\Admin;
  3. use App\Models\WalletWithdrawRecord;
  4. use App\Models\WalletTransRecord;
  5. use App\Enums\WithdrawStatus;
  6. use App\Enums\WithdrawAuditStatus;
  7. use App\Enums\TransactionType;
  8. use App\Enums\TransactionStatus;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use App\Enums\WithdrawMethod;
  12. use EasyWeChat\Pay\Application;
  13. class WalletWithdrawRecordService
  14. {
  15. /**
  16. * 审核提现申请
  17. *
  18. * @param int $id 提现记录ID
  19. * @param int $status 审核状态(2:通过 3:拒绝)
  20. * @param string|null $remark 审核备注
  21. */
  22. public function audit(int $id, int $status, ?string $remark = null): void
  23. {
  24. DB::transaction(function () use ($id, $status, $remark) {
  25. // 获取提现记录
  26. $record = WalletWithdrawRecord::lockForUpdate()->findOrFail($id);
  27. // 验证记录状态
  28. abort_if($record->audit_state !== WithdrawAuditStatus::PENDING->value, 400, '提现申请状态异常');
  29. // 获取用户钱包
  30. $wallet = $record->wallet;
  31. abort_if(!$wallet, 404, '钱包信息不存在');
  32. // 验证冻结金额
  33. abort_if($wallet->frozen_amount < $record->amount, 400, '钱包冻结金额异常');
  34. if ($status === WithdrawAuditStatus::APPROVED->value) {
  35. // 审核通过,发起自动打款
  36. $this->processApproved($record, $remark);
  37. } else {
  38. // 审核拒绝,解冻余额
  39. $this->processRejected($record, $remark);
  40. }
  41. // 记录审核信息
  42. $record->auditor = auth()->user()->name;
  43. $record->audit_time = now();
  44. $record->audit_remark = $remark;
  45. $record->audit_state = $status;
  46. $record->save();
  47. // 记录日志
  48. Log::info('提现申请审核完成', [
  49. 'withdraw_id' => $record->id,
  50. 'external_no' => $record->external_no,
  51. 'status' => $status,
  52. 'remark' => $remark,
  53. 'auditor' => $record->auditor
  54. ]);
  55. });
  56. }
  57. /**
  58. * 处理审核通过
  59. */
  60. private function processApproved(WalletWithdrawRecord $record, ?string $remark): void
  61. {
  62. try {
  63. // 1. 更新提现记录状态为处理中
  64. $record->state = WithdrawStatus::PROCESSING->value;
  65. $record->save();
  66. // 获取钱包当前余额
  67. $wallet = $record->wallet;
  68. $currentBalance = $wallet->available_balance;
  69. $currentRechargeBalance = $wallet->recharge_amount;
  70. // 2. 创建转账记录
  71. $transRecord = WalletTransRecord::create([
  72. 'trans_no' => $this->generateTransNo(),
  73. 'wallet_id' => $record->wallet_id,
  74. 'owner_id' => $record->id,
  75. 'owner_type' => 'withdraw', // 业务类型:提现
  76. 'trans_type' => 2, // 交易类型:支出
  77. 'amount' => $record->amount,
  78. 'before_balance' => $currentBalance,
  79. 'after_balance' => $currentBalance - $record->amount,
  80. 'before_recharge_balance' => $currentRechargeBalance,
  81. 'after_recharge_balance' => $currentRechargeBalance,
  82. 'trans_time' => now(),
  83. 'remark' => '提现打款',
  84. 'state' => TransactionStatus::PROCESSING->value,
  85. 'province' => $record->area_code ? substr($record->area_code, 0, 2) . '0000' : null,
  86. 'city' => $record->area_code ? substr($record->area_code, 0, 4) . '00' : null,
  87. 'district' => $record->area_code
  88. ]);
  89. // 3. 调用支付服务发起转账
  90. // TODO: 对接实际的支付服务
  91. $paymentResult = $this->processPayment($record);
  92. if ($paymentResult['success']) {
  93. // 4. 打款成功
  94. // 4.1 更新提现记录状态
  95. $record->state = WithdrawStatus::SUCCESS->value;
  96. $record->withdraw_time = now();
  97. $record->trans_no = $paymentResult['trade_no'];
  98. $record->remark = '打款成功';
  99. $record->save();
  100. // 4.2 更新转账记录状态
  101. $transRecord->state = TransactionStatus::SUCCESS->value;
  102. $transRecord->trans_no = $paymentResult['trade_no'];
  103. $transRecord->save();
  104. // 4.3 扣除钱包冻结金额
  105. $record->wallet->decrement('frozen_amount', $record->amount);
  106. // 4.4 扣除钱包总金额
  107. $record->wallet->decrement('total_balance', $record->amount);
  108. } else {
  109. // 5. 打款失败
  110. throw new \Exception($paymentResult['message'] ?? '打款失败');
  111. }
  112. } catch (\Exception $e) {
  113. // 6. 处理异常
  114. Log::error('提现打款失败', [
  115. 'withdraw_id' => $record->id,
  116. 'error' => $e->getMessage()
  117. ]);
  118. // 6.1 更新提现记录状态
  119. $record->state = WithdrawStatus::FAILED->value;
  120. $record->remark = $e->getMessage();
  121. $record->save();
  122. // 6.2 更新转账记录状态
  123. if (isset($transRecord)) {
  124. $transRecord->state = TransactionStatus::FAILED->value;
  125. $transRecord->save();
  126. }
  127. // 6.3 解冻余额
  128. $this->unfreezeBalance($record);
  129. throw $e;
  130. }
  131. }
  132. /**
  133. * 处理审核拒绝
  134. */
  135. private function processRejected(WalletWithdrawRecord $record, ?string $remark): void
  136. {
  137. // 1. 更新提现记录状态
  138. $record->state = WithdrawStatus::FAILED->value;
  139. $record->save();
  140. // 2. 解冻余额
  141. $this->unfreezeBalance($record);
  142. // 3. 记录日志
  143. Log::info('提现申请已拒绝', [
  144. 'withdraw_id' => $record->id,
  145. 'external_no' => $record->external_no,
  146. 'amount' => $record->amount,
  147. 'remark' => $remark
  148. ]);
  149. }
  150. /**
  151. * 解冻余额
  152. */
  153. private function unfreezeBalance(WalletWithdrawRecord $record): void
  154. {
  155. $wallet = $record->wallet;
  156. // 获取当前余额
  157. $currentBalance = $wallet->available_balance;
  158. $currentRechargeBalance = $wallet->recharge_amount;
  159. // 从冻结金额中减少
  160. $wallet->decrement('frozen_amount', $record->amount);
  161. // 增加可用余额
  162. $wallet->increment('available_balance', $record->amount);
  163. // 记录解冻操作
  164. WalletTransRecord::create([
  165. 'trans_no' => $this->generateTransNo(),
  166. 'wallet_id' => $wallet->id,
  167. 'owner_id' => $record->id,
  168. 'owner_type' => 'withdraw',
  169. 'trans_type' => 1, // 交易类型:收入
  170. 'amount' => $record->amount,
  171. 'before_balance' => $currentBalance,
  172. 'after_balance' => $currentBalance + $record->amount,
  173. 'before_recharge_balance' => $currentRechargeBalance,
  174. 'after_recharge_balance' => $currentRechargeBalance,
  175. 'trans_time' => now(),
  176. 'remark' => '提现拒绝解冻',
  177. 'state' => TransactionStatus::SUCCESS->value
  178. ]);
  179. }
  180. /**
  181. * 生成交易流水号
  182. */
  183. private function generateTransNo(): string
  184. {
  185. return 'T' . date('YmdHis') . str_pad(random_int(1, 9999), 4, '0', STR_PAD_LEFT);
  186. }
  187. /**
  188. * 处理实际打款
  189. *
  190. * @param WalletWithdrawRecord $record 提现记录
  191. * @return array [
  192. * 'success' => bool 是否成功
  193. * 'trade_no' => string 交易流水号
  194. * 'message' => string 结果信息
  195. * ]
  196. * @throws \Exception
  197. */
  198. private function processPayment(WalletWithdrawRecord $record): array
  199. {
  200. try {
  201. Log::info('开始处理提现打款', [
  202. 'withdraw_id' => $record->id,
  203. 'withdraw_type' => $record->withdraw_type,
  204. 'amount' => $record->amount,
  205. 'account' => $record->withdraw_account
  206. ]);
  207. // 根据提现方式调用不同的转账接口
  208. if ($record->withdraw_type === WithdrawMethod::WECHAT->value) {
  209. Log::info('使用微信转账方式', [
  210. 'withdraw_id' => $record->id,
  211. 'config' => config('wechat.pay.default')
  212. ]);
  213. // 使用 EasyWeChat 支付实例
  214. $config = config('wechat.pay.default');
  215. $app = new \EasyWeChat\Pay\Application($config);
  216. // 生成商户订单号
  217. $outTradeNo = 'W' . date('YmdHis') . str_pad($record->id, 6, '0', STR_PAD_LEFT);
  218. $requestData = [
  219. 'partner_trade_no' => $outTradeNo,
  220. 'openid' => $record->withdraw_account,
  221. 'check_name' => 'NO_CHECK', // 不校验真实姓名
  222. 'amount' => (int)bcmul($record->amount, '100'), // 金额单位为分
  223. 'desc' => '提现到零钱',
  224. ];
  225. Log::info('微信转账请求参数', [
  226. 'withdraw_id' => $record->id,
  227. 'request_data' => $requestData
  228. ]);
  229. try {
  230. // 使用商家转账接口
  231. $response = $app->getClient()->postJson('v3/fund-app/mch-transfer/transfer-bills', [
  232. 'appid' => $config['app_id'],
  233. 'out_bill_no' => $outTradeNo,
  234. 'transfer_scene_id' => '1001', // 现金营销场景
  235. 'openid' => $record->withdraw_account,
  236. // 'user_name' => $record->withdraw_account_name,
  237. 'transfer_amount' => (int)bcmul($record->amount, '100'),
  238. 'transfer_remark' => '提现到零钱',
  239. 'transfer_scene_report_infos' => [
  240. [
  241. 'info_type' => '活动名称',
  242. 'info_content' => '用户提现'
  243. ],
  244. [
  245. 'info_type' => '奖励说明',
  246. 'info_content' => '余额提现'
  247. ]
  248. ]
  249. ]);
  250. $result = $response->toArray();
  251. Log::info('微信转账响应结果', [
  252. 'withdraw_id' => $record->id,
  253. 'result' => $result
  254. ]);
  255. // 检查转账状态
  256. if (isset($result['bill_id'])) {
  257. Log::info('微信转账成功', [
  258. 'withdraw_id' => $record->id,
  259. 'bill_id' => $result['bill_id'],
  260. 'out_bill_no' => $outTradeNo
  261. ]);
  262. return [
  263. 'success' => true,
  264. 'trade_no' => $result['bill_id'],
  265. 'message' => '微信转账成功'
  266. ];
  267. }
  268. throw new \Exception($result['err_code_des'] ?? '微信转账失败');
  269. } catch (\Exception $e) {
  270. Log::error('微信转账请求异常', [
  271. 'withdraw_id' => $record->id,
  272. 'error' => $e->getMessage(),
  273. 'trace' => $e->getTraceAsString()
  274. ]);
  275. throw $e;
  276. }
  277. } elseif ($record->withdraw_type === WithdrawMethod::ALIPAY->value) {
  278. Log::info('使用支付宝转账方式', [
  279. 'withdraw_id' => $record->id,
  280. 'config' => [
  281. 'app_id' => config('alipay.app_id'),
  282. // 不记录敏感信息
  283. ]
  284. ]);
  285. // 使用支付宝 SDK 调用转账接口
  286. $config = [
  287. 'app_id' => config('alipay.app_id'),
  288. 'private_key' => config('alipay.private_key'),
  289. 'alipay_public_key' => config('alipay.alipay_public_key'),
  290. ];
  291. // 初始化支付宝 SDK
  292. \Alipay\EasySDK\Kernel\Factory::setOptions($config);
  293. $requestData = [
  294. 'out_biz_no' => $record->external_no,
  295. 'trans_amount' => $record->amount,
  296. 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
  297. 'biz_scene' => 'DIRECT_TRANSFER',
  298. 'payee_info' => [
  299. 'identity' => $record->withdraw_account,
  300. 'identity_type' => 'ALIPAY_LOGON_ID',
  301. 'name' => $record->withdraw_account_name,
  302. ],
  303. 'remark' => '用户提现',
  304. ];
  305. Log::info('支付宝转账请求参数', [
  306. 'withdraw_id' => $record->id,
  307. 'request_data' => $requestData
  308. ]);
  309. $result = \Alipay\EasySDK\Kernel\Factory::payment()->common()
  310. ->transfer($requestData);
  311. Log::info('支付宝转账响应结果', [
  312. 'withdraw_id' => $record->id,
  313. 'result' => json_encode($result)
  314. ]);
  315. if ($result->code === '10000') {
  316. Log::info('支付宝转账成功', [
  317. 'withdraw_id' => $record->id,
  318. 'trade_no' => $result->order_id
  319. ]);
  320. return [
  321. 'success' => true,
  322. 'trade_no' => $result->order_id,
  323. 'message' => '支付宝转账成功'
  324. ];
  325. }
  326. throw new \Exception($result->sub_msg ?? '支付宝转账失败');
  327. } elseif ($record->withdraw_type === WithdrawMethod::BANK->value) {
  328. Log::info('使用银行卡转账方式(模拟)', [
  329. 'withdraw_id' => $record->id
  330. ]);
  331. // 银行卡转账暂时使用模拟逻辑
  332. $tradeNo = 'BANK' . time() . random_int(1000, 9999);
  333. Log::info('模拟银行卡打款成功', [
  334. 'withdraw_id' => $record->id,
  335. 'trade_no' => $tradeNo
  336. ]);
  337. return [
  338. 'success' => true,
  339. 'trade_no' => $tradeNo,
  340. 'message' => '模拟银行卡打款成功'
  341. ];
  342. } else {
  343. Log::error('不支持的提现方式', [
  344. 'withdraw_id' => $record->id,
  345. 'withdraw_type' => $record->withdraw_type
  346. ]);
  347. throw new \Exception('不支持的提现方式');
  348. }
  349. } catch (\Exception $e) {
  350. Log::error('转账处理失败', [
  351. 'withdraw_id' => $record->id,
  352. 'withdraw_type' => $record->withdraw_type,
  353. 'error_message' => $e->getMessage(),
  354. 'error_trace' => $e->getTraceAsString()
  355. ]);
  356. return [
  357. 'success' => false,
  358. 'trade_no' => '',
  359. 'message' => $e->getMessage()
  360. ];
  361. }
  362. }
  363. }