Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atualização para suportar python 3 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cieloApi3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .recurrentPayment import *

from .cieloEcommerce import *
from .request.exceptions import CieloRequestException
2 changes: 1 addition & 1 deletion cieloApi3/objectJSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def remove_none_dict(obj):
retorno = {}
for chave in obj:
valor = obj[chave]
if valor or isinstance(valor, (int, long, float, complex)):
if valor or isinstance(valor, (int, float, complex)):
retorno[chave] = remove_none(valor)

return retorno
Expand Down
18 changes: 7 additions & 11 deletions cieloApi3/request/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuid, json
import uuid

from future.utils import raise_with_traceback
from requests import Request, Session

class Base(object):
from .exceptions import CieloRequestException


class Base(object):
def __init__(self, merchant):

self.merchant = merchant
Expand Down Expand Up @@ -36,7 +38,7 @@ def send_request(self, method, uri, data=None, params=None):

response = s.send(prep)

if 'json' in response.headers['Content-Type'].lower():
if 'json' in response.headers.get('content-type', '').lower():
answers = response.json()
else:
answers = [{
Expand All @@ -45,13 +47,7 @@ def send_request(self, method, uri, data=None, params=None):
}]

if response.status_code >= 400:
errors = []

for answer in answers:
errors.append('\r\n * [%s] %s\r\n' % (answer['Code'], answer['Message']))

data_send = json.loads(body or 'null')

raise_with_traceback(Exception('\r\n%s\r\nMethod: %s\r\nUri: %s\r\nData: %s' % (''.join(errors), method, response.url, json.dumps(data_send, indent=2))))
raise_with_traceback(CieloRequestException(response=response,
**answers[0]))

return answers
28 changes: 28 additions & 0 deletions cieloApi3/request/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json


class CieloRequestException(Exception):
Code = None
Message = None
request = None
response = None

def __init__(self, *args, **kwargs):
response = kwargs.pop('response', None)
self.response = response
self.request = kwargs.pop('request', None)
if (response is not None and not self.request and
hasattr(response, 'request')):
self.request = self.response.request
self.Code = int(kwargs.pop('Code', None))
self.Message = kwargs.pop('Message', None)

def __str__(self):
data_send = json.loads(self.request.body or 'null')

errors = []
errors.append('* [%s] %s\r\n' % (self.Code, self.Message))
errors.append('Method: %s\r\n' % self.request.method)
errors.append('Uri: %s\r\n' % self.response.url)
errors.append('Data: %s\r\n' % json.dumps(data_send, indent=2))
return ''.join(errors)
14 changes: 7 additions & 7 deletions example/saleBoletoComplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@

# Criar a venda e imprime o retorno
response_create_sale = cielo_ecommerce.create_sale(sale)
print '----------------------response_create_sale----------------------'
print json.dumps(response_create_sale, indent=2)
print '----------------------response_create_sale----------------------'
print('----------------------response_create_sale----------------------')
print(json.dumps(response_create_sale, indent=2))
print('----------------------response_create_sale----------------------')

# Com a venda criada na Cielo, já temos o ID do pagamento, TID e demais
# dados retornados pela Cielo
payment_id = sale.payment.payment_id

# Com o ID do pagamento, podemos fazer uma consulta do pagamento
response_get_sale = cielo_ecommerce.get_sale(payment_id)
print '----------------------response_get_sale----------------------'
print json.dumps(response_get_sale, indent=2)
print '----------------------response_get_sale----------------------'
print('----------------------response_get_sale----------------------')
print(json.dumps(response_get_sale, indent=2))
print('----------------------response_get_sale----------------------')

print '\r\nLink Boleto:', sale.payment.url, '\r\n'
print('\r\nLink Boleto:', sale.payment.url, '\r\n')
14 changes: 7 additions & 7 deletions example/saleBoletoSimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@

# Criar a venda e imprime o retorno
response_create_sale = cielo_ecommerce.create_sale(sale)
print '----------------------response_create_sale----------------------'
print json.dumps(response_create_sale, indent=2)
print '----------------------response_create_sale----------------------'
print('----------------------response_create_sale----------------------')
print(json.dumps(response_create_sale, indent=2))
print('----------------------response_create_sale----------------------')

# Com a venda criada na Cielo, já temos o ID do pagamento, TID e demais
# dados retornados pela Cielo
payment_id = sale.payment.payment_id

# Com o ID do pagamento, podemos fazer uma consulta do pagamento
response_get_sale = cielo_ecommerce.get_sale(payment_id)
print '----------------------response_get_sale----------------------'
print json.dumps(response_get_sale, indent=2)
print '----------------------response_get_sale----------------------'
print('----------------------response_get_sale----------------------')
print(json.dumps(response_get_sale, indent=2))
print('----------------------response_get_sale----------------------')

print '\r\nLink Boleto:', sale.payment.url, '\r\n'
print('\r\nLink Boleto:', sale.payment.url, '\r\n')
18 changes: 9 additions & 9 deletions example/saleCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

# Criar a venda e imprime o retorno
response_create_sale = cielo_ecommerce.create_sale(sale)
print '----------------------response_create_sale----------------------'
print json.dumps(response_create_sale, indent=2)
print '----------------------response_create_sale----------------------'
print('----------------------response_create_sale----------------------')
print(json.dumps(response_create_sale, indent=2))
print('----------------------response_create_sale----------------------')

# Com a venda criada na Cielo, já temos o ID do pagamento, TID e demais
# dados retornados pela Cielo
Expand All @@ -46,12 +46,12 @@
# Com o ID do pagamento, podemos fazer sua captura,
# se ela não tiver sido capturada ainda
response_capture_sale = cielo_ecommerce.capture_sale(payment_id, 15700, 0)
print '----------------------response_capture_sale----------------------'
print json.dumps(response_capture_sale, indent=2)
print '----------------------response_capture_sale----------------------'
print('----------------------response_capture_sale----------------------')
print(json.dumps(response_capture_sale, indent=2))
print('----------------------response_capture_sale----------------------')

# E também podemos fazer seu cancelamento, se for o caso
response_cancel_sale = cielo_ecommerce.cancel_sale(payment_id, 15700)
print '---------------------response_cancel_sale---------------------'
print json.dumps(response_cancel_sale, indent=2)
print '---------------------response_cancel_sale---------------------'
print('---------------------response_cancel_sale---------------------')
print(json.dumps(response_cancel_sale, indent=2))
print('---------------------response_cancel_sale---------------------')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
url='https://github.com/thiago-Malaca/API-3.0-Python',
keywords='api3.0 cielo python sdk ecommerce',
packages=find_packages(),
install_requires=['requests', 'future'],
install_requires=['requests==2.13.*', 'future'],
license='MIT',
classifiers=(
# 'Development Status :: 5 - Production/Stable',
Expand Down