-
Notifications
You must be signed in to change notification settings - Fork 1
/
form.js
140 lines (122 loc) · 4.23 KB
/
form.js
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
function docReady(fn) {
// see if DOM is already available
if (document.readyState === "complete" || document.readyState === "interactive") {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
docReady(function() {
var elements = stripe.elements();
var style = {
base: {
iconColor: '#666EE8',
color: '#31325F',
lineHeight: '40px',
fontWeight: 300,
fontFamily: 'Helvetica Neue',
fontSize: '15px',
'::placeholder': {
color: '#CFD7E0',
},
},
};
var card = elements.create('cardNumber', {
style: style
});
card.mount('#card-element');
var cardCvcElement = elements.create('cardCvc', {
style: style
});
cardCvcElement.mount('#card-cvc-element');
var cardExpiryElement = elements.create('cardExpiry', {
style: style
});
cardExpiryElement.mount('#card-expiry-element');
function setOutcome(result) {
var successElement = document.querySelector('.success');
var errorElement = document.querySelector('.error');
successElement.classList.remove('visible');
errorElement.classList.remove('visible');
if (result.token) {
//successElement.querySelector('.token').textContent = result.token.id;
successElement.classList.add('visible');
var amount = document.getElementById('amount').value.replace(/\./g, '');
var formData = new FormData();
formData.append("token", result.token.id);
formData.append("amount", amount);
formData.append("note", document.getElementById("note").value);
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open("POST", location.href, true);
xhr.send(formData);
xhr.onload = function() {
var jsonResponse = xhr.response;
if(jsonResponse == null) {
errorElement.textContent = "Unknown Server Error";
successElement.classList.remove('visible');
errorElement.classList.add('visible');
unlock_inputs();
} else {
if (jsonResponse.success == false) {
errorElement.textContent = jsonResponse.error;
successElement.classList.remove('visible');
errorElement.classList.add('visible');
unlock_inputs();
} else {
document.getElementById("main").innerHTML = "<div class=\"group\"><label><span>SUCCESS</span>$" + jsonResponse['amount'] + " is making its way to our bank account </label><label><span>Transaction ID</span> " + jsonResponse['id'] + "</label><label><span>Again?</span><a href=\"\" onclick=\"document.reload\">Another Transaction</a><br /></label></div>";
}
}
};
} else if (result.error) {
unlock_inputs();
errorElement.textContent = result.error.message;
errorElement.classList.add('visible');
}
}
card.on('change', function(event) {
setOutcome(event);
});
document.getElementsByName('amount')[0].addEventListener('keydown', function (evt) {
amount = document.getElementById("amount").value;
var errorElement = document.querySelector('.error');
if (amount == "") {
errorElement.textContent = "Please enter a valid amount";
errorElement.classList.add('visible');
} else {
errorElement.classList.remove('visible');
}
});
document.querySelector('form').addEventListener('submit', function(e) {
e.preventDefault();
var errorElement = document.querySelector('.error');
amount = document.getElementById("amount").value;
if (amount == "") {
unlock_inputs();
errorElement.textContent = "Please enter a valid amount";
errorElement.classList.add('visible');
return false;
}
var options = {
address_zip: document.getElementById('postal_code').value,
cvc: document.getElementById('card-cvc-element').value
};
lock_inputs();
stripe.createToken(card, options).then(setOutcome);
});
function lock_inputs(){
var inputs = document.getElementsByClassName("field");
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = true;
}
document.getElementById("submit_button").disabled = true;
}
function unlock_inputs(){
var inputs = document.getElementsByClassName("field");
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}
document.getElementById("submit_button").disabled = false;
}
});