Notification data Pagseguro

Asked

Viewed 306 times

0

I’m trying to integrate Pagseguro with this lib:

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

But, I can’t 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 bug:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'PagSeguroNotificationResponse' object has no attribute '__getitem__'

2 answers

1


From what I read in the library, the method check_notification returns an object of type PagSeguroNotificationResponse.

Observe the method parse_xml of that 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 XML attribute read. So what you really want is:

print notification_data.status

0

Has a detail:

The XML that Pagseguro returns has multiple "levels", hence the object PagSeguroNotificationResponse 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 via key ex.: notification_data.shipping['address'] and if you want the street name by ex.: notification_data.shipping['address']['street']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.