Exemplo n.º 1
0
    def test_all_payment_search(self):
        created_payments = [
            PagSegPayment(status=STATUS_CREATED) for i in xrange(3)
        ]
        ndb.put_multi(created_payments)
        # reversing because search is desc
        created_payments.reverse()

        sent_payments = [
            PagSegPayment(status=STATUS_SENT_TO_PAGSEGURO) for i in xrange(3)
        ]
        ndb.put_multi(sent_payments)
        sent_payments.reverse()
        # Testing search for all
        cmd = pagseguro_facade.search_all_payments(page_size=3).execute()
        self.assertListEqual(sent_payments, cmd.result)
        cmd2 = pagseguro_facade.search_all_payments(
            page_size=3, start_cursor=cmd.cursor).execute()
        self.assertListEqual(created_payments, cmd2.result)
        cmd3 = pagseguro_facade.search_all_payments(
            page_size=3, start_cursor=cmd2.cursor).execute()
        self.assertListEqual([], cmd3.result)

        # Test search based on status
        cmd = pagseguro_facade.search_all_payments(STATUS_SENT_TO_PAGSEGURO,
                                                   page_size=4).execute()
        self.assertListEqual(sent_payments, cmd.result)
        cmd = pagseguro_facade.search_all_payments(STATUS_CREATED,
                                                   page_size=4).execute()
        self.assertListEqual(created_payments, cmd.result)
Exemplo n.º 2
0
    def test_success(self, UrlFetchClassMock):
        # Mocking pagseguro Fetch
        UrlFetchClassMock.return_value = _build_mock()

        # Mocking previous command
        data_cmd_mock = Mock()
        data_cmd_mock.access_data.email = '*****@*****.**'
        data_cmd_mock.access_data.token = '4567890oiuytfgh'
        reference0 = ItemReferenceMock()
        reference1 = ItemReferenceMock()
        items = [PagSegItem(description='Python Course', price=Decimal('120'), quantity=1, reference=reference0.key),
                 PagSegItem(description='Another Python Course', price=Decimal('240'), quantity=2,
                            reference=reference1.key)]
        ndb.put_multi(items)
        data_cmd_mock.items = items
        data_cmd_mock.client_form = ValidateClientCmd(email='*****@*****.**', name='Jhon Doe').form
        data_cmd_mock.address_form = pagseguro_facade.validate_address_cmd('Rua 1', '2', 'meu bairro', '12345678',
                                                                              'São Paulo', 'SP', 'apto 4').form

        payment = PagSegPayment()
        payment.put()
        data_cmd_mock.result = payment

        contact_pagseguro_cmd = ContactPagseguro('https://store.com/pagseguro')
        CommandSequential(data_cmd_mock, contact_pagseguro_cmd).execute()

        self.assertEqual(payment, contact_pagseguro_cmd.result)
        self.assertEqual(payment.status, STATUS_SENT_TO_PAGSEGURO)
        self.assertEqual(contact_pagseguro_cmd.checkout_code, _SUCCESS_PAGSEGURO_CODE,
                         'Should have the code extracted from xml _PAGSEGURO_DETAIL_XML')
        self.assertIsNone(payment.code)
Exemplo n.º 3
0
 def __init__(self, payment_status, page_size=20, start_cursor=None, offset=0, use_cache=True, cache_begin=True,
              relations=None,
              **kwargs):
     query = PagSegPayment.query(PagSegPayment.status == payment_status).order(-PagSegPayment.update)
     super(PaymentsByStatusSearch, self).__init__(query, page_size, start_cursor, offset, use_cache, cache_begin,
                                                  relations=relations,
                                                  **kwargs)
Exemplo n.º 4
0
    def do_business(self):
        super(ProcessExternalPaymentCmd, self).do_business()
        dct = self.__transaction_dct
        if self.result:
            self.result.status = XML_STATUS_TO_MODEL_STATUS.get(dct['transaction']['status'])
            cmd = UpdatePaymentAndSaveLog(self.result)
            cmd.execute()
        else:
            self.result = PagSegPayment(code=dct['transaction']['code'],
                                        status=XML_STATUS_TO_MODEL_STATUS.get(dct['transaction']['status']),
                                        total=dct['transaction']['grossAmount'],
                                        net_amount=dct['transaction']['netAmount'])
            payment_key = self.result.put()


            def create_item_cmd(item):
                return _SimpleSave(PagSegItem(description=item['description'],
                                              price=Decimal(item['amount']),
                                              quantity=int(item['quantity'])))

            items = dct['transaction']['items']['item']
            if isinstance(items, dict):
                items = [items]
            items_cmd = [CreatePaymentToItem(payment_key, create_item_cmd(item)) for item in
                         items]
            cmd = CommandParallel(
                CreatePagSegPaymentToLog(payment_key, _SimpleSave(PagSegLog(status=self.result.status))), *items_cmd)

            sender = dct['transaction'].get('sender')
            if sender:
                user = facade.get_user_by_email(sender['email'])()
                if user is None:
                    user = facade.save_user_cmd(sender['email'], sender['name'])()
                cmd.append(SaveToPayment(user,payment_key))
            cmd.execute()
Exemplo n.º 5
0
    def test_save_pagseguro_data(self):
        access_data = CreateOrUpdateAccessData('*****@*****.**', 'abc1234')()
        command_with_item_forms = Mock()
        command_with_item_forms.items = [PagSegItem(description='Python Birds', price=Decimal('18.99'), quantity=3),
                                         PagSegItem(description='Objetos Pythônicos', price=Decimal('24.99'),
                                                    quantity=2,
                                                    reference=ndb.Key(PagSegItem, 10))]
        address_form_mock = Mock()
        client_form_mock = Mock()
        command_with_item_forms.address_form = address_form_mock
        command_with_item_forms.client_form = client_form_mock
        command_with_item_forms.access_data = access_data

        save_pagseguro_data_cmd = SavePagseguroDataCmd()
        CommandSequential(command_with_item_forms, save_pagseguro_data_cmd).execute()

        items = PagSegItem.query().fetch()
        self.assertEqual(2, len(items))
        self.assertEqual(set(i.key for i in items), set(i.key for i in save_pagseguro_data_cmd.items))
        payment = PagSegPayment.query().get()
        self.assertIsNotNone(payment)
        self.assertEqual(payment, save_pagseguro_data_cmd.result)
        self.assertEqual(Decimal('106.95'), save_pagseguro_data_cmd.result.total,
                         'Total should be the some of the total of items')
        self.assertEqual('*****@*****.**', save_pagseguro_data_cmd.access_data.email)
        self.assertEqual('abc1234', save_pagseguro_data_cmd.access_data.token)

        # Data used for sequential commands
        self.assertEqual(save_pagseguro_data_cmd.address_form, address_form_mock)
        self.assertEqual(save_pagseguro_data_cmd.client_form, client_form_mock)
Exemplo n.º 6
0
    def do_business(self):
        super(ProcessExternalPaymentCmd, self).do_business()
        dct = self.__transaction_dct
        if self.result:
            self.result.status = XML_STATUS_TO_MODEL_STATUS.get(
                dct['transaction']['status'])
            cmd = UpdatePaymentAndSaveLog(self.result)
            cmd.execute()
        else:
            self.result = PagSegPayment(
                code=dct['transaction']['code'],
                status=XML_STATUS_TO_MODEL_STATUS.get(
                    dct['transaction']['status']),
                total=dct['transaction']['grossAmount'],
                net_amount=dct['transaction']['netAmount'])
            payment_key = self.result.put()

            def create_item_cmd(item):
                return _SimpleSave(
                    PagSegItem(description=item['description'],
                               price=Decimal(item['amount']),
                               quantity=int(item['quantity'])))

            items = dct['transaction']['items']['item']
            if isinstance(items, dict):
                items = [items]
            items_cmd = [
                CreatePaymentToItem(payment_key, create_item_cmd(item))
                for item in items
            ]
            cmd = CommandParallel(
                CreatePagSegPaymentToLog(
                    payment_key,
                    _SimpleSave(PagSegLog(status=self.result.status))),
                *items_cmd)

            sender = dct['transaction'].get('sender')
            if sender:
                user = facade.get_user_by_email(sender['email'])()
                if user is None:
                    user = facade.save_user_cmd(sender['email'],
                                                sender['name'])()
                cmd.append(SaveToPayment(user, payment_key))
            cmd.execute()
Exemplo n.º 7
0
    def test_credit_card_reader_payment(self):
        # Payment creation
        cmd = pagseguro_facade.procces_external_payment_cmd(
            create_credit_card_xml())
        cmd()
        payment = PagSegPayment.query().get()
        self.assertEqual('6F09ACBC-BEC1-463F-9174-5E0CF5BE33F1', payment.code)
        self.assertEqual(Decimal('1.00'), payment.total)
        self.assertEqual(Decimal('0.98'), payment.net_amount)
        self.assertEqual(STATUS_AVAILABLE, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertEqual(1, len(logs))
        self.assertEqual(STATUS_AVAILABLE, logs[0].status)
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(1, len(items))
        item = items[0]
        self.assertEqual(Decimal('1.00'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Venda pelo celular com leitor de chip e senha',
                         item.description)

        # Payment update
        cmd = pagseguro_facade.procces_external_payment_cmd(
            create_credit_card_xml(7))
        cmd()
        payments = PagSegPayment.query().fetch()
        self.assertEqual(1, len(payments))
        payment = payments[0]

        self.assertEqual('6F09ACBC-BEC1-463F-9174-5E0CF5BE33F1', payment.code)
        self.assertEqual(Decimal('1.00'), payment.total)
        self.assertEqual(Decimal('0.98'), payment.net_amount)
        self.assertEqual(STATUS_CANCELLED, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertListEqual([STATUS_AVAILABLE, STATUS_CANCELLED],
                             [log.status for log in logs])
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(1, len(items))
        item = items[0]
        self.assertEqual(Decimal('1.00'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Venda pelo celular com leitor de chip e senha',
                         item.description)
Exemplo n.º 8
0
    def test_success(self, UrlFetchClassMock):
        # Mocking pagseguro Fetch
        UrlFetchClassMock.return_value = _build_mock()

        # Mocking previous command
        data_cmd_mock = Mock()
        data_cmd_mock.access_data.email = '*****@*****.**'
        data_cmd_mock.access_data.token = '4567890oiuytfgh'
        reference0 = ItemReferenceMock()
        reference1 = ItemReferenceMock()
        items = [
            PagSegItem(description='Python Course',
                       price=Decimal('120'),
                       quantity=1,
                       reference=reference0.key),
            PagSegItem(description='Another Python Course',
                       price=Decimal('240'),
                       quantity=2,
                       reference=reference1.key)
        ]
        ndb.put_multi(items)
        data_cmd_mock.items = items
        data_cmd_mock.client_form = ValidateClientCmd(email='*****@*****.**',
                                                      name='Jhon Doe').form
        data_cmd_mock.address_form = pagseguro_facade.validate_address_cmd(
            'Rua 1', '2', 'meu bairro', '12345678', 'São Paulo', 'SP',
            'apto 4').form

        payment = PagSegPayment()
        payment.put()
        data_cmd_mock.result = payment

        contact_pagseguro_cmd = ContactPagseguro('https://store.com/pagseguro')
        CommandSequential(data_cmd_mock, contact_pagseguro_cmd).execute()

        self.assertEqual(payment, contact_pagseguro_cmd.result)
        self.assertEqual(payment.status, STATUS_SENT_TO_PAGSEGURO)
        self.assertEqual(
            contact_pagseguro_cmd.checkout_code, _SUCCESS_PAGSEGURO_CODE,
            'Should have the code extracted from xml _PAGSEGURO_DETAIL_XML')
        self.assertIsNone(payment.code)
Exemplo n.º 9
0
    def test_integration_button_payment(self):
        # Payment creation
        cmd = pagseguro_facade.procces_external_payment_cmd(
            generate_xml('non particular id', '1', '18.99'))
        cmd()
        payment = PagSegPayment.query().get()
        self.assertEqual(CODE, payment.code)
        self.assertEqual(Decimal('49900'), payment.total)
        self.assertEqual(Decimal('18.99'), payment.net_amount)
        self.assertEqual(STATUS_SENT_TO_PAGSEGURO, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertEqual(1, len(logs))
        self.assertEqual(STATUS_SENT_TO_PAGSEGURO, logs[0].status)
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(2, len(items))
        item = items[0]
        self.assertEqual(Decimal('24300'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Notebook Prata', item.description)

        # Payment update
        cmd = pagseguro_facade.procces_external_payment_cmd(
            generate_xml('non particular id', '7', '18.99'))
        cmd()
        payments = PagSegPayment.query().fetch()
        self.assertEqual(1, len(payments))
        payment = payments[0]

        self.assertEqual(CODE, payment.code)
        self.assertEqual(Decimal('49900'), payment.total)
        self.assertEqual(Decimal('18.99'), payment.net_amount)
        self.assertEqual(STATUS_CANCELLED, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertListEqual([STATUS_SENT_TO_PAGSEGURO, STATUS_CANCELLED],
                             [log.status for log in logs])
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(2, len(items))
        item = items[0]
        self.assertEqual(Decimal('24300'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Notebook Prata', item.description)
Exemplo n.º 10
0
 def __init__(self,
              page_size=20,
              start_cursor=None,
              offset=0,
              use_cache=True,
              cache_begin=True,
              relations=None,
              **kwargs):
     query = PagSegPayment.query().order(-PagSegPayment.update)
     super(AllPaymentsSearch,
           self).__init__(query, page_size, start_cursor, offset, use_cache,
                          cache_begin, relations, **kwargs)
Exemplo n.º 11
0
    def test_credit_card_reader_payment(self):
        # Payment creation
        cmd = pagseguro_facade.procces_external_payment_cmd(create_credit_card_xml())
        cmd()
        payment = PagSegPayment.query().get()
        self.assertEqual('6F09ACBC-BEC1-463F-9174-5E0CF5BE33F1', payment.code)
        self.assertEqual(Decimal('1.00'), payment.total)
        self.assertEqual(Decimal('0.98'), payment.net_amount)
        self.assertEqual(STATUS_AVAILABLE, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertEqual(1, len(logs))
        self.assertEqual(STATUS_AVAILABLE, logs[0].status)
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(1, len(items))
        item = items[0]
        self.assertEqual(Decimal('1.00'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Venda pelo celular com leitor de chip e senha', item.description)

        # Payment update
        cmd = pagseguro_facade.procces_external_payment_cmd(create_credit_card_xml(7))
        cmd()
        payments = PagSegPayment.query().fetch()
        self.assertEqual(1, len(payments))
        payment = payments[0]

        self.assertEqual('6F09ACBC-BEC1-463F-9174-5E0CF5BE33F1', payment.code)
        self.assertEqual(Decimal('1.00'), payment.total)
        self.assertEqual(Decimal('0.98'), payment.net_amount)
        self.assertEqual(STATUS_CANCELLED, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertListEqual([STATUS_AVAILABLE, STATUS_CANCELLED], [log.status for log in logs])
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(1, len(items))
        item = items[0]
        self.assertEqual(Decimal('1.00'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Venda pelo celular com leitor de chip e senha', item.description)
Exemplo n.º 12
0
    def test_integration_button_payment(self):
        # Payment creation
        cmd = pagseguro_facade.procces_external_payment_cmd(generate_xml('non particular id', '1', '18.99'))
        cmd()
        payment = PagSegPayment.query().get()
        self.assertEqual(CODE, payment.code)
        self.assertEqual(Decimal('49900'), payment.total)
        self.assertEqual(Decimal('18.99'), payment.net_amount)
        self.assertEqual(STATUS_SENT_TO_PAGSEGURO, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertEqual(1, len(logs))
        self.assertEqual(STATUS_SENT_TO_PAGSEGURO, logs[0].status)
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(2, len(items))
        item = items[0]
        self.assertEqual(Decimal('24300'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Notebook Prata', item.description)

        # Payment update
        cmd = pagseguro_facade.procces_external_payment_cmd(generate_xml('non particular id', '7', '18.99'))
        cmd()
        payments = PagSegPayment.query().fetch()
        self.assertEqual(1, len(payments))
        payment = payments[0]

        self.assertEqual(CODE, payment.code)
        self.assertEqual(Decimal('49900'), payment.total)
        self.assertEqual(Decimal('18.99'), payment.net_amount)
        self.assertEqual(STATUS_CANCELLED, payment.status)
        logs = pagseguro_facade.search_logs(payment)()
        self.assertListEqual([STATUS_SENT_TO_PAGSEGURO, STATUS_CANCELLED], [log.status for log in logs])
        items = pagseguro_facade.search_items(payment)()
        self.assertEqual(2, len(items))
        item = items[0]
        self.assertEqual(Decimal('24300'), item.price)
        self.assertEqual(1, item.quantity)
        self.assertEqual('Notebook Prata', item.description)
Exemplo n.º 13
0
    def test_save_pagseguro_data(self):
        access_data = CreateOrUpdateAccessData('*****@*****.**', 'abc1234')()
        command_with_item_forms = Mock()
        command_with_item_forms.items = [
            PagSegItem(description='Python Birds',
                       price=Decimal('18.99'),
                       quantity=3),
            PagSegItem(description='Objetos Pythônicos',
                       price=Decimal('24.99'),
                       quantity=2,
                       reference=ndb.Key(PagSegItem, 10))
        ]
        address_form_mock = Mock()
        client_form_mock = Mock()
        command_with_item_forms.address_form = address_form_mock
        command_with_item_forms.client_form = client_form_mock
        command_with_item_forms.access_data = access_data

        save_pagseguro_data_cmd = SavePagseguroDataCmd()
        CommandSequential(command_with_item_forms,
                          save_pagseguro_data_cmd).execute()

        items = PagSegItem.query().fetch()
        self.assertEqual(2, len(items))
        self.assertEqual(set(i.key for i in items),
                         set(i.key for i in save_pagseguro_data_cmd.items))
        payment = PagSegPayment.query().get()
        self.assertIsNotNone(payment)
        self.assertEqual(payment, save_pagseguro_data_cmd.result)
        self.assertEqual(Decimal('106.95'),
                         save_pagseguro_data_cmd.result.total,
                         'Total should be the some of the total of items')
        self.assertEqual('*****@*****.**',
                         save_pagseguro_data_cmd.access_data.email)
        self.assertEqual('abc1234', save_pagseguro_data_cmd.access_data.token)

        # Data used for sequential commands
        self.assertEqual(save_pagseguro_data_cmd.address_form,
                         address_form_mock)
        self.assertEqual(save_pagseguro_data_cmd.client_form, client_form_mock)
Exemplo n.º 14
0
 def test_get_payment_search(self):
     created_payment = PagSegPayment(status=STATUS_CREATED)
     created_payment.put()
     comand = GetPayment(created_payment.key.id())
     payment = comand()
     self.assertEqual(created_payment, payment)
Exemplo n.º 15
0
 def __init__(self, transaction_code, relations=None,
              **kwargs):
     super(PaymentByPagseguroCode, self).__init__(PagSegPayment.query(PagSegPayment.code == transaction_code),
                                                  page_size=1, relations=relations,
                                                  **kwargs)
Exemplo n.º 16
0
 def __init__(self, transaction_code, relations=None, **kwargs):
     super(PaymentByPagseguroCode, self).__init__(
         PagSegPayment.query(PagSegPayment.code == transaction_code),
         page_size=1,
         relations=relations,
         **kwargs)
Exemplo n.º 17
0
 def __init__(self):
     super(SavePagseguroDataCmd, self).__init__()
     self._create_attributes()
     self.result = PagSegPayment()
Exemplo n.º 18
0
 def test_get_payment_search(self):
     created_payment = PagSegPayment(status=STATUS_CREATED)
     created_payment.put()
     comand = GetPayment(created_payment.key.id())
     payment = comand()
     self.assertEqual(created_payment, payment)