Allow payment providers to define custom form fields / templates

This commit is contained in:
Raphael Michel
2015-03-06 22:20:04 +01:00
parent 29cedbaac3
commit e630858a35
8 changed files with 70 additions and 1 deletions

View File

@@ -1,5 +1,9 @@
from decimal import Decimal from decimal import Decimal
from django.forms import Form
from django.template import Context
from django.template.loader import get_template
from pretix.base.settings import SettingsSandbox from pretix.base.settings import SettingsSandbox
@@ -50,6 +54,41 @@ class BasePaymentProvider:
def settings_form_fields(self) -> dict: def settings_form_fields(self) -> dict:
""" """
A dictionary. The keys should be (unprefixed) EventSetting keys, A dictionary. The keys should be (unprefixed) EventSetting keys,
the values should be corresponding django form fields the values should be corresponding django form fields.
We suggest returning a collections.OrderedDict object instead of a dict.
""" """
raise NotImplementedError() raise NotImplementedError()
@property
def checkout_form_fields(self) -> dict:
"""
A dictionary. The keys should be unprefixed field names,
the values should be corresponding django form fields.
We suggest returning a collections.OrderedDict object instead of a dict.
"""
# TODO: Proper handling of required=True fields in HTML
return {}
def checkout_form(self, request) -> Form:
"""
Returns the Form object of the form that should be displayed when the
user selects this provider as his payment method.
"""
form = Form(
data=(request.POST if request.method == 'POST' else None),
prefix='payment_%s' % self.identifier
)
form.fields = self.checkout_form_fields
return form
def checkout_form_render(self, request) -> str:
"""
Returns the HTML of the form that should be displayed when the user
selects this provider as his payment method.
"""
form = self.checkout_form(request)
template = get_template('pretixpresale/event/checkout_payment_form_default.html')
ctx = Context({'request': request, 'form': form})
return template.render(ctx)

View File

@@ -24,6 +24,8 @@
{% bootstrap_form provider.form layout='horizontal' %} {% bootstrap_form provider.form layout='horizontal' %}
</div> </div>
</div> </div>
{% empty %}
<em>{% trans "There are no payment providers available. Please go to the plugin settings and activate one or more payment plugins." %}</em>
{% endfor %} {% endfor %}
</fieldset> </fieldset>
<div class="form-group submit-group"> <div class="form-group submit-group">

View File

@@ -1,4 +1,6 @@
from collections import OrderedDict from collections import OrderedDict
from django.template import Context
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django import forms from django import forms
@@ -16,3 +18,8 @@ class BankTransfer(BasePaymentProvider):
required=False required=False
)) ))
]) ])
def checkout_form_render(self, request) -> str:
template = get_template('pretixplugins/banktransfer/checkout_payment_form.html')
ctx = Context({'request': request, 'event': self.event, 'settings': self.settings})
return template.render(ctx)

View File

@@ -0,0 +1,10 @@
{% load i18n %}
<p>{% blocktrans trimmed %}
After completing your purchase, we will ask you to transfer the money to the following
bank account, using a personal reference code.
{% endblocktrans %}</p>
<address>
{{ settings.bank_details|linebreaksbr }}
</address>

View File

@@ -10,3 +10,10 @@ class Stripe(BasePaymentProvider):
verbose_name = _('Credit Card via Stripe') verbose_name = _('Credit Card via Stripe')
settings_form_fields = OrderedDict([ settings_form_fields = OrderedDict([
]) ])
checkout_form_fields = OrderedDict([
('cc_number',
forms.CharField(
label=_('Credit card number'),
required=False
))
])

View File

@@ -23,6 +23,7 @@
</div> </div>
<div id="payment_{{ p.provider.identifier }}" class="panel-collapse collapse"> <div id="payment_{{ p.provider.identifier }}" class="panel-collapse collapse">
<div class="panel-body form-horizontal"> <div class="panel-body form-horizontal">
{{ p.form }}
</div> </div>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,2 @@
{% load bootstrap3 %}
{% bootstrap_form form layout='horizontal' %}

View File

@@ -218,6 +218,7 @@ class PaymentDetails(EventViewMixin, CartDisplayMixin, EventLoginRequiredMixin,
providers.append({ providers.append({
'provider': provider, 'provider': provider,
'fee': fee, 'fee': fee,
'form': provider.checkout_form_render(self.request),
}) })
return providers return providers