1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
from __future__ import unicode_literals
try:
from collections import OrderedDict
except ImportError:
from django.utils.datastructures import SortedDict as OrderedDict
from django import forms
from django.utils.translation import ugettext_lazy as _
from .fields import (CreditCardNumberField, CreditCardExpiryField,
CreditCardVerificationField, CreditCardNameField)
class PaymentForm(forms.Form):
'''
Payment form, suitable for Django templates.
When displaying the form remember to use *action* and *method*.
'''
#: Form action URL for template use
action = ''
#: Form method for template use, either "get" or "post"
method = 'post'
def __init__(self, data=None, action=None, method='post', provider=None,
payment=None, hidden_inputs=True, autosubmit=False):
if hidden_inputs and data is not None:
super(PaymentForm, self).__init__(auto_id=False)
for key, val in data.items():
widget = forms.widgets.HiddenInput()
self.fields[key] = forms.CharField(initial=val, widget=widget)
else:
super(PaymentForm, self).__init__(data=data)
self.action = action
self.autosubmit = autosubmit
self.method = method
self.provider = provider
self.payment = payment
class CreditCardPaymentForm(PaymentForm):
number = CreditCardNumberField(label=_('Card Number'), max_length=32,
required=True)
expiration = CreditCardExpiryField()
cvv2 = CreditCardVerificationField(
label=_('CVV2 Security Number'), required=False, help_text=_(
'Last three digits located on the back of your card.'
' For American Express the four digits found on the front side.'))
def __init__(self, *args, **kwargs):
super(CreditCardPaymentForm, self).__init__(
hidden_inputs=False, *args, **kwargs)
if hasattr(self, 'VALID_TYPES'):
self.fields['number'].valid_types = self.VALID_TYPES
class CreditCardPaymentFormWithName(CreditCardPaymentForm):
name = CreditCardNameField(label=_('Name on Credit Card'), max_length=128)
def __init__(self, *args, **kwargs):
super(CreditCardPaymentFormWithName, self).__init__(*args, **kwargs)
name_field = self.fields.pop('name')
fields = OrderedDict({'name': name_field})
fields.update(self.fields)
self.fields = fields
|