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
1 change: 1 addition & 0 deletions lib/fnsapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'fnsapi/kkt_concern'
require 'fnsapi/get_message_service'
require 'fnsapi/kkt_service'
require 'fnsapi/exceptions'

module Fnsapi
class << self
Expand Down
38 changes: 38 additions & 0 deletions lib/fnsapi/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# Fns Exceptions
module Fnsapi
class Error < ::StandardError; end

class APIError < Error; end

# status 400
class FnsBadRequestError < APIError
def message
'Не пройден форматно-логический контроль реквизитов фискальных документов'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Переформулируйте, плиз, по-человечески. Из этого текста не ясно в чем дело)

end
end

# status 404
class FnsNotFoundError < APIError
def message
'Фискальный документ не найден в оперативном хранилище (сформирован более 2,5 месяцев назад)'
end
end

# status 406
class FnsCryptoProtectionError < APIError
def message
'Направленный фискальный признак не прошел проверку системы криптозащиты, поиск фискального документа прерван, дальнейшие попытки проверки запрещены'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ФП не соответствует фискальным данным. Проверка невозможна.

end
end

# status 503
class FnsServiceUnaviableError < APIError
def message
'Недокументированная ошибка в работе сервиса, для выяснения причин следует обратиться в техподдержку, указав: URL запроса к сервису, текст запроса к сервису, текст ответа от сервиса.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что-то пошло не так, напишите в техподдержку.

Хорошо бы сразу ссылку или почту указать.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не уверен с почтой, прямого доступа у меня к ней нет, но у них все таки выдача доступов по запросу, может там разные менеджеры на отдельную интеграцию. Тогда лучше наверное оставить просто как есть, а там уже на месте определяться, к кому обращаться с таким вопросом

end
end

class FnsUnknownError < APIError; end
end
21 changes: 20 additions & 1 deletion lib/fnsapi/kkt_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Fnsapi
class KktService < BaseService
include KktConcern

SUCCESS_STATUS_CODES = %w(200).freeze

def check_data(object, user_id = 'default_user')
ticket = Ticket.new(object)
result = client(auth_params(user_id)).call(:send_message, message: check_ticket_hash(ticket))
Expand All @@ -25,7 +27,7 @@ def get_data(object, user_id = 'default_user')
return unless message

code = message.dig(:get_ticket_response, :result, :code)
return code if code != '200'
handle_response(code)

JSON.parse(message.dig(:get_ticket_response, :result, :ticket))
end
Expand Down Expand Up @@ -89,5 +91,22 @@ def ticket_hash(ticket)
'tns:TypeOperation' => 1
}
end

def handle_response(code)
return if SUCCESS_STATUS_CODES.include?(code)

case code
when '400'
raise ::Fnsapi::FnsBadRequestError
when '404'
raise ::Fnsapi::FnsNotFoundError
when '406'
raise ::Fnsapi::FnsCryptoProtectionError
when '503'
raise ::Fnsapi::FnsServiceUnaviableError
else
raise ::Fnsapi::FnsUnknownError
end
end
end
end
4 changes: 2 additions & 2 deletions spec/kkt_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@
}
}
end
it 'returns code' do
expect(subject).to eq('400')
it 'raise error' do
expect { subject }.to raise_error(Fnsapi::FnsBadRequestError)
end
end
end
Expand Down