def _send(certificado, method, **kwargs): # A little hack to test path = os.path.join(os.path.dirname(__file__), 'templates') if method == 'TesteEnvioLoteRPS' or method == 'EnvioLoteRPS' \ or method == 'CancelamentoNFe': sign_tag(certificado, **kwargs) if method == 'TesteEnvioLoteRPS': xml_send = render_xml(path, 'EnvioLoteRPS.xml', False, **kwargs) else: xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) base_url = 'https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx?wsdl' cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) client = get_authenticated_client(base_url, cert, key) signer = Assinatura(cert, key, certificado.password) xml_send = signer.assina_xml(xml_send, '') try: response = getattr(client.service, method)(1, xml_send) except suds.WebFault as e: return { 'sent_xml': xml_send, 'received_xml': e.fault.faultstring, 'object': None } response, obj = sanitize_response(response) return {'sent_xml': xml_send, 'received_xml': response, 'object': obj}
def _send(certificado, method, **kwargs): # A little hack to test path = os.path.join(os.path.dirname(__file__), "templates") if ( method == "TesteEnvioLoteRPS" or method == "EnvioLoteRPS" or method == "CancelamentoNFe" ): sign_tag(certificado, **kwargs) if method == "TesteEnvioLoteRPS": xml_send = render_xml(path, "EnvioLoteRPS.xml", False, **kwargs) else: xml_send = render_xml(path, "%s.xml" % method, False, **kwargs) base_url = "https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx?wsdl" cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) client = get_authenticated_client(base_url, cert, key) signer = Assinatura(cert, key, certificado.password) xml_send = signer.assina_xml(xml_send, "") try: response = getattr(client.service, method)(1, xml_send) except suds.WebFault as e: return { "sent_xml": xml_send, "received_xml": e.fault.faultstring, "object": None, } response, obj = sanitize_response(response) return {"sent_xml": xml_send, "received_xml": response, "object": obj}
def _send(certificado, method, **kwargs): path = os.path.join(os.path.dirname(__file__), 'templates') if method in ('GerarNfse', 'RecepcionarLoteRps', 'RecepcionarLoteRpsSincrono', 'CancelarNfse', 'SubstituirNfse'): sign_tag(certificado, **kwargs) if kwargs['ambiente'] == 'producao': url = \ 'http://e-gov.betha.com.br/e-nota-contribuinte-test-ws/nfseWS?wsdl' else: url = 'http://e-gov.betha.com.br/e-nota-contribuinte-ws/nfseWS?wsdl' xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) client = get_authenticated_client(url, cert, key) pfx_path = certificado.save_pfx() signer = Assinatura(pfx_path, certificado.password) xml_send = signer.assina_xml(xml_send, '') try: response = getattr(client.service, method)(1, xml_send) except suds.WebFault as e: return { 'sent_xml': xml_send, 'received_xml': e.fault.faultstring, 'object': None } response, obj = sanitize_response(response) return {'sent_xml': xml_send, 'received_xml': response, 'object': obj}
def _send(certificado, method, **kwargs): url = _get_url(**kwargs) path = os.path.join(os.path.dirname(__file__), 'templates') xml_send = _render(path, method, **kwargs) client = get_client(url) response = False if certificado: cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) signer = Assinatura(cert, key, certificado.password) xml_send = signer.assina_xml(xml_send, '') try: response = getattr(client.service, method)(xml_send) response, obj = sanitize_response(response.encode()) except suds.WebFault as e: return { 'sent_xml': xml_send, 'received_xml': e.fault.faultstring, 'object': None } except Exception as e: if response: raise Exception(response) else: raise e return {'sent_xml': xml_send, 'received_xml': response, 'object': obj}
def _send(certificado, method, **kwargs): # A little hack to test path = os.path.join(os.path.dirname(__file__), 'templates') if method == 'TesteEnvioLoteRPS' or method == 'EnvioLoteRPS' \ or method == 'CancelamentoNFe': sign_tag(certificado, **kwargs) if method == 'TesteEnvioLoteRPS': xml_send = render_xml(path, 'EnvioLoteRPS.xml', False, **kwargs) else: xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) base_url = 'https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx?wsdl' cert, key = extract_cert_and_key_from_pfx( certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) client = get_authenticated_client(base_url, cert, key) pfx_path = certificado.save_pfx() signer = Assinatura(pfx_path, certificado.password) xml_send = signer.assina_xml(xml_send, '') try: response = getattr(client.service, method)(1, xml_send) except suds.WebFault, e: return { 'sent_xml': xml_send, 'received_xml': e.fault.faultstring, 'object': None }
def _render(certificado, method, **kwargs): path = os.path.join(os.path.dirname(__file__), "templates") xml_send = render_xml(path, "%s.xml" % method, False, **kwargs) cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) signer = Assinatura(cert, key, certificado.password) xml_send = signer.assina_xml(xml_send, "") return xml_send
def _render(certificado, method, **kwargs): path = os.path.join(os.path.dirname(__file__), 'templates') xml_send = render_xml(path, '%s.xml' % method, True, **kwargs) if method in ('RecepcionarLoteRps','CancelarNfse'): reference = '' if method == 'RecepcionarLoteRps': reference = '%s' % kwargs['nfse']['lista_rps'][0]['numero'] signer = Assinatura(certificado.pfx, certificado.password) xml_send = signer.assina_xml(xml_send, reference) # xml_send = etree.tostring(xml_send) if method == 'CancelarNfse': xml_send = etree.fromstring(xml_send) Signature = xml_send.find(".//{http://www.w3.org/2000/09/xmldsig#}Signature") Pedido = xml_send.find(".//{http://www.betha.com.br/e-nota-contribuinte-ws}Pedido") if Signature: Pedido.append(Signature) xml_send = etree.tostring(xml_send) else: xml_send = etree.tostring(xml_send) return xml_send
def _send(certificado, method, **kwargs): path = os.path.join(os.path.dirname(__file__), "templates") if method in ( "GerarNfse", "RecepcionarLoteRps", "RecepcionarLoteRpsSincrono", "CancelarNfse", "SubstituirNfse", ): sign_tag(certificado, **kwargs) if kwargs["ambiente"] == "producao": url = "http://e-gov.betha.com.br/e-nota-contribuinte-test-ws/nfseWS?wsdl" else: url = "http://e-gov.betha.com.br/e-nota-contribuinte-ws/nfseWS?wsdl" xml_send = render_xml(path, "%s.xml" % method, False, **kwargs) cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) client = get_authenticated_client(url, cert, key) pfx_path = certificado.save_pfx() signer = Assinatura(pfx_path, certificado.password) xml_send = signer.assina_xml(xml_send, "") try: response = getattr(client.service, method)(1, xml_send) except suds.WebFault as e: return { "sent_xml": xml_send, "received_xml": e.fault.faultstring, "object": None, } response, obj = sanitize_response(response) return {"sent_xml": xml_send, "received_xml": response, "object": obj}