1
|
|
|
var _require = require("./"), |
2
|
|
|
isEmpty = _require.isEmpty, |
3
|
|
|
isJson = _require.isJson; |
4
|
|
|
const EventEmitter = require('events'); |
5
|
|
|
// Initializing event emitter instances |
6
|
|
|
const bSecurePaymentsEventHandler = new EventEmitter(); |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
const bSecurePaymentsSubscribeEventHandler = { |
10
|
|
|
onErrorAlert() { |
11
|
|
|
bSecurePaymentsEventHandler.on('errorAlert', (data) => { |
12
|
|
|
return data; |
13
|
|
|
}); |
14
|
|
|
}, |
15
|
|
|
onSuccessAlert() { |
16
|
|
|
bSecurePaymentsEventHandler.on('successAlert', (data) => { |
17
|
|
|
return data; |
18
|
|
|
}); |
19
|
|
|
}, |
20
|
|
|
onValidationErrorAlert() { |
21
|
|
|
bSecurePaymentsEventHandler.on('validationAlert', (data) => { |
22
|
|
|
return data; |
23
|
|
|
}); |
24
|
|
|
}, |
25
|
|
|
onProcessPaymentSuccess() { |
26
|
|
|
bSecurePaymentsEventHandler.on('onProcessPaymentSuccess', (data) => { |
27
|
|
|
return data; |
28
|
|
|
}); |
29
|
|
|
}, |
30
|
|
|
onProcessPaymentFailure() { |
31
|
|
|
bSecurePaymentsEventHandler.on('onProcessPaymentFailure', (data) => { |
32
|
|
|
return data; |
33
|
|
|
}); |
34
|
|
|
}, |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
const bSecurePaymentsHandler = { |
38
|
|
|
initialize: function () { |
39
|
|
|
bSecurePaymentsSubscribeEventHandler.onErrorAlert(); |
40
|
|
|
bSecurePaymentsSubscribeEventHandler.onSuccessAlert(); |
41
|
|
|
bSecurePaymentsSubscribeEventHandler.onValidationErrorAlert(); |
42
|
|
|
bSecurePaymentsSubscribeEventHandler.onValidationErrorAlert(); |
43
|
|
|
bSecurePaymentsSubscribeEventHandler.onProcessPaymentSuccess(); |
44
|
|
|
bSecurePaymentsSubscribeEventHandler.onProcessPaymentFailure(); |
45
|
|
|
}, |
46
|
|
|
onErrorAlert: function (data) { |
47
|
|
|
bSecurePaymentsEventHandler.emit('errorAlert', data) |
48
|
|
|
return data; |
49
|
|
|
}, |
50
|
|
|
onSuccessAlert: function (data) { |
51
|
|
|
bSecurePaymentsEventHandler.emit('successAlert', data) |
52
|
|
|
return data; |
53
|
|
|
}, |
54
|
|
|
onValidationErrorAlert: function (data) { |
55
|
|
|
bSecurePaymentsEventHandler.emit('validationAlert', data) |
56
|
|
|
return data; |
57
|
|
|
}, |
58
|
|
|
onProcessPaymentSuccess: function (data) { |
59
|
|
|
bSecurePaymentsEventHandler.emit('onProcessPaymentSuccess', data) |
60
|
|
|
return data; |
61
|
|
|
}, |
62
|
|
|
onProcessPaymentFailure: function (data) { |
63
|
|
|
bSecurePaymentsEventHandler.emit('onProcessPaymentFailure', data) |
64
|
|
|
return data; |
65
|
|
|
}, |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
const errorHandlers = { |
69
|
|
|
formatValidationError: function (data) { |
70
|
|
|
let _data = { status: 422, message: data, body: {}, exception: {} }; |
71
|
|
|
return bSecurePaymentsHandler.onValidationErrorAlert(isJson(_data)); |
72
|
|
|
}, |
73
|
|
|
onException: function (msg) { |
74
|
|
|
throw new Error(msg) |
75
|
|
|
let _data = { status: 419, message: msg, body: {}, exception: {} }; |
|
|
|
|
76
|
|
|
return bSecurePaymentsHandler.onErrorAlert(isJson(_data)); |
|
|
|
|
77
|
|
|
}, |
78
|
|
|
handleErrors: function (data) { |
79
|
|
|
const responseCode = (data.status).toString(); |
80
|
|
|
const responseException = data.exception; |
81
|
|
|
if (!isEmpty(responseException)) { |
82
|
|
|
throw new Error(responseException) |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (responseCode.startsWith(2)) { |
86
|
|
|
bSecurePaymentsHandler.onSuccessAlert(data); |
87
|
|
|
} else if (responseCode === "422") { |
88
|
|
|
bSecurePaymentsHandler.onValidationErrorAlert(data); |
89
|
|
|
} else if (!isEmpty(responseCode)) { |
90
|
|
|
if (!isEmpty(data.body.result.original)) { |
91
|
|
|
bSecurePaymentsHandler.onProcessPaymentFailure(data.body.result.original); |
92
|
|
|
} else { |
93
|
|
|
bSecurePaymentsHandler.onErrorAlert(data); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
}, |
97
|
|
|
handlePayments: function (data) { |
98
|
|
|
const responseCode = (data.status).toString(); |
99
|
|
|
if (responseCode.startsWith(2)) { |
100
|
|
|
bSecurePaymentsHandler.onProcessPaymentSuccess(data); |
101
|
|
|
resetFrame(); |
102
|
|
|
} else { |
103
|
|
|
bSecurePaymentsHandler.onProcessPaymentFailure(data); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
module.exports = { |
109
|
|
|
errorHandlers: errorHandlers, |
110
|
|
|
bSecurePaymentsHandler: bSecurePaymentsHandler, |
111
|
|
|
bSecurePaymentsEventHandler: bSecurePaymentsEventHandler |
112
|
|
|
}; |