bkeating / python-payflowpro

A simple Python client for PayPal's Payflow Pro API (HTTPS Interface).
https://www.paypal.com/us/webapps/mpp/payflow-payment-gateway
Apache License 2.0
47 stars 22 forks source link

UnicodeDecodeError when querying Recurring Profiles. #1

Open bkeating opened 13 years ago

bkeating commented 13 years ago

When you are querying RecurringProfiles, if any of the field names (FIRSTNAME, SHIPTOCITY, etc.) have accented characters (example: Andrés), python-payflowpro will come back with a UnicodeDecodeError:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 347: ordinal not in range(128)

If you modified the field (via Paypal Manager) and replace the accented characters, the symptom goes away but the correct way to solve the problem is to kindly support Unicode decoding. I found this thread over at Stack Overflow to help shed light on the issue: http://stackoverflow.com/questions/3669436/converting-unicode-objects-with-non-ascii-symbols-in-them-into-strings-objects-i

In particular, the code errors out at/around:

python-payflowpro/payflowpro/client.py", line 189, in _do_request
raise e
johndagostino commented 13 years ago

this looks like a simple fix ben.

the _build_parmlist will need to check if the value is a instance of unicode when calculating the lengh

            if isinstance(value, unicode):
                key = '%s[%d]' % (key.upper(), len(value.encode('utf-8')))
            else:
                key = '%s[%d]' % (key.upper(), len(str(value)))

then update the _do_request to encode the paramlist;

                request = Request(
                       url = self.url_base, 
                       data = parmlist.encode('utf-8'),
                       headers = headers)

update the client.py test file with this at the top

# -*- coding: utf-8 -*-

and I updated the profile_modify call to change the first name to

firstname=u'的'

bkeating commented 12 years ago

It's been exactly 365 days since you replied and Im just now getting this implemented. What you posted above works a charm. Im not sure though that the characters are being sent properly. I also tried firstnam=u'的' but when I check PayPal Manager, It displays as çš„. Perhaps PayPal manager does not decode unicode? So I tried to do a profile_inquiry to see if I can see the proper value in my interactive shell and hit an error; UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 335: ordinal not in range(128) which is similar to what I got when doing a profile_modify but that was due to me not wrapping the value in unicode (firstname="的" --no u''). As far as profile_inquiry goes, im not quite sure what the next step should be to support unicode coming FROM paypal... Or if that is even the issue.

Im going to continue reading up on unicode in Python but if you have any ideas or suggestions, Im all ears.

Thanks again for your support. This module will haunt you forreeevvverrrr ;)

edit: my latest commit contains your suggestions from your reply above. https://github.com/bkeating/python-payflowpro/commit/495df5f461bc6d758f926e1287153bcd885ea2d7