4111

django-paypal ipn not responding

Question:

i am using <a href="https://github.com/johnboxall/django-paypal" rel="nofollow">this app</a> to implement paypal into my application. However when i make the payment and everything, django keeps on complaining that i don't have a csrf_token when i already inserted it into my template form.

This is my template:

<form method="post" action="/paypal/"> {% csrf_token %}

To change your subscription, select a membership and the subscription rate:

<select name="membership_input" id="id_membership"> <option>Silver</option> <option>Gold</option> <option>Platinum</option> </select> <select name="subscription_input" id="id_subscription" style = "float: center; margin-left: 30px;"> <option>Monthly</option> <option>Quarterly</option> <option>Yearly</option> </select></br></br> {{ form }} </form>

And this is my view that handles the paypal elements:

def paypal(request): c = RequestContext(request,{}) c.update(csrf(request)) if request.method == 'POST': if 'membership_input' in request.POST: if 'subscription_input' in request.POST: membership = request.POST['membership_input'] subscription = request.POST['subscription_input'] if membership == "Gold": if subscription == "Quarterly": price = "2400.00" if subscription == "Monthly": price = "1000.00" if subscription == "Yearly": price = "8000.00" elif membership == "Silver": if subscription == "Quarterly": price = "1200.00" if subscription == "Monthly": price = "500.00" if subscription == "Yearly": price = "4000.00" elif membership == "Premium": if subscription == "Quarterly": price = "4800.00" if subscription == "Monthly": price = "2000.00" if subscription == "Yearly": price = "16000.00" paypal_dict = {"business":settings.PAYPAL_RECEIVER_EMAIL,"amount": price ,"item_name": membership+" membership" ,"invoice": "09876543", "notify_url": "%s%s" % (settings.SITE_NAME, reverse('paypal-ipn')),"return_url": "http://rosebud.mosuma.net",} # Create the instance. form = PayPalPaymentsForm(initial=paypal_dict) context = {"form": form.sandbox()} c = RequestContext(request,{"form": form.sandbox()}) return render_to_response("paypal.html", c) else: return HttpResponseRedirect("/")

if anybody needs my ipn view:

@require_POST @csrf_exempt 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 # Clean up the data as PayPal sends some weird values such as "N/A" print "IPN" data = request.POST.copy() print "IPN" date_fields = ('time_created', 'payment_date', 'next_payment_date', 'subscr_date', 'subscr_effective') print "IPN" for date_field in date_fields: print "IPN" if data.get(date_field) == 'N/A': print "IPN" del data[date_field] print "IPN" form = PayPalIPNForm(data) print "IPN" if form.is_valid(): try: ipn_obj = form.save(commit=False) print "IPN" 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")

I have already tried using requestContext as mentioned by django and inserted the csrf token but i don't know why it's not working.

Also if i were to enable recurring paypal subscriptions how do i do it?

Any help is appreciated.

Answer1:

From Django docs, use RequestContext or "Manually import and use the processor to generate the CSRF token and add it to the template context." e.g.:

from django.core.context_processors import csrf from django.shortcuts import render_to_response def my_view(request): c = {} c.update(csrf(request)) # ... view code here return render_to_response("a_template.html", c)

And here's how you should be using render_to_response:

return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request))

So, remove these lines from top:

c = RequestContext(request,{}) c.update(csrf(request))

And change bottom from:

c = RequestContext(request,{"form": form.sandbox()}) return render_to_response("paypal.html", c)

to

return render_to_response("paypal.html", {"form": form.sandbox(), }, context_instance=RequestContext(request))

This should fix the problem. Personally, I recommend just using render (from django.shortcuts import render), which takes care of the RequestContext for you.

Recommend

  • Grails Service Transactions
  • Better way to rewrite conditional sum and count query / Comparison of date ranges
  • L5 How to use trait that hashes id but keep pivot functionality
  • Unable to Dynamically Change panel
  • How to open a specific PayPal link in the PayPal iOS app from my iOS app
  • Merging two backbone collection and models into one object using underscore
  • Sum and distinct dont alter results?
  • How to add custom fields to the order_invoice.tpl, OpenCart 2.0
  • Extract business titles and time periods from string
  • Entity Framework - Saving child entities on update
  • Azure Web app save temporary files limit
  • What is the difference between a CPU and a GPU oriented code?
  • time_t conversion format question
  • dynamically set table titles with jquery's Datatables plugin - get titles from data object
  • Read Values from .csv file and convert them to float arrays
  • Merging Users in Kinvey
  • JavaFX icons randomly disappear from TreeTableView when scrolling, performance slow as well
  • OneToOne bidirectional mapping foreign key auto fill
  • Build entire solution for .NET 4 and .NET 4.5 and copy files to specific folders
  • jquery limit lines in a paragraph and apply three periods to the end
  • Linq to SQL left outer join using Lambda syntax and joining on 2 columns (composite join key)
  • DirectX game with no prerequisite software to run
  • NHibernate Lazy Loading Behaviour
  • Comma within fields in CSV file -import to DB using SSIS
  • Net-ssh session timeout
  • ASP.NET - Efficiently delay code execution for single request?
  • CHAR vs VARCHAR for password security
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • How to develop MS CRM kind of application
  • PXAction seemingly does nothing
  • Peer to peer/adaptive payments with paypal [closed]
  • How do I create an image and save it for later to draw as texture in XNA?
  • View Paypal shopping cart contents on my site
  • Paypal 'Buy Now' button not letting me dynamically set the price
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Time out Error in send mail
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • Grails calculated field in SQL
  • angularjs unit test when to use $rootScope.$new()
  • Web-crawler for facebook in python