-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
forms.py
61 lines (49 loc) · 2.25 KB
/
forms.py
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
import braintree
from ..forms import CreditCardPaymentFormWithName
class BraintreePaymentForm(CreditCardPaymentFormWithName):
transaction_id = None
def clean(self):
data = self.cleaned_data
if not self.errors and not self.payment.transaction_id:
result = braintree.Transaction.sale({
'amount': str(self.payment.total),
'billing': self.get_billing_data(),
'credit_card': self.get_credit_card_clean_data(),
'customer': self.get_customer_data(),
'options': {
'submit_for_settlement': True
},
'order_id': self.payment.description
})
if result.is_success:
self.transaction_id = result.transaction.id
else:
self._errors['__all__'] = self.error_class([result.message])
self.payment.change_status('error')
return data
def get_credit_card_clean_data(self):
if self.cleaned_data:
return {
'number': self.cleaned_data.get('number'),
'cvv': self.cleaned_data.get('cvv2'),
'cardholder_name': self.cleaned_data.get('name'),
'expiration_month': self.cleaned_data.get('expiration').month,
'expiration_year': self.cleaned_data.get('expiration').year}
def get_billing_data(self):
return {
'first_name': self.payment.billing_first_name,
'last_name': self.payment.billing_last_name,
'street_address': self.payment.billing_address_1,
'extended_address': self.payment.billing_address_2,
'locality': self.payment.billing_city,
'region': self.payment.billing_country_area,
'postal_code': self.payment.billing_postcode,
'country_code_alpha2': self.payment.billing_country_code}
def get_customer_data(self):
return {
'first_name': self.payment.billing_first_name,
'last_name': self.payment.billing_last_name}
def save(self):
braintree.Transaction.submit_for_settlement(self.transaction_id)
self.payment.transaction_id = self.transaction_id
self.payment.change_status('confirmed')