48 $dateTime = $data[
'date_create'];
51 'timestamp' => $dateTime->format(
'd.m.Y H:i:s'),
52 'external_id' => static::buildUuid(static::UUID_TYPE_CHECK, $data[
'unique_id']),
59 'attributes' => array(
62 'payments' => array(),
64 'total' => (
float)$data[
'total_sum']
68 $phone = \NormalizePhone($data[
'client_phone']);
69 if (is_string($phone))
71 if ($phone[0] ===
'7')
72 $phone = mb_substr($phone, 1);
79 $email = $data[
'client_email'] ?:
'';
82 if ($clientInfo ===
'PHONE')
84 $result[
'receipt'][
'attributes'] = [
'phone' => $phone];
86 elseif ($clientInfo ===
'EMAIL')
88 $result[
'receipt'][
'attributes'] = [
'email' => $email];
92 $result[
'receipt'][
'attributes'] = [
98 foreach ($data[
'payments'] as $payment)
100 $result[
'receipt'][
'payments'][] = array(
102 'sum' => (
float)$payment[
'sum']
106 foreach ($data[
'items'] as $i => $item)
110 $result[
'receipt'][
'items'][] = array(
111 'name' => mb_substr($item[
'name'], 0, self::MAX_NAME_LENGTH),
112 'price' => (
float)$item[
'price'],
113 'sum' => (
float)$item[
'sum'],
114 'quantity' => $item[
'quantity'],
127 $context = Main\Application::getInstance()->getContext();
128 $scheme = $context->getRequest()->isHttps() ?
'https' :
'http';
129 $server = $context->getServer();
130 $domain = $server->getServerName();
132 if (preg_match(
'/^(?<domain>.+):(?<port>\d+)$/', $domain, $matches))
134 $domain = $matches[
'domain'];
135 $port = $matches[
'port'];
139 $port = $server->getServerPort();
141 $port = in_array($port, array(80, 443)) ?
'' :
':'.$port;
143 return sprintf(
'%s://%s%s/bitrix/tools/sale_farm_check_print.php', $scheme, $domain, $port);
151 return Localization\Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_TITLE');
168 if (empty($checkInfo))
175 $errorType = static::getErrorType($data[
'error'][
'code']);
177 $result[
'ERROR'] = array(
178 'CODE' => $data[
'error'][
'code'],
179 'MESSAGE' => $data[
'error'][
'text'],
180 'TYPE' => ($errorType === Errors\Error::TYPE) ? Errors\Error::TYPE : Errors\Warning::TYPE
184 $result[
'ID'] = $checkInfo[
'ID'];
185 $result[
'CHECK_TYPE'] = $checkInfo[
'TYPE'];
188 $dateTime =
new Main\Type\DateTime($data[
'payload'][
'receipt_datetime'],
'd.m.Y H:i:s');
189 $result[
'LINK_PARAMS'] = array(
241 protected function getRequestUrl($operation, $token, array $queryData = array())
243 $groupCode = $this->
getField(
'NUMBER_KKM');
245 if ($operation === static::OPERATION_CHECK_REGISTRY)
247 return static::SERVICE_URL.
'/'.$groupCode.
'/'.$queryData[
'CHECK_TYPE'].
'?tokenid='.$token;
249 elseif ($operation === static::OPERATION_CHECK_CHECK)
251 return static::SERVICE_URL.
'/'.$groupCode.
'/report/'.$queryData[
'EXTERNAL_UUID'].
'?tokenid='.$token;
253 elseif ($operation === static::OPERATION_GET_TOKEN)
255 return static::SERVICE_URL.
'/getToken';
270 if (!$validateResult->isSuccess())
272 return $validateResult;
278 $operation =
'sell_refund';
292 $printResult =
new Result();
294 $token = $this->getAccessToken();
297 $token = $this->requestAccessToken();
300 $printResult->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_REQUEST_TOKEN_ERROR')));
305 $url = $this->
getRequestUrl(static::OPERATION_CHECK_REGISTRY, $token, [
'CHECK_TYPE' => $operation]);
307 $result = $this->send(static::REQUEST_TYPE_POST, $url, $check);
308 if (!$result->isSuccess())
313 $response = $result->getData();
314 if ($response[
'http_code'] === static::RESPONSE_HTTP_CODE_401)
316 $token = $this->requestAccessToken();
319 $printResult->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_REQUEST_TOKEN_ERROR')));
323 $url = $this->
getRequestUrl(static::OPERATION_CHECK_REGISTRY, $token, array(
'CHECK_TYPE' => $operation));
324 $result = $this->send(static::REQUEST_TYPE_POST, $url, $check);
325 if (!$result->isSuccess())
330 $response = $result->getData();
333 if ($response[
'http_code'] === static::RESPONSE_HTTP_CODE_200)
335 if ($response[
'uuid'])
337 $printResult->setData(array(
'UUID' => $response[
'uuid']));
341 $printResult->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_CHECK_REG_ERROR')));
346 if (isset($response[
'error'][
'text']))
348 $printResult->addError(
new Main\
Error($response[
'error'][
'text']));
352 $printResult->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_CHECK_REG_ERROR')));
373 static::OPERATION_CHECK_CHECK,
374 $this->getAccessToken(),
375 [
'EXTERNAL_UUID' => $uuid]
378 $result = $this->send(static::REQUEST_TYPE_GET, $url);
379 if (!$result->isSuccess())
384 $response = $result->getData();
385 if ($response[
'http_code'] === static::RESPONSE_HTTP_CODE_401)
387 $token = $this->requestAccessToken();
390 $result->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_REQUEST_TOKEN_ERROR')));
395 static::OPERATION_CHECK_CHECK,
396 $this->getAccessToken(),
397 [
'EXTERNAL_UUID' => $uuid]
400 $result = $this->send(static::REQUEST_TYPE_GET, $url);
401 if (!$result->isSuccess())
406 $response = $result->getData();
409 $response[
'uuid'] = $uuid;
411 if ($response[
'status'] ===
'wait')
413 $result->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_REQUEST_STATUS_WAIT')));
417 return static::applyCheckResult($response);
428 if ($checkData[
'receipt'][
'attributes'][
'email'] ===
'' && $checkData[
'receipt'][
'attributes'][
'phone'] ===
'')
430 $result->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_ERR_EMPTY_PHONE_EMAIL')));
433 foreach ($checkData[
'receipt'][
'items'] as $item)
435 if ($item[
'tax'] ===
null)
437 $result->addError(
new Main\
Error(Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_ERR_EMPTY_TAX')));
451 private function send($method, $url, array $data = array())
456 $http->setHeader(
'Content-Type',
'application/json; charset=utf-8');
458 if ($method === static::REQUEST_TYPE_POST)
460 $http->disableSslVerification();
461 $data = $this->encode($data);
465 $response = $http->post($url, $data);
469 $response = $http->get($url);
472 if ($response !==
false)
478 $response = $this->decode($response);
479 if (!is_array($response))
484 $response[
'http_code'] = $http->getStatus();
485 $result->addData($response);
487 catch (Main\ArgumentException $e)
489 $result->addError(
new Main\
Error($e->getMessage()));
494 $error = $http->getError();
495 foreach ($error as $code =>$message)
497 $result->addError(
new Main\
Error($message, $code));
512 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_AUTH'),
517 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_AUTH_LOGIN_LABEL')
521 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_AUTH_PASS_LABEL')
526 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_SERVICE'),
531 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_SERVICE_INN_LABEL')
533 'P_ADDRESS' => array(
535 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_SERVICE_URL_LABEL')
540 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_CLIENT'),
545 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_CLIENT_INFO'),
547 'NONE' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_CLIENT_NONE'),
548 'PHONE' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_CLIENT_PHONE'),
549 'EMAIL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_CLIENT_EMAIL'),
556 $settings[
'PAYMENT_TYPE'] = array(
557 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_P_TYPE'),
562 $systemPaymentType = array(
566 foreach ($systemPaymentType as $type => $value)
568 $settings[
'PAYMENT_TYPE'][
'ITEMS'][$type] = array(
570 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_P_TYPE_LABEL_'.ToUpper($type)),
575 $settings[
'VAT'] = array(
576 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_BITRIX_SETTINGS_VAT'),
581 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_BITRIX_SETTINGS_VAT_LABEL_NOT_VAT'),
587 if (Main\Loader::includeModule(
'catalog'))
589 $dbRes = Catalog\VatTable::getList([
'filter' => [
'ACTIVE' =>
'Y']]);
590 $vatList = $dbRes->fetchAll();
599 foreach ($vatList as $vat)
602 if (isset($defaultVatList[(
int)$vat[
'RATE']]))
603 $value = $defaultVatList[(int)$vat[
'RATE']];
605 $settings[
'VAT'][
'ITEMS'][(int)$vat[
'ID']] = array(
607 'LABEL' => $vat[
'NAME'].
' ['.(
int)$vat[
'RATE'].
'%]',
614 $settings[
'TAX'] = array(
615 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_SNO'),
620 'LABEL' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SETTINGS_SNO_LABEL'),
623 'osn' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SNO_OSN'),
624 'usn_income' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SNO_UI'),
625 'usn_income_outcome' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SNO_UIO'),
626 'envd' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SNO_ENVD'),
627 'esn' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SNO_ESN'),
628 'patent' => Localization\
Loc::getMessage(
'SALE_CASHBOX_ATOL_FARM_SNO_PATENT')
634 if (static::hasMeasureSettings())
636 $settings[
'MEASURE'] = static::getMeasureSettings();
658 'LABEL' => Localization\Loc::getMessage(
'SALE_CASHBOX_MEASURE_SUPPORT_SETTINGS_DEFAULT_VALUE'),
662 if (Main\Loader::includeModule(
'catalog'))
664 $measuresList = \CCatalogMeasure::getList();
665 while ($measure = $measuresList->fetch())
667 $measureItems[$measure[
'CODE']] = [
669 'LABEL' => $measure[
'MEASURE_TITLE'],
676 'LABEL' => Localization\Loc::getMessage(
'SALE_CASHBOX_MEASURE_SUPPORT_SETTINGS'),
677 'ITEMS' => $measureItems,
686 $generalRequiredFields = parent::getGeneralRequiredFields();
688 $map = CashboxTable::getMap();
689 $generalRequiredFields[
'NUMBER_KKM'] = $map[
'NUMBER_KKM'][
'title'];
690 return $generalRequiredFields;
696 private function getAccessToken()
698 return Main\Config\Option::get(
'sale', $this->getOptionName(),
'');
704 private function setAccessToken($token)
706 Main\Config\Option::set(
'sale', $this->getOptionName(), $token);
712 private function getOptionName()
714 return static::getOptionPrefix() .
'_' .ToLower($this->
getField(
'NUMBER_KKM'));
722 return static::TOKEN_OPTION_NAME;
729 private function encode(array $data)
731 return Main\Web\Json::encode($data);
739 private function decode($data)
741 return Main\Web\Json::decode($data);
748 private function requestAccessToken()
750 $url = $this->
getRequestUrl(static::OPERATION_GET_TOKEN,
'');
757 $result = $this->send(static::REQUEST_TYPE_POST, $url, $data);
758 if ($result->isSuccess())
760 $response = $result->getData();
761 if (isset($response[
'token']))
763 $this->setAccessToken($response[
'token']);
765 return $response[
'token'];
779 return Errors\Error::TYPE;
static getMessage($code, $replace=null, $language=null)