TransferLogic.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. namespace App\Module\Transfer\Logics;
  3. use App\Module\Transfer\Enums\TransferStatus;
  4. use App\Module\Transfer\Enums\TransferType;
  5. use App\Module\Transfer\Events\FeeCalculatedEvent;
  6. use App\Module\Transfer\Models\TransferApp;
  7. use App\Module\Transfer\Models\TransferOrder;
  8. use App\Module\Transfer\Services\FeeService;
  9. use App\Module\Transfer\Validations\TransferInValidation;
  10. use App\Module\Transfer\Validations\TransferOutValidation;
  11. use App\Module\Fund\Services\FundService;
  12. use App\Module\Transfer\Logics\OrderLogic;
  13. use App\Module\Transfer\Logics\CallbackLogic;
  14. use Illuminate\Support\Str;
  15. use Illuminate\Support\Facades\Log;
  16. /**
  17. * Transfer模块核心业务逻辑
  18. */
  19. class TransferLogic
  20. {
  21. /**
  22. * 创建转出订单(对外接口)
  23. *
  24. * @param int $transferAppId 划转应用ID
  25. * @param int $userId 用户ID
  26. * @param string $amount 转出金额
  27. * @param string $password 安全密码
  28. * @param string|null $googleCode 谷歌验证码
  29. * @param string|null $outUserId 外部用户ID
  30. * @param string|null $remark 备注
  31. * @param array $callbackData 回调数据
  32. * @return TransferOrder
  33. * @throws \Exception
  34. */
  35. public static function createTransferOut(
  36. int $transferAppId,
  37. int $userId,
  38. string $amount,
  39. string $password,
  40. ?string $googleCode = null,
  41. ?string $outUserId = null,
  42. ?string $remark = null,
  43. array $callbackData = []
  44. ): TransferOrder
  45. {
  46. $data = [
  47. 'transfer_app_id' => $transferAppId,
  48. 'user_id' => $userId,
  49. 'amount' => $amount,
  50. 'password' => $password,
  51. 'google_code' => $googleCode,
  52. 'out_user_id' => $outUserId,
  53. 'remark' => $remark,
  54. 'callback_data' => $callbackData,
  55. ];
  56. return self::createTransferOutFromArray($data);
  57. }
  58. /**
  59. * 创建第三方应用转出订单(跳过密码验证)
  60. * 我 =》 三方
  61. *
  62. * @param int $transferAppId 划转应用ID
  63. * @param int $userId 用户ID
  64. * @param string $amount 转出金额(外部金额,第三方系统的金额)
  65. * @param string|null $outUserId 外部用户ID
  66. * @param string|null $remark 备注
  67. * @param array $callbackData 回调数据
  68. * @return TransferOrder
  69. * @throws \Exception
  70. */
  71. public static function createTransferOutForThirdParty(
  72. int $transferAppId,
  73. int $userId,
  74. string $amount,
  75. string $out_order_id,
  76. ?string $outUserId = null,
  77. ?string $remark = null,
  78. array $callbackData = []
  79. ): TransferOrder
  80. {
  81. $data = [
  82. 'transfer_app_id' => $transferAppId,
  83. 'user_id' => $userId,
  84. 'out_order_id' => $out_order_id,
  85. 'amount' => $amount,
  86. 'out_user_id' => $outUserId,
  87. 'remark' => $remark,
  88. 'callback_data' => $callbackData,
  89. ];
  90. return self::createTransferOutFromArrayForThirdParty($data);
  91. }
  92. /**
  93. * 创建第三方应用转出订单(内部实现,跳过密码验证)
  94. * 我 =》 三方
  95. * 注意:第三方应用传入的金额是外部金额,需要转换为内部金额进行处理
  96. *
  97. * @param array $data 订单数据
  98. * @return TransferOrder
  99. * @throws \Exception
  100. */
  101. public static function createTransferOutFromArrayForThirdParty(array $data): TransferOrder
  102. {
  103. // 获取应用配置(需要先获取以便进行金额转换)
  104. /**
  105. * @var TransferApp $app
  106. */
  107. $app = TransferApp::findOrFail($data['transfer_app_id']);
  108. if (!$app->is_enabled) {
  109. throw new \Exception('应用已禁用');
  110. }
  111. // 验证汇率是否合理
  112. if (bccomp((string)$app->exchange_rate, '0', 10) <= 0) {
  113. throw new \Exception('汇率配置错误:汇率必须大于0');
  114. }
  115. // 第三方应用金额处理:传入的是外部金额,需要转换为内部金额
  116. $outAmount = (string)$data['amount']; // 外部金额(第三方系统金额)
  117. $amount = bcmul($outAmount, (string)$app->exchange_rate, 10); // 内部金额 = 外部金额 × 汇率
  118. // 验证外部金额是否合理
  119. if (bccomp($outAmount, '0', 10) <= 0) {
  120. throw new \Exception('转出金额必须大于0');
  121. }
  122. // 验证内部金额计算结果是否合理
  123. if (bccomp($amount, '0', 10) <= 0) {
  124. throw new \Exception('汇率计算结果异常:内部金额必须大于0');
  125. }
  126. // 修改验证数据:将外部金额替换为内部金额,这样验证器就能正确验证余额
  127. $validationData = $data;
  128. $validationData['amount'] = $amount; // 传递内部金额给验证器
  129. // 使用第三方应用专用验证类(跳过密码验证)
  130. $validation = new \App\Module\Transfer\Validations\TransferOutThirdPartyValidation($validationData);
  131. $validation->validated();
  132. // 计算手续费(基于内部金额)
  133. $FeeCalculatedEvent = \App\Module\Transfer\Services\FeeService::calculateOutFee($app, $amount, [ 'user_id' => $data['user_id'] ]);
  134. // 创建订单
  135. /**
  136. * @var TransferOrder $order
  137. */
  138. $order = TransferOrder::create([
  139. 'transfer_app_id' => $app->id,
  140. 'out_id' => $app->out_id2 ?? 0,
  141. 'out_order_id' => $data['out_order_id'] ?? null,
  142. 'out_user_id' => $data['out_user_id'] ?? null,
  143. 'user_id' => $data['user_id'],
  144. 'currency_id' => $app->currency_id,
  145. 'fund_id' => $app->fund_id,
  146. 'type' => TransferType::OUT,
  147. 'status' => TransferStatus::CREATED,
  148. 'out_amount' => $outAmount,
  149. 'amount' => $FeeCalculatedEvent->amount,
  150. 'exchange_rate' => $app->exchange_rate,
  151. 'fee_rate' => $FeeCalculatedEvent->feeRate,
  152. 'fee_amount' => $FeeCalculatedEvent->feeAmount,
  153. 'actual_amount' => $FeeCalculatedEvent->actualAmount,
  154. 'totle_amount' => $FeeCalculatedEvent->totleAmount,
  155. 'callback_data' => $data['callback_data'] ?? [],
  156. 'remark' => $data['remark'] ?? null,
  157. ]);
  158. // 验证fund_to_uid必须存在
  159. if (empty($app->fund_to_uid)) {
  160. $order->updateStatus(TransferStatus::FAILED, '应用配置错误:未配置转入目标账户');
  161. throw new \Exception('应用配置错误:未配置转入目标账户(fund_to_uid)');
  162. }
  163. // 验证目标账户是否存在
  164. $targetFundService = new FundService($app->fund_to_uid, $app->fund_id);
  165. if (!$targetFundService->getAccount()) {
  166. $order->updateStatus(TransferStatus::FAILED, '目标账户不存在');
  167. throw new \Exception('目标账户不存在');
  168. }
  169. // 预先验证用户余额是否充足
  170. $userFundService = new FundService($data['user_id'], $app->fund_id);
  171. if ($userFundService->balance() < $order->actual_amount) {
  172. $order->updateStatus(TransferStatus::FAILED, '用户余额不足');
  173. throw new \Exception('用户余额不足');
  174. }
  175. // 使用trade方法从用户转账到目标账户(完整转出金额,不扣除手续费)
  176. $tradeResult = $userFundService->trade(
  177. $app->fund_to_uid,
  178. $order->amount, // 转出完整金额(不扣除手续费)
  179. 'transfer_out',
  180. $order->id,
  181. "转出到{$app->title}"
  182. );
  183. // 如果有手续费,处理手续费收取
  184. if ($order->hasFee()) {
  185. // 获取手续费收取账户UID(默认为1)
  186. $feeAccountUid = $app->getFeeAccountUid();
  187. $feeTradeResult = $userFundService->trade(
  188. $feeAccountUid,
  189. $order->fee_amount,
  190. 'transfer_out_fee',
  191. $order->id,
  192. "转出手续费-{$app->title}"
  193. );
  194. if (is_string($feeTradeResult)) {
  195. Log::warning('Transfer out fee collection failed', [
  196. 'order_id' => $order->id,
  197. 'fee_amount' => $order->fee_amount,
  198. 'fee_account_uid' => $feeAccountUid,
  199. 'error' => $feeTradeResult
  200. ]);
  201. } else {
  202. Log::info('Transfer out fee collected successfully', [
  203. 'order_id' => $order->id,
  204. 'fee_amount' => $order->fee_amount,
  205. 'fee_account_uid' => $feeAccountUid,
  206. 'configured_account' => $app->fee_account_uid
  207. ]);
  208. }
  209. }
  210. if (is_string($tradeResult)) {
  211. $order->updateStatus(TransferStatus::FAILED, '资金转移失败: ' . $tradeResult);
  212. throw new \Exception('余额不足或资金操作失败: ' . $tradeResult);
  213. }
  214. // 检查是否需要调用外部API
  215. if (!empty($app->order_out_create_url)) {
  216. // 配置了外部转出API,调用外部API处理
  217. OrderLogic::processTransferOut($order);
  218. } else {
  219. // 没有配置外部API,直接完成订单
  220. $order->updateStatus(TransferStatus::COMPLETED);
  221. }
  222. return $order;
  223. }
  224. /**
  225. * 创建转出订单(内部实现)
  226. * 我 =》 三方
  227. * 处理流程:
  228. * 1. 验证请求数据
  229. * 2. 获取应用配置
  230. * 3. 创建转出订单
  231. * 4. 验证fund_to_uid必须存在,进行用户间资金转移
  232. * 5. 根据是否配置外部API决定后续处理(调用外部API或直接完成)
  233. *
  234. * @param array $data 订单数据
  235. * @return TransferOrder
  236. * @throws \Exception
  237. */
  238. public static function createTransferOutFromArray(array $data): TransferOrder
  239. {
  240. // 验证请求数据
  241. $validation = new TransferOutValidation($data);
  242. $validation->validated();
  243. // 获取应用配置
  244. $app = TransferApp::findOrFail($data['transfer_app_id']);
  245. if (!$app->is_enabled) {
  246. throw new \Exception('应用已禁用');
  247. }
  248. // 计算金额(转出:内部金额转换为外部金额)
  249. $amount = (string)$data['amount']; // 内部金额
  250. // 验证汇率是否合理
  251. if (bccomp((string)$app->exchange_rate, '0', 10) <= 0) {
  252. throw new \Exception('汇率配置错误:汇率必须大于0');
  253. }
  254. // 验证内部金额是否合理
  255. if (bccomp($amount, '0', 10) <= 0) {
  256. throw new \Exception('转出金额必须大于0');
  257. }
  258. $outAmount = bcdiv($amount, (string)$app->exchange_rate, 10); // 外部金额 = 内部金额 ÷ 汇率
  259. // 验证计算结果是否合理
  260. if (bccomp($outAmount, '0', 10) <= 0) {
  261. throw new \Exception('汇率计算结果异常:外部金额必须大于0');
  262. }
  263. // 计算手续费
  264. $feeInfo = FeeService::calculateOutFee($app, $amount, [ 'user_id' => $data['user_id'] ]);
  265. // 生成外部订单ID
  266. $outOrderId = self::generateOutOrderId('OUT');
  267. // 创建订单
  268. $order = TransferOrder::create([
  269. 'transfer_app_id' => $app->id,
  270. 'out_id' => $app->out_id2 ?? 0,
  271. 'out_order_id' => $outOrderId,
  272. 'out_user_id' => $data['out_user_id'] ?? null,
  273. 'user_id' => $data['user_id'],
  274. 'currency_id' => $app->currency_id,
  275. 'fund_id' => $app->fund_id,
  276. 'type' => TransferType::OUT,
  277. 'status' => TransferStatus::CREATED,
  278. 'out_amount' => $outAmount,
  279. 'amount' => $amount,
  280. 'exchange_rate' => $app->exchange_rate,
  281. 'fee_rate' => $feeInfo->feeRate,
  282. 'fee_amount' => $feeInfo->feeAmount,
  283. 'actual_amount' => $feeInfo->actualAmount,
  284. 'totle_amount' => $feeInfo->totleAmount,
  285. 'callback_data' => $data['callback_data'] ?? [],
  286. 'remark' => $data['remark'] ?? null,
  287. ]);
  288. // 验证fund_to_uid必须存在
  289. if (empty($app->fund_to_uid)) {
  290. $order->updateStatus(TransferStatus::FAILED, '应用配置错误:未配置转入目标账户');
  291. throw new \Exception('应用配置错误:未配置转入目标账户(fund_to_uid)');
  292. }
  293. // 验证目标账户是否存在
  294. $targetFundService = new FundService($app->fund_to_uid, $app->fund_id);
  295. if (!$targetFundService->getAccount()) {
  296. $order->updateStatus(TransferStatus::FAILED, '目标账户不存在');
  297. throw new \Exception('目标账户不存在');
  298. }
  299. // 预先验证用户余额是否充足(转出金额 + 手续费)
  300. $userFundService = new FundService($data['user_id'], $app->fund_id);
  301. $totalRequired = bcadd($order->amount, $order->fee_amount, 10);
  302. if (bccomp((string)$userFundService->balance(), $totalRequired, 10) < 0) {
  303. $order->updateStatus(TransferStatus::FAILED, '用户余额不足');
  304. throw new \Exception('用户余额不足');
  305. }
  306. // 使用trade方法从用户转账到目标账户(完整转出金额,不扣除手续费)
  307. $tradeResult = $userFundService->trade(
  308. $app->fund_to_uid,
  309. $order->amount, // 转出完整金额(不扣除手续费)
  310. 'transfer_out',
  311. $order->id,
  312. "转出到{$app->title}"
  313. );
  314. // 如果有手续费,处理手续费收取
  315. if ($order->hasFee()) {
  316. // 获取手续费收取账户UID(默认为1)
  317. $feeAccountUid = $app->getFeeAccountUid();
  318. $feeTradeResult = $userFundService->trade(
  319. $feeAccountUid,
  320. $order->fee_amount,
  321. 'transfer_out_fee',
  322. $order->id,
  323. "转出手续费-{$app->title}"
  324. );
  325. if (is_string($feeTradeResult)) {
  326. Log::warning('Transfer out fee collection failed', [
  327. 'order_id' => $order->id,
  328. 'fee_amount' => $order->fee_amount,
  329. 'fee_account_uid' => $feeAccountUid,
  330. 'error' => $feeTradeResult
  331. ]);
  332. } else {
  333. Log::info('Transfer out fee collected successfully', [
  334. 'order_id' => $order->id,
  335. 'fee_amount' => $order->fee_amount,
  336. 'fee_account_uid' => $feeAccountUid,
  337. 'configured_account' => $app->fee_account_uid
  338. ]);
  339. }
  340. }
  341. if (is_string($tradeResult)) {
  342. $order->updateStatus(TransferStatus::FAILED, '资金转移失败: ' . $tradeResult);
  343. throw new \Exception('余额不足或资金操作失败: ' . $tradeResult);
  344. }
  345. // 检查是否需要调用外部API
  346. if (!empty($app->order_out_create_url)) {
  347. // 配置了外部转出API,调用外部API处理
  348. OrderLogic::processTransferOut($order);
  349. } else {
  350. // 没有配置外部API,直接完成订单
  351. $order->updateStatus(TransferStatus::COMPLETED);
  352. }
  353. return $order;
  354. }
  355. /**
  356. * 创建转入订单(对外接口)
  357. * 三方 =》 我
  358. *
  359. * @param int $transferAppId 划转应用ID
  360. * @param int $userId 用户ID
  361. * @param string $businessId 业务订单ID
  362. * @param string $amount 转入金额
  363. * @param string|null $outUserId 外部用户ID
  364. * @param string|null $remark 备注
  365. * @param array $callbackData 回调数据
  366. * @return TransferOrder
  367. * @throws \Exception
  368. */
  369. public static function createTransferIn(
  370. int $transferAppId,
  371. int $userId,
  372. string $out_order_id,
  373. string $amount,
  374. ?string $outUserId = null,
  375. ?string $remark = null,
  376. array $callbackData = []
  377. ): TransferOrder
  378. {
  379. $data = [
  380. 'transfer_app_id' => $transferAppId,
  381. 'user_id' => $userId,
  382. 'out_order_id' => $out_order_id,
  383. 'amount' => $amount,
  384. 'out_user_id' => $outUserId,
  385. 'remark' => $remark,
  386. 'callback_data' => $callbackData,
  387. ];
  388. return self::createTransferInFromArray($data, $userId);
  389. }
  390. /**
  391. * 创建转入订单(内部实现)
  392. * 三方 =》 我
  393. * 处理流程:
  394. * 1. 验证请求数据
  395. * 2. 获取应用配置
  396. * 3. 创建转入订单
  397. * 4. 验证fund_in_uid必须存在,从指定账户转账到用户
  398. * 5. 根据是否配置回调URL决定后续处理(发送回调或直接完成)
  399. *
  400. * @param array $data 订单数据
  401. * @return TransferOrder
  402. * @throws \Exception
  403. */
  404. public static function createTransferInFromArray(array $data, $user_id): TransferOrder
  405. {
  406. // 验证请求数据
  407. $validation = new TransferInValidation($data);
  408. $validation->validated();
  409. // 获取应用配置
  410. /**
  411. * @var TransferApp $app
  412. */
  413. $app = TransferApp::findOrFail($data['transfer_app_id']);
  414. if (!$app->is_enabled) {
  415. throw new \Exception('应用已禁用');
  416. }
  417. // 检查外部订单ID是否已存在
  418. $existingOrder = TransferOrder::where('out_order_id', $data['out_order_id'])
  419. ->where('out_id', $app->out_id2 ?? 0)
  420. ->first();
  421. if ($existingOrder) {
  422. throw new \Exception('订单已存在');
  423. }
  424. // 计算金额(转入:外部金额转换为内部金额)
  425. $outAmount = (string)$data['amount']; // 外部金额
  426. $amount = bcmul($outAmount, (string)$app->exchange_rate, 10); // 内部金额 = 外部金额 × 汇率
  427. // 计算手续费
  428. $feeInfo = FeeService::calculateInFee($app, $amount, [
  429. 'user_id' => $user_id
  430. ]);
  431. // 创建订单
  432. /**
  433. * @var TransferOrder $order
  434. */
  435. $order = TransferOrder::create([
  436. 'transfer_app_id' => $app->id,
  437. 'out_id' => $app->out_id2 ?? 0,
  438. 'out_order_id' => $data['out_order_id'] ?? null,
  439. 'out_user_id' => $data['out_user_id'] ?? null,
  440. 'user_id' => $data['user_id'],
  441. 'currency_id' => $app->currency_id,
  442. 'fund_id' => $app->fund_id,
  443. 'type' => TransferType::IN,
  444. 'status' => TransferStatus::CREATED,
  445. 'out_amount' => $outAmount,
  446. 'amount' => $feeInfo->amount,
  447. 'exchange_rate' => $app->exchange_rate,
  448. 'fee_rate' => $feeInfo->feeRate,
  449. 'fee_amount' => $feeInfo->feeAmount,
  450. 'actual_amount' => $feeInfo->actualAmount,
  451. 'totle_amount' => $feeInfo->totleAmount,
  452. 'callback_data' => $data['callback_data'] ?? [],
  453. 'remark' => $data['remark'] ?? null,
  454. ]);
  455. // 验证fund_in_uid必须存在
  456. if (empty($app->fund_in_uid)) {
  457. $order->updateStatus(TransferStatus::FAILED, '应用配置错误:未配置转入来源账户');
  458. throw new \Exception('应用配置错误:未配置转入来源账户(fund_in_uid)');
  459. }
  460. // 验证来源账户是否存在
  461. $sourceFundService = new FundService($app->fund_in_uid, $app->fund_id);
  462. if (!$sourceFundService->getAccount()) {
  463. $order->updateStatus(TransferStatus::FAILED, '来源账户不存在');
  464. throw new \Exception('来源账户不存在');
  465. }
  466. // 验证来源账户余额是否充足
  467. if ($sourceFundService->balance() < $order->totle_amount) {
  468. $order->updateStatus(TransferStatus::FAILED, '来源账户余额不足');
  469. throw new \Exception('来源账户余额不足');
  470. }
  471. // 使用trade方法从来源账户转账到用户账户(扣除手续费后的实际金额)
  472. $tradeResult = $sourceFundService->trade(
  473. $data['user_id'],
  474. $order->actual_amount, // 转入实际到账金额(扣除手续费后)
  475. 'transfer_in',
  476. $order->id,
  477. "从{$app->title}转入"
  478. );
  479. // 如果有手续费,处理手续费收取
  480. if ($order->hasFee()) {
  481. // 获取手续费收取账户UID(默认为1)
  482. $feeAccountUid = $app->getFeeAccountUid();
  483. $feeTradeResult = $sourceFundService->trade(
  484. $feeAccountUid,
  485. $order->fee_amount,
  486. 'transfer_in_fee',
  487. $order->id,
  488. "转入手续费-{$app->title}"
  489. );
  490. if (is_string($feeTradeResult)) {
  491. Log::warning('Transfer in fee collection failed', [
  492. 'order_id' => $order->id,
  493. 'fee_amount' => $order->fee_amount,
  494. 'fee_account_uid' => $feeAccountUid,
  495. 'error' => $feeTradeResult
  496. ]);
  497. } else {
  498. Log::info('Transfer in fee collected successfully', [
  499. 'order_id' => $order->id,
  500. 'fee_amount' => $order->fee_amount,
  501. 'fee_account_uid' => $feeAccountUid,
  502. 'configured_account' => $app->fee_account_uid
  503. ]);
  504. }
  505. }
  506. if (is_string($tradeResult)) {
  507. $order->updateStatus(TransferStatus::FAILED, '资金转移失败: ' . $tradeResult);
  508. throw new \Exception('来源账户资金不足或操作失败: ' . $tradeResult);
  509. }
  510. // 检查是否需要发送回调通知
  511. if ($app->supportsCallback()) {
  512. // 配置了回调URL,更新状态为处理中并发送回调
  513. $order->updateStatus(TransferStatus::PROCESSING);
  514. CallbackLogic::sendCallback($order);
  515. } else {
  516. // 没有配置回调URL,直接完成订单
  517. $order->updateStatus(TransferStatus::COMPLETED);
  518. }
  519. return $order;
  520. }
  521. /**
  522. * 处理回调结果
  523. *
  524. * @param array $callbackData 回调数据
  525. * @return bool
  526. */
  527. public static function processCallback(array $callbackData): bool
  528. {
  529. // 查找订单
  530. $order = TransferOrder::where('out_order_id', $callbackData['out_order_id'])
  531. ->where('out_id', $callbackData['out_id'])
  532. ->first();
  533. if (!$order) {
  534. Log::warning('Transfer callback order not found', $callbackData);
  535. return false;
  536. }
  537. // 更新订单状态
  538. $status = $callbackData['success'] ? TransferStatus::COMPLETED : TransferStatus::FAILED;
  539. $message = $callbackData['message'] ?? null;
  540. return $order->updateStatus($status, $message);
  541. }
  542. /**
  543. * 生成外部订单ID
  544. *
  545. * @param string $prefix 前缀
  546. * @return string
  547. */
  548. private static function generateOutOrderId(string $prefix = 'TR'): string
  549. {
  550. return $prefix . date('YmdHis') . Str::random(6);
  551. }
  552. /**
  553. * 重试订单处理
  554. *
  555. * @param int $orderId 订单ID
  556. * @return bool
  557. */
  558. public static function retryOrder(int $orderId): bool
  559. {
  560. $order = TransferOrder::findOrFail($orderId);
  561. if (!$order->canRetry()) {
  562. throw new \Exception('订单状态不允许重试');
  563. }
  564. // 重置状态为已创建
  565. $order->updateStatus(TransferStatus::CREATED);
  566. // 根据订单类型重新处理
  567. if ($order->isTransferOut()) {
  568. OrderLogic::processTransferOut($order);
  569. } else {
  570. // 转入订单重试主要是重新发送回调
  571. if ($order->transferApp->supportsCallback()) {
  572. CallbackLogic::sendCallback($order);
  573. }
  574. }
  575. return true;
  576. }
  577. /**
  578. * 手动完成订单
  579. *
  580. * @param int $orderId 订单ID
  581. * @param string $remark 备注
  582. * @return bool
  583. */
  584. public static function manualComplete(int $orderId, string $remark = ''): bool
  585. {
  586. $order = TransferOrder::findOrFail($orderId);
  587. if ($order->isFinalStatus()) {
  588. throw new \Exception('订单已处于最终状态');
  589. }
  590. // 更新状态为已完成
  591. $order->updateStatus(TransferStatus::COMPLETED, $remark);
  592. return true;
  593. }
  594. }