-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkamba-web-sdk.js
503 lines (387 loc) · 23.4 KB
/
kamba-web-sdk.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
function ready (fn)
{
if (document.readyState != 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(function(){
//Style for button Pay with Kamba - Merchant
var btnOpenWidgetKamba = document.querySelector(".btnOpenWidgetKamba");
btnOpenWidgetKamba.innerHTML = "Pagar com a Kamba";
var imgButtonKamba = document.createElement("img");
imgButtonKamba.src="https://image.ibb.co/mFZUTz/Pay_Logo_kamba.png";
imgButtonKamba.classList.add("classImgButtonKamba");
btnOpenWidgetKamba.appendChild(imgButtonKamba);
var classImgButtonKamba = document.querySelector(".classImgButtonKamba");
classImgButtonKamba.style.width = '25%';
classImgButtonKamba.style.marginLeft = '0.5rem';
btnOpenWidgetKamba.style.backgroundImage = 'linear-gradient(to left, #00FFB3, #00ff5f)';
btnOpenWidgetKamba.style.border = 'none';
btnOpenWidgetKamba.style.padding = '0.5rem';
btnOpenWidgetKamba.style.cursor = 'pointer';
btnOpenWidgetKamba.style.fontSize = '1rem';
btnOpenWidgetKamba.style.borderRadius = '0.3rem';
btnOpenWidgetKamba.style.fontFamily = "'Montserrat', sans-serif";
btnOpenWidgetKamba.style.display = 'flex';
btnOpenWidgetKamba.style.justifyContent = 'center';
btnOpenWidgetKamba.style.alignItems = 'center';
btnOpenWidgetKamba.style.boxSizing = 'border-box';
});
(function () {
(function bootstrap() {
'use strict'
window.KAMBA = window.KAMBA || {};
window.kamba = function kamba(initial_config, secondary_config) {
function ready (fn){
if (document.readyState != 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(function(){
//Send - Post request
let url;
let token = 'Token ';
if (secondary_config.environment == 'sandbox'){
url = "https://sandbox.usekamba.com/v1/checkouts/";
}else{
url = "https://api.usekamba.com/v1/checkouts/";
}
fetch(url, {method: 'POST',
headers: {
'Content-Type': 'application/json',
'authorization': token.concat(secondary_config.api_key)
},
body: JSON.stringify({
channel: initial_config.channel,
currency: initial_config.currency,
initial_amount: initial_config.initial_amount,
notes: initial_config.notes,
redirect_url_success: initial_config.redirect_url_success,
payment_method: initial_config.payment_method
})
}).then(function(response) {
if(response.ok) {
response.json().then(data => {
var initial_amount = new Number(data.initial_amount);
var total_amount = new Number(data.total_amount);
var dateConvert = new Date(data.created_at);
var newDateConvert = [dateConvert.getDate(), dateConvert.getMonth(), dateConvert.getFullYear()].join('/')+' às '+[dateConvert.getHours(), dateConvert.getMinutes(), dateConvert.getSeconds()].join(':');
var convertQrCode = data.qr_code.html;
console.log(convertQrCode);
var mainKambaModalContainer = document.createElement("main");
//Modal Container
var kambaModalContainer = document.getElementsByTagName("body")[0].appendChild(mainKambaModalContainer);
kambaModalContainer.classList.add("kambaModalContainer");
kambaModalContainer.style.width = '100vw';
kambaModalContainer.style.height = '100%';
kambaModalContainer.style.background = 'rgba(0,0,0,.25)';
kambaModalContainer.style.position = 'fixed';
kambaModalContainer.style.top = '0';
kambaModalContainer.style.left = '0';
kambaModalContainer.style.zIndex = '1000000000000000000000';
kambaModalContainer.style.display = 'flex';
kambaModalContainer.style.justifyContent = 'center';
kambaModalContainer.style.alignItems = 'center';
kambaModalContainer.style.boxSizing = 'border-box';
kambaModalContainer.style.paddingRight = '1rem';
kambaModalContainer.style.overflow = 'auto';
//Template
const kambaWidget = `
<div class="kambaModalWidget">
<header class="checkoutHeader">
<div class="securityPay">
<div class="textSecurityPay"><img src="https://image.ibb.co/bxv8MK/icons8_lock_kamba.png" class="lock"> <span class="ps"> Pagamento seguro</span></div>
</div>
</header>
<section>
<article class="headerWidget">
<div class="qrPart">
<div class="detailQr">
<div class="divSvg">
<svg viewBox="0 0 625 625" preserveAspectRatio="xMidYMid meet" class="imgQr">
${data.qr_code.svg}
</svg>
<div class="textValidate">
Válido até ${newDateConvert}
</div>
</div>
</div>
</div>
<div class="partDetailPay">
<div class="payDetail">
<ul class="listProprietyProduct">
<li class="nameProduct"><b> ${data.notes} </b></li>
<li class="priceProduct"><b>${initial_amount.toLocaleString('pt-ao', {style: 'currency', currency: initial_config.currency})} </b></li>
</ul>
<ul class="listTotal">
<li class="descriptionTotal"><b>TOTAL</b></li>
<li class="priceTotal"><b>${total_amount.toLocaleString('pt-ao', {style: 'currency', currency: initial_config.currency})} </b></li>
</ul>
</div>
</div>
</article>
<article>
<div class="descriptionKamba">
<div class="helpKamba">
<div class="optionHelpKamba1">
- Abra o App em seu telefone e escaneie o código
</div>
<div class="optionHelpKamba2">
- Como faço para digitalizar o código?
</div>
<div class="optionHelpKamba3">- Não tem uma conta Kamba? <a href="#" class="appLinkKamba"> Baixe o App</a>
</div>
</div>
</div>
</article>
<footer class="footerKamba">
<div class="descritionKambaMerchant">Pagar <b> ${data.merchant.business_name} </b>
</div>
<div class="btnCloseWidgetKamba">
Fechar
</div>
</footer>
</section>
</div>`
kambaModalContainer.innerHTML = kambaWidget;
//Style Widget Modal
var kambaModalWidget = document.querySelector("main .kambaModalWidget");
kambaModalWidget.style.borderRadius = '0.2rem';
kambaModalWidget.style.overflow = 'auto';
kambaModalWidget.style.background = '#fff';
kambaModalWidget.style.width = '100%';
kambaModalWidget.style.height = '100%';
kambaModalWidget.style.position = 'absolute';
kambaModalWidget.style.fontFamily = "'Montserrat', sans-serif";
kambaModalWidget.style.fontSize = '0.85rem';
kambaModalWidget.style.boxShadow = '0 5px 8px 0 rgba(0,0,0,.2), 0 7px 20px 0 rgba(0,0,0,.10)';
//Header
var checkoutHeader = document.querySelector(".checkoutHeader");
checkoutHeader.style.padding = '1rem 0 0 1rem';
//Body
var headerWidget = document.querySelector(".headerWidget");
headerWidget.style.width = '100%';
headerWidget.style.float = 'left';
headerWidget.style.marginTop = '1rem';
headerWidget.style.background = 'white';
var securityPay = document.querySelector(".securityPay");
securityPay.style.marginRight = '1rem';
securityPay.style.float = 'right';
var textSecurityPay = document.querySelector(".textSecurityPay");
textSecurityPay.style.textDecoration = 'none';
textSecurityPay.style.display = 'flex';
textSecurityPay.style.justifyContent = 'center';
textSecurityPay.style.alignItems = 'center';
textSecurityPay.style.boxSizing = 'border-box';
var ps = document.querySelector(".ps");
ps.style.marginLeft = '0.2rem';
ps.style.color = '#666666';
ps.fontSize = '0.8rem';
var qrPart = document.querySelector(".qrPart");
qrPart.style.width = '100%';
qrPart.style.background = "#00ff5f";
qrPart.style.position = 'relative';
var detailQr = document.querySelector(".detailQr");
detailQr.style.width = '90%';
detailQr.style.float = 'left';
detailQr.style.background = 'white';
detailQr.style.boxSizing = 'border-box';
detailQr.style.textAlign = 'center';
var divSvg = document.querySelector(".divSvg");
divSvg.style.textAlign = 'center';
divSvg.style.padding = '0 1rem 1rem 1rem';
divSvg.style.width = '100%';
var imgQr = document.querySelector(".imgQr");
imgQr.style.width = '50%';
imgQr.style.height = '50%';
imgQr.style.textAlign = 'center';
imgQr.style.boxShadow = '0px 0px 5px #7f7f7f';
imgQr.style.padding = '0.5rem';
imgQr.style.borderRadius = '0.3rem';
var textValidate = document.querySelector(".textValidate");
textValidate.style.textAlign = 'center';
textValidate.style.fontSize = '0.72rem';
textValidate.style.marginTop = '1rem';
textValidate.style.float = 'left';
textValidate.style.width = '100%';
//Pay Detail
var partDetailPay = document.querySelector(".partDetailPay");
partDetailPay.style.width = '100%';
partDetailPay.style.float = 'left';
partDetailPay.style.background = 'white';
var payDetail = document.querySelector(".payDetail");
payDetail.style.width = '92%';
payDetail.style.float = 'left';
payDetail.style.margin = '1rem 1rem 0 1rem';
var listProprietyProduct = document.querySelector(".listProprietyProduct");
listProprietyProduct.style.width = '100%';
listProprietyProduct.style.listStyle = 'none';
listProprietyProduct.style.float = 'left';
listProprietyProduct.style.marginLeft = '0';
listProprietyProduct.style.paddingLeft = '0';
listProprietyProduct.style.background = 'white';
var nameProduct = document.querySelector(".nameProduct");
nameProduct.style.float = 'left';
var priceProduct = document.querySelector(".priceProduct");
priceProduct.style.float = 'right';
var listTotal = document.querySelector(".listTotal");
listTotal.style.width = '100%';
listTotal.style.listStyle = 'none';
listTotal.style.float = 'left';
listTotal.style.marginLeft = '0';
listTotal.style.paddingLeft = '0';
listTotal.style.background = 'white';
listTotal.style.borderBottom = '1px solid #D2CFCF';
listTotal.style.paddingBottom = '0.1rem';
var descriptionTotal = document.querySelector(".descriptionTotal");
descriptionTotal.style.float = 'left';
var priceTotal = document.querySelector(".priceTotal");
priceTotal.style.float = 'right';
var descriptionKamba = document.querySelector(".descriptionKamba");
descriptionKamba.style.width = '90%';
descriptionKamba.style.padding = '0 1rem';
descriptionKamba.style.textAlign = 'center';
var helpKamba = document.querySelector(".helpKamba");
helpKamba.style.textAlign = 'center';
var optionHelpKamba1 = document.querySelector(".optionHelpKamba1");
optionHelpKamba1.style.marginTop = '0.5rem';
var optionHelpKamba2 = document.querySelector(".optionHelpKamba2");
optionHelpKamba2.style.marginTop = '0.8rem';
var optionHelpKamba3 = document.querySelector(".optionHelpKamba3");
optionHelpKamba3.style.marginTop = '0.8rem';
var appLinkKamba = document.querySelector(".appLinkKamba");
appLinkKamba.style.textDecoration = 'none';
appLinkKamba.style.color = '#0099ff';
var footerKamba = document.querySelector(".footerKamba");
footerKamba.style.width = '90%';
footerKamba.style.float = 'left';
footerKamba.style.padding = '0 1rem';
var descritionKambaMerchant = document.querySelector(".descritionKambaMerchant");
descritionKambaMerchant.style.marginTop = '1.7rem';
descritionKambaMerchant.style.float = 'left';
var btnCloseWidgetKamba = document.querySelector(".btnCloseWidgetKamba");
btnCloseWidgetKamba.title = 'Sair do pagamento';
btnCloseWidgetKamba.style.border = 'none';
btnCloseWidgetKamba.style.cursor = 'pointer';
btnCloseWidgetKamba.style.fontSize = '1rem';
btnCloseWidgetKamba.style.borderRadius = '0.3rem';
btnCloseWidgetKamba.style.float = 'right';
btnCloseWidgetKamba.style.color = 'red';
btnCloseWidgetKamba.style.paddingTop = '1.5rem';
btnCloseWidgetKamba.onclick = function(){
kambaModalContainer.style.display = 'none';
};
//Button for Pay Kamba
document.querySelector(".btnOpenWidgetKamba").onclick = function(){
kambaModalContainer.style.display = 'flex';
};
//Function Midia Query
//MEDIUM and LARGE
function midiaMediumDivice(x) {
if (x.matches) {
kambaModalWidget.style.width = '360px';
kambaModalWidget.style.height = '490px';
partDetailPay.style.width = '100%';
partDetailPay.style.float = 'left';
descritionKambaMerchant.style.float = 'left';
descriptionKamba.style.width = '91%';
}
}
var x = window.matchMedia("(min-width: 641px)")
midiaMediumDivice(x)
x.addListener(midiaMediumDivice)
});
} else {
response.json().then(data => {
templateModalErrorPayKamba();
var textErrorKamba = document.querySelector(".textErrorKamba");
textErrorKamba.innerHTML = "Falha!... Verifique suas configurações de pagamento ou entra em contacto com a equipe da Kamba";
});
}
})
.catch(function(error) {
templateModalErrorPayKamba();
var textErrorKamba = document.querySelector(".textErrorKamba");
textErrorKamba.innerHTML = "Falha!... Verifique sua conexão com a internet, ela pode estar muito lenta";
});
function templateModalErrorPayKamba(){
var mainKambaModalContainer = document.createElement("main");
//Modal Container
var kambaModalContainer = document.getElementsByTagName("body")[0].appendChild(mainKambaModalContainer);
kambaModalContainer.classList.add("kambaModalContainer");
kambaModalContainer.style.width = '100vw';
kambaModalContainer.style.height = '100%';
kambaModalContainer.style.background = 'rgba(0,0,0,.4)';
kambaModalContainer.style.position = 'fixed';
kambaModalContainer.style.top = '0';
kambaModalContainer.style.left = '0';
kambaModalContainer.style.zIndex = '1000000000000000000000';
kambaModalContainer.style.display = 'flex';
kambaModalContainer.style.justifyContent = 'center';
kambaModalContainer.style.alignItems = 'center';
kambaModalContainer.style.boxSizing = 'border-box';
kambaModalContainer.style.paddingRight = '1rem';
kambaModalContainer.style.overflow = 'auto';
kambaModalContainer.style.cursor = 'pointer';
kambaModalContainer.addEventListener('click', function(){
kambaModalContainer.style.display = 'none';
});
//Button for Pay Kamba
document.querySelector(".btnOpenWidgetKamba").onclick = function(){
kambaModalContainer.style.display = 'flex';
};
//Template
const kambaWidget = `
<div class="kambaModalWidget">
<section>
<p class="textErrorKamba"></p>
</section>
</div>`
kambaModalContainer.innerHTML = kambaWidget;
//Style Widget Modal
var kambaModalWidget = document.querySelector("main .kambaModalWidget");
kambaModalWidget.style.borderRadius = '0.2rem';
kambaModalWidget.style.overflow = 'auto';
kambaModalWidget.style.background = '#fff';
kambaModalWidget.style.width = '65%';
kambaModalWidget.style.height = '35%';
kambaModalWidget.style.position = 'absolute';
kambaModalWidget.style.fontFamily = "'Montserrat', sans-serif";
kambaModalWidget.style.fontSize = '0.95rem';
kambaModalWidget.style.boxShadow = '0 5px 8px 0 rgba(0,0,0,.2), 0 7px 20px 0 rgba(0,0,0,.10)';
kambaModalWidget.style.display = 'flex';
kambaModalWidget.style.justifyContent = 'center';
kambaModalWidget.style.alignItems = 'center';
kambaModalWidget.style.boxSizing = 'border-box';
kambaModalWidget.style.textAlign = 'center';
kambaModalWidget.style.padding = '1.5rem';
kambaModalWidget.style.color = 'red';
//MEDIUM
function midiaMediumDivice(x) {
if (x.matches) {
kambaModalWidget.style.width = '40%';
kambaModalWidget.style.height = '30%';
}
}
var x = window.matchMedia("(min-width: 641px)")
midiaMediumDivice(x)
x.addListener(midiaMediumDivice)
//LARGE
function midiaLargeDivice(x) {
if (x.matches) {
kambaModalWidget.style.width = '25%';
kambaModalWidget.style.height = '30%';
}
}
var x = window.matchMedia("(min-width: 1025PX)")
midiaLargeDivice(x)
x.addListener(midiaLargeDivice)
}
})
}
})();
})();