OrderLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Module\Transfer\Logics;
  3. use App\Module\Transfer\Enums\TransferStatus;
  4. use App\Module\Transfer\Models\TransferOrder;
  5. use App\Module\Transfer\Services\ExternalApiService;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 订单处理逻辑
  9. */
  10. class OrderLogic
  11. {
  12. /**
  13. * 处理转出订单
  14. *
  15. * @param TransferOrder $order 订单对象
  16. * @return bool
  17. */
  18. public static function processTransferOut(TransferOrder $order): bool
  19. {
  20. try {
  21. $app = $order->transferApp;
  22. // 检查是否配置了外部创建API
  23. if (empty($app->order_out_create_url)) {
  24. // 没有配置外部API,直接完成
  25. $order->updateStatus(TransferStatus::COMPLETED);
  26. return true;
  27. }
  28. // 更新状态为处理中
  29. $order->updateStatus(TransferStatus::PROCESSING);
  30. // 调用外部API创建订单
  31. $apiData = [
  32. 'business_id' => $order->out_order_id,
  33. 'user_id' => $order->out_user_id,
  34. 'amount' => $order->out_amount,
  35. 'currency' => $order->currency_id,
  36. 'remark' => $order->remark,
  37. 'callback_data' => $order->callback_data,
  38. ];
  39. $result = ExternalApiService::createOutOrder($app, $apiData);
  40. if ($result['success']) {
  41. // API调用成功,等待查询结果
  42. Log::info('Transfer out order created successfully', [
  43. 'order_id' => $order->id,
  44. 'out_order_id' => $order->out_order_id,
  45. 'api_response' => $result
  46. ]);
  47. return true;
  48. } else {
  49. // API调用失败
  50. $order->updateStatus(TransferStatus::FAILED, $result['message']);
  51. Log::error('Transfer out order creation failed', [
  52. 'order_id' => $order->id,
  53. 'out_order_id' => $order->out_order_id,
  54. 'error' => $result['message']
  55. ]);
  56. return false;
  57. }
  58. } catch (\Exception $e) {
  59. $order->updateStatus(TransferStatus::FAILED, $e->getMessage());
  60. Log::error('Transfer out order processing exception', [
  61. 'order_id' => $order->id,
  62. 'error' => $e->getMessage(),
  63. 'trace' => $e->getTraceAsString()
  64. ]);
  65. return false;
  66. }
  67. }
  68. /**
  69. * 查询转出订单状态
  70. *
  71. * @param TransferOrder $order 订单对象
  72. * @return bool
  73. */
  74. public static function queryTransferOutStatus(TransferOrder $order): bool
  75. {
  76. try {
  77. $app = $order->transferApp;
  78. // 检查是否配置了查询API
  79. if (empty($app->order_out_info_url)) {
  80. // 没有配置查询API,无法查询状态
  81. Log::warning('No query API configured for transfer out order', [
  82. 'order_id' => $order->id,
  83. 'app_id' => $app->id
  84. ]);
  85. return false;
  86. }
  87. // 调用外部API查询订单状态
  88. $result = ExternalApiService::queryOutOrder($app, $order->out_order_id);
  89. if ($result['success']) {
  90. $status = $result['data']['status'] ?? null;
  91. // 根据外部状态更新内部状态
  92. switch ($status) {
  93. case 'completed':
  94. case 'success':
  95. $order->updateStatus(TransferStatus::COMPLETED);
  96. break;
  97. case 'failed':
  98. case 'error':
  99. $order->updateStatus(TransferStatus::FAILED, $result['data']['message'] ?? '外部处理失败');
  100. break;
  101. case 'processing':
  102. case 'pending':
  103. // 保持处理中状态
  104. break;
  105. default:
  106. Log::warning('Unknown external order status', [
  107. 'order_id' => $order->id,
  108. 'external_status' => $status
  109. ]);
  110. }
  111. return true;
  112. } else {
  113. Log::error('Transfer out order query failed', [
  114. 'order_id' => $order->id,
  115. 'error' => $result['message']
  116. ]);
  117. return false;
  118. }
  119. } catch (\Exception $e) {
  120. Log::error('Transfer out order query exception', [
  121. 'order_id' => $order->id,
  122. 'error' => $e->getMessage()
  123. ]);
  124. return false;
  125. }
  126. }
  127. /**
  128. * 查询转入订单状态
  129. *
  130. * @param TransferOrder $order 订单对象
  131. * @return bool
  132. */
  133. public static function queryTransferInStatus(TransferOrder $order): bool
  134. {
  135. try {
  136. $app = $order->transferApp;
  137. // 检查是否配置了查询API
  138. if (empty($app->order_in_info_url)) {
  139. // 没有配置查询API,无法查询状态
  140. return false;
  141. }
  142. // 调用外部API查询订单状态
  143. $result = ExternalApiService::queryInOrder($app, $order->out_order_id);
  144. if ($result['success']) {
  145. $status = $result['data']['status'] ?? null;
  146. // 根据外部状态更新内部状态
  147. switch ($status) {
  148. case 'completed':
  149. case 'success':
  150. $order->updateStatus(TransferStatus::COMPLETED);
  151. break;
  152. case 'failed':
  153. case 'error':
  154. $order->updateStatus(TransferStatus::FAILED, $result['data']['message'] ?? '外部处理失败');
  155. break;
  156. case 'processing':
  157. case 'pending':
  158. // 保持处理中状态
  159. break;
  160. }
  161. return true;
  162. }
  163. return false;
  164. } catch (\Exception $e) {
  165. Log::error('Transfer in order query exception', [
  166. 'order_id' => $order->id,
  167. 'error' => $e->getMessage()
  168. ]);
  169. return false;
  170. }
  171. }
  172. /**
  173. * 批量处理待处理的订单
  174. *
  175. * @param int $limit 处理数量限制
  176. * @return int 处理的订单数量
  177. */
  178. public static function processePendingOrders(int $limit = 100): int
  179. {
  180. $processedCount = 0;
  181. // 处理转出订单
  182. $outOrders = TransferOrder::where('type', \App\Module\Transfer\Enums\TransferType::OUT)
  183. ->where('status', TransferStatus::PROCESSING)
  184. ->where('created_at', '>', now()->subHours(24)) // 只处理24小时内的订单
  185. ->limit($limit)
  186. ->get();
  187. foreach ($outOrders as $order) {
  188. if (self::queryTransferOutStatus($order)) {
  189. $processedCount++;
  190. }
  191. }
  192. // 处理转入订单
  193. $inOrders = TransferOrder::where('type', \App\Module\Transfer\Enums\TransferType::IN)
  194. ->where('status', TransferStatus::PROCESSING)
  195. ->where('created_at', '>', now()->subHours(24))
  196. ->limit($limit - $processedCount)
  197. ->get();
  198. foreach ($inOrders as $order) {
  199. if (self::queryTransferInStatus($order)) {
  200. $processedCount++;
  201. }
  202. }
  203. return $processedCount;
  204. }
  205. }