TransferLogic.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 $outUserId = null,
  76. ?string $remark = null,
  77. array $callbackData = []
  78. ): TransferOrder
  79. {
  80. $data = [
  81. 'transfer_app_id' => $transferAppId,
  82. 'user_id' => $userId,
  83. 'amount' => $amount,
  84. 'out_user_id' => $outUserId,
  85. 'remark' => $remark,
  86. 'callback_data' => $callbackData,
  87. ];
  88. return self::createTransferOutFromArrayForThirdParty($data);
  89. }
  90. /**
  91. * 创建第三方应用转出订单(内部实现,跳过密码验证)
  92. * 我 =》 三方
  93. * 注意:第三方应用传入的金额是外部金额,需要转换为内部金额进行处理
  94. *
  95. * @param array $data 订单数据
  96. * @return TransferOrder
  97. * @throws \Exception
  98. */
  99. public static function createTransferOutFromArrayForThirdParty(array $data): TransferOrder
  100. {
  101. // 获取应用配置(需要先获取以便进行金额转换)
  102. /**
  103. * @var TransferApp $app
  104. */
  105. $app = TransferApp::findOrFail($data['transfer_app_id']);
  106. if (!$app->is_enabled) {
  107. throw new \Exception('应用已禁用');
  108. }
  109. // 验证汇率是否合理
  110. if (bccomp((string)$app->exchange_rate, '0', 10) <= 0) {
  111. throw new \Exception('汇率配置错误:汇率必须大于0');
  112. }
  113. // 第三方应用金额处理:传入的是外部金额,需要转换为内部金额
  114. $outAmount = (string)$data['amount']; // 外部金额(第三方系统金额)
  115. $amount = bcmul($outAmount, (string)$app->exchange_rate, 10); // 内部金额 = 外部金额 × 汇率
  116. // 验证外部金额是否合理
  117. if (bccomp($outAmount, '0', 10) <= 0) {
  118. throw new \Exception('转出金额必须大于0');
  119. }
  120. // 验证内部金额计算结果是否合理
  121. if (bccomp($amount, '0', 10) <= 0) {
  122. throw new \Exception('汇率计算结果异常:内部金额必须大于0');
  123. }
  124. // 修改验证数据:将外部金额替换为内部金额,这样验证器就能正确验证余额
  125. $validationData = $data;
  126. $validationData['amount'] = $amount; // 传递内部金额给验证器
  127. // 使用第三方应用专用验证类(跳过密码验证)
  128. $validation = new \App\Module\Transfer\Validations\TransferOutThirdPartyValidation($validationData);
  129. $validation->validated();
  130. // 计算手续费(基于内部金额)
  131. $FeeCalculatedEvent = \App\Module\Transfer\Services\FeeService::calculateOutFee($app, $amount, [ 'user_id' => $data['user_id'] ]);
  132. // 生成外部订单ID
  133. $outOrderId = self::generateOutOrderId('OUT');
  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' => $outOrderId,
  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 = $app->calculateOutFee($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['fee_rate'],
  282. 'fee_amount' => $feeInfo['fee_amount'],
  283. 'actual_amount' => $feeInfo['actual_amount'],
  284. 'callback_data' => $data['callback_data'] ?? [],
  285. 'remark' => $data['remark'] ?? null,
  286. ]);
  287. // 验证fund_to_uid必须存在
  288. if (empty($app->fund_to_uid)) {
  289. $order->updateStatus(TransferStatus::FAILED, '应用配置错误:未配置转入目标账户');
  290. throw new \Exception('应用配置错误:未配置转入目标账户(fund_to_uid)');
  291. }
  292. // 验证目标账户是否存在
  293. $targetFundService = new FundService($app->fund_to_uid, $app->fund_id);
  294. if (!$targetFundService->getAccount()) {
  295. $order->updateStatus(TransferStatus::FAILED, '目标账户不存在');
  296. throw new \Exception('目标账户不存在');
  297. }
  298. // 预先验证用户余额是否充足(转出金额 + 手续费)
  299. $userFundService = new FundService($data['user_id'], $app->fund_id);
  300. $totalRequired = bcadd($order->amount, $order->fee_amount, 10);
  301. if (bccomp((string)$userFundService->balance(), $totalRequired, 10) < 0) {
  302. $order->updateStatus(TransferStatus::FAILED, '用户余额不足');
  303. throw new \Exception('用户余额不足');
  304. }
  305. // 使用trade方法从用户转账到目标账户(完整转出金额,不扣除手续费)
  306. $tradeResult = $userFundService->trade(
  307. $app->fund_to_uid,
  308. $order->amount, // 转出完整金额(不扣除手续费)
  309. 'transfer_out',
  310. $order->id,
  311. "转出到{$app->title}"
  312. );
  313. // 如果有手续费,处理手续费收取
  314. if ($order->hasFee()) {
  315. // 获取手续费收取账户UID(默认为1)
  316. $feeAccountUid = $app->getFeeAccountUid();
  317. $feeTradeResult = $userFundService->trade(
  318. $feeAccountUid,
  319. $order->fee_amount,
  320. 'transfer_out_fee',
  321. $order->id,
  322. "转出手续费-{$app->title}"
  323. );
  324. if (is_string($feeTradeResult)) {
  325. Log::warning('Transfer out fee collection failed', [
  326. 'order_id' => $order->id,
  327. 'fee_amount' => $order->fee_amount,
  328. 'fee_account_uid' => $feeAccountUid,
  329. 'error' => $feeTradeResult
  330. ]);
  331. } else {
  332. Log::info('Transfer out fee collected successfully', [
  333. 'order_id' => $order->id,
  334. 'fee_amount' => $order->fee_amount,
  335. 'fee_account_uid' => $feeAccountUid,
  336. 'configured_account' => $app->fee_account_uid
  337. ]);
  338. }
  339. }
  340. if (is_string($tradeResult)) {
  341. $order->updateStatus(TransferStatus::FAILED, '资金转移失败: ' . $tradeResult);
  342. throw new \Exception('余额不足或资金操作失败: ' . $tradeResult);
  343. }
  344. // 检查是否需要调用外部API
  345. if (!empty($app->order_out_create_url)) {
  346. // 配置了外部转出API,调用外部API处理
  347. OrderLogic::processTransferOut($order);
  348. } else {
  349. // 没有配置外部API,直接完成订单
  350. $order->updateStatus(TransferStatus::COMPLETED);
  351. }
  352. return $order;
  353. }
  354. /**
  355. * 创建转入订单(对外接口)
  356. * 三方 =》 我
  357. *
  358. * @param int $transferAppId 划转应用ID
  359. * @param int $userId 用户ID
  360. * @param string $businessId 业务订单ID
  361. * @param string $amount 转入金额
  362. * @param string|null $outUserId 外部用户ID
  363. * @param string|null $remark 备注
  364. * @param array $callbackData 回调数据
  365. * @return TransferOrder
  366. * @throws \Exception
  367. */
  368. public static function createTransferIn(
  369. int $transferAppId,
  370. int $userId,
  371. string $businessId,
  372. string $amount,
  373. ?string $outUserId = null,
  374. ?string $remark = null,
  375. array $callbackData = []
  376. ): TransferOrder
  377. {
  378. $data = [
  379. 'transfer_app_id' => $transferAppId,
  380. 'user_id' => $userId,
  381. 'business_id' => $businessId,
  382. 'amount' => $amount,
  383. 'out_user_id' => $outUserId,
  384. 'remark' => $remark,
  385. 'callback_data' => $callbackData,
  386. ];
  387. return self::createTransferInFromArray($data, $userId);
  388. }
  389. /**
  390. * 创建转入订单(内部实现)
  391. * 三方 =》 我
  392. * 处理流程:
  393. * 1. 验证请求数据
  394. * 2. 获取应用配置
  395. * 3. 创建转入订单
  396. * 4. 验证fund_in_uid必须存在,从指定账户转账到用户
  397. * 5. 根据是否配置回调URL决定后续处理(发送回调或直接完成)
  398. *
  399. * @param array $data 订单数据
  400. * @return TransferOrder
  401. * @throws \Exception
  402. */
  403. public static function createTransferInFromArray(array $data, $user_id): TransferOrder
  404. {
  405. // 验证请求数据
  406. $validation = new TransferInValidation($data);
  407. $validation->validated();
  408. // 获取应用配置
  409. /**
  410. * @var TransferApp $app
  411. */
  412. $app = TransferApp::findOrFail($data['transfer_app_id']);
  413. if (!$app->is_enabled) {
  414. throw new \Exception('应用已禁用');
  415. }
  416. // 检查外部订单ID是否已存在
  417. $existingOrder = TransferOrder::where('out_order_id', $data['business_id'])
  418. ->where('out_id', $app->out_id2 ?? 0)
  419. ->first();
  420. if ($existingOrder) {
  421. throw new \Exception('订单已存在');
  422. }
  423. // 计算金额(转入:外部金额转换为内部金额)
  424. $outAmount = (string)$data['amount']; // 外部金额
  425. $amount = bcmul($outAmount, (string)$app->exchange_rate, 10); // 内部金额 = 外部金额 × 汇率
  426. // 计算手续费
  427. $feeInfo = FeeService::calculateInFee($app, $amount, [
  428. 'user_id' => $user_id
  429. ]);
  430. // 创建订单
  431. /**
  432. * @var TransferOrder $order
  433. */
  434. $order = TransferOrder::create([
  435. 'transfer_app_id' => $app->id,
  436. 'out_id' => $app->out_id2 ?? 0,
  437. 'out_order_id' => $data['business_id'],
  438. 'out_user_id' => $data['out_user_id'] ?? null,
  439. 'user_id' => $data['user_id'],
  440. 'currency_id' => $app->currency_id,
  441. 'fund_id' => $app->fund_id,
  442. 'type' => TransferType::IN,
  443. 'status' => TransferStatus::CREATED,
  444. 'out_amount' => $outAmount,
  445. 'amount' => $feeInfo->amount,
  446. 'exchange_rate' => $app->exchange_rate,
  447. 'fee_rate' => $feeInfo->feeRate,
  448. 'fee_amount' => $feeInfo->feeAmount,
  449. 'actual_amount' => $feeInfo->actualAmount,
  450. 'totle_amount' => $feeInfo->totleAmount,
  451. 'callback_data' => $data['callback_data'] ?? [],
  452. 'remark' => $data['remark'] ?? null,
  453. ]);
  454. // 验证fund_in_uid必须存在
  455. if (empty($app->fund_in_uid)) {
  456. $order->updateStatus(TransferStatus::FAILED, '应用配置错误:未配置转入来源账户');
  457. throw new \Exception('应用配置错误:未配置转入来源账户(fund_in_uid)');
  458. }
  459. // 验证来源账户是否存在
  460. $sourceFundService = new FundService($app->fund_in_uid, $app->fund_id);
  461. if (!$sourceFundService->getAccount()) {
  462. $order->updateStatus(TransferStatus::FAILED, '来源账户不存在');
  463. throw new \Exception('来源账户不存在');
  464. }
  465. // 验证来源账户余额是否充足
  466. if ($sourceFundService->balance() < $order->totle_amount) {
  467. $order->updateStatus(TransferStatus::FAILED, '来源账户余额不足');
  468. throw new \Exception('来源账户余额不足');
  469. }
  470. // 使用trade方法从来源账户转账到用户账户(扣除手续费后的实际金额)
  471. $tradeResult = $sourceFundService->trade(
  472. $data['user_id'],
  473. $order->actual_amount, // 转入实际到账金额(扣除手续费后)
  474. 'transfer_in',
  475. $order->id,
  476. "从{$app->title}转入"
  477. );
  478. // 如果有手续费,处理手续费收取
  479. if ($order->hasFee()) {
  480. // 获取手续费收取账户UID(默认为1)
  481. $feeAccountUid = $app->getFeeAccountUid();
  482. $feeTradeResult = $sourceFundService->trade(
  483. $feeAccountUid,
  484. $order->fee_amount,
  485. 'transfer_in_fee',
  486. $order->id,
  487. "转入手续费-{$app->title}"
  488. );
  489. if (is_string($feeTradeResult)) {
  490. Log::warning('Transfer in fee collection failed', [
  491. 'order_id' => $order->id,
  492. 'fee_amount' => $order->fee_amount,
  493. 'fee_account_uid' => $feeAccountUid,
  494. 'error' => $feeTradeResult
  495. ]);
  496. } else {
  497. Log::info('Transfer in fee collected successfully', [
  498. 'order_id' => $order->id,
  499. 'fee_amount' => $order->fee_amount,
  500. 'fee_account_uid' => $feeAccountUid,
  501. 'configured_account' => $app->fee_account_uid
  502. ]);
  503. }
  504. }
  505. if (is_string($tradeResult)) {
  506. $order->updateStatus(TransferStatus::FAILED, '资金转移失败: ' . $tradeResult);
  507. throw new \Exception('来源账户资金不足或操作失败: ' . $tradeResult);
  508. }
  509. // 检查是否需要发送回调通知
  510. if ($app->supportsCallback()) {
  511. // 配置了回调URL,更新状态为处理中并发送回调
  512. $order->updateStatus(TransferStatus::PROCESSING);
  513. CallbackLogic::sendCallback($order);
  514. } else {
  515. // 没有配置回调URL,直接完成订单
  516. $order->updateStatus(TransferStatus::COMPLETED);
  517. }
  518. return $order;
  519. }
  520. /**
  521. * 处理回调结果
  522. *
  523. * @param array $callbackData 回调数据
  524. * @return bool
  525. */
  526. public static function processCallback(array $callbackData): bool
  527. {
  528. // 查找订单
  529. $order = TransferOrder::where('out_order_id', $callbackData['business_id'])
  530. ->where('out_id', $callbackData['out_id'])
  531. ->first();
  532. if (!$order) {
  533. Log::warning('Transfer callback order not found', $callbackData);
  534. return false;
  535. }
  536. // 更新订单状态
  537. $status = $callbackData['success'] ? TransferStatus::COMPLETED : TransferStatus::FAILED;
  538. $message = $callbackData['message'] ?? null;
  539. return $order->updateStatus($status, $message);
  540. }
  541. /**
  542. * 生成外部订单ID
  543. *
  544. * @param string $prefix 前缀
  545. * @return string
  546. */
  547. private static function generateOutOrderId(string $prefix = 'TR'): string
  548. {
  549. return $prefix . date('YmdHis') . Str::random(6);
  550. }
  551. /**
  552. * 重试订单处理
  553. *
  554. * @param int $orderId 订单ID
  555. * @return bool
  556. */
  557. public static function retryOrder(int $orderId): bool
  558. {
  559. $order = TransferOrder::findOrFail($orderId);
  560. if (!$order->canRetry()) {
  561. throw new \Exception('订单状态不允许重试');
  562. }
  563. // 重置状态为已创建
  564. $order->updateStatus(TransferStatus::CREATED);
  565. // 根据订单类型重新处理
  566. if ($order->isTransferOut()) {
  567. OrderLogic::processTransferOut($order);
  568. } else {
  569. // 转入订单重试主要是重新发送回调
  570. if ($order->transferApp->supportsCallback()) {
  571. CallbackLogic::sendCallback($order);
  572. }
  573. }
  574. return true;
  575. }
  576. /**
  577. * 手动完成订单
  578. *
  579. * @param int $orderId 订单ID
  580. * @param string $remark 备注
  581. * @return bool
  582. */
  583. public static function manualComplete(int $orderId, string $remark = ''): bool
  584. {
  585. $order = TransferOrder::findOrFail($orderId);
  586. if ($order->isFinalStatus()) {
  587. throw new \Exception('订单已处于最终状态');
  588. }
  589. // 更新状态为已完成
  590. $order->updateStatus(TransferStatus::COMPLETED, $remark);
  591. return true;
  592. }
  593. }