config->isEnabled()) { return; } foreach ($this->syncRepository->getDue($this->config->getOrderBatchSize()) as $row) { $entityId = (int)$row['entity_id']; if (!$this->syncRepository->claim($entityId)) { continue; } $started = microtime(true); $payload = []; try { $order = $this->orderRepository->get((int)$row['order_id']); if ((string)$order->getState() === Order::STATE_CANCELED) { $this->syncRepository->markCreationCanceled($entityId); $this->logWriter->write([ 'direction' => 'push', 'operation' => 'order_create', 'entity_type' => 'order', 'local_entity_id' => $entityId, 'increment_id' => (string)$row['increment_id'], 'status' => 'ignored', 'attempt_number' => (int)$row['attempt_count'] + 1, 'request_payload' => ['reason' => 'Magento order was canceled before export.'], ]); continue; } // Capture the sanitized request in the attempt log even when Lingxing rejects the order. $payload = $this->payloadBuilder->build($order); $result = $this->exporter->export($order, $payload); $requestId = (string)($result['response']['request_id'] ?? ''); $hash = hash('sha256', (string)json_encode($result['payload'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); $this->syncRepository->markSuccess($entityId, $result['global_order_no'], $requestId, $hash); $this->logWriter->write([ 'direction' => 'push', 'operation' => 'order_create', 'entity_type' => 'order', 'local_entity_id' => $entityId, 'increment_id' => (string)$row['increment_id'], 'external_id' => $result['global_order_no'], 'status' => 'success', 'attempt_number' => (int)$row['attempt_count'] + 1, 'request_id' => $requestId, 'request_payload' => $result['payload'], 'response_payload' => $result['response'], 'duration_ms' => (int)round((microtime(true) - $started) * 1000), ]); } catch (\Throwable $exception) { $code = $exception instanceof ApiException ? $exception->getErrorCode() : (string)$exception->getCode(); $response = $exception instanceof ApiException ? $exception->getResponse() : []; $requestId = (string)($response['request_id'] ?? ''); $status = $this->syncRepository->markFailure( $entityId, (int)$row['attempt_count'], $this->config->getOrderMaxAttempts(), $code, $exception->getMessage(), $requestId ); $this->logWriter->write([ 'direction' => 'push', 'operation' => 'order_create', 'entity_type' => 'order', 'local_entity_id' => $entityId, 'increment_id' => (string)$row['increment_id'], 'external_id' => (string)($row['global_order_no'] ?? ''), 'status' => $status, 'attempt_number' => (int)$row['attempt_count'] + 1, 'error_code' => $code, 'error_message' => $exception->getMessage(), 'request_id' => $requestId, 'request_payload' => $payload, 'response_payload' => $response, 'duration_ms' => (int)round((microtime(true) - $started) * 1000), ]); $this->logger->error('Lingxing order synchronization failed.', [ 'order_id' => $row['order_id'], 'exception' => $exception, ]); } } } }