-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
192 lines (189 loc) · 5.44 KB
/
index.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
import { NativeEventEmitter, NativeModules } from "react-native";
const AdyenDropIn = NativeModules.AdyenDropInPayment;
const EventEmitter = new NativeEventEmitter(AdyenDropIn);
const eventMap = {};
let onPaymentProvideListener;
let onPaymentFailListener;
let onPaymentSubmitListener;
let onPaymentCancelListener;
const addListener = (key, listener) => {
if (eventMap[key]) {
return eventMap[key];
}
eventMap[key] = listener;
EventEmitter.addListener(key, listener);
};
export default {
/**
* Starting payment process.
* @returns {*}
*/
configPayment(publicKey, env) {
return AdyenDropIn.configPayment(publicKey, env);
},
/**
* list paymentMethods
*
* @param {String} encodedToken
*
* @returns {*}
*/
paymentMethods(paymentMethodJson) {
if (typeof paymentMethodJson === "object") {
paymentMethodJson = JSON.stringify(paymentMethodJson);
}
this._validateParam(paymentMethodJson, "paymentMethods", "string");
return AdyenDropIn.paymentMethods(paymentMethodJson);
},
cardPaymentMethod(
paymentMethodJson,
name,
showHolderField,
showStoreField,
buttonTitle
) {
if (typeof paymentMethodJson === "object") {
paymentMethodJson = JSON.stringify(paymentMethodJson);
}
this._validateParam(paymentMethodJson, "cardPaymentMethod", "string");
showHolderField = showHolderField || false;
showStoreField = showStoreField || false;
buttonTitle = buttonTitle || "";
return AdyenDropIn.cardPaymentMethod(
paymentMethodJson,
name,
showHolderField,
showStoreField,
buttonTitle
);
},
/**
* use card paymentMethod
* @param paymentMethodJson
* @returns {*}
*/
storedCardPaymentMethod(paymentMethodJson, index) {
if (typeof paymentMethodJson === "object") {
paymentMethodJson = JSON.stringify(paymentMethodJson);
}
this._validateParam(paymentMethodJson, "storedCardPaymentMethod", "string");
index = index || 0;
return AdyenDropIn.storedCardPaymentMethod(paymentMethodJson, index);
},
contractPaymentMethod(paymentMethodJson, index) {
if (typeof paymentMethodJson === "object") {
paymentMethodJson = JSON.stringify(paymentMethodJson);
}
this._validateParam(paymentMethodJson, "contractPaymentMethod", "string");
index = index || 0;
return AdyenDropIn.contractPaymentMethod(paymentMethodJson, index);
},
/**
* handle Action from payments
* @param actionJson
* @returns {*}
*/
handleAction(actionJson) {
if (typeof actionJson === "object") {
actionJson = JSON.stringify(actionJson);
}
this._validateParam(actionJson, "handleAction", "string");
return AdyenDropIn.handleAction(actionJson);
},
handlePaymentResult(paymentJson) {
if (typeof paymentJson === "object") {
paymentJson = JSON.stringify(paymentJson);
}
this._validateParam(paymentJson, "handlePaymentResult", "string");
return AdyenDropIn.handlePaymentResult(paymentJson);
},
encryptCard(cardNumber, expiryMonth, expiryYear, securityCode) {
return AdyenDropIn.encryptCard(
cardNumber,
expiryMonth,
expiryYear,
securityCode
);
},
/**
* call when need to do more action
*/
onPaymentProvide(mOnPaymentProvide) {
this._validateParam(mOnPaymentProvide, "onPaymentProvide", "function");
onPaymentProvideListener = addListener("onPaymentProvide", e => {
mOnPaymentProvide(e);
});
},
// /**
// * call when cancel a payment
// * @param mOnPaymentCancel
// */
// onPaymentCancel(mOnPaymentCancel) {
// this._validateParam(
// mOnPaymentCancel,
// 'onPaymentCancel',
// 'function',
// );
// onPaymentCancelListener = events.addListener(
// 'mOnPaymentCancel',
// e => {
// mOnPaymentCancel(e);
// },
// );
// },
/**
* call when payment fail
* @param {mOnError} mOnError
*/
onPaymentFail(mOnPaymentFail) {
this._validateParam(mOnPaymentFail, "onPaymentFail", "function");
if (onPaymentFailListener) {
EventEmitter.removeListener(onPaymentFailListener);
}
onPaymentFailListener = addListener("onPaymentFail", e => {
mOnPaymentFail(e);
});
},
/**
* call when payment submit ,send to server do payments
*/
onPaymentSubmit(mOnPaymentSubmit) {
this._validateParam(mOnPaymentSubmit, "onPaymentSubmit", "function");
if (onPaymentSubmitListener) {
EventEmitter.removeListener(onPaymentSubmitListener);
}
onPaymentSubmitListener = addListener("onPaymentSubmit", e => {
mOnPaymentSubmit(e);
});
},
/**
* @param {*} param
* @param {String} methodName
* @param {String} requiredType
* @private
*/
_validateParam(param, methodName, requiredType) {
if (typeof param !== requiredType) {
throw new Error(
`Error: AdyenDropIn.${methodName}() requires a ${
requiredType === "function" ? "callback function" : requiredType
} but got a ${typeof param}`
);
}
},
events: EventEmitter,
removeListeners() {
if (onPaymentProvideListener) {
EventEmitter.removeListener(onPaymentProvideListener);
}
if (onPaymentFailListener) {
EventEmitter.removeListener(onPaymentFailListener);
}
if (onPaymentSubmitListener) {
EventEmitter.removeListener(onPaymentSubmitListener);
}
if (onPaymentCancelListener) {
EventEmitter.removeListener(onPaymentCancelListener);
}
}
};