-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient.js
More file actions
159 lines (142 loc) · 4.04 KB
/
client.js
File metadata and controls
159 lines (142 loc) · 4.04 KB
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
const DomUtils = {
getEl: (selector) => window.document.querySelector(selector),
hasClass: (el, cssClass) => {
if (el.classList) {
return el.classList.contains(cssClass);
}
return !!el.className.match(new RegExp(`(\\s|^)${cssClass}(\\s|$)`));
},
removeClass: (el, cssClass) => {
if (el.classList) {
el.classList.remove(cssClass);
} else if (DomUtils.hasClass(el, cssClass)) {
const reg = new RegExp(`(\\s|^)${cssClass}(\\s|$)`);
el.className = el.className.replace(reg, ' ');
}
},
};
const SubmitButton = {
buttonElement: DomUtils.getEl('[data-submit-btn]'),
loaderElement: DomUtils.getEl('.btn__loader'),
enable: () => {
SubmitButton.buttonElement.disabled = false;
DomUtils.removeClass(SubmitButton.buttonElement, 'disabled-bkg');
},
setSubmitState: () => {
SubmitButton.buttonElement.disabled = true;
SubmitButton.loaderElement.style.display = 'inline-block';
},
removeSubmitState: () => {
SubmitButton.buttonElement.disabled = false;
SubmitButton.loaderElement.style.display = 'none';
}
};
const config = {
fields: {
card: {
selector: '[data-cc-card]',
},
cvv: {
selector: '[data-cc-cvv]',
},
exp: {
selector: '[data-cc-exp]',
},
name: {
selector: '[data-cc-name]',
placeholder: 'Full Name',
},
},
// css classes to be injected into the iframes.
// the properties allowed are restricted via whitelist.
// further, unrestricted styling can be applied to the div's in which the iframes are injected.
styles: {
input: {
'font-size': '16px',
color: '#00a9e0',
'font-family': 'monospace',
background: 'black',
},
'.card': {
'font-family': 'monospace',
},
':focus': {
color: '#00a9e0',
},
'.valid': {
color: '#43B02A',
},
'.invalid': {
color: '#C01324',
},
'@media screen and (max-width: 700px)': {
input: {
'font-size': '18px',
},
},
'input:-webkit-autofill': {
'-webkit-box-shadow': '0 0 0 50px white inset',
},
'input:focus:-webkit-autofill': {
'-webkit-text-fill-color': '#00a9e0',
},
'input.valid:-webkit-autofill': {
'-webkit-text-fill-color': '#43B02A',
},
'input.invalid:-webkit-autofill': {
'-webkit-text-fill-color': '#C01324',
},
'input::placeholder': {
color: '#aaa',
},
},
// these values correspond to css class names defined above
classes: {
empty: 'empty',
focus: 'focus',
invalid: 'invalid',
valid: 'valid',
},
};
function authorizeSession(callback) {
let request = new XMLHttpRequest();
request.onload = () => {
if (request.status >= 200 && request.status < 300) {
callback(JSON.parse(request.responseText));
} else {
throw new Error("error response: " + request.responseText);
}
request = null;
};
request.open("POST", "/api/authorize-session", true);
request.send();
}
const hooks = {
preFlowHook: authorizeSession,
};
const onCreate = (paymentForm) => {
const onSuccess = (clientToken) => {
console.log("submit success; clientToken=\""+clientToken+"\"");
alert("Tokenization request sent!\nCheck your webhook for results using clientToken.\n\nclientToken=\""+clientToken+"\" (also available in console)");
SubmitButton.removeSubmitState();
paymentForm.reset(() => {});
};
const onError = (error) => {
console.log("Tokenize Error: " + error.message);
alert("Tokenization request error: \"" + error.message + "\"");
SubmitButton.removeSubmitState();
paymentForm.reset(() => {});
};
const form = DomUtils.getEl('#form')
form.addEventListener('submit', (e) => {
e.preventDefault();
SubmitButton.setSubmitState();
paymentForm.onSubmit(onSuccess, onError);
});
const ccFields = window.document.getElementsByClassName('payment-fields');
for (let i = 0; i < ccFields.length; i++) {
DomUtils.removeClass(ccFields[i], 'disabled');
}
SubmitButton.enable();
};
window.firstdata.createPaymentForm(config, hooks, onCreate);