我正在尝试使用 django-paypal。我正在关注中提到的内容 Jay on Django
这是我做的...
##in my view.py file
def ask_payment(request):
# What you want the button to do.
paypal_dict = {
"business": settings.PAYPAL_RECEIVER_EMAIL,
"amount": "0.10",
"item_name": "book",
"invoice": "yong138peng",
"notify_url": "http://127.0.0.1:8000/accounts/my-ipn-location/",
"return_url": "http://127.0.0.1:8000/accounts/my-return-location/",
"cancel_return": "http://127.0.0.1:8000/accounts/my-cancel-location/",
}
# Create the instance.
form = PayPalPaymentsForm(initial=paypal_dict)
context = {"PP_form": form}
return render_to_response("paypal/payment.html",{'PP_form':form},context_instance=RequestContext(request))
@csrf_exempt
def payment_status(request,status):
return render_to_response("paypal/payment_status.html",
{'status':status},context_instance=RequestContext(request))
##then in my urls.py file
(r'^askforpayment/$','coltrane.views.ask_payment'),
(r'^my-ipn-location/', include('paypal.standard.ipn.urls')),
(r'^my-return-location/$','coltrane.views.payment_status',{'status':'success'}),
(r'^my-cancel-location/$','coltrane.views.payment_status',{'status':'cancel'}),
##in my models.py
def show_me_the_money(sender, **kwargs):
ipn_obj = sender
print "payment was successful!"
# Undertake some action depending upon `ipn_obj`.
if ipn_obj.custom == "Upgrade all users!": ## what is this for, this is sent by paypal??
Users.objects.update(paid=True)
payment_was_successful.connect(show_me_the_money)
我的问题是:
根据 jay on django ,我必须在 paypay.standard.ipn.views.ipn 函数之前放置一个 @csrf_exempt 以避免 django 提示 @csrf_token 问题。我做到了,但我仍然面临同样的问题。然后我将 @csrf_exempt 放在我的返回 url View 函数之前,在这种情况下 payment_status(request,status),csrf_token 问题消失了。所以我不确定为什么会这样。
信号处理程序中的这条语句是做什么用的? “如果 ipn_obj.custom == “升级所有用户!”:....“ 这是来自paypay吗?除了“升级所有用户”之外,还有什么可能的值(value)?
我设法在沙盒中进行购买并完成整个付款流程。但现在的问题是 Paypal 不会将 IPN 发布回我的本地主机 notify_url。我读自 Paypal Sandbox IPN Problem我不能使用本地主机 ( http://127.0.0.1:8000 ) 来测试 IPN。那么测试需要哪些步骤呢?我不明白 post 中提供的解决方案.谁能教我如何在不部署真实生产服务器的情况下测试 IPN?
最佳答案
关于您的第三点,正如 Daniel 在回答您所链接的问题时所说,您需要允许 Paypal POST 到您的本地计算机。这意味着您需要在路由器上打开端口 80,并将请求转发到端口 8000 上的开发机器。转到 http://whatismyip.com。 ,获取 IP 并尝试在浏览器中访问它。除非您正确配置了路由器,否则您什么也得不到。
设置路由器后,您需要运行 django 服务器:
python manage.py runserver 0.0.0.0:8000
然后您就可以从外部访问它了。您可以通过将 Internet 连接的 IP 放入浏览器来对此进行测试——您应该会看到您的 Django 站点。如果您不这样做,那么 Paypal 也无法“看到”您,并且永远无法回发。
关于django-paypal 收不到 ipn 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5833926/