django-paypal ipn 没有响应

标签 django django-paypal

我正在使用 this app在我的应用程序中实现 Paypal 。然而,当我支付所有费用时,django 一直提示我没有 csrf_token,而我已经将它插入到我的模板表单中。

这是我的模板:

    <form method="post" action="/paypal/">
        {% csrf_token %}
        <p>
            To change your subscription, select a membership and the subscription rate:
        </p>
        <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>

这是我处理 paypal 元素的 View :

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("/")

如果有人需要我的 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")

我已经尝试使用 django 提到的 requestContext 并插入了 csrf token ,但我不知道为什么它不起作用。

另外,如果我要启用定期的 paypal 订阅,我该怎么做?

感谢任何帮助。

最佳答案

在 Django 文档中,使用 RequestContext 或“手动导入并使用处理器生成 CSRF token 并将其添加到模板上下文中。”例如:

    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)

下面是您应该如何使用 render_to_response:

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

因此,从顶部删除这些行:

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

并从以下更改底部:

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

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

这应该可以解决问题。就个人而言,我建议只使用 render(from django.shortcuts import render),它会为您处理 RequestContext。

关于django-paypal ipn 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9861531/

相关文章:

python - Django 管理员内联 : select_related

paypal - 无法使用 django-paypal 获得有效的 IPN 回发

python - django-paypal 无法处理通知信号

python - 如何在 Django 中调试 PayPal IPN?

python - 检查由 nginx 反向代理(Django 应用程序)转发的代理集 header

html - 我无法将我的 css 链接到 html

python - Django:获取连接到某些实例集的所有实例

python - Django 重组列表

python - Django 支付处理的良好解决方案