1

我正在使用Django PayPal。PayPal 有一个选项列表,您可以通过按钮传递。我正在尝试将其中一些添加到我的paypal_dict

paypal_dict = {
    # ...

    # preopulate paypal checkout page
    "email": invoice.user.email,
    "first_name": invoice.user.first_name,
    "last_name": invoice.user.last_name,
    "address1": invoice.user.address.street,
    "city": invoice.user.address.city,
    "country": invoice.user.address.get_country_display,
    "address_country_code": invoice.user.address.country
}

form = PayPalPaymentsForm(initial=paypal_dict)

但是当我检查表单时,这些字段永远不会添加。我怎样才能让它们被添加?

4

3 回答 3

4

以下是一些附加字段:

https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_WebsitePaymentsStandard_IntegrationGuide.pdf

于 2012-11-29T14:12:46.000 回答
1

我破解了它:

def __init__(self, button_type="buy", extra_options={}, *args, **kwargs):
    super(PayPalPaymentsForm, self).__init__(*args, **kwargs)
    self.extra_options = extra_options
    self.button_type = button_type

def render(self):
    extra_fields = u''.join(['<input type="hidden" name="%s" value="%s" />' % (escape(name), escape(value)) for name, value in self.extra_options.iteritems()])          
    return mark_safe(u"""<form action="%s" method="post">
%s
%s
<input type="image" src="%s" border="0" name="submit" alt="Buy it Now" />
</form>""" % (POSTBACK_ENDPOINT, self.as_p(), extra_fields, self.get_image()))


def sandbox(self):
    extra_fields = u''.join(['<input type="hidden" name="%s" value="%s" />' % (escape(name), escape(value)) for name, value in self.extra_options.iteritems()])  
    return mark_safe(u"""<form action="%s" method="post">
%s
%s
<input type="image" src="%s" border="0" name="submit" alt="Buy it Now" />
</form>""" % (SANDBOX_POSTBACK_ENDPOINT, self.as_p(), extra_fields, self.get_image()))
于 2010-10-07T01:22:05.557 回答
1

由于 Python 多重继承,我找到了更简洁的解决方案,而无需修改原始代码。

表格.py

from django import forms
from paypal.standard.widgets import ValueHiddenInput
from paypal.standard.forms import PayPalEncryptedPaymentsForm


class PayPalAddressFormMixin(forms.Form):

    address1 = forms.CharField(widget=ValueHiddenInput())
    address2 = forms.CharField(widget=ValueHiddenInput())
    city = forms.CharField(widget=ValueHiddenInput())
    country = forms.CharField(widget=ValueHiddenInput())
    zip = forms.CharField(widget=ValueHiddenInput())

    email = forms.CharField(widget=ValueHiddenInput())
    first_name = forms.CharField(widget=ValueHiddenInput())
    last_name = forms.CharField(widget=ValueHiddenInput())


class PayPalEncryptedPaymentsAddressForm(PayPalEncryptedPaymentsForm, PayPalAddressFormMixin):
    pass

在views.py中你可以像往常一样设置初始值

paypal_dict = {
    # ...

    # preopulate paypal checkout page
    "email": invoice.user.email,
    "first_name": invoice.user.first_name,
    "last_name": invoice.user.last_name,
    "address1": invoice.user.address.street,
    "city": invoice.user.address.city,
    "country": invoice.user.address.get_country_display,
    "address_country_code": invoice.user.address.country
}

form = PayPalEncryptedPaymentsAddressForm(initial=paypal_dict)
于 2013-09-23T13:53:25.453 回答