TransferLogic.php 23 KB

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