-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSmallcaseGateway.js
More file actions
296 lines (265 loc) · 7.99 KB
/
SmallcaseGateway.js
File metadata and controls
296 lines (265 loc) · 7.99 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
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
import { NativeModules, Platform } from 'react-native';
import { ENV } from './constants';
import { safeObject, platformSpecificColorHex } from './util';
import { version } from '../package.json';
const { SmallcaseGateway: SmallcaseGatewayNative } = NativeModules;
/**
*
* @typedef {Object} envConfig
* @property {string} gatewayName - unique name on consumer
* @property {boolean} isLeprechaun - leprechaun mode toggle
* @property {boolean} isAmoEnabled - support AMO (subject to broker support)
* @property {Array<string>} brokerList - list of broker names
* @property {'production' | 'staging' | 'development'} environmentName - environment name
*
* @typedef {Object} transactionRes
* @property {string} data - response data
* @property {boolean} success - success flag
* @property {Number} [errorCode] - error code
* @property {string} transaction - transaction name
*
* @typedef {Object} userDetails
* @property {String} name - name of user
* @property {String} email - email of user
* @property {String} contact - contact of user
* @property {String} pinCode - pin-code of user
*
* @typedef {Object} SmallplugUiConfig
* @property {String} headerColor - color of the header background
* @property {Number} headerOpacity - opacity of the header background
* @property {String} backIconColor - color of the back icon
* @property {Number} backIconOpacity - opacity of the back icon
*
*/
let defaultBrokerList = [];
/**
* configure the sdk with
* @param {envConfig} envConfig
*/
const setConfigEnvironment = async (envConfig) => {
const safeConfig = safeObject(envConfig);
await SmallcaseGatewayNative.setHybridSdkVersion(version);
const {
brokerList,
gatewayName,
isLeprechaun,
isAmoEnabled,
environmentName,
} = safeConfig;
const safeIsLeprechaun = Boolean(isLeprechaun);
const safeIsAmoEnabled = Boolean(isAmoEnabled);
const safeBrokerList = Array.isArray(brokerList) ? brokerList : [];
const safeGatewayName = typeof gatewayName === 'string' ? gatewayName : '';
const safeEnvName =
typeof environmentName === 'string' ? environmentName : ENV.PROD;
defaultBrokerList = safeBrokerList;
await SmallcaseGatewayNative.setConfigEnvironment(
safeEnvName,
safeGatewayName,
safeIsLeprechaun,
safeIsAmoEnabled,
safeBrokerList
);
};
/**
* initialize sdk with a session
*
* note: this must be called after `setConfigEnvironment()`
* @param {string} sdkToken
*/
const initSDK = async (sdkToken) => {
const safeToken = typeof sdkToken === 'string' ? sdkToken : '';
return SmallcaseGatewayNative.initSDK(safeToken);
};
/**
* triggers a transaction with a transaction id
*
* @param {string} transactionId
* @param {Object} [utmParams]
* @param {Array<string>} [brokerList]
* @returns {Promise<transactionRes>}
*/
const triggerTransaction = async (transactionId, utmParams, brokerList) => {
const safeUtm = safeObject(utmParams);
const safeId = typeof transactionId === 'string' ? transactionId : '';
const safeBrokerList =
Array.isArray(brokerList) && brokerList.length
? brokerList
: defaultBrokerList;
return SmallcaseGatewayNative.triggerTransaction(
safeId,
safeUtm,
safeBrokerList
);
};
/**
* triggers a transaction with a transaction id
* @deprecated triggerMfTransaction will be removed soon. Please use triggerTransaction.
* @param {string} transactionId
* @returns {Promise<transactionRes>}
*/
const triggerMfTransaction = async (transactionId) => {
console.warn(
'Calling deprecated function! triggerMfTransaction will be removed soon. Please use triggerTransaction.'
);
const safeTransactionId =
typeof transactionId === 'string' ? transactionId : '';
return SmallcaseGatewayNative.triggerMfTransaction(safeTransactionId);
};
/**
* launches smallcases module
*
* @param {string} targetEndpoint
* @param {string} params
*/
const launchSmallplug = async (targetEndpoint, params) => {
const safeEndpoint = typeof targetEndpoint === 'string' ? targetEndpoint : '';
const safeParams = typeof params === 'string' ? params : '';
return SmallcaseGatewayNative.launchSmallplug(safeEndpoint, safeParams);
};
/**
* launches smallcases module
*
* @param {string} targetEndpoint
* @param {string} params
* @param {string} headerColor
* @param {number} headerOpacity
* @param {string} backIconColor
* @param {number} backIconOpacity
*/
const launchSmallplugWithBranding = async (
targetEndpoint,
params,
headerColor,
headerOpacity,
backIconColor,
backIconOpacity
) => {
const safeEndpoint = typeof targetEndpoint === 'string' ? targetEndpoint : '';
const safeParams = typeof params === 'string' ? params : '';
const safeHeaderColor =
typeof headerColor === 'string'
? headerColor
: platformSpecificColorHex('2F363F');
const safeHeaderOpacity =
typeof headerOpacity === 'number' ? headerOpacity : 1;
const safeBackIconColor =
typeof backIconColor === 'string'
? backIconColor
: platformSpecificColorHex('FFFFFF');
const safeBackIconOpacity =
typeof backIconOpacity === 'number' ? backIconOpacity : 1;
return Platform.OS === 'android'
? SmallcaseGatewayNative.launchSmallplugWithBranding(
safeEndpoint,
safeParams,
{
headerColor: safeHeaderColor,
headerOpacity: safeHeaderOpacity,
backIconColor: safeBackIconColor,
backIconOpacity: safeBackIconOpacity,
}
)
: SmallcaseGatewayNative.launchSmallplugWithBranding(
safeEndpoint,
safeParams,
safeHeaderColor,
safeHeaderOpacity,
safeBackIconColor,
safeBackIconOpacity
);
};
/**
* Logs the user out and removes the web session.
*
* This promise will be rejected if logout was unsuccessful
*
* @returns {Promise}
*/
const logoutUser = async () => {
return SmallcaseGatewayNative.logoutUser();
};
/**
* This will display a list of all the orders that a user recently placed.
* This includes pending, successful, and failed orders.
* @returns
*/
const showOrders = async () => {
return SmallcaseGatewayNative.showOrders();
};
/**
* triggers the lead gen flow
*
* @param {userDetails} [userDetails]
* @param {Object} [utmParams]
*/
const triggerLeadGen = (userDetails, utmParams) => {
const safeParams = safeObject(userDetails);
const safeUtm = safeObject(utmParams);
return SmallcaseGatewayNative.triggerLeadGen(safeParams, safeUtm);
};
/**
* triggers the lead gen flow
*
* @param {userDetails} [userDetails]
* * @returns {Promise}
*/
const triggerLeadGenWithStatus = async (userDetails) => {
const safeParams = safeObject(userDetails);
return SmallcaseGatewayNative.triggerLeadGenWithStatus(safeParams);
};
/**
* triggers the lead gen flow with an option of "login here" cta
*
* @param {userDetails} [userDetails]
* @param {Object} [utmParams]
* @param {boolean} [showLoginCta]
* @returns {Promise}
*/
const triggerLeadGenWithLoginCta = async (
userDetails,
utmParams,
showLoginCta
) => {
const safeParams = safeObject(userDetails);
const safeUtm = safeObject(utmParams);
const safeShowLoginCta = Boolean(showLoginCta);
return SmallcaseGatewayNative.triggerLeadGenWithLoginCta(
safeParams,
safeUtm,
safeShowLoginCta
);
};
/**
* Marks a smallcase as archived
*
* @param {String} iscid
*/
const archiveSmallcase = async (iscid) => {
const safeIscid = typeof iscid === 'string' ? iscid : '';
return SmallcaseGatewayNative.archiveSmallcase(safeIscid);
};
/**
* Returns the native android/ios and react-native sdk version
* (internal-tracking)
* @returns {Promise}
*/
const getSdkVersion = async () => {
return SmallcaseGatewayNative.getSdkVersion(version);
};
const SmallcaseGateway = {
initSDK,
logoutUser,
triggerLeadGen,
triggerLeadGenWithStatus,
triggerLeadGenWithLoginCta,
archiveSmallcase,
triggerTransaction,
triggerMfTransaction,
setConfigEnvironment,
launchSmallplug,
launchSmallplugWithBranding,
getSdkVersion,
showOrders,
};
export default SmallcaseGateway;