-
-
Save luizvaz/e4c924d0516acfb361e965cbe8b61ef6 to your computer and use it in GitHub Desktop.
BHISS Método ConsultarNfse
This file contains 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
import os | |
import re | |
import ssl | |
import html | |
import http.client | |
import datetime | |
def do_request(body=None, cert_file=None, cert_secret=None, action="ConsultarNfse"): | |
if not (body and cert_file and cert_secret and action): | |
Print("Parâmetros insuficientes") | |
return | |
_HOST = 'bhisshomologa.pbh.gov.br' | |
_API = "/bhiss-ws/nfse" | |
_HEADERS = { | |
"Acept": "*/*", | |
"Content-type": "text/xml; charset=utf-8", | |
"SOAPAction": action, | |
"Content-length": "%d" % len(body) | |
} | |
# PROTOCOL TLS-1 ou TLS-2 | |
# Define o certificado do cliente para a conexão https | |
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) | |
context.load_cert_chain(certfile=cert_file, password=cert_secret) | |
# Cria a conexão para enviar o request HTTP | |
connection = http.client.HTTPSConnection(_HOST, port=443, context=context) | |
# Usa a conexão para enviar um HTTP POST | |
connection.request(method="POST", url=_API, headers=_HEADERS, body=body) | |
# HTTP response | |
response = connection.getresponse() | |
# 200 = OK | |
if response.status == 200: | |
# Resposta está entre as tags OUTPUT | |
data = str(response.read()) | |
matches = re.search(r'<outputXML>(.*)</outputXML>', data) | |
if matches.groups: | |
data = html.unescape(matches.groups()[0]) | |
print(data) | |
else: | |
print(f"Resposta com erro: \n{data}") | |
else: | |
data = response.read() | |
print(f"Resposta com erro: {response.status} \n {data}") | |
if __name__ == '__main__': | |
cert = os.path.join(os.path.dirname(__file__), 'cert_file.pem') | |
secret = '12345678' | |
_CNPJ = "<SEU CNPJ>" # Sem Pontuação | |
_INSM = "<SUA INSC. MUN>" # Sem Pontuação | |
_DATA_INI = "2023-01-01" | |
_DATA_FIN = "2022-01-23" | |
_HEAD = ("<?xml version='1.0' encoding='UTF-8'?>\n" | |
" <cabecalho xmlns=\"http://www.abrasf.org.br/nfse.xsd\" versao=\"1.00\">\n" | |
" <versaoDados>1.00</versaoDados>\n" | |
" </cabecalho>\n" | |
" ")\ | |
.replace(">", ">") \ | |
.replace("<", "<") | |
_BODY = ("<?xml version='1.0' encoding='UTF-8'?>\n" | |
"<ConsultarNfseEnvio xmlns=\"http://www.abrasf.org.br/nfse.xsd\">\n" | |
" <Prestador>\n" | |
f" <Cnpj>{_CNPJ}</Cnpj>\n" | |
f" <InscricaoMunicipal>{_INSM}</InscricaoMunicipal>\n" | |
" </Prestador>\n" | |
" <PeriodoEmissao>\n" | |
f" <DataInicial>{_DATA_INI}</DataInicial>\n" | |
f" <DataFinal>{_DATA_FIN}</DataFinal>\n" | |
" </PeriodoEmissao>\n" | |
"</ConsultarNfseEnvio>\n")\ | |
.replace(">", ">")\ | |
.replace("<", "<") | |
SOAP = ("<SOAP-ENV:Envelope\n" | |
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" | |
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" | |
" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" | |
"<SOAP-ENV:Body>\n" | |
"<ConsultarNfseRequest xmlns=\"http://ws.bhiss.pbh.gov.br\">\n" | |
f"<nfseCabecMsg xmlns=\"\">{_HEAD}</nfseCabecMsg>\n" | |
f"<nfseDadosMsg xmlns=\"\">{_BODY}</nfseDadosMsg>\n" | |
"</ConsultarNfseRequest>\n" | |
"</SOAP-ENV:Body>\n" | |
"</SOAP-ENV:Envelope>\n") | |
do_request(body=SOAP, cert_file=cert, cert_secret=secret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment