-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmondu_checkout.js
123 lines (103 loc) · 3.34 KB
/
mondu_checkout.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
class MonduCheckout {
init() {
this._isWidgetLoaded = this._initWidget(widgetUrl);
this._registerProperties();
this._registerEvents();
}
async _initWidget(src) {
return new Promise((resolve, reject) => {
const widget = document.createElement('script');
widget.src = src;
document.head.appendChild(widget);
widget.onload = () => resolve(true);
widget.onerror = () => resolve(false);
});
}
_registerProperties() {
this._form = document.getElementById('orderConfirmAgbBottom');
this._inputEl = document.getElementById('mondu-checkout-input');
this._paymentUrl = paymentUrl;
}
_registerEvents() {
if (this._form) {
this._form.addEventListener('submit', this._submitForm.bind(this));
}
}
async _submitForm(event) {
if (this._isWidgetComplete()) {
return true;
}
event.preventDefault();
if (this._isWidgetLoaded) {
const monduOrderData = await this._getMonduOrderData();
if (!monduOrderData || !monduOrderData.token) {
window.location.href = this._paymentUrl;
}
if (monduOrderData.hostedCheckoutUrl) {
window.location.href = monduOrderData.hostedCheckoutUrl;
} else {
this._renderWidget(monduOrderData.token);
}
}
}
async _createMonduOrder() {
return new Promise((resolve, reject) => {
const widget = document.createElement('script');
widget.src = src;
document.head.appendChild(widget);
widget.onload = () => resolve(true);
widget.onerror = () => resolve(false);
});
}
async _getMonduOrderData() {
try {
const client = new HttpRequest();
const { data } = await client.post('?cl=oemonducheckout&fnc=createOrder', {});
if (data.token !== 'error') {
return data;
} else {
return null;
}
} catch (e) {
return null;
}
}
_renderWidget(token) {
const that = this;
const removeWidgetContainer = this._removeWidgetContainer.bind(this);
window.monduCheckout.render({
token,
onClose() {
removeWidgetContainer();
if (that._isWidgetComplete()) {
that._form.submit();
} else {
window.location.href = that._paymentUrl;
}
},
onSuccess() {
that._setMonduComplete('1');
}
});
}
_removeWidgetContainer() {
const widgetContainer = document.getElementById('mondu-checkout-widget');
if (widgetContainer) {
widgetContainer.style.display = 'none';
window.monduCheckout.destroy();
}
}
_setMonduComplete(flag) {
this._inputEl.dataset.monduComplete = parseInt(flag, 10);
}
_isWidgetComplete() {
return parseInt(this._inputEl.dataset.monduComplete, 10) === 1;
}
}
window.onload = function () {
if (!widgetUrl) {
var widgetUrl = 'http://localhost:3002/widget.js';
}
var mondu = new MonduCheckout();
mondu.init();
};