update empik

This commit is contained in:
2026-03-15 20:22:14 +01:00
parent 910807beb9
commit 5871764385
5 changed files with 153 additions and 153 deletions

View File

@@ -106,17 +106,39 @@ class OrderFulfiller
*/
protected function validate($data)
{
if (
(
empty($data['customer']['shipping_address']) &&
empty($data['customer']['billing_address'])
) ||
empty($data['order_id']) ||
empty($data['order_lines']) ||
empty($data['order_state']) ||
empty($data['total_price'])
) {
throw new OrderProcessException(sprintf('Invalid order data for order: %s', $data['order_id']));
$customer = isset($data['customer']) && is_array($data['customer']) ? $data['customer'] : [];
$shippingAddress = isset($customer['shipping_address']) ? $customer['shipping_address'] : null;
$billingAddress = isset($customer['billing_address']) ? $customer['billing_address'] : null;
$orderId = isset($data['order_id']) ? $data['order_id'] : 'unknown';
$errors = [];
if (empty($shippingAddress) && empty($billingAddress)) {
$errors[] = 'both customer.shipping_address and customer.billing_address are empty';
}
if (empty($data['order_id'])) {
$errors[] = 'order_id is empty';
}
if (empty($data['order_lines'])) {
$errors[] = 'order_lines is empty';
}
if (empty($data['order_state'])) {
$errors[] = 'order_state is empty';
}
if (empty($data['total_price'])) {
$errors[] = 'total_price is empty';
}
if (!empty($errors)) {
throw new OrderProcessException(sprintf(
'Invalid order data for order: %s. Validation errors: %s',
$orderId,
implode('; ', $errors)
));
}
}
}

View File

@@ -58,8 +58,8 @@ class OrderProcessor
$this->empikClient = $this->empikClientFactory->createClient();
$this->allowAccept = (bool)Configuration::get(ConfigurationAdapter::CONF_AUTO_ACCEPT_ORDERS);
$this->allowImport = (bool)Configuration::get(ConfigurationAdapter::CONF_IMPORT_ORDERS);
$this->allowAccept = (bool) Configuration::get(ConfigurationAdapter::CONF_AUTO_ACCEPT_ORDERS);
$this->allowImport = (bool) Configuration::get(ConfigurationAdapter::CONF_IMPORT_ORDERS);
}
public function process()
@@ -85,7 +85,7 @@ class OrderProcessor
$this->import($empikOrder);
}
} catch (OrderProcessException $exception) {
$this->logger->logError('Empikplaces: import order process, order id ' . $order['order_id'] . ', message:' . $exception->getMessage());
$this->logger->logError('Empikplaces: import order process, order id ' . $order['order_id'] . ', message:' . $exception->getMessage());
} catch (Exception $exception) {
$this->logger->logError('Empikplaces: ' . $exception->getMessage());
}
@@ -106,13 +106,13 @@ class OrderProcessor
$empikOrderId = isset($data['order_id']) ? $data['order_id'] : 'unknown';
$errorMsg = sprintf('Error importing EMPIK order [%s]: %s', $empikOrderId, $e->getMessage());
$this->logger->logError($errorMsg);
$this->sendFailureNotification($empikOrderId, $e);
$this->sendFailureNotification($empikOrderId, $e, $data);
}
}
/**
* Resetuje AUTO_INCREMENT tabeli ps_orders do MAX(id_order) + 1
* aby uniknąć dziur w numeracji zamówień po ROLLBACK.
* Resets AUTO_INCREMENT of ps_orders to MAX(id_order) + 1
* to avoid gaps after ROLLBACK.
*/
protected function resetOrderAutoIncrement()
{
@@ -129,24 +129,18 @@ class OrderProcessor
}
/**
* Wysyła email z powiadomieniem o nieudanym imporcie zamówienia EMPIK.
* Sends notification email when EMPIK order import fails.
*
* @param string $empikOrderId
* @param Exception $exception
* @param array $orderData
*/
protected function sendFailureNotification($empikOrderId, Exception $exception)
protected function sendFailureNotification($empikOrderId, Exception $exception, array $orderData = [])
{
try {
$shopName = Configuration::get('PS_SHOP_NAME');
$subject = sprintf('[%s] Błąd importu zamówienia EMPIK: %s', $shopName, $empikOrderId);
$body = sprintf(
"Zamówienie EMPIK: %s\nData: %s\nBłąd: %s\n\nStack trace:\n%s",
$empikOrderId,
date('Y-m-d H:i:s'),
$exception->getMessage(),
$exception->getTraceAsString()
);
$subject = sprintf('[%s] Blad importu zamowienia EMPIK: %s', $shopName, $empikOrderId);
$orderContext = $this->buildOrderContext($orderData);
Mail::send(
(int) Configuration::get('PS_LANG_DEFAULT'),
@@ -156,6 +150,8 @@ class OrderProcessor
'{empik_order_id}' => $empikOrderId,
'{error_message}' => $exception->getMessage(),
'{error_date}' => date('Y-m-d H:i:s'),
'{order_context}' => $orderContext,
'{order_context_html}' => nl2br(htmlspecialchars($orderContext, ENT_QUOTES, 'UTF-8')),
'{stack_trace}' => nl2br($exception->getTraceAsString()),
],
self::ERROR_NOTIFICATION_EMAIL,
@@ -171,6 +167,32 @@ class OrderProcessor
}
}
/**
* @param array $orderData
* @return string
*/
protected function buildOrderContext(array $orderData)
{
if (empty($orderData)) {
return 'No order payload available.';
}
$customer = isset($orderData['customer']) && is_array($orderData['customer']) ? $orderData['customer'] : [];
$summary = [
'order_id' => isset($orderData['order_id']) ? $orderData['order_id'] : null,
'order_state' => isset($orderData['order_state']) ? $orderData['order_state'] : null,
'total_price' => isset($orderData['total_price']) ? $orderData['total_price'] : null,
'shipping_price' => isset($orderData['shipping_price']) ? $orderData['shipping_price'] : null,
'order_lines_count' => isset($orderData['order_lines']) && is_array($orderData['order_lines']) ? count($orderData['order_lines']) : 0,
'has_shipping_address' => !empty($customer['shipping_address']) ? 1 : 0,
'has_billing_address' => !empty($customer['billing_address']) ? 1 : 0,
];
$json = json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
return $json !== false ? $json : 'Unable to serialize order payload summary.';
}
protected function accept(EmpikOrderWrapper $empikOrder)
{
$acceptLines = $empikOrder->getAcceptanceLines();