Python interface to PayPal - urllib.urlencode non-ASCII characters failing

前端 未结 3 2125
粉色の甜心
粉色の甜心 2021-01-31 19:10

I am trying to implement PayPal IPN functionality. The basic protocol is as such:

  1. The client is redirected from my site to PayPal\'s site to complete payment. He l
相关标签:
3条回答
  • 2021-01-31 19:55

    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()])

    0 讨论(0)
  • 2021-01-31 20:02

    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()))
    
    0 讨论(0)
  • 2021-01-31 20:06

    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.

    0 讨论(0)
提交回复
热议问题