Skip to content

Commit 5f34448

Browse files
committed
Merge pull request #4 from anacarolinats/master
Added HTTPErrorException, fixed processor_code bug and improve readability
2 parents b4c161c + fdbc93c commit 5f34448

File tree

8 files changed

+130
-7
lines changed

8 files changed

+130
-7
lines changed

maxipago/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ class CardException(MaxipagoException):
2929

3030
class PaymentException(MaxipagoException):
3131
pass
32+
33+
34+
#http
35+
class HttpErrorException(MaxipagoException):
36+
pass

maxipago/managers/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding: utf-8
22
import requests
3+
from maxipago.exceptions import HttpErrorException
34
from maxipago.utils import etree, create_element_recursively
45

56

@@ -24,6 +25,9 @@ def __init__(self, maxid, api_key, api_version, sandbox):
2425
def request(self, xml_data, api_type=None):
2526
uri = self.get_uri(api_type)
2627
response = requests.post(url=uri, data=xml_data, headers={'content-type': 'text/xml'})
28+
29+
if not str(response.status_code).startswith('2'):
30+
raise HttpErrorException(u'Error %s: %s' % (response.status_code, response.reason))
2731
return response
2832

2933
def get_uri(self, api_type=None):

maxipago/resources/base.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ def __init__(self, data, requester, manager):
77
self.data = data
88
self.requester = requester
99
self.manager = manager
10-
11-
print self.data
12-
1310
self.process()
1411

1512
def process(self):

maxipago/resources/payment.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ def process(self):
1313
self.captured = False
1414

1515
tree = etree.parse(StringIO(self.data))
16+
error_code = tree.find('errorCode')
17+
if error_code is not None and error_code.text != '0':
18+
error_message = tree.find('errorMsg').text
19+
raise PaymentException(message=error_message)
1620

1721
processor_code = tree.find('processorCode')
18-
if processor_code.text.lower() == 'a':
22+
23+
if processor_code.text is not None and processor_code.text.lower() == 'a':
1924
self.approved = True
2025

2126
if self.approved:

maxipago/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from xml import *
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
TEST, REDECARD, AMEX, CIELO, TEF, CHASEPAYMENTECH = '1', '2', '3', '4', '5', '8'
2+
3+
PROCESSORS_CHOICES = (
4+
(TEST, 'Simulador de testes'),
5+
(REDECARD, 'Redecard'),
6+
(AMEX, 'Amex'),
7+
(CIELO, 'Cielo'),
8+
(TEF, 'TEF'),
9+
(CHASEPAYMENTECH, 'ChasePaymentech'),
10+
)
File renamed without changes.

tests.py

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from datetime import date
55
from maxipago import Maxipago, exceptions
6+
from maxipago.utils import payment_processors
67
from random import randint
78

89

@@ -157,7 +158,7 @@ def test_payment_authorize(self):
157158
REFERENCE = randint(1, 100000)
158159

159160
response = self.maxipago.payment.authorize(
160-
processor_id=1,
161+
processor_id=payment_processors.TEST,
161162
reference_num=REFERENCE,
162163

163164
billing_name=u'Fulano de Tal',
@@ -184,7 +185,7 @@ def test_payment_direct(self):
184185
REFERENCE = randint(1, 100000)
185186

186187
response = self.maxipago.payment.direct(
187-
processor_id=1,
188+
processor_id=payment_processors.TEST,
188189
reference_num=REFERENCE,
189190

190191
billing_name=u'Fulano de Tal',
@@ -211,7 +212,7 @@ def test_payment_direct_declined(self):
211212
REFERENCE = randint(1, 100000)
212213

213214
response = self.maxipago.payment.direct(
214-
processor_id=1,
215+
processor_id=payment_processors.TEST,
215216
reference_num=REFERENCE,
216217

217218
billing_name=u'Fulano de Tal',
@@ -234,5 +235,105 @@ def test_payment_direct_declined(self):
234235
self.assertFalse(response.authorized)
235236
self.assertFalse(response.captured)
236237

238+
def test_payment_direct_with_token(self):
239+
CUSTOMER_ID = randint(1, 100000)
240+
241+
response = self.maxipago.customer.add(
242+
customer_id=CUSTOMER_ID,
243+
first_name=u'Fulano',
244+
last_name=u'de Tal',
245+
)
246+
247+
self.assertTrue(hasattr(response, 'id'))
248+
249+
maxipago_customer_id = response.id
250+
251+
response = self.maxipago.card.add(
252+
customer_id=maxipago_customer_id,
253+
number=u'4111111111111111',
254+
expiration_month=u'02',
255+
expiration_year=date.today().year + 3,
256+
billing_name=u'Fulano de Tal',
257+
billing_address1=u'Rua das Alamedas, 123',
258+
billing_city=u'Rio de Janeiro',
259+
billing_state=u'RJ',
260+
billing_zip=u'20123456',
261+
billing_country=u'BR',
262+
billing_phone=u'552140634666',
263+
billing_email=u'[email protected]',
264+
)
265+
266+
self.assertTrue(getattr(response, 'token', False))
267+
REFERENCE = randint(1, 100000)
268+
269+
response = self.maxipago.payment.direct(
270+
processor_id=payment_processors.TEST,
271+
reference_num=REFERENCE,
272+
273+
customer_id=maxipago_customer_id,
274+
token=response.token,
275+
276+
charge_total='100.00',
277+
)
278+
279+
self.assertTrue(response.authorized)
280+
self.assertTrue(response.captured)
281+
282+
def test_payment_direct_with_token_decline(self):
283+
CUSTOMER_ID = randint(1, 100000)
284+
285+
response = self.maxipago.customer.add(
286+
customer_id=CUSTOMER_ID,
287+
first_name=u'Fulano',
288+
last_name=u'de Tal',
289+
)
290+
291+
self.assertTrue(hasattr(response, 'id'))
292+
293+
maxipago_customer_id = response.id
294+
295+
response = self.maxipago.card.add(
296+
customer_id=maxipago_customer_id,
297+
number=u'4111111111111111',
298+
expiration_month=u'02',
299+
expiration_year=date.today().year + 3,
300+
billing_name=u'Fulano de Tal',
301+
billing_address1=u'Rua das Alamedas, 123',
302+
billing_city=u'Rio de Janeiro',
303+
billing_state=u'RJ',
304+
billing_zip=u'20123456',
305+
billing_country=u'BR',
306+
billing_phone=u'552140634666',
307+
billing_email=u'[email protected]',
308+
)
309+
310+
self.assertTrue(getattr(response, 'token', False))
311+
REFERENCE = randint(1, 100000)
312+
313+
response = self.maxipago.payment.direct(
314+
processor_id=payment_processors.TEST,
315+
reference_num=REFERENCE,
316+
317+
customer_id=maxipago_customer_id,
318+
token=response.token,
319+
320+
charge_total='100.01',
321+
)
322+
323+
self.assertFalse(response.authorized)
324+
self.assertFalse(response.captured)
325+
326+
def test_http_exception(self):
327+
CUSTOMER_ID = randint(1, 100000)
328+
customer_manager = self.maxipago.customer
329+
customer_manager.uri_api = 'https://testapi.maxipago.net/UniversalAPI/WrongUri'
330+
with self.assertRaises(exceptions.HttpErrorException):
331+
customer_manager.add(
332+
customer_id=CUSTOMER_ID,
333+
first_name='Fulano',
334+
last_name='de Tal',
335+
)
336+
337+
237338
if __name__ == '__main__':
238339
unittest.main()

0 commit comments

Comments
 (0)