From b1a801dc45b597025ecea973f365156e719bd4e3 Mon Sep 17 00:00:00 2001 From: Fabio Del Valle Date: Thu, 24 Mar 2022 12:07:15 -0300 Subject: [PATCH] Ajustes para Python3 - add pix e link de pagamento --- CONTRIBUTORS | 8 +- __init__.py | 0 maxipago/__init__.py | 3 +- maxipago/client.py | 2 +- maxipago/exceptions.py | 6 +- maxipago/managers/base.py | 3 +- maxipago/managers/link.py | 29 ++ maxipago/managers/payment/payment.py | 2 +- maxipago/managers/pix.py | 32 ++ maxipago/managers/transaction.py | 4 +- maxipago/requesters/base.py | 1 + maxipago/requesters/link.py | 6 + maxipago/requesters/pix.py | 6 + maxipago/resources/card.py | 6 +- maxipago/resources/customer.py | 8 +- maxipago/resources/link.py | 28 ++ maxipago/resources/payment.py | 4 +- maxipago/resources/transaction.py | 39 ++ maxipago/utils/__init__.py | 2 +- setup.py | 10 +- tests.py | 678 +++++++++++++-------------- 21 files changed, 514 insertions(+), 363 deletions(-) create mode 100644 __init__.py create mode 100644 maxipago/managers/link.py create mode 100644 maxipago/managers/pix.py create mode 100644 maxipago/requesters/link.py create mode 100644 maxipago/requesters/pix.py create mode 100644 maxipago/resources/link.py create mode 100644 maxipago/resources/transaction.py diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2801b73..0fb8af1 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,5 +1,5 @@ -Contributions were provided under the MIT License, as appropriate. - -The following people have contributed to this lib: - +Contributions were provided under the MIT License, as appropriate. + +The following people have contributed to this lib: + * [Vinicius Cainelli](https://github.com/viniciuscainelli) \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/maxipago/__init__.py b/maxipago/__init__.py index 423bf00..b948078 100644 --- a/maxipago/__init__.py +++ b/maxipago/__init__.py @@ -2,12 +2,11 @@ # :copyright: (c) 2013 by Stored (www.stored.com.br). # :license: BSD, see LICENSE for more details. -VERSION = (1, 0, 0) +VERSION = (1, 0, 3) __version__ = '.'.join(map(str, VERSION[0:3])) + ''.join(VERSION[3:]) __author__ = 'Stored' __contact__ = 'contato@stored.com.br' __docformat__ = 'restructuredtext' __license__ = 'MIT' - from maxipago.client import Maxipago diff --git a/maxipago/client.py b/maxipago/client.py index 413a0de..2b3d2ca 100644 --- a/maxipago/client.py +++ b/maxipago/client.py @@ -15,7 +15,7 @@ def __getattr__(self, name): module = __import__('maxipago.managers.{0}'.format(name), fromlist=['']) klass = getattr(module, class_name) return klass(self.maxid, self.api_key, self.api_version, self.sandbox) - except ImportError, AttributeError: + except (ImportError, AttributeError) as e: if name in self.__dict__: return self.__dict__.get('name') else: diff --git a/maxipago/exceptions.py b/maxipago/exceptions.py index b44ae89..763b9c0 100644 --- a/maxipago/exceptions.py +++ b/maxipago/exceptions.py @@ -31,6 +31,10 @@ class PaymentException(MaxipagoException): pass -#http +class TransactionException(MaxipagoException): + pass + + +# http class HttpErrorException(MaxipagoException): pass diff --git a/maxipago/managers/base.py b/maxipago/managers/base.py index 083ddfc..ff51815 100644 --- a/maxipago/managers/base.py +++ b/maxipago/managers/base.py @@ -117,9 +117,10 @@ def send(self, command, params=None, requester=None, api_type=None, resource=Non etree.SubElement(root, 'command').text = command request = etree.SubElement(root, 'request') + options = etree.SubElement(request, 'filterOptions') for key, value in params: - create_element_recursively(request, key).text = value + create_element_recursively(options, key).text = value xml_data = etree.tostring(root, pretty_print=True, encoding='UTF-8', xml_declaration=True) diff --git a/maxipago/managers/link.py b/maxipago/managers/link.py new file mode 100644 index 0000000..e5cd38d --- /dev/null +++ b/maxipago/managers/link.py @@ -0,0 +1,29 @@ +# coding: utf-8 +from maxipago.managers.base import ManagerTransaction, ManagerApi +from maxipago.requesters.link import LinkRequester +from maxipago.resources.link import LinkResource + + +class LinkManager(ManagerApi): + + def create(self, **kwargs): + fields = ( + ('consumer_authentication', {'translated_name': 'consumerAuthentication', 'default': 'N'}), + ('reference_num', {'translated_name': 'referenceNum'}), + ('fraud_check', {'translated_name': 'fraudCheck', 'default': 'N'}), + + ('billing_email', {'translated_name': 'billing/email'}), + ('billing_language', {'translated_name': 'billing/language', 'default': 'pt'}), + ('billing_name', {'translated_name': 'billing/firstName'}), + ('customer_id', {'translated_name': 'billing/customerIdExt'}), + + ('payment_info', {'translated_name': 'transactionDetail/description'}), + ('email_subject', {'translated_name': 'transactionDetail/emailSubject'}), + ('expiration_date', {'translated_name': 'transactionDetail/expirationDate', 'required': False}), + ('accept_pix', {'translated_name': 'transactionDetail/acceptPix', 'default': 'Y'}), + ('amount', {'translated_name': 'transactionDetail/payType/creditCard/amount'}), + ('soft_descriptor', {'translated_name': 'transactionDetail/payType/creditCard/softDescriptor'}), + ) + + requester = LinkRequester(fields, kwargs) + return self.send(command='add-payment-order', requester=requester, resource=LinkResource) diff --git a/maxipago/managers/payment/payment.py b/maxipago/managers/payment/payment.py index 3007881..7482232 100644 --- a/maxipago/managers/payment/payment.py +++ b/maxipago/managers/payment/payment.py @@ -1,5 +1,5 @@ # coding: utf-8 -from urllib import urlencode +from urllib.parse import urlencode from hashlib import md5 from maxipago.managers.base import ManagerTransaction from maxipago.requesters.payment import PaymentRequester diff --git a/maxipago/managers/pix.py b/maxipago/managers/pix.py new file mode 100644 index 0000000..f4a8a12 --- /dev/null +++ b/maxipago/managers/pix.py @@ -0,0 +1,32 @@ +# coding: utf-8 +from maxipago.managers.base import ManagerTransaction, ManagerApi +from maxipago.requesters.pix import PixRequester + + +class PixManager(ManagerTransaction): + + def create(self, **kwargs): + fields = ( + ('processor_id', {'translated_name': 'processorID'}), + ('reference_num', {'translated_name': 'referenceNum'}), + ('fraud_check', {'translated_name': 'fraudCheck', 'default': 'N'}), + ('customer_id', {'translated_name': 'customerIdExt'}), + ('billing_name', {'translated_name': 'billing/name'}), + ('billing_address1', {'translated_name': 'billing/address'}), + ('billing_complement', {'translated_name': 'billing/address2'}), + ('billing_neighborhood', {'translated_name': 'billing/district'}), + ('billing_city', {'translated_name': 'billing/city'}), + ('billing_state', {'translated_name': 'billing/state'}), + ('billing_zip', {'translated_name': 'billing/postalcode'}), + ('billing_country', {'translated_name': 'billing/country', 'default': 'BR'}), + ('billing_phone', {'translated_name': 'billing/phone'}), + ('billing_email', {'translated_name': 'billing/email'}), + ('document_type', {'translated_name': 'billing/documents/document/documentType', 'default': 'CPF'}), + ('document', {'translated_name': 'billing/documents/document/documentValue'}), + ('expiration_time', {'translated_name': 'transactionDetail/payType/pix/expirationTime'}), + ('payment_info', {'translated_name': 'transactionDetail/payType/pix/paymentInfo'}), + ('charge_total', {'translated_name': 'payment/chargeTotal'}), + ) + + requester = PixRequester(fields, kwargs) + return self.send(command='sale', requester=requester) diff --git a/maxipago/managers/transaction.py b/maxipago/managers/transaction.py index ff95eb8..9d9238f 100644 --- a/maxipago/managers/transaction.py +++ b/maxipago/managers/transaction.py @@ -1,6 +1,8 @@ # coding: utf-8 from maxipago.managers.base import ManagerRapi from maxipago.requesters.transaction import TransactionRequester +from maxipago.resources.payment import PaymentResource +from maxipago.resources.transaction import TransactionResource class TransactionManager(ManagerRapi): @@ -10,7 +12,7 @@ def get(self, **kwargs): ('transaction_id', {'translated_name': 'transactionId'}), ) requester = TransactionRequester(fields, kwargs) - return self.send(command='transactionDetailReport', requester=requester) + return self.send(command='transactionDetailReport', requester=requester, resource=TransactionResource) def list(self, **kwargs): fields = ( diff --git a/maxipago/requesters/base.py b/maxipago/requesters/base.py index bd3bdfd..3e6622a 100644 --- a/maxipago/requesters/base.py +++ b/maxipago/requesters/base.py @@ -1,4 +1,5 @@ # coding: utf-8 +from lxml.builder import unicode from maxipago.exceptions import ValidationError diff --git a/maxipago/requesters/link.py b/maxipago/requesters/link.py new file mode 100644 index 0000000..f7a19a9 --- /dev/null +++ b/maxipago/requesters/link.py @@ -0,0 +1,6 @@ +# coding: utf-8 +from maxipago.requesters.base import Requester + + +class LinkRequester(Requester): + pass diff --git a/maxipago/requesters/pix.py b/maxipago/requesters/pix.py new file mode 100644 index 0000000..ea8e870 --- /dev/null +++ b/maxipago/requesters/pix.py @@ -0,0 +1,6 @@ +# coding: utf-8 +from maxipago.requesters.base import Requester + + +class PixRequester(Requester): + pass diff --git a/maxipago/resources/card.py b/maxipago/resources/card.py index 0fe36c4..64e7465 100644 --- a/maxipago/resources/card.py +++ b/maxipago/resources/card.py @@ -1,5 +1,5 @@ # coding: utf-8 -from StringIO import StringIO +# from StringIO import StringIO from maxipago.utils import etree from maxipago.resources.base import Resource from maxipago.exceptions import CardException @@ -8,7 +8,7 @@ class CardAddResource(Resource): def process(self): - tree = etree.parse(StringIO(self.data)) + tree = etree.fromstring(self.data) error_code = tree.find('errorCode').text @@ -27,7 +27,7 @@ def process(self): class CardDeleteResource(Resource): def process(self): - tree = etree.parse(StringIO(self.data)) + tree = etree.fromstring(self.data) error_code = tree.find('errorCode').text diff --git a/maxipago/resources/customer.py b/maxipago/resources/customer.py index bfb635e..1063047 100644 --- a/maxipago/resources/customer.py +++ b/maxipago/resources/customer.py @@ -1,5 +1,5 @@ # coding: utf-8 -from StringIO import StringIO +# from io import StringIO from maxipago.utils import etree from maxipago.resources.base import Resource from maxipago.exceptions import CustomerAlreadyExists, CustomerException @@ -8,7 +8,7 @@ class CustomerAddResource(Resource): def process(self): - tree = etree.parse(StringIO(self.data)) + tree = etree.fromstring(self.data) error_code = tree.find('errorCode').text @@ -31,7 +31,7 @@ def process(self): class CustomerDeleteResource(Resource): def process(self): - tree = etree.parse(StringIO(self.data)) + tree = etree.fromstring(self.data) error_code = tree.find('errorCode').text @@ -51,7 +51,7 @@ def process(self): class CustomerUpdateResource(Resource): def process(self): - tree = etree.parse(StringIO(self.data)) + tree = etree.fromstring(self.data) error_code = tree.find('errorCode').text diff --git a/maxipago/resources/link.py b/maxipago/resources/link.py new file mode 100644 index 0000000..1aeb102 --- /dev/null +++ b/maxipago/resources/link.py @@ -0,0 +1,28 @@ +# coding: utf-8 +# from io import StringIO +from maxipago.utils import etree +from maxipago.resources.base import Resource +from maxipago.exceptions import TransactionException + + +class LinkResource(Resource): + + def process(self): + tree = etree.fromstring(self.data) + error_code = tree.find('errorCode') + if error_code is not None and error_code.text != '0': + error_message = tree.find('errorMessage').text + raise TransactionException(message=error_message) + + result = tree.find('result') + + fields = [ + ('url', 'url'), + ('pay_order_id', 'pay_order_id'), + ('message', 'message') + ] + + for f_name, f_translated in fields: + field = result.find(f_name) + if field is not None: + setattr(self, f_translated, field.text) diff --git a/maxipago/resources/payment.py b/maxipago/resources/payment.py index 1af573d..129ca77 100644 --- a/maxipago/resources/payment.py +++ b/maxipago/resources/payment.py @@ -1,5 +1,5 @@ # coding: utf-8 -from StringIO import StringIO +# from io import StringIO from maxipago.utils import etree from maxipago.resources.base import Resource from maxipago.exceptions import PaymentException @@ -12,7 +12,7 @@ def process(self): self.authorized = False self.captured = False - tree = etree.parse(StringIO(self.data)) + tree = etree.fromstring(self.data) error_code = tree.find('errorCode') if error_code is not None and error_code.text != '0': error_message = tree.find('errorMsg').text diff --git a/maxipago/resources/transaction.py b/maxipago/resources/transaction.py new file mode 100644 index 0000000..6cc2bb1 --- /dev/null +++ b/maxipago/resources/transaction.py @@ -0,0 +1,39 @@ +# coding: utf-8 +# from io import StringIO +from maxipago.utils import etree +from maxipago.resources.base import Resource +from maxipago.exceptions import TransactionException + + +class TransactionResource(Resource): + + def process(self): + tree = etree.fromstring(self.data) + header = tree.find('header') + error_code = header.find('errorCode') + if error_code is not None and error_code.text != '0': + error_message = header.find('errorMsg').text + raise TransactionException(message=error_message) + + transaction = tree.find('result').find('records').find('record') + + fields = [ + ('transactionId', 'transaction_id'), + ('approvalCode', 'auth_code'), + ('orderId', 'order_id'), + ('referenceNumber', 'reference_num'), + ('transactionDate', 'transaction_date'), + ('boletoUrl', 'boleto_url'), + ('responseCode', 'response_code'), + ('transactionAmount', 'amount'), + ('transactionStatus', 'status') + ] + + for f_name, f_translated in fields: + field = transaction.find(f_name) + if field is not None: + setattr(self, f_translated, field.text) + + response_message = tree.find('responseMessage') + if response_message is not None and response_message.text: + response_message = response_message.text.lower() diff --git a/maxipago/utils/__init__.py b/maxipago/utils/__init__.py index 3de2188..366a9ee 100644 --- a/maxipago/utils/__init__.py +++ b/maxipago/utils/__init__.py @@ -1 +1 @@ -from xml import * +from .xml import * diff --git a/setup.py b/setup.py index 22c4f8c..4bc2a9f 100644 --- a/setup.py +++ b/setup.py @@ -25,13 +25,17 @@ def get_version(): license='MIT', keywords='', url='', - packages=['maxipago'], + packages=['maxipago', + 'maxipago.managers', + 'maxipago.managers.payment', + 'maxipago.requesters', + 'maxipago.requesters.payment', + 'maxipago.resources', + 'maxipago.utils'], long_description=read_file('README.md'), classifiers=[ "Topic :: Utilities", ], install_requires=[ - 'requests==1.1.0', - 'lxml==3.1.0', ], ) diff --git a/tests.py b/tests.py index 35a2217..bfcce1b 100644 --- a/tests.py +++ b/tests.py @@ -1,339 +1,339 @@ -# coding: utf-8 -import os -import unittest -from datetime import date -from maxipago import Maxipago, exceptions -from maxipago.utils import payment_processors -from random import randint - - -MAXIPAGO_ID = os.getenv('MAXIPAGO_ID') -MAXIPAGO_API_KEY = os.getenv('MAXIPAGO_API_KEY') - - -class MaxipagoTestCase(unittest.TestCase): - - def setUp(self): - self.maxipago = Maxipago(MAXIPAGO_ID, MAXIPAGO_API_KEY, sandbox=True) - - def test_add_customer(self): - CUSTOMER_ID = randint(1, 100000) - - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name='Fulano', - last_name='de Tal', - ) - - self.assertTrue(getattr(response, 'id', False)) - - def test_add_customer_already_existing(self): - CUSTOMER_ID = randint(1, 100000) - - # creating customer with random id. - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name='Fulano', - last_name='de Tal', - ) - - self.assertTrue(hasattr(response, 'id')) - - # creating customer with the same id. - with self.assertRaises(exceptions.CustomerAlreadyExists): - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name='Fulano', - last_name='de Tal', - ) - - def test_delete_customer(self): - CUSTOMER_ID = randint(1, 100000) - - # creating customer with random id. - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name='Fulano', - last_name='de Tal', - ) - - self.assertTrue(hasattr(response, 'id')) - - maxipago_customer_id = response.id - - response = self.maxipago.customer.delete( - id=maxipago_customer_id, - ) - - def test_update_customer(self): - CUSTOMER_ID = randint(1, 100000) - - # creating customer with random id. - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name=u'Fulano', - last_name=u'de Tal', - ) - - self.assertTrue(hasattr(response, 'id')) - - maxipago_customer_id = response.id - - response = self.maxipago.customer.update( - id=maxipago_customer_id, - customer_id=CUSTOMER_ID, - first_name=u'Antonio', - ) - - def test_add_card(self): - CUSTOMER_ID = randint(1, 100000) - - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name=u'Fulano', - last_name=u'de Tal', - ) - - self.assertTrue(hasattr(response, 'id')) - - maxipago_customer_id = response.id - - response = self.maxipago.card.add( - customer_id=maxipago_customer_id, - number=u'4111111111111111', - expiration_month=u'02', - expiration_year=date.today().year + 3, - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20123456', - billing_country=u'BR', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - ) - - self.assertTrue(getattr(response, 'token', False)) - - def test_delete_card(self): - CUSTOMER_ID = randint(1, 100000) - - customer_response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name=u'Fulano', - last_name=u'de Tal', - ) - - self.assertTrue(hasattr(customer_response, 'id')) - - maxipago_customer_id = customer_response.id - - card_response = self.maxipago.card.add( - customer_id=maxipago_customer_id, - number=u'4111111111111111', - expiration_month=u'02', - expiration_year=date.today().year + 3, - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20345678', - billing_country=u'RJ', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - ) - - self.assertTrue(getattr(card_response, 'token', False)) - - token = card_response.token - - response = self.maxipago.card.delete( - customer_id=maxipago_customer_id, - token=token, - ) - - self.assertTrue(getattr(response, 'success', False)) - - def test_payment_authorize(self): - REFERENCE = randint(1, 100000) - - response = self.maxipago.payment.authorize( - processor_id=payment_processors.TEST, - reference_num=REFERENCE, - - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20345678', - billing_country=u'RJ', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - - card_number='4111111111111111', - card_expiration_month=u'02', - card_expiration_year=date.today().year + 3, - card_cvv='123', - - charge_total='100.00', - ) - - self.assertTrue(response.authorized) - self.assertFalse(response.captured) - - def test_payment_direct(self): - REFERENCE = randint(1, 100000) - - response = self.maxipago.payment.direct( - processor_id=payment_processors.TEST, - reference_num=REFERENCE, - - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20345678', - billing_country=u'RJ', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - - card_number='4111111111111111', - card_expiration_month=u'02', - card_expiration_year=date.today().year + 3, - card_cvv='123', - - charge_total='100.00', - ) - - self.assertTrue(response.authorized) - self.assertTrue(response.captured) - - def test_payment_direct_declined(self): - REFERENCE = randint(1, 100000) - - response = self.maxipago.payment.direct( - processor_id=payment_processors.TEST, - reference_num=REFERENCE, - - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20345678', - billing_country=u'RJ', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - - card_number='4111111111111111', - card_expiration_month=u'02', - card_expiration_year=date.today().year + 3, - card_cvv='123', - - charge_total='100.01', - ) - - self.assertFalse(response.authorized) - self.assertFalse(response.captured) - - def test_payment_direct_with_token(self): - CUSTOMER_ID = randint(1, 100000) - - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name=u'Fulano', - last_name=u'de Tal', - ) - - self.assertTrue(hasattr(response, 'id')) - - maxipago_customer_id = response.id - - response = self.maxipago.card.add( - customer_id=maxipago_customer_id, - number=u'4111111111111111', - expiration_month=u'02', - expiration_year=date.today().year + 3, - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20123456', - billing_country=u'BR', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - ) - - self.assertTrue(getattr(response, 'token', False)) - REFERENCE = randint(1, 100000) - - response = self.maxipago.payment.direct( - processor_id=payment_processors.TEST, - reference_num=REFERENCE, - - customer_id=maxipago_customer_id, - token=response.token, - - charge_total='100.00', - ) - - self.assertTrue(response.authorized) - self.assertTrue(response.captured) - - def test_payment_direct_with_token_decline(self): - CUSTOMER_ID = randint(1, 100000) - - response = self.maxipago.customer.add( - customer_id=CUSTOMER_ID, - first_name=u'Fulano', - last_name=u'de Tal', - ) - - self.assertTrue(hasattr(response, 'id')) - - maxipago_customer_id = response.id - - response = self.maxipago.card.add( - customer_id=maxipago_customer_id, - number=u'4111111111111111', - expiration_month=u'02', - expiration_year=date.today().year + 3, - billing_name=u'Fulano de Tal', - billing_address1=u'Rua das Alamedas, 123', - billing_city=u'Rio de Janeiro', - billing_state=u'RJ', - billing_zip=u'20123456', - billing_country=u'BR', - billing_phone=u'552140634666', - billing_email=u'fulano@detal.com', - ) - - self.assertTrue(getattr(response, 'token', False)) - REFERENCE = randint(1, 100000) - - response = self.maxipago.payment.direct( - processor_id=payment_processors.TEST, - reference_num=REFERENCE, - - customer_id=maxipago_customer_id, - token=response.token, - - charge_total='100.01', - ) - - self.assertFalse(response.authorized) - self.assertFalse(response.captured) - - def test_http_exception(self): - CUSTOMER_ID = randint(1, 100000) - customer_manager = self.maxipago.customer - customer_manager.uri_api = 'https://testapi.maxipago.net/UniversalAPI/WrongUri' - with self.assertRaises(exceptions.HttpErrorException): - customer_manager.add( - customer_id=CUSTOMER_ID, - first_name='Fulano', - last_name='de Tal', - ) - - -if __name__ == '__main__': - unittest.main() +# coding: utf-8 +import os +import unittest +from datetime import date +from maxipago import Maxipago, exceptions +from maxipago.utils import payment_processors +from random import randint + + +MAXIPAGO_ID = os.getenv('MAXIPAGO_ID') +MAXIPAGO_API_KEY = os.getenv('MAXIPAGO_API_KEY') + + +class MaxipagoTestCase(unittest.TestCase): + + def setUp(self): + self.maxipago = Maxipago(MAXIPAGO_ID, MAXIPAGO_API_KEY, sandbox=True) + + def test_add_customer(self): + CUSTOMER_ID = randint(1, 100000) + + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(getattr(response, 'id', False)) + + def test_add_customer_already_existing(self): + CUSTOMER_ID = randint(1, 100000) + + # creating customer with random id. + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(response, 'id')) + + # creating customer with the same id. + with self.assertRaises(exceptions.CustomerAlreadyExists): + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + def test_delete_customer(self): + CUSTOMER_ID = randint(1, 100000) + + # creating customer with random id. + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(response, 'id')) + + maxipago_customer_id = response.id + + response = self.maxipago.customer.delete( + id=maxipago_customer_id, + ) + + def test_update_customer(self): + CUSTOMER_ID = randint(1, 100000) + + # creating customer with random id. + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(response, 'id')) + + maxipago_customer_id = response.id + + response = self.maxipago.customer.update( + id=maxipago_customer_id, + customer_id=CUSTOMER_ID, + first_name='Antonio', + ) + + def test_add_card(self): + CUSTOMER_ID = randint(1, 100000) + + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(response, 'id')) + + maxipago_customer_id = response.id + + response = self.maxipago.card.add( + customer_id=maxipago_customer_id, + number='4111111111111111', + expiration_month='02', + expiration_year=date.today().year + 3, + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20123456', + billing_country='BR', + billing_phone='552140634666', + billing_email='fulano@detal.com', + ) + + self.assertTrue(getattr(response, 'token', False)) + + def test_delete_card(self): + CUSTOMER_ID = randint(1, 100000) + + customer_response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(customer_response, 'id')) + + maxipago_customer_id = customer_response.id + + card_response = self.maxipago.card.add( + customer_id=maxipago_customer_id, + number='4111111111111111', + expiration_month='02', + expiration_year=date.today().year + 3, + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20345678', + billing_country='RJ', + billing_phone='552140634666', + billing_email='fulano@detal.com', + ) + + self.assertTrue(getattr(card_response, 'token', False)) + + token = card_response.token + + response = self.maxipago.card.delete( + customer_id=maxipago_customer_id, + token=token, + ) + + self.assertTrue(getattr(response, 'success', False)) + + def test_payment_authorize(self): + REFERENCE = randint(1, 100000) + + response = self.maxipago.payment.authorize( + processor_id=payment_processors.TEST, + reference_num=REFERENCE, + + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20345678', + billing_country='RJ', + billing_phone='552140634666', + billing_email='fulano@detal.com', + + card_number='4111111111111111', + card_expiration_month='02', + card_expiration_year=date.today().year + 3, + card_cvv='123', + + charge_total='100.00', + ) + + self.assertTrue(response.authorized) + self.assertFalse(response.captured) + + def test_payment_direct(self): + REFERENCE = randint(1, 100000) + + response = self.maxipago.payment.direct( + processor_id=payment_processors.TEST, + reference_num=REFERENCE, + + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20345678', + billing_country='RJ', + billing_phone='552140634666', + billing_email='fulano@detal.com', + + card_number='4111111111111111', + card_expiration_month='02', + card_expiration_year=date.today().year + 3, + card_cvv='123', + + charge_total='100.00', + ) + + self.assertTrue(response.authorized) + self.assertTrue(response.captured) + + def test_payment_direct_declined(self): + REFERENCE = randint(1, 100000) + + response = self.maxipago.payment.direct( + processor_id=payment_processors.TEST, + reference_num=REFERENCE, + + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20345678', + billing_country='RJ', + billing_phone='552140634666', + billing_email='fulano@detal.com', + + card_number='4111111111111111', + card_expiration_month='02', + card_expiration_year=date.today().year + 3, + card_cvv='123', + + charge_total='100.01', + ) + + self.assertFalse(response.authorized) + self.assertFalse(response.captured) + + def test_payment_direct_with_token(self): + CUSTOMER_ID = randint(1, 100000) + + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(response, 'id')) + + maxipago_customer_id = response.id + + response = self.maxipago.card.add( + customer_id=maxipago_customer_id, + number='4111111111111111', + expiration_month='02', + expiration_year=date.today().year + 3, + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20123456', + billing_country='BR', + billing_phone='552140634666', + billing_email='fulano@detal.com', + ) + + self.assertTrue(getattr(response, 'token', False)) + REFERENCE = randint(1, 100000) + + response = self.maxipago.payment.direct( + processor_id=payment_processors.TEST, + reference_num=REFERENCE, + + customer_id=maxipago_customer_id, + token=response.token, + + charge_total='100.00', + ) + + self.assertTrue(response.authorized) + self.assertTrue(response.captured) + + def test_payment_direct_with_token_decline(self): + CUSTOMER_ID = randint(1, 100000) + + response = self.maxipago.customer.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + self.assertTrue(hasattr(response, 'id')) + + maxipago_customer_id = response.id + + response = self.maxipago.card.add( + customer_id=maxipago_customer_id, + number='4111111111111111', + expiration_month='02', + expiration_year=date.today().year + 3, + billing_name='Fulano de Tal', + billing_address1='Rua das Alamedas, 123', + billing_city='Rio de Janeiro', + billing_state='RJ', + billing_zip='20123456', + billing_country='BR', + billing_phone='552140634666', + billing_email='fulano@detal.com', + ) + + self.assertTrue(getattr(response, 'token', False)) + REFERENCE = randint(1, 100000) + + response = self.maxipago.payment.direct( + processor_id=payment_processors.TEST, + reference_num=REFERENCE, + + customer_id=maxipago_customer_id, + token=response.token, + + charge_total='100.01', + ) + + self.assertFalse(response.authorized) + self.assertFalse(response.captured) + + def test_http_exception(self): + CUSTOMER_ID = randint(1, 100000) + customer_manager = self.maxipago.customer + customer_manager.uri_api = 'https://testapi.maxipago.net/UniversalAPI/WrongUri' + with self.assertRaises(exceptions.HttpErrorException): + customer_manager.add( + customer_id=CUSTOMER_ID, + first_name='Fulano', + last_name='de Tal', + ) + + +if __name__ == '__main__': + unittest.main()