-
Notifications
You must be signed in to change notification settings - Fork 8
/
example1-he.js
384 lines (318 loc) · 13.4 KB
/
example1-he.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* Created by thoryachev on 22.11.2018.
*/
(function (document, apiKey) {
// Cache DOM Nodes ---------------------------------------------------------------------------------------------------
const form = document.getElementById('checkout-form-he');
const submitButton = document.getElementById('submit-button-he');
const cardProvider = document.getElementById('card-provider-he');
const errorsMessagesContainer = document.getElementById('errors-he');
const successQuery = document.querySelector('.first-example-he .success');
const backFormButton = document.querySelector('.back-on-form1-he');
const firstNameInput = document.getElementById('first-name-input-he');
const lastNameInput = document.getElementById('last-name-input-he');
const emailInput = document.getElementById('email-input-he');
const phoneInput = document.getElementById('phone-input-he');
const socialIdInput = document.getElementById('social-id-input-he');
// Helpers -----------------------------------------------------------------------------------------------------------
const errorsFromField = {};
function tokenizationStarted() {
form.classList.add('fadeOut');
form.style.display = 'none';
successQuery.style.display = 'block';
successQuery.querySelector('.wrap-loading').style.display = 'block';
successQuery.classList.add('fadeIn');
submitButton.disabled = true;
console.log('Tokenization started!');
}
function tokenizationFinished(error) {
successQuery.querySelector('.wrap-loading').style.display = 'none';
submitButton.disabled = false;
console.log('Tokenization finished!');
if(error) {
console.error(error);
const failedValidation = {
field: PayMe.fields.NONE, isValid: false, message: ''
};
// Checking is tokenization processing error
if(error.type && error.type === 'tokenize-error') {
// Handle tokenization processing error
const [ firstErrorMessage ] = Object.values(error.errors);
failedValidation.message = firstErrorMessage;
} else {
// Handle other errors from PayMe
failedValidation.message = error.message;
}
toggleValidationMessages(failedValidation);
} else {
firstNameInput.value = lastNameInput.value = emailInput.value = phoneInput.value = socialIdInput.value = '';
}
}
function showErrors(errorsFromField, validationResult) {
let lastElement = errorsFromField[Object.keys(errorsFromField).pop()];
errorsMessagesContainer.classList.remove('fadeOutDown');
if (!validationResult.message) {
errorsMessagesContainer.innerText = lastElement;
} else {
errorsMessagesContainer.innerText = validationResult.message;
}
}
function toggleValidationMessages(validationResult) {
delete errorsFromField[PayMe.fields.NONE];
if (validationResult.isValid) {
errorsMessagesContainer.classList.remove('fadeInUp');
errorsMessagesContainer.classList.add('fadeOutDown');
delete errorsFromField[validationResult.field]; // delete error from the object that passed validation
if (Object.keys(errorsFromField).length > 0) { // if the object still has errors - output them
showErrors(errorsFromField, validationResult);
errorsMessagesContainer.classList.remove('fadeOutDown');
errorsMessagesContainer.classList.add('fadeInUp');
}
} else {
errorsFromField[validationResult.field] = validationResult.message; // write errors to the object
errorsMessagesContainer.classList.remove('fadeOutDown');
errorsMessagesContainer.classList.add('fadeInUp');
if (Object.keys(errorsFromField).length > 0) { // check if there is an error in the object
showErrors(errorsFromField, validationResult); // and show its
}
}
}
function changeCardProviderIcon(cardVendor) {
const vendorsToClasses = {
'unknown': ['fas', 'fa-credit-card'],
'amex': ['fab', 'fa-cc-amex'],
'diners': ['fab', 'fa-cc-diners-club'],
'jcb': ['fab', 'fa-cc-jcb'],
'visa': ['fab', 'fa-cc-visa'],
'mastercard': ['fab', 'fa-cc-mastercard'],
'discover': ['fab', 'fa-cc-discover'],
};
for(let i = cardProvider.classList.length-1; i >= 0; i-- ){
cardProvider.classList.remove(cardProvider.classList[i]);
}
let item = vendorsToClasses[cardVendor] || vendorsToClasses['unknown'];
item.forEach( el => {
cardProvider.classList.add(el);
})
}
function addClass(fieldId, className) {
document.getElementById(fieldId).classList.add(className);
}
function removeClass(fieldId, className) {
document.getElementById(fieldId).classList.remove(className);
}
function showSuccessQuery(data) {
successQuery.querySelector('.name').innerHTML = "<span>שם פרטי:</span> " + data.payerName;
successQuery.querySelector('.email').innerHTML = "<span>דואר אלקטרוני:</span> " + data.payerEmail;
successQuery.querySelector('.phone').innerHTML = "<span>טלפון נייד:</span> " + data.payerPhone;
successQuery.querySelector('.socialId').innerHTML = "<span>תעודת זהות:</span> " + data.payerSocialId;
successQuery.querySelector('.token').innerHTML = "<span>טוקן:</span> " + data.token;
}
function runNativeFieldValidator(value, field, messages){
const validator = PayMe.validators[field];
const errors = validator.test(value);
let message;
if(errors && errors.required) {
message = messages.required
}
if(errors && errors.invalid) {
message = messages.invalid
}
return { isValid: !errors, field: field, message: message };
}
function createNativeFieldValidatorHandler(fieldName, messagesObject) {
return function(ev) {
const inputNode = this;
const validation = runNativeFieldValidator(ev.target.value, fieldName, messagesObject);
if (validation.isValid) {
inputNode.classList.remove('invalid');
inputNode.classList.add('valid');
} else {
inputNode.classList.remove('valid');
inputNode.classList.add('invalid');
}
toggleValidationMessages(validation);
}
}
// Misc --------------------------------------------------------------------------------------------------------------
const allFieldsReady = [];
const DEFAULT_SETTINGS = {
styles: {
base: {
'font-size': '16px',
'::placeholder': {'color': '#ACD7E4'},
'text-align': 'right'
},
invalid: {
'color': '#FF0000',
},
valid: {
'color': '#fff',
},
}
};
// Main --------------------------------------------------------------------------------------------------------------
function init() {
// Disable submit button until protected fields initialization
submitButton.disabled = true;
// Getting hosted fields integration manager
PayMe.create(apiKey, { testMode: true, language: 'he' }).then((instance) => {
const fields = instance.hostedFields();
// Protected fields ------------------------------------------------------
// Card Number
const cardNumberSettings = Object.assign({}, DEFAULT_SETTINGS, {
placeholder: 'מספר כרטיס אשראי',
messages: {
invalid: 'מספר כרטיס אשראי לא תקין',
required: 'שדה מספר כרטיס אשראי הינו חובה'
},
});
const cardNumber = fields.create(PayMe.fields.NUMBER, cardNumberSettings);
allFieldsReady.push(
cardNumber.mount('#card-number-container-he')
);
cardNumber.on('card-type-changed', ev => changeCardProviderIcon(ev.cardType));
cardNumber.on('keyup', toggleValidationMessages);
cardNumber.on('keyup', (e) => {
if (e.isValid) {
expiration.focus();
}
e.isEmpty ? removeClass('card-expiration-group-he', 'animate-card-option') : addClass('card-expiration-group-he', 'animate-card-option');
e.isEmpty ? removeClass('card-cvv-group-he', 'animate-card-option') : addClass('card-cvv-group-he', 'animate-card-option');
});
// Expiry Date
const expirationField = Object.assign({}, DEFAULT_SETTINGS, {
messages: {
invalid: 'כרטיס פג תוקף',
required: 'שדה תוקף הינו חובה'
},
});
const expiration = fields.create(PayMe.fields.EXPIRATION, expirationField);
allFieldsReady.push(
expiration.mount('#card-expiration-container-he')
);
expiration.on('keyup', toggleValidationMessages);
expiration.on('validity-changed', toggleValidationMessages);
expiration.on('keyup', (e) => {
if (e.isValid) {
cvc.focus();
}
});
// CVC/CVV
const cvcField = Object.assign({}, DEFAULT_SETTINGS, {
placeholder: 'CVV',
messages: {
invalid: 'CVV שגוי',
required: 'שדה CVV הינו חובה'
},
});
const cvc = fields.create(PayMe.fields.CVC, cvcField);
allFieldsReady.push(
cvc.mount('#card-cvv-container-he')
);
cvc.on('keyup', toggleValidationMessages);
cvc.on('validity-changed', toggleValidationMessages);
// AUX fields ------------------------------------------------------------
// First Name
const firstNameMessages = {
invalid: 'שדה שם פרטי חייב להכיל אותיות בלבד', required: 'שדה שם פרטי הינו חובה'
};
firstNameInput.addEventListener(
'keyup', createNativeFieldValidatorHandler(PayMe.fields.NAME_FIRST, firstNameMessages)
);
firstNameInput.addEventListener(
'focus', createNativeFieldValidatorHandler(PayMe.fields.NAME_FIRST, firstNameMessages)
);
// Last Name
const lastNameMessages = {
invalid: 'שדה שם משפחה חייב להכיל אותיות בלבד', required: 'שדה שם משפחה הינו חובה'
};
lastNameInput.addEventListener(
'keyup', createNativeFieldValidatorHandler(PayMe.fields.NAME_LAST, lastNameMessages)
);
lastNameInput.addEventListener(
'focus', createNativeFieldValidatorHandler(PayMe.fields.NAME_LAST, lastNameMessages)
);
// Email
const emailMessages = {
invalid: 'דואר אלקטרוני לא תקין', required: 'שדה דואר אלקטרוני הינו חובה'
};
emailInput.addEventListener(
'keyup', createNativeFieldValidatorHandler(PayMe.fields.EMAIL, emailMessages)
);
emailInput.addEventListener(
'focus', createNativeFieldValidatorHandler(PayMe.fields.EMAIL, emailMessages)
);
// Phone Number
const phoneMessages = {
invalid: 'טלפון לא תקין', required: 'שדה טלפון הינו חובה'
};
phoneInput.addEventListener(
'keyup', createNativeFieldValidatorHandler(PayMe.fields.PHONE, phoneMessages)
);
phoneInput.addEventListener(
'focus', createNativeFieldValidatorHandler(PayMe.fields.PHONE, phoneMessages)
);
// Social Id
const socialIdMessages = {
invalid: 'תעודת זהות שגויה', required: 'שדה תעודת זהות הינו חובה'
};
socialIdInput.addEventListener(
'keyup', createNativeFieldValidatorHandler(PayMe.fields.SOCIAL_ID, socialIdMessages)
);
socialIdInput.addEventListener(
'focus', createNativeFieldValidatorHandler(PayMe.fields.SOCIAL_ID, socialIdMessages)
);
// Wait for fields initialization ----------------------------------------
Promise.all(allFieldsReady).then(() => submitButton.disabled = false);
// Form submission handler -----------------------------------------------
const formSubmit = ev => {
ev.preventDefault();
const sale = {
payerFirstName: firstNameInput.value,
payerLastName: lastNameInput.value,
payerEmail: emailInput.value,
payerPhone: phoneInput.value,
payerSocialId: socialIdInput.value,
total: {
label: '🚀 Rubber duck',
amount: {
currency: 'ILS',
value: '55.00',
}
}
};
tokenizationStarted();
toggleValidationMessages({ field: PayMe.fields.NONE, isValid: true});
instance.tokenize(sale)
.then(data => {
console.log('Tokenization result::: ', data);
showSuccessQuery(data);
tokenizationFinished();
})
.catch(err => {
alert('Tokenization failed');
successQuery.style.display = 'none';
form.style.display = 'block';
form.classList.remove('fadeOut');
tokenizationFinished(err);
});
};
// Return and recreate handler -------------------------------------------
const clickToBackOnForm = () => {
successQuery.style.display = 'none';
instance.teardown();
form.removeEventListener('submit', formSubmit);
backFormButton.removeEventListener('click', clickToBackOnForm);
form.classList.remove('fadeOut');
form.classList.add('fadeIn');
form.style.display = 'block';
init();
};
// Events binding --------------------------------------------------------
form.addEventListener('submit', formSubmit);
backFormButton.addEventListener('click', clickToBackOnForm);
});
}
init();
})(document, 'ac76cbc1-9a83-47a4-82bd-1c82c4979fdd');