Skip to content

Commit

Permalink
Implementa NFse de Campinas
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioluna authored and danimaribeiro committed Dec 21, 2017
1 parent c2e2d1e commit 22ac348
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 474 deletions.
58 changes: 27 additions & 31 deletions pytrustnfe/nfse/campinas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,42 @@
import suds
from lxml import etree
from pytrustnfe.xml import render_xml, sanitize_response
from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key
from pytrustnfe.nfse.assinatura import Assinatura
from pytrustnfe import HttpClient
from pytrustnfe.client import get_client


def _render_xml(certificado, method, **kwargs):
def _render(certificado, method, **kwargs):
path = os.path.join(os.path.dirname(__file__), 'templates')
xml_send = render_xml(path, '%s.xml' % method, True, **kwargs)
xml_send = etree.tostring(xml_send)

return xml_send

if method == "testeEnviar":
xml_send = render_xml(path, 'enviar.xml', True, **kwargs)
else:
xml_send = render_xml(path, '%s.xml' % method, False, **kwargs)

def _validate(method, xml):
path = os.path.join(os.path.dirname(__file__), 'templates')
schema = os.path.join(path, '%s.xsd' % method)
if type(xml_send) != str:
xml_send = etree.tostring(xml_send)

nfe = etree.fromstring(xml)
esquema = etree.XMLSchema(etree.parse(schema))
esquema.validate(nfe)
erros = [x.message for x in esquema.error_log]
return erros
return xml_send


def _send(certificado, method, **kwargs):
url = 'http://issdigital.campinas.sp.gov.br/WsNFe2/LoteRps.jws?wsdl' # noqa

path = os.path.join(os.path.dirname(__file__), 'templates')

if method == "testeEnviar":
xml_send = render_xml(path, 'testeEnviar', **kwargs)
else:
xml_send = render_xml(path, '%s.xml' % method, False)
client = HttpClient(url)
xml_send = _render(path, method, **kwargs)
client = get_client(url)

pfx_path = certificado.save_pfx()
signer = Assinatura(pfx_path, certificado.password)
xml_signed = signer.assina_xml(xml_send, '')
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_signed)
response, obj = sanitize_response(response)
response = getattr(client.service, method)(xml_send)
response, obj = sanitize_response(response.encode())
except suds.WebFault as e:
return {
'sent_xml': xml_send,
Expand All @@ -62,18 +57,19 @@ def _send(certificado, method, **kwargs):


def enviar(certificado, **kwargs):
if kwargs['ambiente'] == 'producao':
return _send(certificado, 'enviar', **kwargs)
else:
return _send(certificado, 'testeEnviar', **kwargs)
return _send(certificado, 'enviar', **kwargs)


def teste_enviar(certificado, **kwargs):
return _send(certificado, 'testeEnviar', **kwargs)


def cancelar(certificado, ** kwargs):
return _send(certificado, 'cancelar', **kwargs)


def consulta_lote(certificado, **kwargs):
return _send(certificado, 'ConsultarLote', **kwargs)
def consulta_lote(**kwargs):
return _send(False, 'consultarLote', **kwargs)


def consultar_lote_rps(certificado, **kwarg):
Expand Down
30 changes: 0 additions & 30 deletions pytrustnfe/nfse/campinas/templates/ConsultaSeqRps.xsd

This file was deleted.

46 changes: 0 additions & 46 deletions pytrustnfe/nfse/campinas/templates/ConsultarLote.xsd

This file was deleted.

55 changes: 0 additions & 55 deletions pytrustnfe/nfse/campinas/templates/cancelar.xsd

This file was deleted.

88 changes: 0 additions & 88 deletions pytrustnfe/nfse/campinas/templates/consulta_notas.xsd

This file was deleted.

19 changes: 0 additions & 19 deletions pytrustnfe/nfse/campinas/templates/consultarNFSeRps.xml

This file was deleted.

55 changes: 0 additions & 55 deletions pytrustnfe/nfse/campinas/templates/consultarNFSeRps.xsd

This file was deleted.

Loading

0 comments on commit 22ac348

Please sign in to comment.