如何使用 django-paypal 获取付款通知

Posted

技术标签:

【中文标题】如何使用 django-paypal 获取付款通知【英文标题】:how to get a notification of payment with django-paypal 【发布时间】:2015-04-06 13:46:03 【问题描述】:

我已经使用 django-paypal 网站上的加密按钮文档设置了所有内容。我只是似乎没有收到任何付款已发生的通知。我知道我一定遗漏了一些东西或做错了什么,但我没有收到任何意见的 POST,除非在付款后我点击返回网站。我需要先收到付款通知,然后才能继续。谢谢。

这是我的看法:

from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, HttpResponseRedirect, render_to_response
from paypal.standard.forms import PayPalEncryptedPaymentsForm
from django.core.urlresolvers import reverse
import eccomerce.settings as settings
from datetime import datetime
from paypal.standard.models import ST_PP_COMPLETED
from django.views.decorators.csrf import csrf_exempt
from paypal.standard.ipn.signals import valid_ipn_received

def show_me_the_money(sender, **kwargs):
    ipn_obj = sender
    if ipn_obj.payment_status == ST_PP_COMPLETED:
        # Undertake some action depending upon `ipn_obj`.
        payment = True
    else:
        payment = False

valid_ipn_received.connect(show_me_the_money)

def index(request):

    # What you want the button to do.
    paypal_dict = 
    "business": settings.PAYPAL_RECEIVER_EMAIL,
    "amount": "0.01",
    "currency_code": "GBP",
    "item_name": "picture01",
    "invoice": "unique-%s" % (str(datetime.now())),
    "notify_url": "http://127.0.0.1:8000/notify/",
    "return_url": "http://127.0.0.1:8000/return/",
    "cancel_return": "http://127.0.0.1:8000/cancel/",

    
    valid_ipn_received.connect(show_me_the_money)

    # Create the instance.
    form = PayPalEncryptedPaymentsForm(initial=paypal_dict)
    context = "form": form
    return render_to_response("eccomerce_webapp/index.html", context)


@csrf_exempt
def notify(request):
    valid_ipn_received.connect(show_me_the_money)

    context = 
    return render_to_response("eccomerce_webapp/notify.html", context)


@csrf_exempt
def cancel(request):
    valid_ipn_received.connect(show_me_the_money)

    context = 
    return render_to_response("eccomerce_webapp/cancel.html", context)

@csrf_exempt
def return_view(request):
    valid_ipn_received.connect(show_me_the_money)

    context = 
    return render_to_response("eccomerce_webapp/return.html", context)

这是我的网址:

from django.conf.urls import patterns, include, url
from eccomerce_webapp import views

urlpatterns = patterns('',
    url(r'^notify/$', views.notify, name='notify'),
    url(r'^return/$', views.return_view, name='return'),
    url(r'^cancel/$', views.cancel, name='cancel'),
    url(r'^$', views.index, name='index'),
    (r'^something/paypal/', include('paypal.standard.ipn.urls')),
)

【问题讨论】:

你能告诉任何sn-p你是怎么解决的吗 【参考方案1】:

PayPal 的 IPN 不会回调您的本地 URL:http://127.0.0.1:8000/notify/

在IPN testing 上查看他们的文档。

PayPal 建议使用直接提交到通知端点的表单来编写您自己的测试页面。这可能看起来像(来自他们的文档):

<form target="_new" method="post" action="% url 'notify' %">
    <input type="hidden" name="SomePayPalVar" value="SomeValue1"/>
    <input type="hidden" name="SomeOtherPPVar" value="SomeValue2"/>

    <!-- code for other variables to be tested ... -->

    <input type="submit"/>
</form>

在某个沙盒环境中启动应用后,您就可以开始使用 IPN 模拟器进行测试了。

【讨论】:

以上是关于如何使用 django-paypal 获取付款通知的主要内容,如果未能解决你的问题,请参考以下文章

Django Paypal 订阅

Paypal 发送了 IPN 的警告电子邮件

如何在 IPN 通知 URL 中获取 pay_key?

获取 paypal ruccuring 付款详情

Paypal IPN 未发送定期付款信息

如何在 PHP 中使用 IPN(即时付款通知)在 Paypal 中配置定期付款