Last active
June 26, 2019 03:30
-
-
Save artemklevtsov/c736892c22185d004640cd77ff448f3d to your computer and use it in GitHub Desktop.
Получение ИНН физического лица на сайте ФНС
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @title Запрос ИНН физического лица | |
#' | |
#' @param first_name (Строка) Имя. | |
#' @param last_name (Строка) Фамилияю | |
#' @param middle_name (Строка) Отчество. | |
#' @param birth_date (Дата) Дата рождения. | |
#' @param passport_series (Строка) Серия паспорта. | |
#' @param passport_number (Строка) Номер паспорта. | |
#' | |
#' @examples | |
#' fetch_inn( | |
#' first_name = "Иван", | |
#' last_name = "Иванов", | |
#' middle_name = "Иванович", | |
#' birth_date = as.Date("1970-01-01"), | |
#' passport_series = "0000", | |
#' passport_number = "000000" | |
#' ) | |
#' | |
fetch_inn <- function(first_name, last_name, middle_name = NULL, birth_date, passport_series, passport_number) { | |
# Check args | |
checkmate::assert_string(first_name, min.chars = 1L) | |
checkmate::assert_string(last_name, min.chars = 1L) | |
checkmate::assert_string(middle_name, min.chars = 0L, null.ok = TRUE) | |
checkmate::assert_date(birth_date, len = 1L, lower = as.Date("1900-01-01"), upper = Sys.Date()) | |
checkmate::assert_string(passport_series, pattern = "[0-9]{4}") | |
checkmate::assert_string(passport_number, pattern = "[0-9]{6}") | |
u <- "https://service.nalog.ru/inn-proc.do" | |
h <- curl::new_handle() | |
l <- list( | |
c = "innMy", | |
fam = last_name, | |
nam = first_name, | |
bdate = format(birth_date, "%d.%m.%Y"), | |
doctype = "21", | |
docno = paste(substr(passport_series, 1, 2), | |
substr(passport_series, 3, 4), | |
passport_number), | |
captchaToken = "" | |
) | |
if (is.null(middle_name) || !nzchar(middle_name)) { | |
l$opt_otch <- "1" | |
} else { | |
l$otch = middle_name | |
} | |
curl::handle_setform(h = h, .list = l) | |
resp <- curl::curl_fetch_memory(url = u, handle = h) | |
hdr <- curl::parse_headers_list(resp$headers) | |
cnt <- rawToChar(resp$content) | |
is_json <- grepl("application/json", hdr[["content-type"]], fixed = TRUE) | |
if (isTRUE(is_json)) { | |
json <- jsonlite::fromJSON(cnt) | |
} | |
if (resp$status_code >= 400L) { | |
if (isTRUE(is_json)) { | |
msg <- paste("-", json$ERRORS, collapse = "\n") | |
} else { | |
msg <- cnt | |
} | |
err <- errorCondition( | |
message = sprintf("\n<HTTPError: %s>\n%s", resp$status_code, msg), | |
class = "HTTPError" | |
) | |
stop(err) | |
} | |
return(json$inn) | |
} | |
get_captcha_token <- function(h) { | |
u <- "https://service.nalog.ru/static/captcha.html" | |
t <- as.numeric(Sys.time()) | |
u <- paste0(u, "?r=", t) | |
resp <- curl::curl_fetch_memory(u, h) | |
cnt <- rawToChar(resp$content) | |
res <- list( | |
time = t, | |
token = cnt | |
) | |
return(res) | |
} | |
get_captcha_image <- function(h, time, token) { | |
checkmate::assert_number(time) | |
checkmate::assert_string(token) | |
u <- "https://service.nalog.ru/static/captcha.html" | |
u <- paste0(u, "?r=", time, "&a=", token) | |
f <- tempfile() | |
resp <- curl::curl_fetch_disk(u, f, h) | |
return(f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment