Esempio n. 1
0
def receive_notification(request):
    notification_code = request.POST.get('notificationCode', None)
    notification_type = request.POST.get('notificationType', None)

    if notification_code and notification_type == 'transaction':
        pagseguro_api = PagSeguroApi()
        response = pagseguro_api.get_notification(notification_code)

        if response.status_code == 200:
            return HttpResponse(six.b('Notificação recebida com sucesso.'))

    return HttpResponse(six.b('Notificação inválida.'), status=400)
Esempio n. 2
0
def receive_notification(request):
    notification_code = request.POST.get("notificationCode", None)
    notification_type = request.POST.get("notificationType", None)

    if notification_code and notification_type == "transaction":
        pagseguro_api = PagSeguroApi()
        response = pagseguro_api.get_notification(notification_code)

        if response.status_code == 200:
            return HttpResponse("Notificação recebida com sucesso.")

    return HttpResponse("Notificação inválida.", status=400)
Esempio n. 3
0
def receive_notification_aline(request):
    notification_code = request.POST.get('notificationCode', None)
    notification_type = request.POST.get('notificationType', None)

    if notification_type == 'transaction':
        pagseguro_api = PagSeguroApi(setting='aline')
        response = pagseguro_api.get_notification(notification_code)

        if response.status_code == 200:
            if six.PY2:
                return HttpResponse(six.b('Notificação recebida com sucesso.'))
            return HttpResponse(six.u('Notificação recebida com sucesso.'))

    if six.PY2:
        return HttpResponse(six.b('Notificação inválida.'), status=400)
    return HttpResponse(six.u('Notificação inválida.'), status=400)
Esempio n. 4
0
class PagSeguroApiTest(TestCase):
    def setUp(self):
        self.item1 = PagSeguroItem(id='1',
                                   description='My item 1',
                                   amount='10.00',
                                   quantity=1)
        self.item2 = PagSeguroItem(id='2',
                                   description='My item 2',
                                   amount='10.00',
                                   quantity=1)
        self.item3 = PagSeguroItem(id='3',
                                   description='My item 3',
                                   amount='10.00',
                                   quantity=1,
                                   shipping_cost='10.00',
                                   weight=300)
        self.pagseguro_api = PagSeguroApi()

    def test_add_item(self):
        self.pagseguro_api.add_item(self.item1)
        self.assertIn(self.item1, self.pagseguro_api.get_items())

        self.pagseguro_api.add_item(self.item2)
        self.assertIn(self.item1, self.pagseguro_api.get_items())
        self.assertIn(self.item2, self.pagseguro_api.get_items())

        self.pagseguro_api.add_item(self.item3)
        self.assertIn(self.item1, self.pagseguro_api.get_items())
        self.assertIn(self.item2, self.pagseguro_api.get_items())
        self.assertIn(self.item3, self.pagseguro_api.get_items())

    def test_clear_items(self):
        self.pagseguro_api.add_item(self.item1)
        self.pagseguro_api.add_item(self.item2)
        self.pagseguro_api.add_item(self.item3)
        self.pagseguro_api.clear_items()
        self.assertNotIn(self.item1, self.pagseguro_api.get_items())
        self.assertNotIn(self.item2, self.pagseguro_api.get_items())
        self.assertNotIn(self.item3, self.pagseguro_api.get_items())

    @httpretty.activate
    def test_invalid_checkout(self):
        self.pagseguro_api.add_item(self.item1)
        self.pagseguro_api.add_item(self.item2)
        self.pagseguro_api.add_item(self.item3)

        # mock requests
        httpretty.register_uri(
            httpretty.POST,
            CHECKOUT_URL,
            body='Unauthorized',
            status=401,
        )

        data = self.pagseguro_api.checkout()
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'Unauthorized')
        self.assertEqual(data['status_code'], 401)

    @httpretty.activate
    def test_valid_checkout(self):
        self.pagseguro_api.add_item(self.item1)
        self.pagseguro_api.add_item(self.item2)
        self.pagseguro_api.add_item(self.item3)

        # mock requests
        httpretty.register_uri(
            httpretty.POST,
            CHECKOUT_URL,
            body=checkout_response_xml,
            status=200,
        )

        data = self.pagseguro_api.checkout()
        self.assertEqual(data['success'], True)
        self.assertEqual(data['status_code'], 200)
        self.assertEqual(data['code'], '67DB59D3BDBD84EAA4396F929DB350A7')
        self.assertEqual(data['date'], parse('2014-06-07T00:52:04.000-03:00'))
        self.assertEqual(data['redirect_url'],
                         '{0}?code={1}'.format(PAYMENT_URL, data['code']))

    @httpretty.activate
    def test_get_invalid_notification(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            NOTIFICATION_URL +
            '/{0}'.format('A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948'),
            body='Unauthorized',
            status=401,
        )

        response = self.pagseguro_api.get_notification(
            'A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948')
        self.assertEqual(response.status_code, 401)
        self.assertEqual(response.text, 'Unauthorized')

    @httpretty.activate
    def test_get_valid_notification(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            NOTIFICATION_URL +
            '/{0}'.format('A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948'),
            body=notification_response_xml,
            status=200,
        )

        response = self.pagseguro_api.get_notification(
            'A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948')
        self.assertEqual(response.status_code, 200)

    @httpretty.activate
    def test_get_invalid_transaction(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            TRANSACTION_URL +
            '/{0}'.format('9E884542-81B3-4419-9A75-BCC6FB495EF1'),
            body='Unauthorized',
            status=401,
        )

        data = self.pagseguro_api.get_transaction(
            '9E884542-81B3-4419-9A75-BCC6FB495EF1')
        self.assertEqual(data['status_code'], 401)
        self.assertEqual(data['message'], 'Unauthorized')

    @httpretty.activate
    def test_get_valid_transaction(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            TRANSACTION_URL +
            '/{0}'.format('9E884542-81B3-4419-9A75-BCC6FB495EF1'),
            body=transaction_response_xml,
            status=200,
        )

        data = self.pagseguro_api.get_transaction(
            '9E884542-81B3-4419-9A75-BCC6FB495EF1')
        self.assertEqual(data['status_code'], 200)
        self.assertEqual(data['transaction']['code'],
                         '9E884542-81B3-4419-9A75-BCC6FB495EF1')

    def test_base_params_is_instance_variable(self):
        # Regression test
        api1 = PagSeguroApi()
        api2 = PagSeguroApi()
        self.assertIsNot(api1.base_params, api2.base_params)

    def test_params_is_instance_variable(self):
        # Regression test
        api1 = PagSeguroApi()
        api2 = PagSeguroApi()
        self.assertIsNot(api1.params, api2.params)

    def test_items_is_instance_variable(self):
        # Regression test
        api1 = PagSeguroApi()
        api2 = PagSeguroApi()
        self.assertIsNot(api1.items, api2.items)
class PagSeguroApiTest(TestCase):

    def setUp(self):
        self.item1 = PagSeguroItem(
            id='1',
            description='My item 1',
            amount='10.00',
            quantity=1
        )
        self.item2 = PagSeguroItem(
            id='2',
            description='My item 2',
            amount='10.00',
            quantity=1
        )
        self.item3 = PagSeguroItem(
            id='3',
            description='My item 3',
            amount='10.00',
            quantity=1,
            shipping_cost='10.00',
            weight=300
        )
        self.pagseguro_api = PagSeguroApi()

    def test_add_item(self):
        self.pagseguro_api.add_item(self.item1)
        self.assertIn(self.item1, self.pagseguro_api.get_items())

        self.pagseguro_api.add_item(self.item2)
        self.assertIn(self.item1, self.pagseguro_api.get_items())
        self.assertIn(self.item2, self.pagseguro_api.get_items())

        self.pagseguro_api.add_item(self.item3)
        self.assertIn(self.item1, self.pagseguro_api.get_items())
        self.assertIn(self.item2, self.pagseguro_api.get_items())
        self.assertIn(self.item3, self.pagseguro_api.get_items())

    def test_clear_items(self):
        self.pagseguro_api.add_item(self.item1)
        self.pagseguro_api.add_item(self.item2)
        self.pagseguro_api.add_item(self.item3)
        self.pagseguro_api.clear_items()
        self.assertNotIn(self.item1, self.pagseguro_api.get_items())
        self.assertNotIn(self.item2, self.pagseguro_api.get_items())
        self.assertNotIn(self.item3, self.pagseguro_api.get_items())

    @httpretty.activate
    def test_invalid_checkout(self):
        self.pagseguro_api.add_item(self.item1)
        self.pagseguro_api.add_item(self.item2)
        self.pagseguro_api.add_item(self.item3)

        # mock requests
        httpretty.register_uri(
            httpretty.POST,
            CHECKOUT_URL,
            body='Unauthorized',
            status=401,
        )

        data = self.pagseguro_api.checkout()
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'Unauthorized')
        self.assertEqual(data['status_code'], 401)

    @httpretty.activate
    def test_valid_checkout(self):
        self.pagseguro_api.add_item(self.item1)
        self.pagseguro_api.add_item(self.item2)
        self.pagseguro_api.add_item(self.item3)

        # mock requests
        httpretty.register_uri(
            httpretty.POST,
            CHECKOUT_URL,
            body=checkout_response_xml,
            status=200,
        )

        data = self.pagseguro_api.checkout()
        self.assertEqual(data['success'], True)
        self.assertEqual(data['status_code'], 200)
        self.assertEqual(data['code'], '67DB59D3BDBD84EAA4396F929DB350A7')
        self.assertEqual(data['date'], parse('2014-06-07T00:52:04.000-03:00'))
        self.assertEqual(
            data['redirect_url'],
            '{0}?code={1}'.format(PAYMENT_URL, data['code'])
        )

    @httpretty.activate
    def test_get_invalid_notification(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            NOTIFICATION_URL + '/{0}'.format(
                'A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948'
            ),
            body='Unauthorized',
            status=401,
        )

        response = self.pagseguro_api.get_notification(
            'A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948'
        )
        self.assertEqual(response.status_code, 401)
        self.assertEqual(response.text, 'Unauthorized')

    @httpretty.activate
    def test_get_valid_notification(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            NOTIFICATION_URL + '/{0}'.format(
                'A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948'
            ),
            body=notification_response_xml,
            status=200,
        )

        response = self.pagseguro_api.get_notification(
            'A5182C-C9EF48EF48D2-1FF4AF6FAC82-EB2948'
        )
        self.assertEqual(response.status_code, 200)

    @httpretty.activate
    def test_get_invalid_transaction(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            TRANSACTION_URL + '/{0}'.format(
                '9E884542-81B3-4419-9A75-BCC6FB495EF1'
            ),
            body='Unauthorized',
            status=401,
        )

        data = self.pagseguro_api.get_transaction(
            '9E884542-81B3-4419-9A75-BCC6FB495EF1'
        )
        self.assertEqual(data['status_code'], 401)
        self.assertEqual(data['message'], 'Unauthorized')

    @httpretty.activate
    def test_get_valid_transaction(self):
        # mock requests
        httpretty.register_uri(
            httpretty.GET,
            TRANSACTION_URL + '/{0}'.format(
                '9E884542-81B3-4419-9A75-BCC6FB495EF1'
            ),
            body=transaction_response_xml,
            status=200,
        )

        data = self.pagseguro_api.get_transaction(
            '9E884542-81B3-4419-9A75-BCC6FB495EF1'
        )
        self.assertEqual(data['status_code'], 200)
        self.assertEqual(
            data['transaction']['code'],
            '9E884542-81B3-4419-9A75-BCC6FB495EF1'
        )

    def test_base_params_is_instance_variable(self):
        # Regression test
        api1 = PagSeguroApi()
        api2 = PagSeguroApi()
        self.assertIsNot(api1.base_params, api2.base_params)

    def test_params_is_instance_variable(self):
        # Regression test
        api1 = PagSeguroApi()
        api2 = PagSeguroApi()
        self.assertIsNot(api1.params, api2.params)

    def test_items_is_instance_variable(self):
        # Regression test
        api1 = PagSeguroApi()
        api2 = PagSeguroApi()
        self.assertIsNot(api1.items, api2.items)