python - 如何在基于类的 View 中使用 django-paypal

标签 python django paypal django-paypal

我正在尝试在我的网站中添加一个 Paypal 支付按钮,但我在我的网站中使用基于类的 View ,我尝试了 django-paypal但我无法获得信号(payment_was_successful,payment_status,...)

Views.py

from paypal.standard.forms import PayPalPaymentsForm
from django.conf import settings
from paypal.standard.ipn.signals import payment_was_successful
class Student_Billing(View, Tools):
    def dispatch(self, request, *args, **kwargs):
        if not (self.session_check(self.request):
            raise PermissionDenied
        else:
            return View.dispatch(self,request, *args, **kwargs)
    def get(self, request, *args, **kwargs):
        return render(request, self.get_template_name(),
                                self.get_context_data())

    def get_template_name(self):
        """Returns the name of the template we should render"""
        return "Class_Manager/student_billing.html"

    def get_context_data(self):
        """Returns the data passed to the template"""
        paypal_dict = {
        "business": settings.PAYPAL_RECEIVER_EMAIL,
        "amount": str(self.get_object().billing.fees_amount)[3:],
        "item_name": "Monthly Fees",
        "invoice": self.get_object().pk,
        "notify_url": "http://nai.dyndns-free.com",
        "return_url": "https://www.example.com/your-return-location/",
        "cancel_return": "https://www.example.com/your-cancel-location/",
        }
        return {
            "form":PayPalPaymentsForm(initial=paypal_dict),
            "user_billing": self.get_object().billing,
            "online_users": self.online_users_count(),
        }

    def get_object(self):
        """Returns the BlogPost instance that the view displays"""
        return get_object_or_404(Student, user=self.kwargs.get("user"))

模板:

{% extends "Class_Manager/base.html" %}
{% load staticfiles %}
{% block title %}Profile - {{user.user}}{% endblock %}
{% block statics %}
<link rel="stylesheet" href="{% static "Class_Manager/css/profile.css" %}" />
{% endblock %}
{% block main %}
<table>
<tr>
<td><strong>Billing Information</strong></td>
</tr>
<tr>
<td>This Month Fees: </td>
<td>{{user_billing.this_month}} {% if user_billing.this_month == "Not Paid" %}{{form.render}}{% endif %}</td>
</tr>
<tr>
<td>Monthly Fees amount: </td>
<td>{{user_billing.fees_amount}}</td>
</tr>
<tr>
<td>Total Fees: </td>
<td>{{user_billing.total_fees}}</td>
</tr>
<tr>
<td>Monthly Fees amount: </td>
<td>{{user_billing.total_paid_fees}}</td>
</tr>
<tr>
<td>Monthly Fees amount: </td>
<td>{{user_billing.total_unpaid_fees}}</td>
</tr>
</table>
{% endblock %}

基于函数的信号 Controller :

def show_me_the_money(sender, **kwargs):
    ipn_obj = sender
    # You need to check 'payment_status' of the IPN

    if ipn_obj.payment_status == "Completed":
        # Undertake some action depending upon `ipn_obj`.
        if ipn_obj.custom == "Upgrade all users!":
            Users.objects.update(paid=True)
    else
        ...

payment_was_successful.connect(show_me_the_money)

还有如何将 Paypal 按钮从“立即购买”更改为其他按钮。

最佳答案

在您的 init.py 文件中导入信号。

初始化.py

导入信号

关于python - 如何在基于类的 View 中使用 django-paypal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822029/

相关文章:

javascript - 在没有响应的情况下执行 python 函数 - Django

python - Elastic Beanstalk 中的导入错误 : No module named django. core.wsgi

c# - Paypal 订阅按钮错误

python - 是否有用于 Linux "at"任务调度程序的 python 模块

python - 创建 Pony ORM 实体而不获取相关对象?

android - 使用 django-rest-auth 在 Android 中登录 Google

php - PayPal 正在截断我的返回查询字符串

Paypal Express 结帐 - 未获取计费协议(protocol) ID

python - logging 如何控制刷新到日志文件的次数

python - nose.collector 在哪里寻找测试?