From e047c9025ce02f661bdb5cb3c035fb32c241aea5 Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Mon, 19 Dec 2011 23:21:20 +0000 Subject: [PATCH] Added support for Buy Now button with product/price options. See: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_buynow_buttons#id08A2G0Y06X4 fields = {'item_name': 'name', 'item_options': (('10.00', 'Ten dollars'), ('20.00', 'Twenty dollars')), ... } --- paypal/standard/forms.py | 10 ++++++++- paypal/standard/widgets.py | 42 +++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/paypal/standard/forms.py b/paypal/standard/forms.py index e9992a5..63a7da4 100644 --- a/paypal/standard/forms.py +++ b/paypal/standard/forms.py @@ -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) @@ -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 @@ -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"""
%s diff --git a/paypal/standard/widgets.py b/paypal/standard/widgets.py index 51aea94..46011f0 100644 --- a/paypal/standard/widgets.py +++ b/paypal/standard/widgets.py @@ -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'' % flatatt(final_attrs)) \ No newline at end of file + return mark_safe(u'' % 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 = """ + + """ + 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 = """
+ %(name)s +
+
+ + %(hiddens)s + """ % {'options': '\n'.join(options), + 'hiddens': '\n'.join(hiddens), + 'name': 'Amount'} + + return mark_safe(output)