-
Notifications
You must be signed in to change notification settings - Fork 0
/
enterbrain.js
172 lines (156 loc) · 5.25 KB
/
enterbrain.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
164
165
166
167
168
169
170
171
172
Drupal.behaviors.enterbrain = {
attach: function (context, settings) {
var $ibanField = $('[data-enterbrain-field=iban]', context);
if (!$ibanField.length) {
return;
}
var $ibanFieldWrapper = $ibanField.closest('.form-item');
var $ibanMessage = $('<div class="enterbrain-iban-message">').appendTo($ibanFieldWrapper);
var $bicField = $('[data-enterbrain-field=bic]', context);
var $bicFieldWrapper = $bicField.closest('.form-item');
var $methodForm = $ibanField.closest('.payment-method-form');
var $selector = $methodForm.closest('.paymethod-select-wrapper').find('.paymethod-select-radios');
var valid = false;
var active = !$selector.length;
var firstTry = true;
var querying = false;
var lastValidatedIban = null;
var lastValidatedBic = null;
// Run only in the right context.
if (!$ibanField.length || !$bicField.length) {
return;
}
function checkActive() {
active = !$selector.length || $selector.find('.form-radio:checked').val() == $methodForm.data('pmid');
disableSubmitButton(!valid);
}
$selector.change(checkActive);
checkActive();
// Hide the bic field only if it’s empty (it might not be due to server side validation!)
if (!$bicField.val()) {
$bicFieldWrapper.hide();
}
function preValidateIban(iban) {
// Check for special characters other than spaces and hyphens.
// The IBAN library is too forgiving here.
var match = iban.match(/[0-9a-zA-Z -]+/)
if (match === null || match[0] !== iban) {
return false;
}
// Validate iban.
if (typeof IBAN !== 'undefined' && !IBAN.isValid(iban)) {
return false;
}
return true;
}
function validateIban(iban) {
if (querying) {
return;
}
// Space and hyphen are the only characters that pass prevalidation.
var strippedIban = iban.replace(/[ -]/g, '');
// Don’t validate the same iban twice.
if (strippedIban === lastValidatedIban) {
if (lastValidatedBic) {
$bicField.val(lastValidatedBic);
showMessage('show-bic');
} else {
showMessage('ok');
}
return;
}
if (preValidateIban(iban)) {
firstTry = false;
querying = true;
// Lock the field while checking.
$ibanField.prop('disabled', true);
showMessage('wait');
$.get('/enterbrain/check-iban/' + strippedIban).done(function (response) {
if (response.valid) {
lastValidatedIban = strippedIban;
if (response.bic) {
lastValidatedBic = response.bic;
$bicField.val(response.bic);
showBicField(false);
showMessage('show-bic');
} else {
lastValidatedBic = null;
resetBicField();
showBicField(true);
$bicField[0].focus();
showMessage('ok');
}
} else {
resetBicField();
showMessage('bad');
}
}).fail(function (response) {
resetBicField();
showBicField(true);
$bicField[0].focus();
showMessage('error');
}).always(function () {
querying = false;
// Release the iban field.
$ibanField.prop('disabled', false);
});
} else {
// Prevalidation failed.
resetBicField();
showMessage('bad');
}
}
function resetBicField() {
if ($bicFieldWrapper.is(':hidden')) {
$bicField.val('');
}
}
function showBicField(show) {
if (show) {
$bicFieldWrapper.slideDown(200);
} else {
$bicFieldWrapper.slideUp(200);
}
}
function showMessage(type) {
if (firstTry) {
return;
}
var message = '';
var cls = '';
if (type == 'wait') {
message = '<span class="enterbrain-icon-spinner"></span>' + Drupal.t('Checking your IBAN...');
disableSubmitButton(true);
} else if (type == 'ok') {
message = '<span class="enterbrain-icon-ok"></span>' + Drupal.t('IBAN checked. Please enter your BIC below.');
cls = 'field-success';
disableSubmitButton(false);
} else if (type == 'show-bic') {
message = '<span class="enterbrain-icon-ok"></span>' + Drupal.t('BIC: @bic', {'@bic': lastValidatedBic});
cls = 'field-success';
disableSubmitButton(false);
} else if (type == 'bad') {
message = '<span class="enterbrain-icon-bad"></span>' + Drupal.t('Please enter a valid IBAN.');
cls = 'field-error';
disableSubmitButton(true);
} else if (type == 'error') {
message = Drupal.t('Unable to check IBAN.');
disableSubmitButton(false);
}
$ibanMessage.html(message);
$ibanFieldWrapper.removeClass('field-success field-error').addClass(cls);
}
function disableSubmitButton(state) {
valid = !state;
state = state && active;
$ibanField.closest('form').find('input.webform-submit').prop('disabled', state);
}
$ibanField.on('change', function () {
firstTry = false;
validateIban(this.value);
});
$ibanField.on('input', function () {
validateIban(this.value);
});
}
};