Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for price/option drop-down on Buy Now button #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion paypal/standard/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.conf import settings
from django.utils.safestring import mark_safe
from paypal.standard.conf import *
from paypal.standard.widgets import ValueHiddenInput, ReservedValueHiddenInput
from paypal.standard.widgets import (ValueHiddenInput, ReservedValueHiddenInput,
ItemOptionsInput)
from paypal.standard.conf import (POSTBACK_ENDPOINT, SANDBOX_POSTBACK_ENDPOINT,
RECEIVER_EMAIL)

Expand Down Expand Up @@ -59,6 +60,7 @@ class PayPalPaymentsForm(forms.Form):
item_name = forms.CharField(widget=ValueHiddenInput())
item_number = forms.CharField(widget=ValueHiddenInput())
quantity = forms.CharField(widget=ValueHiddenInput())
item_options = forms.ChoiceField(widget=ItemOptionsInput())

# Subscription Related.
a1 = forms.CharField(widget=ValueHiddenInput()) # Trial 1 Price
Expand Down Expand Up @@ -99,6 +101,12 @@ def __init__(self, button_type="buy", *args, **kwargs):
super(PayPalPaymentsForm, self).__init__(*args, **kwargs)
self.button_type = button_type

# If item_options have been specified, populate the item_options select
# with the given choices.
item_options = kwargs['initial'].get('item_options', ())
if item_options:
self.fields['item_options'].choices = item_options

def render(self):
return mark_safe(u"""<form action="%s" method="post">
%s
Expand Down
42 changes: 41 additions & 1 deletion paypal/standard/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,44 @@ def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type=self.input_type)
if value != '':
final_attrs['value'] = force_unicode(value)
return mark_safe(u'<input%s />' % flatatt(final_attrs))
return mark_safe(u'<input%s />' % flatatt(final_attrs))

class ItemOptionsInput(ValueHiddenInput):
""" Displays a list of options on the PayPal button. Each option has a
different price.

choices is a tuple of amount/name pairs:
(('10.00', 'Ten Dollars'),
('20.00', 'Twenty Dollars'))
"""
def render(self, name, value, attrs=None, choices=()):
if not self.choices:
return ''

hiddens = []
options = []
count = 0
hidden = """<input type="hidden" name="option_select%(count)s" value="%(name)s">
<input type="hidden" name="option_amount%(count)s" value="%(amount)s">
"""
option = '<option value="%(name)s">%(name)s</option>'

for amount, opt_name in self.choices:
vars = {'count': count, 'amount': amount, 'name': opt_name}
options.append(option % vars)
hiddens.append(hidden % vars)
count += 1

output = """<table><tr><td>
<input type="hidden" name="on0" value="%(name)s">%(name)s
</td></tr><tr><td>
<select name="os0">
%(options)s
</select></table>
<input type="hidden" name="option_index" value="0">
%(hiddens)s
""" % {'options': '\n'.join(options),
'hiddens': '\n'.join(hiddens),
'name': 'Amount'}

return mark_safe(output)