gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:25 26 4
gpt4 key购买 nike

我正在尝试在我的网站中添加一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com