| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace App\Module\Transfer\Logics;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Services\ExternalApiService;
- use Illuminate\Support\Facades\Log;
- /**
- * 订单处理逻辑
- */
- class OrderLogic
- {
- /**
- * 处理转出订单
- *
- * @param TransferOrder $order 订单对象
- * @return bool
- */
- public static function processTransferOut(TransferOrder $order): bool
- {
- try {
- $app = $order->transferApp;
- // 检查是否配置了外部创建API
- if (empty($app->order_out_create_url)) {
- // 没有配置外部API,直接完成
- $order->updateStatus(TransferStatus::COMPLETED);
- return true;
- }
- // 更新状态为处理中
- $order->updateStatus(TransferStatus::PROCESSING);
- // 调用外部API创建订单
- $apiData = [
- 'business_id' => $order->out_order_id,
- 'user_id' => $order->out_user_id,
- 'amount' => $order->out_amount,
- 'currency' => $order->currency_id,
- 'remark' => $order->remark,
- 'callback_data' => $order->callback_data,
- ];
- $result = ExternalApiService::createOutOrder($app, $apiData);
- if ($result['success']) {
- // API调用成功,等待查询结果
- Log::info('Transfer out order created successfully', [
- 'order_id' => $order->id,
- 'out_order_id' => $order->out_order_id,
- 'api_response' => $result
- ]);
- return true;
- } else {
- // API调用失败
- $order->updateStatus(TransferStatus::FAILED, $result['message']);
- Log::error('Transfer out order creation failed', [
- 'order_id' => $order->id,
- 'out_order_id' => $order->out_order_id,
- 'error' => $result['message']
- ]);
- return false;
- }
- } catch (\Exception $e) {
- $order->updateStatus(TransferStatus::FAILED, $e->getMessage());
- Log::error('Transfer out order processing exception', [
- 'order_id' => $order->id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- /**
- * 查询转出订单状态
- *
- * @param TransferOrder $order 订单对象
- * @return bool
- */
- public static function queryTransferOutStatus(TransferOrder $order): bool
- {
- try {
- $app = $order->transferApp;
- // 检查是否配置了查询API
- if (empty($app->order_out_info_url)) {
- // 没有配置查询API,无法查询状态
- Log::warning('No query API configured for transfer out order', [
- 'order_id' => $order->id,
- 'app_id' => $app->id
- ]);
- return false;
- }
- // 调用外部API查询订单状态
- $result = ExternalApiService::queryOutOrder($app, $order->out_order_id);
- if ($result['success']) {
- $status = $result['data']['status'] ?? null;
-
- // 根据外部状态更新内部状态
- switch ($status) {
- case 'completed':
- case 'success':
- $order->updateStatus(TransferStatus::COMPLETED);
- break;
- case 'failed':
- case 'error':
- $order->updateStatus(TransferStatus::FAILED, $result['data']['message'] ?? '外部处理失败');
- break;
- case 'processing':
- case 'pending':
- // 保持处理中状态
- break;
- default:
- Log::warning('Unknown external order status', [
- 'order_id' => $order->id,
- 'external_status' => $status
- ]);
- }
- return true;
- } else {
- Log::error('Transfer out order query failed', [
- 'order_id' => $order->id,
- 'error' => $result['message']
- ]);
- return false;
- }
- } catch (\Exception $e) {
- Log::error('Transfer out order query exception', [
- 'order_id' => $order->id,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 查询转入订单状态
- *
- * @param TransferOrder $order 订单对象
- * @return bool
- */
- public static function queryTransferInStatus(TransferOrder $order): bool
- {
- try {
- $app = $order->transferApp;
- // 检查是否配置了查询API
- if (empty($app->order_in_info_url)) {
- // 没有配置查询API,无法查询状态
- return false;
- }
- // 调用外部API查询订单状态
- $result = ExternalApiService::queryInOrder($app, $order->out_order_id);
- if ($result['success']) {
- $status = $result['data']['status'] ?? null;
-
- // 根据外部状态更新内部状态
- switch ($status) {
- case 'completed':
- case 'success':
- $order->updateStatus(TransferStatus::COMPLETED);
- break;
- case 'failed':
- case 'error':
- $order->updateStatus(TransferStatus::FAILED, $result['data']['message'] ?? '外部处理失败');
- break;
- case 'processing':
- case 'pending':
- // 保持处理中状态
- break;
- }
- return true;
- }
- return false;
- } catch (\Exception $e) {
- Log::error('Transfer in order query exception', [
- 'order_id' => $order->id,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 批量处理待处理的订单
- *
- * @param int $limit 处理数量限制
- * @return int 处理的订单数量
- */
- public static function processePendingOrders(int $limit = 100): int
- {
- $processedCount = 0;
- // 处理转出订单
- $outOrders = TransferOrder::where('type', \App\Module\Transfer\Enums\TransferType::OUT)
- ->where('status', TransferStatus::PROCESSING)
- ->where('created_at', '>', now()->subHours(24)) // 只处理24小时内的订单
- ->limit($limit)
- ->get();
- foreach ($outOrders as $order) {
- if (self::queryTransferOutStatus($order)) {
- $processedCount++;
- }
- }
- // 处理转入订单
- $inOrders = TransferOrder::where('type', \App\Module\Transfer\Enums\TransferType::IN)
- ->where('status', TransferStatus::PROCESSING)
- ->where('created_at', '>', now()->subHours(24))
- ->limit($limit - $processedCount)
- ->get();
- foreach ($inOrders as $order) {
- if (self::queryTransferInStatus($order)) {
- $processedCount++;
- }
- }
- return $processedCount;
- }
- }
|