Skip to content

Commit

Permalink
Merge branch '0.1.X' into 0.2.X
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcanovas committed Jun 7, 2018
2 parents 81ddb57 + 7e622cd commit 87f9008
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion djpg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
__author__ = 'Rafael Canovas'
__license__ = 'MIT'

from .models import Cart, Item, Notification
from .models import Cart, Item, Transaction, Notification
from .codes import codes
from . import signals
34 changes: 31 additions & 3 deletions djpg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import xmltodict

from django.conf import settings
from django.http import HttpResponseRedirect
from django.core.exceptions import ImproperlyConfigured

from djpg.exceptions import PagSeguroUnauthorizedException, \
Expand All @@ -32,6 +31,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
Expand Down Expand Up @@ -143,13 +147,37 @@ def get_checkout_url(self, code):
return '%s?code=%s' % (PAYMENT_URL, code)


class Transaction(object):
def __init__(self, code):
self.code = code

def __str__(self):
return '<Transaction code=%s>' % self.code

def get_data(self):
endpoint = urljoin(TRANSACTIONS_URL, self.code)
response = requests.get(endpoint, params={
'email': PAGSEGURO_EMAIL,
'token': PAGSEGURO_TOKEN
})
data = xmltodict.parse(response.content)

if response.status_code == 200:
return data
elif response.status_code == 400:
raise PagSeguroInvalidRequestException(
code=data['errors']['error']['code'],
msg=data['errors']['error']['message']
)


class Notification(object):
def __init__(self, code, type=''):
def __init__(self, code, type='transaction'):
self.code = code
self.type = type

def __str__(self):
return '%s (%s)' % (self.code, self.type)
return '<Notification code=%s type=%s>' % (self.code, self.type)

def get_data(self):
endpoint = urljoin(NOTIFICATIONS_URL, self.code)
Expand Down
4 changes: 3 additions & 1 deletion djpg/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

def dispatch_transaction(sender, **kwargs):
transaction = kwargs.pop('transaction')
code = transaction['code']
status = int(transaction['status'])

signals = {
Expand All @@ -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)
9 changes: 5 additions & 4 deletions djpg/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
23 changes: 19 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DjpgTestCase(unittest.TestCase):
def test_entry_points(self):
djpg.Item
djpg.Cart
djpg.Transaction
djpg.Notification
djpg.codes
djpg.signals
Expand Down Expand Up @@ -94,15 +95,29 @@ def test_notification(self):
djpg.Notification('123456')
djpg.Notification('123456', type='transaction')
djpg.Notification(code='123456')
notification = djpg.Notification(code='123456', type='transaction')
n = djpg.Notification(code='123456', type='transaction')

self.assertIsNotNone(str(notification))
self.assertIsNotNone(str(n))

def test_invalid_notification(self):
notification = djpg.Notification('123456')
n = djpg.Notification('123456')

with self.assertRaises(djpg.exceptions.PagSeguroInvalidRequestException):
notification.get_data()
n.get_data()

def test_transaction(self):
djpg.Transaction('123456')
djpg.Transaction(code='123456')

t = djpg.Transaction('123456')

self.assertIsNotNone(str(t))

def test_invalid_transaction(self):
t = djpg.Transaction('123456')

with self.assertRaises(djpg.exceptions.PagSeguroInvalidRequestException):
t.get_data()


if __name__ == '__main__':
Expand Down

0 comments on commit 87f9008

Please sign in to comment.