96 lines
4.8 KiB
PHP
96 lines
4.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Cron;
|
|
|
|
use Esun\LingxingErp\Model\Api\ApiException;
|
|
use Esun\LingxingErp\Model\Config;
|
|
use Esun\LingxingErp\Model\Order\OrderExporter;
|
|
use Esun\LingxingErp\Model\Order\OrderPayloadBuilder;
|
|
use Esun\LingxingErp\Model\Sync\LogWriter;
|
|
use Esun\LingxingErp\Model\Sync\OrderSyncRepository;
|
|
use Magento\Sales\Api\OrderRepositoryInterface;
|
|
use Magento\Sales\Model\Order;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ProcessOrders
|
|
{
|
|
public function __construct(
|
|
private readonly Config $config,
|
|
private readonly OrderSyncRepository $syncRepository,
|
|
private readonly OrderRepositoryInterface $orderRepository,
|
|
private readonly OrderPayloadBuilder $payloadBuilder,
|
|
private readonly OrderExporter $exporter,
|
|
private readonly LogWriter $logWriter,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
public function execute(): void
|
|
{
|
|
if (!$this->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,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|