django-paypal ipn没有响应 [英] django-paypal ipn not responding
问题描述
我正在使用此应用在我的应用程序中实施PayPal。然而,当我付款和一切,django不断抱怨说,我没有一个csrf_token当我已经插入到我的模板表单。
这是我的模板:
< form method =postaction =/ paypal />
{%csrf_token%}
< p>
要更改订阅,请选择会员资格和订阅费率:
< / p>
< select name =membership_inputid =id_membership>
< option> Silver< / option>
< option> Gold< / option>
< option> Platinum< / option>
< / select>
< select name =subscription_inputid =id_subscriptionstyle =float:center; margin-left:30px;>
< option>每月< / option>
< option>季度< / option>
< option> Yearly< / option>
< / select>< / br>< / br>
{{form}}
< / form>
这是我处理paypal元素的观点:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
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']
如果会员==金:
如果订阅==季度:
price =2400.00
如果订阅==每月:
price =1000.00
如果订阅==年度:
price =8000.00
elif membership ==Silver
如果订阅==季度:
price =1200.00
如果订阅者iption ==Monthly:
price =500.00
如果订阅==年度:
price =4000.00
elif membership ==Premium:
如果订阅==季度:
price =4800.00
如果订阅==每月:
price =2000.00
如果订阅== $
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,}
#创建实例。
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视图:
@require_POST
@csrf_exempt
def ipn(request,item_check_callable = None):
PayPal IPN端点(notify_url)
由PayPal Payments Pro和Payments Standard用于确认交易
http://tinyurl.com/d9vu9d
PayPal IPN模拟器:
https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-会话
flag =无
ipn_obj =无
#清理数据,因为PayPal发送一些奇怪的值,如N / A
打印IPN
data = request.POST.copy()
printIPN
date_fields =('time_created','payment_date','next_payment_date','subscr_date','subscr_effective')
打印IPN
for date_field in date_fields :
打印IPN
如果data.get(date_field)=='N / A':
打印IPN
del data [date_field]
打印IPN
form = PayPalIPNForm(data)
打印IPN
如果form.is_valid():
try:
ipn_obj = form.save(commit = False)
打印IPN
除了异常,e:
flag =处理时异常。 (%s)%e
else:
flag =无效的表单。 (%s)%form.errors
如果ipn_obj为无:
ipn_obj = PayPalIPN()
ipn_obj.initialize(请求)
if标志不是无:
ipn_obj.set_flag(flag)
else:
#秘密应该只能通过SSL使用
如果request.is_secure()和请求中的secret .GET:
ipn_obj.verify_secret(form,request.GET ['secret'])
else:
ipn_obj.verify(item_check_callable)
ipn_obj.save )
return HttpResponse(OKAY)
我已经尝试使用requestContext,如django并插入了csrf令牌,但我不知道为什么它不起作用。
另外如果我要启用定期付款支付订阅我该怎么办? >
任何帮助都不胜感激。
从Django文档中,使用 RequestContext
或手动导入并使用处理器进行ge将CSRF令牌加密,并将其添加到模板上下文中。例如:
从django.core.context_processors导入csrf
从django.shortcuts import render_to_response
def my_view(request):
c = {}
c.update(csrf(request))
#...视图这里的代码
返回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:for m.sandbox()})
返回render_to_response(paypal.html,c)
to
return render_to_response(paypal.html,
{form:form.sandbox() },
context_instance = RequestContext(request))
这应该可以解决问题。个人而言,我建议只需使用 render
(django.shortcuts import render )中的),它会为您处理RequestContext 。
i am using this app 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 %}
<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>
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.
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.
这篇关于django-paypal ipn没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!