From 3ea238fa922b5ececee0a646c8e5af471b898129 Mon Sep 17 00:00:00 2001 From: Skyen Hasus Date: Fri, 28 Jul 2017 01:22:29 -0400 Subject: [PATCH 1/5] Support for Django >1.6 --- djpg/urls.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/djpg/urls.py b/djpg/urls.py index 1f6f4d7..bdd08f9 100644 --- a/djpg/urls.py +++ b/djpg/urls.py @@ -1,5 +1,6 @@ -from django.conf.urls import patterns, url +from django.conf.urls import url +from .views import notifications -urlpatterns = patterns('djpg.views', - url(r'notifications/$', 'notifications', name='pagseguro_notifications'), -) +urlpatterns = [ + url(r'notifications/$', notifications, name='pagseguro_notifications'), +] From 081c43821831b9083b9aaa917780bdb8f7eaee67 Mon Sep 17 00:00:00 2001 From: Skyen Hasus Date: Fri, 28 Jul 2017 03:00:15 -0400 Subject: [PATCH 2/5] Allow requesting of transaction data by code with Transaction class --- djpg/__init__.py | 2 +- djpg/models.py | 22 ++++++++++++++++++++++ djpg/signals.py | 4 +++- djpg/views.py | 6 ++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/djpg/__init__.py b/djpg/__init__.py index 703fa5c..464baf9 100644 --- a/djpg/__init__.py +++ b/djpg/__init__.py @@ -3,6 +3,6 @@ __author__ = 'Rafael Canovas' __license__ = 'MIT' -from .models import Cart, Item +from .models import Cart, Item, Transaction from .codes import codes from . import signals diff --git a/djpg/models.py b/djpg/models.py index e2318aa..369be13 100644 --- a/djpg/models.py +++ b/djpg/models.py @@ -32,6 +32,11 @@ if PAGSEGURO_SANDBOX else 'https://pagseguro.uol.com.br/v2/checkout/payment.html' ) +TRANSACTIONS_URL = ( + 'https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/' + if PAGSEGURO_SANDBOX else + 'https://ws.pagseguro.uol.com.br/v2/transactions/' +) NOTIFICATIONS_URL = ( 'https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/notifications/' if PAGSEGURO_SANDBOX else @@ -145,6 +150,23 @@ def proceed(self, code): return HttpResponseRedirect(endpoint) +class Transaction(object): + def __init__(self, code): + self.code = code + + def fetch_content(self): + endpoint = urljoin(TRANSACTIONS_URL, self.code) + params = { + 'email': PAGSEGURO_EMAIL, + 'token': PAGSEGURO_TOKEN + } + + r = requests.get(endpoint, params=params) + + if r.status_code == 200: + return xmltodict.parse(r.content, encoding='ISO-8859-1') + + class Notification(object): def __init__(self, type, code): self.type = type diff --git a/djpg/signals.py b/djpg/signals.py index 8ec0892..8f39abf 100644 --- a/djpg/signals.py +++ b/djpg/signals.py @@ -19,6 +19,7 @@ def dispatch_transaction(sender, **kwargs): transaction = kwargs.pop('transaction') + code = int(transaction['code']) status = int(transaction['status']) signals = { @@ -35,6 +36,7 @@ def dispatch_transaction(sender, **kwargs): .get(status, transaction_unknown) \ .send(sender=None, transaction=transaction) - logger.info('Transaction with status "%s" dispatched' % (status,)) + logger.info('Transaction with status "%s" and code "%s" dispatched' + % (status, code)) transaction_received.connect(dispatch_transaction) diff --git a/djpg/views.py b/djpg/views.py index 8bdb05b..b225e7f 100644 --- a/djpg/views.py +++ b/djpg/views.py @@ -14,8 +14,14 @@ def notifications(request): try: notification_type = request.POST['notificationType'] + except KeyError: + logger.info('Notification received without "notificationType"') + return HttpResponseBadRequest() + + try: notification_code = request.POST['notificationCode'] except KeyError: + logger.info('Notification received without "notificationCode"') return HttpResponseBadRequest() logger.info('Notification with type "%s" and code "%s" received' From b49c40dddb5b90881f9f86872cb6a3b6044e1637 Mon Sep 17 00:00:00 2001 From: Skyen Hasus Date: Fri, 28 Jul 2017 16:49:41 -0400 Subject: [PATCH 3/5] Fix bug when trying to convert transaction code to int --- djpg/signals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djpg/signals.py b/djpg/signals.py index 8f39abf..fe40f3f 100644 --- a/djpg/signals.py +++ b/djpg/signals.py @@ -19,7 +19,7 @@ def dispatch_transaction(sender, **kwargs): transaction = kwargs.pop('transaction') - code = int(transaction['code']) + code = transaction['code'] status = int(transaction['status']) signals = { From c49643db7de1d44ee56edbf68ea3ee12467e887c Mon Sep 17 00:00:00 2001 From: Skyen Hasus Date: Sun, 30 Jul 2017 22:59:44 -0400 Subject: [PATCH 4/5] Remove logger message for notifications from the old PagSeguro API --- djpg/views.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/djpg/views.py b/djpg/views.py index b225e7f..8bdb05b 100644 --- a/djpg/views.py +++ b/djpg/views.py @@ -14,14 +14,8 @@ def notifications(request): try: notification_type = request.POST['notificationType'] - except KeyError: - logger.info('Notification received without "notificationType"') - return HttpResponseBadRequest() - - try: notification_code = request.POST['notificationCode'] except KeyError: - logger.info('Notification received without "notificationCode"') return HttpResponseBadRequest() logger.info('Notification with type "%s" and code "%s" received' From 7e622cdc5f9bbeff63ecdb28e0111581971e2472 Mon Sep 17 00:00:00 2001 From: Rafael Canovas Date: Thu, 7 Jun 2018 12:30:30 -0300 Subject: [PATCH 5/5] 0.1.6 release --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 5a8fdb0..081846c 100755 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ setup( name='djpg', - version='0.1.5', + version='0.1.6', description='djpg is a Django module that integrates with the online payment service PagSeguro.', long_description=long_description, author='Rafael Canovas',