Files
@ 3001324d5e94
Branch filter:
Location: symposion_app/vendor/registripe/forms.py
3001324d5e94
6.1 KiB
text/x-python
git subrepo pull vendor/registrasion
subrepo:
subdir: "vendor/registrasion"
merged: "3545a80"
upstream:
origin: "git@gitlab.com:tchaypo/registrasion.git"
branch: "lca2018"
commit: "3545a80"
git-subrepo:
version: "0.3.1"
origin: "???"
commit: "???"
subrepo:
subdir: "vendor/registrasion"
merged: "3545a80"
upstream:
origin: "git@gitlab.com:tchaypo/registrasion.git"
branch: "lca2018"
commit: "3545a80"
git-subrepo:
version: "0.3.1"
origin: "???"
commit: "???"
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import copy
from registripe import models
from django import forms
from django.core.urlresolvers import reverse
from django.db.models import F, Q
from django.forms import widgets
from django.utils import timezone
from django_countries import countries
from django_countries.fields import LazyTypedChoiceField
from django_countries.widgets import CountrySelectWidget
class NoRenderWidget(forms.widgets.HiddenInput):
def render(self, name, value, attrs=None):
return "<!-- no widget: " + name + " -->"
def secure_striped(field):
''' Calls stripe() with secure=True. '''
return striped(field, True)
def striped(field, secure=False):
oldwidget = field.widget
field.widget = StripeWidgetProxy(oldwidget, secure)
return field
class StripeWidgetProxy(widgets.Widget):
def __init__(self, underlying, secure=False):
self.underlying = underlying
self.secure = secure
def __deepcopy__(self, memo):
copy_underlying = copy.deepcopy(self.underlying, memo)
return type(self)(copy_underlying, self.secure)
def __getattribute__(self, attr):
spr = super(StripeWidgetProxy, self).__getattribute__
if attr in ("underlying", "render", "secure", "__deepcopy__"):
return spr(attr)
else:
return getattr(self.underlying, attr)
def render(self, name, value, attrs=None):
if not attrs:
attrs = {}
attrs["data-stripe"] = name
if self.secure:
name = ""
return self.underlying.render(name, value, attrs=attrs)
class CreditCardForm(forms.Form):
required_css_class = 'label-required'
def _media(self):
js = (
'https://js.stripe.com/v2/',
reverse("registripe_pubkey"),
)
return forms.Media(js=js)
media = property(_media)
number = secure_striped(forms.CharField(
required=False,
label="Credit card Number",
help_text="Your credit card number, with or without spaces.",
max_length=255,
))
exp_month = secure_striped(forms.IntegerField(
required=False,
label="Card expiry month",
min_value=1,
max_value=12,
))
exp_year = secure_striped(forms.IntegerField(
required=False,
label="Card expiry year",
help_text="The expiry year for your card in 4-digit form",
min_value=timezone.now().year,
))
cvc = secure_striped(forms.CharField(
required=False,
min_length=3,
max_length=4,
))
stripe_token = forms.CharField(
max_length=255,
# required=True,
widget=NoRenderWidget(),
)
name = striped(forms.CharField(
required=True,
label="Cardholder name",
help_text="The cardholder's name, as it appears on the credit card",
max_length=255,
))
address_line1 = striped(forms.CharField(
required=True,
label="Cardholder account address, line 1",
max_length=255,
))
address_line2 = striped(forms.CharField(
required=False,
label="Cardholder account address, line 2",
max_length=255,
))
address_city = striped(forms.CharField(
required=True,
label="Cardholder account city",
max_length=255,
))
address_state = striped(forms.CharField(
required=True,
max_length=255,
label="Cardholder account state or province",
))
address_zip = striped(forms.CharField(
required=True,
max_length=255,
label="Cardholder account postal code",
))
address_country = striped(LazyTypedChoiceField(
label="Cardholder account country",
choices=countries,
widget=CountrySelectWidget,
))
class StripeRefundForm(forms.Form):
required_css_class = 'label-required'
def __init__(self, *args, **kwargs):
'''
Arguments:
user (User): The user whose charges we should filter to.
min_value (Decimal): The minimum value of the charges we should
show (currently, credit notes can only be cashed out in full.)
'''
user = kwargs.pop('user', None)
min_value = kwargs.pop('min_value', None)
super(StripeRefundForm, self).__init__(*args, **kwargs)
payment_field = self.fields['payment']
qs = payment_field.queryset
if user:
qs = qs.filter(
charge__customer__user=user,
)
if min_value is not None:
# amount >= amount_to_refund + amount_refunded
# No refunds yet
q1 = (
Q(charge__amount_refunded__isnull=True) &
Q(charge__amount__gte=min_value)
)
# There are some refunds
q2 = (
Q(charge__amount_refunded__isnull=False) &
Q(charge__amount__gte=(
F("charge__amount_refunded") + min_value))
)
qs = qs.filter(q1 | q2)
payment_field.queryset = qs
payment = forms.ModelChoiceField(
required=True,
queryset=models.StripePayment.objects.all(),
)
'''{
From stripe.js details:
Card details:
The first argument to createToken is a JavaScript object containing credit
card data entered by the user. It should contain the following required
members:
number: card number as a string without any separators
(e.g., "4242424242424242")
exp_month: two digit number representing the card's expiration month
(e.g., 12)
exp_year: two or four digit number representing the card's expiration year
(e.g., 2017)
(The expiration date can also be passed as a single string.)
cvc: optional, but we highly recommend you provide it to help prevent fraud.
This is the card's security code, as a string (e.g., "123").
The following fields are entirely optional and cannot result in a token
creation failure:
name: cardholder name
address_line1: billing address line 1
address_line2: billing address line 2
address_city: billing address city
address_state: billing address state
address_zip: billing postal code as a string (e.g., "94301")
address_country: billing address country
}
'''
|