I am trying to implement PayPal IPN functionality. The basic protocol is as such:
Instead of encoding to utf-8
, one should encode to what ever the paypal is using for the post.
It is available under key 'charset' in the form paypal sends.
So the following code worked for me:
data = dict([(k, v.encode(data['charset'])) for k, v in data.items()])
Try converting the params dictionary to utf-8 first... urlencode seems to like that better than unicode:
params = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in params.items()))
Of course, this assumes your input is unicode. If your input is something other than unicode, you'll want to decode it to unicode first, then encode it:
params['foo'] = my_raw_input.decode('iso-8859-1')
params = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in params.items()))
I know it's a bit late to chime in here, but the best solution I found was to not even parse what they were giving back. In django (don't know what you're using) I was able to get the raw request they sent, which I passed back verbatim. Then it was just a matter of putting the cmd key onto that.
This way it never matters what encoding they send you, you're just sending it right back.