Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
msbrogli committed Jun 28, 2017
1 parent 96fc4ac commit 4631a33
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions santander.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# encoding: utf-8

import requests
import os
from lxml import etree
from collections import namedtuple

basedir = os.path.dirname(__file__)

GetTicketResponse = namedtuple('TicketResponse', 'return_code ticket response')
IncluirTituloResponse = namedtuple('IncluirTituloResponse', 'situacao descricao response')
ConsultarTituloResponse = namedtuple('ConsultarTituloResponse', 'situacao descricao response')

def xml_prettyprint(txt):
import xml.dom.minidom
return xml.dom.minidom.parseString(txt.encode('utf-8')).toprettyxml()

def render(template, data):
fullpath = os.path.join(basedir, 'templates/', template)
template = file(fullpath, 'r').read()
Expand All @@ -14,10 +24,10 @@ def get_ticket(data, cert=None):
url = 'https://ymbdlb.santander.com.br/dl-ticket-services/TicketEndpointService'
request_xml = render('solicitacao_ticket.xml', data)
ret = requests.post(url, data=request_xml, cert=cert)
ret_xml = etree.fromstring(str(ret.text))
ret_xml = etree.fromstring(ret.text.encode('utf-8'))
(ticket,) = ret_xml.xpath('//ticket')
(retCode,) = ret_xml.xpath('//retCode')
return retCode.text, ticket.text
return GetTicketResponse(int(retCode.text), ticket.text, xml_prettyprint(ret.text))

def incluir_titulo(ticket, estacao, data_nsu, nsu='', teste=False, cert=None):
url = 'https://ymbcash.santander.com.br/ymbsrv/CobrancaEndpointService'
Expand All @@ -30,18 +40,24 @@ def incluir_titulo(ticket, estacao, data_nsu, nsu='', teste=False, cert=None):
}
request_xml = render('inclusao_titulo.xml', data)
ret = requests.post(url, data=request_xml, cert=cert)
return ret
ret_xml = etree.fromstring(ret.text.encode('utf-8'))
(situacao,) = ret_xml.xpath('//situacao')
(descricao,) = ret_xml.xpath('//descricaoErro')
return IncluirTituloResponse(situacao.text, descricao.text, xml_prettyprint(ret.text))

def consultar_titulo(ticket, nsu, data_nsu, estacao, teste=False, cert=None):
url = 'https://ymbcash.santander.com.br/ymbsrv/CobrancaEndpointService'
data = {
'data_nsu': data_nsu,
'nsu': nsu,
'ticket': ticket,
'estacao': estacao,
'tipo_ambiente': 'T' if teste else 'P',
'nsu': nsu,
'data_nsu': data_nsu,
'estacao': estacao,
}
request_xml = render('consulta_titulo.xml', data)
ret = requests.post(url, data=request_xml, cert=cert)
return ret
ret_xml = etree.fromstring(ret.text.encode('utf-8'))
(situacao,) = ret_xml.xpath('//situacao')
(descricao,) = ret_xml.xpath('//descricaoErro')
return ConsultarTituloResponse(situacao.text, descricao.text, xml_prettyprint(ret.text))

0 comments on commit 4631a33

Please sign in to comment.