Paypal Confirm IPN with Django

January 9, 2011

Here's some code to confirm a PayPal IPN using Django

def _request_confirm(request):
    """
    Confirm IPN with paypal
    """
    lConfirm = "/cgi-bin/webscr?cmd=_notify-validate&"
    lConfirm += request.META['QUERY_STRING']
    lConnection = httplib.HTTPSConnection(settings.PAYPAL_CONFIRM_URL)
    lHeaders = {"Host" : settings.PAYPAL_CONFIRM_URL,
                "Content-type": 'text/plain; charset="UTF-8"',
                "Content-length": "0",
                }
    lConnection.request("GET", lConfirm, None, lHeaders)
    lResponseObject = lConnection.getresponse()
    lStatusCode = lResponseObject.status
    lResponse = lResponseObject.read()
    if lResponse != "VERIFIED":
        raise Http404

This is designed to be called from a view, passing in the request object.

Tags: paypal ipn django