Notification data PagSeguro

0

I'm trying to integrate PagSeguro with this lib:

https://github.com/rochacbruno/python-pagseguro

But, I can not access the notification data that Pagseguro sends me. I'm using the following code:

notification_code = request.POST['notificationCode']
pg = PagSeguro(email="[email protected]", token="token")
notification_data = pg.check_notification(notification_code)
print notification_data['status']

On the last line I get the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'PagSeguroNotificationResponse' object has no attribute '__getitem__'
    
asked by anonymous 10.06.2014 / 21:31

2 answers

1

As I read from the library, the check_notification method returns an object of type PagSeguroNotificationResponse .

Note the parse_xml method of this class:

def parse_xml(self, xml):
    try:
        parsed = xmltodict.parse(xml, encoding="iso-8859-1")
    except Exception as e:
        logger.debug(
            "Cannot parse the returned xml '{0}' -> '{1}'".format(xml, e)
        )
        parsed = {}

    transaction = parsed.get('transaction', {})
    for k, v in transaction.iteritems():
        setattr(self, k, v)

A field is created for each attribute of the read XML. So what you really want is:

print notification_data.status
    
11.06.2014 / 00:00
0

It has a detail:

The XML that PagSeguro returns has several "levels", so the PagSeguroNotificationResponse object will actually have the corresponding attributes with XML, but with a caveat:

From the second level they will remain as dictionaries, for example, using the name of your object:

notification_data.status # returns the status number

notification_data.shipping # returns a dictionary, where all properties from here will have to be accessed through eg key notification_data.shipping['address'] and if you want the street name eg: notification_data.shipping['address']['street']

    
27.06.2014 / 15:17