Django-paypal ipn not responding

I am using this application to embed paypal in my application. However, when I make a payment and that's it, django keeps 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">
        <select name="subscription_input" id="id_subscription" style = "float: center; margin-left: 30px;">
        {{ form }}


And this is my opinion which handles PayPal items:

def paypal(request):
    c = RequestContext(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": "",}
                # 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)                 
        return HttpResponseRedirect("/")


if anyone needs my ipn view:

def ipn(request, item_check_callable=None):
    PayPal IPN endpoint (notify_url).
    Used by both PayPal Payments Pro and Payments Standard to confirm transactions.

    PayPal IPN Simulator:
    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():
            ipn_obj =
        print "IPN"
        except Exception, e:
            flag = "Exception while processing. (%s)" % e
        flag = "Invalid form. (%s)" % form.errors

    if ipn_obj is None:
        ipn_obj = PayPalIPN()

    if flag is not None:
        # Secrets should only be used over SSL.
        if request.is_secure() and 'secret' in request.GET:
            ipn_obj.verify_secret(form, request.GET['secret'])
    return HttpResponse("OKAY")


I've already tried using requestContext as django mentioned and inserted the csrf token, but I don't know why it doesn't work.

Also, if I enabled recurring paypal subscriptions, how do I do this?

Any help is appreciated.


source to share

1 answer

In Django docs, use RequestContext

either "Manually import and use the processor to generate a CSRF token and add it to the template context." eg:.

    from django.core.context_processors import csrf
    from django.shortcuts import render_to_response

    def my_view(request):
        c = {}
        # ... view code here
        return render_to_response("a_template.html", c)


And how should you use render_to_response


    return render_to_response('my_template.html', 


So remove these lines from the top:

    c = RequestContext(request,{})


And change the bottom to:

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



    return render_to_response("paypal.html", 
              {"form": form.sandbox(), },


This should fix the problem. Personally, I recommend just using render

( from django.shortcuts import render

), which takes care of the RequestContext for you.



All Articles