django - 了解在 django-paypal 中放置 IPN 接收函数的位置

标签 django paypal django-views django-paypal

我有一个与帖子类似的问题 here关于为 paypal-ipn 设置接收函数

我们正在尝试做的(我不认识这个人,但我想我们在这个话题上站在一起)是了解如何推断出接收 paypal IPN 信号的路径,然后我们可以在其中更新 django数据库。

我已经按照说明 here 实现了 django-paypal API

总的来说,我所做的是在我的 views.py 中创建一个 View ,如下所示

def payment_page(request):
   """This fucntion returns payment page for the form"""
   if not request.session.get('form-submitted', False):
       return HttpResponseRedirect(reverse('grap_main:error_page'))
   else:
       amount = set_payment()
       paypal_dict = {
           "business": "business@gmail.com",
           "amount": str(amount),
           "item_name": "2017 USGF Championships",
           "notify_url": "https://15b6b6cb.ngrok.io" + reverse('paypal-ipn'),
           "return_url": "https://15b6b6cb.ngrok.io/Confirm",
           "cancel_return": "https://15b6b6cb.ngrok.io/RegistrationForm",

  }
       form = PayPalPaymentsForm(initial=paypal_dict)
       context = confirm_information()
       context["amount"] = amount
       context["form"] = form
       request.session['form-submitted'] = False
       valid_ipn_received.connect(show_me_the_money)
       return render(request, "grap_main/payment.html", context)

然后我有 payment.html,然后只需使用文档中建议的行即可创建 Paypal 按钮

{{ form.render }}

现在我可以收到我在文档中指定的 paypal url 的 POST,但是我不知道我应该把我的信号函数放在哪里,一旦有人完成购买就会获取 IPN。

def show_me_the_money(sender, **kwargs):
   """signal function"""
   ipn_obj = sender
   if ipn_obj.payment_status == ST_PP_COMPLETED:
       print 'working'
   else:
       print "not working"

现在我在 View payment_page() 中调用这个函数但是我知道这是在 POST 到 paypal 之前并且不正确 我不明白应该在哪里调用 show_me_the_money() 函数。我习惯于创建一个从 html 脚本调用的 View ,如下所示

def register(request):
   """aquire information from new entry"""
   if request.method != 'POST':
       form = RegisterForm()
   else:
       if 'refill' in request.POST:
           form = RegisterForm()
       else:
           form = RegisterForm(data=request.POST)
           if form.is_valid():
               form.save()
               request.session['form-submitted'] = True
               return HttpResponseRedirect(reverse('grap_main:payment_page'))

.html

<form action="{% url 'grap_main:register' %}" method='post' class="form">
     {% csrf_token %}
     {% bootstrap_form form %}
     <br>
     {% buttons %}
     <center>
       <button name='submit' class="btn btn-primary">Submit</button>
     </center>
     {% endbuttons %}
   </form>

我认为我需要在用户完成购买后调用该函数,但我不知道如何在我的代码中定位该时间窗口。我想确保我处理的情况是,用户在完成付款后并不总是返回商家网站。

关于这个主题的任何帮助不仅对我有好处,而且对早期的海报也有好处。我希望在解决这个问题时制作一个教程,以帮助其他可能也遇到困难的人。

请注意,我还使用 ngrok 来确保我的项目可以访问 paypal IPN 服务。我也使用了两个 urls.py 文件,主要的文件看起来是这样

urlpatterns = [
   url(r'^paypal/', include('paypal.standard.ipn.urls')),
   url(r'^admin/', admin.site.urls),
   url(r'', include('grap_main.urls', namespace='grap_main')),
]

其中“grap_main.urls”是我网站的所有特定 View ,例如。

urlpatterns = [
   url(r'^$', views.index, name='index'),
   url(r'^Confirm', views.confirm, name='confirm'),
   url(r'^Events', views.events, name='events'),
   .........]

最佳答案

[更新]:忘记提及将您编写的代码(如果您愿意,也可以是处理程序)放在何处以及如何放置。在 handlers.py 文件中这样写:

# grap_main/signals/handlers.py

from paypal.standard.ipn.signals import valid_ipn_received, invalid_ipn_received

@receiver(valid_ipn_received)
def show_me_the_money(sender, **kwargs):
    """Do things here upon a valid IPN message received"""
    ...

@receiver(invalid_ipn_received)
def do_not_show_me_the_money(sender, **kwargs):
    """Do things here upon an invalid IPN message received"""
    ...

虽然信号(和处理程序)可以存在于任何地方,但一个很好的约定是将它们存储在应用程序的 signals 目录中。因此,您的 grap_main 应用程序的结构应如下所示:

project/
    grap_main/
        apps.py
        models.py
        views.py
        ...
        migrations/
        signals/
            __init__.py
            signals.py
            handlers.py            

现在,为了加载handlers,在grap_main/apps.py中写入(或添加)这个

# apps.py

from django.apps.config import AppConfig


class GrapMainConfig(AppConfig):
    name = 'grapmain'
    verbose_name = 'grap main' # this name will display in the Admin. You may translate this value if you want using ugettex_lazy

    def ready(self):
        import grap_main.signals.handlers

最后,在您的 settings.py 文件中,在 INSTALLED_APPS 设置下,不要使用 'grap_main':

# settings.py

INSTALLED_APPS = [
    ... # other apps here
    'grap_main.apps.GrapMainConfig',
    ... # other apps here
]

一些旁注:

  1. 使用 encrypted buttons 而不是使用标准表单来呈现 paypal 按钮.这样您就可以防止对表格进行任何潜在的修改(主要是价格更改)。

  2. 几个月前我就在你的轨道上使用了不起的 django-paypal 包。然而,我需要将我的项目升级到 Python 3。但是因为我使用了 encrypted buttons 我无法升级。为什么?因为加密按钮依赖于 M2Crypto 不支持(还)Python 3。我做了什么?我放弃了 django-paypal 并加入了 Braintree这是一家 Paypal 公司。现在,我不仅可以接受 PayPal 付款,还可以接受信用卡付款。全是 Python 3!

关于django - 了解在 django-paypal 中放置 IPN 接收函数的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42943041/

相关文章:

php - PayPal折扣优惠券集成

python - Django: 'module' 对象没有属性 'index'

django - 通过过滤对象列表来获取对象列表

python - Django - 通用 FormView - 将输入传递到模板中

python - 我们可以使用django中/admin的登录页面供自己使用吗?

django - 错误: invalid command 'bdist_wheel'

codeigniter - CodeIgniter 2.x.x 的 Paypal_lib

python - Django 邮件未保存(文件后端)

python - 将 Python 函数参数存储为变量以供以后调用

买家支付的PayPal费用