-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathform.js
163 lines (129 loc) · 5.13 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function show_error(input, message){
$('#input_' + input).addClass('error');
$('#error_' + input).html(message).show();
return false
}
function clear_error(input){
$('#input_' + input).removeClass('error');
$('#error_' + input).html('').hide();
}
function lock_inputs(){
$('input').attr('disabled', 'disabled');
$('#submit_button').attr('disabled', 'disabled');
$('#transaction_error').hide();
}
function unlock_inputs(){
$('input').removeAttr('disabled');
$('#submit_button').removeAttr('disabled');
$('#progress_message').hide();
}
function validate(field, value){
if(field == 'number'){
if(!value.length) return show_error(field, 'Please enter a credit card number');
}
if(field == 'cvc'){
if(!value.length) return show_error(field, 'Please enter a CVC');
}
if(field == 'exp_month'){
if(!value.length) return show_error(field, 'Please enter a month');
if(value < 1 || value > 12) return show_error(field, 'Month should be between 1 and 12');
}
if(field == 'exp_year'){
if(!value.length) return show_error(field, 'Please enter a year');
}
if(field == 'amount'){
if(!value.length) return show_error(field, 'Please enter an amount');
if(value < 0.50) return show_error(field, 'Minimum charge is $0.50');
}
clear_error(field);
return true;
}
function sanitize(field){
var input = $('#input_' + field);
var value = $.trim(input.val());
if(field == 'number'){
value = value.replace(/[^\d]+/g, '');
}
if(field == 'cvc'){
value = value.replace(/[^\d]+/g, '');
}
if(field == 'exp_month'){
value = value.replace(/[^\d]+/g, '');
value = value.replace(/^0+/, '');
}
if(field == 'exp_year'){
value = value.replace(/[^\d]+/g, '');
if(value.length == 2) value = '20' + value;
}
if(field == 'amount'){
value = value.replace(/[^\d\.]+/g, '');
if(value.length) value = parseFloat(value).toFixed(2);
}
input.val(value);
}
$(function(){
$('input').attr('autocomplete', 'off');
unlock_inputs();
$('input').blur(function(){
var input = $(this);
var name = input.attr('id').replace(/^input_/, '');
sanitize(name);
validate(name, input.val());
});
$('input').keydown(function(){
$('#transaction_error').hide();
});
$('#payment_form').submit(function(){
$('input').blur(); //trigger sanitize/validate functions
if($('input.error').length > 0){
unlock_inputs()
$('input.error:first').focus();
return false;
}
lock_inputs();
var params = {};
params['number'] = $('#input_number').val();
params['cvc'] = $('#input_cvc').val();
params['exp_month'] = $('#input_exp_month').val();
params['exp_year'] = $('#input_exp_year').val();
var amount = $('#input_amount').val().replace(/\./g, '');
$('#progress_message').html('Validating card data...').show();
Stripe.createToken(params, amount, function(status, response){
if(response.error){
$('#transaction_error').html(response.error.message).slideDown();
unlock_inputs();
return false;
}
var charge = {};
charge['token'] = response['id'];
charge['amount'] = amount;
charge['note'] = $('#input_note').val();
$('#progress_message').html('Submitting charge...').show();
$.post(location.href, charge, function(response){
try{
response = JSON.parse(response);
}catch(err){
//first way to fail: server returns something that's not json
if(!$.trim(response).length){
response = {error: 'Server returned empty response during charge attempt'};
}else{
response = {error: 'Server returned invalid response:<br /><br />' + response};
}
}
if(response['success']){
$('#wrap').html("<h2>MISSION ACCOMPLISHED</h2><b>$" + response['amount'] + " is making its way to our bank account.</b><br />Transaction ID: " + response['id'] + "<br /><br />").css('background-color', '#fff');
$("<a href='javascript:void(0);' class='red'>Make another charge</a>").click(function(){ location.href = location.href; }).appendTo('#wrap');
}else{
//second way to fail: stripe declined the charge
$('#transaction_error').html(response['error']).slideDown();
unlock_inputs();
}
}).error(function(response){
//third way to fail: server responds with an error code
$('#transaction_error').html('Error on charge attempt:<br /><br />' + response.responseText).slideDown();
unlock_inputs();
});
});
return false;
});
});