Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions assets/components/minishop3/js/web/core/CustomerAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,40 @@ class CustomerAPI {
return this.api.put(`/api/v1/customer/addresses/${id}/set-default`)
}

/**
* List current customer orders
*
* GET /api/v1/customer/orders?limit=&offset=&status=
*
* @param {Object} [params]
* @param {number} [params.limit]
* @param {number} [params.offset]
* @param {number} [params.status]
* @returns {Promise<Object>}
*/
async getOrders (params = {}) {
const query = new URLSearchParams()
for (const key of ['limit', 'offset', 'status']) {
if (params[key] != null) {
query.set(key, String(params[key]))
}
}
const qs = query.toString()
return this.api.get(`/api/v1/customer/orders${qs ? `?${qs}` : ''}`)
}

/**
* Get one order owned by the current customer
*
* GET /api/v1/customer/orders/{orderId}
*
* @param {number} orderId - Order ID
* @returns {Promise<Object>}
*/
async getOrder (orderId) {
return this.api.get(`/api/v1/customer/orders/${orderId}`)
}

/**
* Cancel order
*
Expand Down
10 changes: 10 additions & 0 deletions core/components/minishop3/config/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@
return $controller->verify($params);
});

$router->get('/orders', function($params) use ($modx) {
$controller = new \MiniShop3\Controllers\Api\Web\CustomerOrderController($modx);
return $controller->getList($params);
}, [$tokenMiddleware]);

$router->get('/orders/{id}', function($params) use ($modx) {
$controller = new \MiniShop3\Controllers\Api\Web\CustomerOrderController($modx);
return $controller->get($params);
}, [$tokenMiddleware]);

$router->post('/orders/{id}/cancel', function($params) use ($modx) {
$controller = new \MiniShop3\Controllers\Api\Web\CustomerOrderController($modx);
return $controller->cancel($params);
Expand Down
3 changes: 3 additions & 0 deletions core/components/minishop3/lexicon/en/customer.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,6 @@
$_lang['ms3_customer_order_cancel_err_not_found'] = 'Order not found';
$_lang['ms3_customer_order_cancel_err_status'] = 'This order cannot be cancelled';
$_lang['ms3_customer_order_cancel_err_failed'] = 'Failed to cancel order';
$_lang['ms3_customer_order_err_unauthorized'] = 'Authorization required';
$_lang['ms3_customer_order_err_no_id'] = 'Order ID is required';
$_lang['ms3_customer_order_err_not_found'] = 'Order not found';
3 changes: 3 additions & 0 deletions core/components/minishop3/lexicon/ru/customer.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,6 @@
$_lang['ms3_customer_order_cancel_err_not_found'] = 'Заказ не найден';
$_lang['ms3_customer_order_cancel_err_status'] = 'Этот заказ нельзя отменить';
$_lang['ms3_customer_order_cancel_err_failed'] = 'Не удалось отменить заказ';
$_lang['ms3_customer_order_err_unauthorized'] = 'Требуется авторизация';
$_lang['ms3_customer_order_err_no_id'] = 'Не указан номер заказа';
$_lang['ms3_customer_order_err_not_found'] = 'Заказ не найден';
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

namespace MiniShop3\Controllers\Api\Web;

use MiniShop3\Model\msCustomer;
use MiniShop3\Model\msOrder;
use MiniShop3\Router\HttpStatus;
use MiniShop3\Router\Response;
use MiniShop3\Services\Order\OrderStatusService;
use MiniShop3\Services\Customer\CustomerOrderService;
use MODX\Revolution\modX;

/**
* API controller for customer orders (Web API)
*
* Handles customer-initiated order actions, e.g. cancel order.
* List / get / cancel for the authorized customer only.
*
* @package MiniShop3\Controllers\Api\Web
*/
Expand All @@ -29,56 +27,108 @@ public function __construct(modX $modx)
}

/**
* Cancel order (customer-initiated)
* POST /api/v1/customer/orders/{id}/cancel
* List current customer orders
* GET /api/v1/customer/orders?limit=&offset=&status=
*
* @param array $params URL parameters
* @return array
*/
public function getList(array $params = []): array
{
$customer = $this->getAuthorizedCustomer();

if (!$customer) {
return Response::error($this->modx->lexicon('ms3_customer_order_err_unauthorized'), HttpStatus::UNAUTHORIZED)->getData();
}

/** @var CustomerOrderService $service */
$service = $this->modx->services->get('ms3_customer_order');
$query = CustomerOrderService::normalizeListParams($_GET, $service->getDraftStatusId());

return Response::success($service->listForCustomer(
(int)$customer->get('id'),
$query['limit'],
$query['offset'],
$query['status_id']
))->getData();
}

/**
* Get one order owned by the current customer
* GET /api/v1/customer/orders/{id}
*
* @param array $params URL parameters (id = order ID)
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
* @return array
*/
public function cancel(array $params = []): array
public function get(array $params = []): array
{
$customer = $this->getAuthorizedCustomer();

if (!$customer) {
return Response::error($this->modx->lexicon('ms3_customer_order_cancel_err_unauthorized'), HttpStatus::UNAUTHORIZED)->getData();
return Response::error($this->modx->lexicon('ms3_customer_order_err_unauthorized'), HttpStatus::UNAUTHORIZED)->getData();
}

$orderId = (int)($params['id'] ?? 0);

if (!$orderId) {
return Response::error($this->modx->lexicon('ms3_customer_order_cancel_err_no_order'), HttpStatus::BAD_REQUEST)->getData();
if ($orderId < 1) {
return Response::error($this->modx->lexicon('ms3_customer_order_err_no_id'), HttpStatus::BAD_REQUEST)->getData();
}

/** @var msOrder|null $order */
$order = $this->modx->getObject(msOrder::class, [
'id' => $orderId,
'customer_id' => $customer->get('id'),
]);
/** @var CustomerOrderService $service */
$service = $this->modx->services->get('ms3_customer_order');
$detail = $service->getForCustomer((int)$customer->get('id'), $orderId);

if (!$order) {
return Response::error($this->modx->lexicon('ms3_customer_order_cancel_err_not_found'), HttpStatus::NOT_FOUND)->getData();
if ($detail === null) {
return Response::error($this->modx->lexicon('ms3_customer_order_err_not_found'), HttpStatus::NOT_FOUND)->getData();
}

/** @var OrderStatusService $orderStatusService */
$orderStatusService = $this->modx->services->get('ms3_order_status');
$allowedStatusIds = $orderStatusService->getAllowedCancelStatusIds();
$currentStatusId = (int) $order->get('status_id');
return Response::success($detail)->getData();
}

if (!in_array($currentStatusId, $allowedStatusIds, true)) {
return Response::error($this->modx->lexicon('ms3_customer_order_cancel_err_status'), HttpStatus::BAD_REQUEST)->getData();
/**
* Cancel order (customer-initiated)
* POST /api/v1/customer/orders/{id}/cancel
*
* @param array $params URL parameters (id = order ID)
* @return array Response ['success' => bool, 'message' => '', 'data' => [...]]
*/
public function cancel(array $params = []): array
{
$customer = $this->getAuthorizedCustomer();

if (!$customer) {
return Response::error($this->modx->lexicon('ms3_customer_order_cancel_err_unauthorized'), HttpStatus::UNAUTHORIZED)->getData();
}

$cancelledStatusId = (int) $this->modx->getOption('ms3_status_canceled', null, 5);
$orderId = (int)($params['id'] ?? 0);

$result = $orderStatusService->change($orderId, $cancelledStatusId);
if ($orderId < 1) {
return Response::error($this->modx->lexicon('ms3_customer_order_cancel_err_no_order'), HttpStatus::BAD_REQUEST)->getData();
}

if ($result !== true) {
$message = is_string($result) ? $result : $this->modx->lexicon('ms3_customer_order_cancel_err_failed');
return Response::error($message, HttpStatus::BAD_REQUEST)->getData();
/** @var CustomerOrderService $service */
$service = $this->modx->services->get('ms3_customer_order');
$result = $service->cancelForCustomer((int)$customer->get('id'), $orderId);

if (!$result['ok']) {
return match ($result['error']) {
'not_found' => Response::error(
$this->modx->lexicon('ms3_customer_order_cancel_err_not_found'),
HttpStatus::NOT_FOUND
)->getData(),
'status' => Response::error(
$this->modx->lexicon('ms3_customer_order_cancel_err_status'),
HttpStatus::BAD_REQUEST
)->getData(),
default => Response::error(
$result['message'] ?? $this->modx->lexicon('ms3_customer_order_cancel_err_failed'),
HttpStatus::BAD_REQUEST
)->getData(),
};
}

return Response::success(
['order_id' => $orderId, 'status_id' => $cancelledStatusId],
['order_id' => $result['order_id'], 'status_id' => $result['status_id']],
$this->modx->lexicon('ms3_customer_order_cancelled')
)->getData();
}
Expand Down
4 changes: 4 additions & 0 deletions core/components/minishop3/src/ServiceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class ServiceRegistry
'class' => \MiniShop3\Services\Order\OrderService::class,
'interface' => null,
],
'ms3_customer_order' => [
'class' => \MiniShop3\Services\Customer\CustomerOrderService::class,
'interface' => null,
],
// Order workflow services (used by Order controller)
// All services can be overridden via ms3.services.php config
'ms3_order_draft_manager' => [
Expand Down
Loading