自定义 Django-PayPal 视图

问题描述 投票:0回答:2

我正在使用 django paypal 允许用户在我的网站上付款,但我有一些疑问。

它目前对我来说的工作方式是我有一个名为 profile.html 的模板。当用户点击“点击更多订阅选项”按钮时,他将被重定向到 subscriptions.html 模板,其中显示订阅表和 paypal 按钮。单击该按钮时,用户将被重定向到另一个名为 paypal.html 的模板,该模板显示另一个从 django-paypal 的 forms.py 派生的 paypal 按钮

我的问题是如何修改 paypal 视图,以便我可以取消 paypal.html 并在用户单击 subscription.html 中的 paypal 按钮时将用户直接定向到实际的 paypal 网站?

我希望我对问题的描述足够清楚。

在我看来.py:

def paypal(request):
    paypal_dict = {"business":settings.PAYPAL_RECEIVER_EMAIL,"amount": "1.00","item_name": "Milk" ,"invoice": "12345678", "notify_url": "%s%s" % (settings.SITE_NAME, reverse('paypal-ipn')),"return_url": "http://rosebud.mosuma.net",}
    # Create the instance.
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form.sandbox()}
    return render_to_response("paypal.html", context)

在我的 profile.html 中:

....
<INPUT TYPE="submit" Value="Click to find out subscription plans" name="subscription" onClick="/subscribe/>

在我的订阅.html 中:

<form method="post" action="paypal/">
<select name="subscription_input" id="id_subscription" style = "float: center">
<option>Monthly</option>
<option>Yearly</option>
</select></br></br>

{{ form }}
</form>

在我的 urls.py 中:

url(r'^paypal/$', 'r2.views.paypal', name='paypal'),
url(r'^profile/paypal/$', 'r2.views.paypal', name='paypal'),
django paypal django-paypal
2个回答
1
投票

如果您希望用户在点击 subscription.html 中的 PayPal 按钮时直接访问 PayPal 网站,则必须在 subscription.html 而不是 paypal.html 中呈现 PayPal 表单。此外,您需要在 forms.py 中对 PayPalPaymentsForm 进行子类化,以覆盖“立即购买”的默认 PayPal 图像,因为您希望自己的按钮首先能够工作。


forms.py

from paypal.standard.forms import PayPalPaymentsForm
from django.utils.html import format_html

class ExtPayPalPaymentsForm(PayPalPaymentsForm):
    def render(self):
        form_open  = u'''<form action="%s" id="PayPalForm" method="post">''' % (self.get_endpoint())
        form_close = u'</form>'
        # format html as you need
        submit_elm = u'''<input type="submit" class="btn btn-success my-custom-class">'''
        return format_html(form_open+self.as_p()+submit_elm+form_close)

views.py

from .forms import ExtPayPalPaymentsForm
def paypal(request):
    paypal_dict = {"business":settings.PAYPAL_RECEIVER_EMAIL,"amount": "1.00","item_name": "Milk" ,"invoice": "12345678", "notify_url": "%s%s" % (settings.SITE_NAME, reverse('paypal-ipn')),"return_url": "http://rosebud.mosuma.net",}
    # Create the instance.
    form = ExtPayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form.sandbox()}
    return render_to_response("subscription.html", context)

0
投票

自 2021 年 4 月 8 日起,django-paypal 1.1.1 版中 self.get_endpoint() 已更改为 self.get_login_url()。

https://django-paypal.readthedocs.io/en/latest/release_notes.html#version-1-1-1-2021-04-08

© www.soinside.com 2019 - 2024. All rights reserved.