• Модуль: messageservice
  • Путь к файлу: ~/bitrix/modules/messageservice/lib/sender/sms/ednaruimhpx.php
  • Класс: BitrixMessageServiceSenderSmsEdnaruImHpx
  • Вызов: EdnaruImHpx::processServiceResponse
public function processServiceResponse(DTOResponse $response, SenderResultSendMessage $result): void
{
	if ($response->statusCode !== 200)
	{
		$result->addError(new Error("Response status code is {$response->statusCode}", 'WRONG_SERVICE_RESPONSE_CODE'));
		return;
	}
	$parseResult = $this->parseXml($response->body);
	if (!$parseResult->isSuccess())
	{
		$result->addError(
			new Error(
				'XML parse error: ' . implode('; ', $parseResult->getErrorMessages()),
				'XML_PARSE_ERROR'
			)
		);
		return;
	}

	/** @var SimpleXMLElement $instantMessageResponse */
	$instantMessageResponse = $parseResult->getData()['root'];

	// hack to convert SimpleXMLElement to array
	$instantMessageResponse = Json::decode(Json::encode($instantMessageResponse));

	// response structure
	if (
		!isset($instantMessageResponse['payload'])
		|| (!isset($instantMessageResponse['payload']['code'])
			&& !isset($instantMessageResponse['payload']['instantMessageList'])
		)
	)
	{
		$result->addError(new Error('Wrong xml response structure', 'SERVICE_RESPONSE_PARSE_ERROR'));
		return;
	}

	if ($instantMessageResponse['payload']['code'] !== 'ok')
	{
		$result->setStatus(BitrixMessageServiceMessageStatus::ERROR);
		$result->addError(new Error($instantMessageResponse['payload']['code'], $instantMessageResponse['payload']['code']));
		return;
	}

	foreach ($instantMessageResponse['payload']['instantMessageList'] as $instantMessage)
	{
		if ($instantMessage['code'] === 'ok' && isset($instantMessage['@attributes']['providerId']))
		{
			$result->setExternalId($instantMessage['@attributes']['providerId']);
			$result->setAccepted();
		}
		else
		{
			$result->setStatus(BitrixMessageServiceMessageStatus::ERROR);
			$result->addError(new Error('', $instantMessage['code']));
		}
		// we expect only one message response here
		return;
	}

	$result->addError(new Error('Could not find message status in response', 'SERVICE_RESPONSE_PARSE_ERROR'));
}