开发者

Django PayPal & Additional Options

I'm using Django PayPal. PayPal has a list of options you can pass on your button. I'm trying to add some of these to my 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)

But when I inspect the form, these fields are never added. How开发者_Python百科 can I get them to be added?


Here are some additional fields:

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


I hacked it in:

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()))


I found much cleaner solution without modifying original code thanks to Python multiple inheritance.

forms.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

in views.py you can set initial values as usual

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜