【问题标题】:How to add the PayPal Smart Payment Buttons to my forms.py如何将 PayPal 智能支付按钮添加到我的 forms.py
【发布时间】:2021-01-08 02:17:42
【问题描述】:

你好,

我发现在我的表单中添加 PayPal 智能支付按钮时遇到了困难。在我的项目中,有一些表单可以通过选择 Radio Button Stripe 或 Paypal 来选择所需的付款方式。

我不知道如何安排,以便在结帐页面显示本网站使用的信用卡和 PP 的标志:

https://developer.paypal.com/demo/checkout/#/pattern/radio

条纹支付方式运行良好,我只想添加 PayPal 支付选项。

我的项目是这样安排的: Forms.py

PAYMENT_CHOICES = (
    ('S', 'Stripe'),
    ('P', 'Paypal')
)

class CheckoutForm(forms.Form):
----address related forms-----------------------------------
    payment_option = forms.ChoiceField(
        widget=forms.RadioSelect, choices=PAYMENT_CHOICES)

这是结帐模板:

              <h3>Payment option</h3>

              <div class="d-block my-3">
                {% for value, name in form.fields.payment_option.choices %}
                <div class="custom-control custom-radio">
                  <input id="{{ name }}" name="payment_option" value="{{ value }}" type="radio" class="custom-control-input" required>
                  <label class="custom-control-label" for="{{ name }}">{{ name }}</label>
                </div>
                {% endfor %}
              </div>

这里是views.py

class CheckoutView(View):
    def get(self, *args, **kwargs):
        try:
            order = Order.objects.get(user=self.request.user, ordered=False)
            form = CheckoutForm()
            context = {
                'form': form,
                'couponform': CouponForm(),
                'order': order,
                'DISPLAY_COUPON_FORM': True
            }
-----------------Shipping address codes-----------------------------

                payment_option = form.cleaned_data.get('payment_option')

                if payment_option == 'S':
                    return redirect('core:payment', payment_option='stripe')
                elif payment_option == 'P':
                    return redirect('core:payment', payment_option='paypal')
                else:
                    messages.warning(
                        self.request, "Invalid payment option selected")
                    return redirect('core:checkout')

        except ObjectDoesNotExist:
            messages.warning(self.request, "You do not have an active order")
            return redirect("core:order-summary")

这是models.py

class Payment(models.Model):
    stripe_charge_id = models.CharField(max_length=50)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.SET_NULL, blank=True, null=True)
    amount = models.FloatField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user.username

我需要帮助将 PayPal 付款集成到我的结帐页面

【问题讨论】:

    标签: python django paypal


    【解决方案1】:

    您没有合并来自 https://developer.paypal.com/demo/checkout/#/pattern/radio 的任何示例 JS,而是使用 django 表单单选按钮

    如果您想从那里使用卡片徽标,则必须在选择卡片选项时至少合并资产card-mark.png

    并且您必须包含一种机制,以便在选择 PayPal 选项时显示 PayPal 按钮。我在您共享的代码中没有看到任何 PayPal 按钮呈现代码,因此您需要添加它

    【讨论】:

    • 我应该将它们添加到 forms.py 或 html 模板的什么位置?
    • 模板,很可能。
    猜你喜欢
    • 2020-03-23
    • 2020-05-25
    • 2019-11-16
    • 2020-05-09
    • 2019-10-31
    • 2021-01-28
    • 2021-07-14
    • 2020-08-13
    • 2020-10-16
    相关资源
    最近更新 更多