title: How to debug Django PayPal IPN?
body: <p>I'm using <a href="http://github.com/johnboxall/django-paypal" rel="nofollow noreferrer">this django app</a> to implement PayPal IPN. I'm testing it using PayPal's <a href="https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session" rel="nofollow noreferrer">IPN simulator</a>, but it's telling me</p>
  
  <blockquote>
    <p>IPN delivery failed. HTTP error code 500: Internal Server Error</p>
  </blockquote>
  
  <p>So how can I debug this and see what's really going on? I've dug into code:</p>
  
  <pre><code>@require_POST
  def ipn(request, item_check_callable=None):
      """
      PayPal IPN endpoint (notify_url).
      Used by both PayPal Payments Pro and Payments Standard to confirm transactions.
      http://tinyurl.com/d9vu9d
  
      PayPal IPN Simulator:
      https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session
      """
      flag = None
      ipn_obj = None
      form = PayPalIPNForm(request.POST)
      if form.is_valid():
          try:
              ipn_obj = form.save(commit=False)
          except Exception, e:
              flag = "Exception while processing. (%s)" % e
      else:
          flag = "Invalid form. (%s)" % form.errors
  
      if ipn_obj is None:
          ipn_obj = PayPalIPN()    
  
      ipn_obj.initialize(request)
  
      if flag is not None:
          ipn_obj.set_flag(flag)
      else:
          # Secrets should only be used over SSL.
          if request.is_secure() and 'secret' in request.GET:
              ipn_obj.verify_secret(form, request.GET['secret'])
          else:
              ipn_obj.verify(item_check_callable)
  
      ipn_obj.save()
      return HttpResponse("OKAY")
  </code></pre>
  
  <p>All <em>looks</em> fine and dandy there, but since it's not sending a response to <em>my</em> browser, it's kinda tricky to debug. What should I do? I'm trying to look at my apache logs, but it really isn't telling me much.</p>
  
  <blockquote>
    <p>216.113.191.33 - - [06/Mar/2010:14:10:30 -0600] "POST /paypal/ipn HTTP/1.0" 500 16282 "-" "-"</p>
  </blockquote>
  
  <hr>
  
  <p>I tried to send emails and log messages when this view was called, but neither wanted to work. It's possible that I entered the wrong URL into the IPN simulator :) I disabled the "post required" decorator and went to the page directly to see what was going on. My system started to logging "invalid transactions" as expected (since there was no post-data) and then I took a random stab in the dark and figured that Django's CSRF protection was kicking in and preventing PayPal from sending me the data. Adding the <code>@csrf_exempt</code> decorator seems to have fixed it. Yay for guessing errors.</p>