emesik/mamona — pull request #9 by radiosilence (2013-03-06 14:34:46+00:00)
Title: Robustness fixes (PayPal encoding and bad request handling)

First issue was that we get a 500 error if it's not an IPN request, so I solved this by sending a HttpResponseBadRequest.

The big issue was the encoding of the form data - if PayPal sent you weird characters (which Django pulls into unicode strings), the code fell over because urllib.urlencode doesn't support that (url data is bytes). First try was I used Django's urlencode function which automatically encodes into UTF-8, *however* this required the PayPal account holder to change their settings (bad, especially if you don't control the account - it's a clients), so I looked at the POST and it turns out there's a 'charset' field.

If one sets request.encoding to request.POST['charset'], it picks it up properly. You can then encode the form data as per this, too.