17 private const ERROR_CODE_ADDRESSEE_IS_NOT_SPECIFIED =
'ADDRESSEE_IS_NOT_SPECIFIED';
18 private const ERROR_CODE_ADDRESSEE_UNEXPECTED_VALUE =
'ADDRESSEE_UNEXPECTED_VALUE';
19 private const ERROR_CODE_MESSAGE_NOT_SPECIFIED =
'MESSAGE_NOT_SPECIFIED';
20 private const ERROR_CODE_MESSAGE_STATUS_NOT_SPECIFIED =
'MESSAGE_STATUS_NOT_SPECIFIED';
21 private const ERROR_CODE_MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED =
'MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED';
22 private const ERROR_CODE_UNEXPECTED_MESSAGE_STATUS_SEMANTIC =
'UNEXPECTED_MESSAGE_STATUS_SEMANTIC';
23 private const ERROR_CODE_DELIVERY_ID_NOT_SPECIFIED =
'DELIVERY_ID_NOT_SPECIFIED';
24 private const ERROR_CODE_DELIVERY_NOT_FOUND =
'DELIVERY_NOT_FOUND';
25 private const ERROR_CODE_REQUEST_ID_NOT_SPECIFIED =
'REQUEST_ID_NOT_SPECIFIED';
26 private const ERROR_CODE_REQUEST_NOT_FOUND =
'REQUEST_NOT_FOUND';
27 private const ERROR_CODE_REQUEST_SHIPMENT_NOT_FOUND =
'REQUEST_SHIPMENT_NOT_FOUND';
28 private const ERROR_CODE_DELETE_REQUEST_INTERNAL_ERROR =
'DELETE_REQUEST_INTERNAL_ERROR';
29 private const ERROR_CODE_PROPERTIES_UNEXPECTED_FORMAT =
'PROPERTIES_UNEXPECTED_FORMAT';
30 private const ERROR_CODE_PROPERTY_VALUE_UNEXPECTED_FORMAT =
'PROPERTY_VALUE_UNEXPECTED_FORMAT';
31 private const ERROR_CODE_PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT =
'PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT';
32 private const ERROR_CODE_PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT =
'PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT';
33 private const ERROR_CODE_UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE =
'UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE';
34 private const ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE =
'UNEXPECTED_OVERWRITE_PROPERTIES_VALUE';
35 private const ERROR_CODE_EMPTY_UPDATE_PAYLOAD =
'EMPTY_UPDATE_PAYLOAD';
36 private const ERROR_CODE_UPDATE_REQUEST_INTERNAL_ERROR =
'UPDATE_REQUEST_INTERNAL_ERROR';
37 private const ERROR_CODE_STATUS_UNEXPECTED_FORMAT =
'STATUS_UNEXPECTED_FORMAT';
38 private const ERROR_CODE_STATUS_TEXT_NOT_SPECIFIED =
'STATUS_TEXT_NOT_SPECIFIED';
39 private const ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED =
'STATUS_SEMANTIC_NOT_SPECIFIED';
40 private const ERROR_CODE_DATE_VALUE_UNEXPECTED_FORMAT =
'DATE_VALUE_UNEXPECTED_FORMAT';
54 $requestId = self::getRequestId(
55 self::getDeliveryId(
$params,
'DELIVERY_ID'),
64 if (!in_array(
$params[
'FINALIZE'], [
'Y',
'N'],
true))
67 'Unexpected parameter FINALIZE value: Y, N expected',
68 self::ERROR_CODE_UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE
72 if (
$params[
'FINALIZE'] ===
'Y')
78 $requestStatus = self::getRequestStatus(
$params,
'STATUS');
79 if (!is_null($requestStatus))
81 $fields[
'EXTERNAL_STATUS'] = $requestStatus[
'TEXT'];
82 $fields[
'EXTERNAL_STATUS_SEMANTIC'] = $requestStatus[
'SEMANTIC'];
85 $properties = self::getRequestProperties(
$params,
'PROPERTIES');
86 if (!is_null($properties))
88 $fields[
'EXTERNAL_PROPERTIES'] = $properties;
91 $overwriteProperties =
false;
92 if (isset(
$params[
'OVERWRITE_PROPERTIES']))
94 if (!in_array(
$params[
'OVERWRITE_PROPERTIES'], [
'Y',
'N'],
true))
97 'Unexpected parameter OVERWRITE_PROPERTIES value: Y, N expected',
98 self::ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE
101 $overwriteProperties =
$params[
'OVERWRITE_PROPERTIES'] ===
'Y';
107 'Empty update payload',
108 self::ERROR_CODE_EMPTY_UPDATE_PAYLOAD
117 if (!$updateResult->isSuccess())
119 throw new RestException(
'Internal error', self::ERROR_CODE_UPDATE_REQUEST_INTERNAL_ERROR);
141 sprintf(
'Unexpected status (%s) format: array expected',
$key),
142 self::ERROR_CODE_STATUS_UNEXPECTED_FORMAT
149 'Status text has not been specified',
150 self::ERROR_CODE_STATUS_TEXT_NOT_SPECIFIED
156 throw new RestException(
157 'Status semantic has not been specified',
158 self::ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED
162 if (!in_array(
$params[
$key][
'SEMANTIC'], Requests\Manager::getRequestStatusSemantics(),
true))
164 throw new RestException(
165 sprintf(
'Unexpected request status semantic: %s',
$params[
$key][
'SEMANTIC']),
166 self::ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED
191 throw new RestException(
192 sprintf(
'Unexpected properties (%s) format: array expected',
$key),
193 self::ERROR_CODE_PROPERTIES_UNEXPECTED_FORMAT
200 $isExpectedFormat = (
208 if (!$isExpectedFormat)
210 throw new RestException(
211 sprintf(
'Unexpected property value (%s.%s) format',
$key, $propertyKey),
212 self::ERROR_CODE_PROPERTY_VALUE_UNEXPECTED_FORMAT
224 throw new RestException(
226 'Unexpected property value\'s tags format (%s.%s) format: array expected',
230 self::ERROR_CODE_PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT
236 if (!is_string($tag))
238 throw new RestException(
240 'Property value (%s.%s) tag must be of string type',
244 self::ERROR_CODE_PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT
270 $deliveryId = self::getDeliveryId(
$params,
'DELIVERY_ID');
271 $requestId = self::getRequestId($deliveryId,
$params,
'REQUEST_ID');
274 if (!$deleteResult->isSuccess())
276 throw new RestException(
'Internal error', self::ERROR_CODE_DELETE_REQUEST_INTERNAL_ERROR);
294 $delivery = self::getDelivery(
$params,
'DELIVERY_ID');
296 $requestId = self::getRequestId($delivery->getId(),
$params,
'REQUEST_ID');
299 self::getAddressee(
$params,
'ADDRESSEE'),
300 self::getMessage(
$params,
'MESSAGE')->setCurrency($delivery->getCurrency()),
302 self::getShipmentId($requestId,
$params,
'SHIPMENT_REQUEST_ID')
317 return $delivery->getId();
331 sprintf(
'Parameter %s is not specified',
$key),
332 self::ERROR_CODE_DELIVERY_ID_NOT_SPECIFIED
341 'Delivery service has not been found',
342 self::ERROR_CODE_DELIVERY_NOT_FOUND
359 throw new RestException(
360 sprintf(
'Parameter %s is not specified',
$key),
361 self::ERROR_CODE_ADDRESSEE_IS_NOT_SPECIFIED
365 if (!in_array(
$params[
$key], Requests\Manager::getMessageAddressees(),
true))
367 throw new RestException(
368 sprintf(
'Unexpected %s parameter value',
$key),
369 self::ERROR_CODE_ADDRESSEE_UNEXPECTED_VALUE
383 private static function getRequestId(
int $deliveryId,
array $params,
string $key): int
387 throw new RestException(
388 sprintf(
'Parameter %s is not specified',
$key),
389 self::ERROR_CODE_REQUEST_ID_NOT_SPECIFIED
393 $requestList = Requests\RequestTable::getList([
395 '=DELIVERY_ID' => $deliveryId,
403 throw new RestException(
404 'Request has not been found',
405 self::ERROR_CODE_REQUEST_NOT_FOUND
419 private static function getShipmentId(
int $requestId,
array $params,
string $key): int
421 $requestShipmentFilter = [
'=REQUEST_ID' => $requestId];
424 $requestShipmentFilter[
'=EXTERNAL_ID'] = (int)
$params[
$key];
426 $requestShipment = Requests\ShipmentTable::getList([
'filter' => $requestShipmentFilter])->fetch();
427 if (!$requestShipment)
429 throw new RestException(
430 'Shipment has not been found',
431 self::ERROR_CODE_REQUEST_SHIPMENT_NOT_FOUND
435 return (
int)$requestShipment[
'SHIPMENT_ID'];
444 private static function getMessage(
array $params,
string $key): Requests\Message\Message
455 throw new RestException(
456 sprintf(
'Parameter %s is not specified',
$key),
457 self::ERROR_CODE_MESSAGE_NOT_SPECIFIED
461 $message =
new Requests\Message\Message();
474 foreach (
$params[
$key][
'MONEY_VALUES'] as $moneyKey => $moneyValue)
485 foreach (
$params[
$key][
'DATE_VALUES'] as $dateKey => $dateValue)
487 if (!isset($dateValue[
'VALUE']) || !isset($dateValue[
'FORMAT']))
489 throw new RestException(
490 'Unexpected date value format',
491 self::ERROR_CODE_DATE_VALUE_UNEXPECTED_FORMAT
497 (
int)$dateValue[
'VALUE'],
498 (
string)$dateValue[
'FORMAT']
507 throw new RestException(
508 'Status message is not specified',
509 self::ERROR_CODE_MESSAGE_STATUS_NOT_SPECIFIED
513 if (!is_string(
$params[
$key][
'STATUS'][
'SEMANTIC']) || empty(
$params[
'MESSAGE'][
'STATUS'][
'SEMANTIC']))
515 throw new RestException(
516 'Message status semantic is not specified',
517 self::ERROR_CODE_MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED
521 if (!in_array(
$params[
$key][
'STATUS'][
'SEMANTIC'], Requests\Message\Status::getAvailableSemantics(),
true))
523 throw new RestException(
524 sprintf(
'Unexpected message status semantic: %s',
$params[
'MESSAGE'][
'STATUS'][
'SEMANTIC']),
525 self::ERROR_CODE_UNEXPECTED_MESSAGE_STATUS_SEMANTIC
530 new Requests\Message\
Status(
static includeModule($moduleName)