如何在基于类的视图中使用django-paypal

时间:2014-05-23 06:04:38

标签: python django paypal django-paypal

我试图在我的网站中添加Paypal Pay按钮,但我在我的网站中使用基于类的视图,我尝试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 %}

基于功能的信号控制器:

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按钮从“立即购买”更改为其他内容。

1 个答案:

答案 0 :(得分:0)

init .py文件中导入信号。

初始化的.py

导入信号