@cuenca-mx/cuenca-js 0.0.1-dev.4 → 0.0.1-dev.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/errors/index.mjs +64 -0
- package/build/index.mjs +1475 -0
- package/build/jwt/index.mjs +46 -0
- package/build/package.json +5 -5
- package/build/{queries-395c7467.js → queries-f146b91b.js} +1 -1
- package/build/requests/index.mjs +3 -0
- package/build/types/index.mjs +3 -0
- package/build/{walletTransactionRequest-17132eb5.js → walletTransactionRequest-bbfb87bd.js} +1 -1
- package/package.json +5 -5
package/build/index.mjs
ADDED
|
@@ -0,0 +1,1475 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { CuencaResponseException, CuencaException, NoResultFound, MultipleResultsFound, InvalidPassword } from './errors/index.mjs';
|
|
3
|
+
export { CuencaException, CuencaResponseException, InvalidPassword, MalformedJwtToken, MultipleResultsFound, NoResultFound, ValidationError } from './errors/index.mjs';
|
|
4
|
+
import { Jwt } from './jwt/index.mjs';
|
|
5
|
+
export { Jwt } from './jwt/index.mjs';
|
|
6
|
+
import { P as Phase, F as FileFormat, E as EntryType, T as TransactionStatus, C as CardIssuer, a as CardStatus, b as CardType, c as CardFundingType, d as CardErrorType, e as CardTransactionType, f as CommissionType, D as DepositNetwork, S as SavingCategory, g as ServiceProviderCategory, h as TransferNetwork, W as WalletTransactionType, A as AccountQuery, i as ApiKeyQuery, B as BalanceEntryQuery, j as BillPaymentQuery, k as CardsQuery, l as CardTransactionQuery, Q as QueryParams, m as DepositQuery, n as WalletQuery, o as StatementQuery, p as TransferQuery, q as WalletTransactionQuery } from './queries-f146b91b.js';
|
|
7
|
+
export { A as AccountQuery, i as ApiKeyQuery, B as BalanceEntryQuery, j as BillPaymentQuery, d as CardErrorType, c as CardFundingType, C as CardIssuer, a as CardStatus, l as CardTransactionQuery, e as CardTransactionType, b as CardType, k as CardsQuery, f as CommissionType, D as DepositNetwork, m as DepositQuery, E as EntryType, F as FileFormat, s as PageSize, P as Phase, Q as QueryParams, S as SavingCategory, g as ServiceProviderCategory, o as StatementQuery, r as TrackDataMethod, T as TransactionStatus, h as TransferNetwork, p as TransferQuery, n as WalletQuery, q as WalletTransactionQuery, W as WalletTransactionType } from './queries-f146b91b.js';
|
|
8
|
+
import { d as dateToUTC, e as enumValueFromString } from './data-7d3d5fcc.js';
|
|
9
|
+
import { A as ApiKeyUpdateRequest, a as ArpcRequest, C as CardActivationRequest, b as CardRequest, c as CardUpdateRequest, d as CardValidationRequest, S as SavingRequest, T as TransferRequest, U as UserCredentialRequest, e as UserCredentialUpdateRequest, f as UserLoginRequest, W as WalletTransactionRequest } from './walletTransactionRequest-bbfb87bd.js';
|
|
10
|
+
|
|
11
|
+
class Client {
|
|
12
|
+
constructor({ apiKey, apiSecret, phase = Phase.Sandbox } = {}) {
|
|
13
|
+
this.phase = phase;
|
|
14
|
+
this.basicAuth = { apiKey, apiSecret };
|
|
15
|
+
this.jwtToken = null;
|
|
16
|
+
this._session = axios.create();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get session() {
|
|
20
|
+
return this._session;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get origin() {
|
|
24
|
+
return `https://${this.phase.value}.cuenca.com`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get authHeader() {
|
|
28
|
+
const { apiKey, apiSecret } = this.basicAuth;
|
|
29
|
+
if (!apiKey || !apiSecret) return '';
|
|
30
|
+
return `Basic ${Buffer.from(
|
|
31
|
+
Buffer.from(`${apiKey}:${apiSecret}`).toString('utf-8'),
|
|
32
|
+
).toString('base64')}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
addHeadersToRequest(obj) {
|
|
36
|
+
const interceptorId = this._session.interceptors.request.use((config) => {
|
|
37
|
+
const currentConfig = config;
|
|
38
|
+
const {
|
|
39
|
+
headers: { common },
|
|
40
|
+
} = currentConfig;
|
|
41
|
+
Object.keys(obj).forEach((k) => (common[k] = obj[k]));
|
|
42
|
+
return currentConfig;
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
interceptorId,
|
|
46
|
+
eject: () => this._session.interceptors.request.eject(interceptorId),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
deleteRequestHeader(header) {
|
|
51
|
+
const interceptorId = this._session.interceptors.request.use((config) => {
|
|
52
|
+
const currentConfig = config;
|
|
53
|
+
const {
|
|
54
|
+
headers: { common },
|
|
55
|
+
} = currentConfig;
|
|
56
|
+
delete common[header];
|
|
57
|
+
return currentConfig;
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
interceptorId,
|
|
61
|
+
eject: () => this._session.interceptors.request.eject(interceptorId),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
addConfigToRequest(obj) {
|
|
66
|
+
const interceptorId = this._session.interceptors.request.use((config) => {
|
|
67
|
+
const currentConfig = config;
|
|
68
|
+
Object.keys(obj).forEach((k) => (currentConfig[k] = obj[k]));
|
|
69
|
+
return currentConfig;
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
interceptorId,
|
|
73
|
+
eject: () => this._session.interceptors.request.eject(interceptorId),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async configure({ apiKey, apiSecret, loginToken, phase, useJwt = false }) {
|
|
78
|
+
this.basicAuth = {
|
|
79
|
+
apiKey: apiKey || this.basicAuth.apiKey,
|
|
80
|
+
apiSecret: apiSecret || this.basicAuth.apiSecret,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
if (phase) this.phase = phase;
|
|
84
|
+
|
|
85
|
+
if (useJwt) this.jwtToken = await Jwt.create(this);
|
|
86
|
+
|
|
87
|
+
if (loginToken) {
|
|
88
|
+
this.addHeadersToRequest({ 'X-Cuenca-LoginToken': loginToken });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async get({ endpoint, format, params }) {
|
|
93
|
+
return this.request({ endpoint, format, params });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async post({ endpoint, data }) {
|
|
97
|
+
return this.request({ method: 'POST', endpoint, data });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async patch({ endpoint, data }) {
|
|
101
|
+
return this.request({ method: 'PATCH', endpoint, data });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async delete({ endpoint, data }) {
|
|
105
|
+
return this.request({ method: 'DELETE', endpoint, data });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async request({
|
|
109
|
+
endpoint,
|
|
110
|
+
data = null,
|
|
111
|
+
format = FileFormat.Json,
|
|
112
|
+
method = 'GET',
|
|
113
|
+
params = null,
|
|
114
|
+
}) {
|
|
115
|
+
const headers = {
|
|
116
|
+
Authorization: this.authHeader,
|
|
117
|
+
'X-User-Agent': `cuenca-js/0.0.1`, // TODO: Change for client version
|
|
118
|
+
'Content-Type': 'application/json',
|
|
119
|
+
Accept: `application/${format.value}`,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
if (this.jwtToken) {
|
|
123
|
+
if (this.jwtToken.isExpired) {
|
|
124
|
+
this.jwtToken = await Jwt.create(this);
|
|
125
|
+
}
|
|
126
|
+
headers['X-Cuenca-Token'] = this.jwtToken.token;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const headersInterceptor = this.addHeadersToRequest(headers);
|
|
130
|
+
|
|
131
|
+
const modifiedData = data;
|
|
132
|
+
if (modifiedData) {
|
|
133
|
+
Object.keys(modifiedData).forEach((k) => {
|
|
134
|
+
if (modifiedData[k] instanceof Date) {
|
|
135
|
+
modifiedData[k] = modifiedData[k].toISOString();
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const configInterceptor = this.addConfigToRequest({
|
|
141
|
+
method,
|
|
142
|
+
params,
|
|
143
|
+
data: modifiedData,
|
|
144
|
+
url: endpoint,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
let response;
|
|
148
|
+
try {
|
|
149
|
+
response = await this._session.request({ baseURL: this.origin });
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (error.response) {
|
|
152
|
+
throw new CuencaResponseException(
|
|
153
|
+
error.response.data,
|
|
154
|
+
error.response.status,
|
|
155
|
+
);
|
|
156
|
+
} else if (error.request) {
|
|
157
|
+
throw new CuencaException(
|
|
158
|
+
`No response received: ${error.errno}: ${error.code}`,
|
|
159
|
+
);
|
|
160
|
+
} else {
|
|
161
|
+
throw new CuencaException(error.message);
|
|
162
|
+
}
|
|
163
|
+
} finally {
|
|
164
|
+
headersInterceptor.eject();
|
|
165
|
+
configInterceptor.eject();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return response.data;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class Account {
|
|
173
|
+
constructor({ accountNumber, createdAt, id, institutionName, name, userId }) {
|
|
174
|
+
this.accountNumber = accountNumber;
|
|
175
|
+
this.createdAt = dateToUTC(createdAt);
|
|
176
|
+
this.id = id;
|
|
177
|
+
this.institutionName = institutionName;
|
|
178
|
+
this.name = name;
|
|
179
|
+
this.userId = userId;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static fromObject = ({ id, name, ...obj }) =>
|
|
183
|
+
new Account({
|
|
184
|
+
id,
|
|
185
|
+
name,
|
|
186
|
+
accountNumber: obj.account_number,
|
|
187
|
+
createdAt: obj.created_at,
|
|
188
|
+
institutionName: obj.institution_name,
|
|
189
|
+
userId: obj.user_id,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
class ApiKey {
|
|
194
|
+
constructor({ createdAt, deactivatedAt, id, secret, userId, updatedAt }) {
|
|
195
|
+
this.createdAt = dateToUTC(createdAt);
|
|
196
|
+
this.deactivatedAt = dateToUTC(deactivatedAt);
|
|
197
|
+
this.id = id;
|
|
198
|
+
this.secret = secret;
|
|
199
|
+
this.userId = userId;
|
|
200
|
+
this.updatedAt = dateToUTC(updatedAt);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
static fromObject = ({ id, secret, ...obj }) =>
|
|
204
|
+
new ApiKey({
|
|
205
|
+
id,
|
|
206
|
+
secret,
|
|
207
|
+
createdAt: obj.created_at,
|
|
208
|
+
deactivatedAt: obj.deactivated_at,
|
|
209
|
+
userId: obj.user_id,
|
|
210
|
+
updatedAt: obj.updated_at,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
get isActive() {
|
|
214
|
+
const todayUTC = dateToUTC(Date.now());
|
|
215
|
+
return (
|
|
216
|
+
!this.deactivatedAt || this.deactivatedAt.getTime() > todayUTC.getTime()
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
class Arpc {
|
|
222
|
+
constructor({ arpc, createdAt, cardUri, isValidArqc }) {
|
|
223
|
+
this.arpc = arpc;
|
|
224
|
+
this.createdAt = dateToUTC(createdAt);
|
|
225
|
+
this.cardUri = cardUri;
|
|
226
|
+
this.isValidArqc = isValidArqc;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static fromObject = ({ arpc, ...obj }) =>
|
|
230
|
+
new Arpc({
|
|
231
|
+
arpc,
|
|
232
|
+
createdAt: obj.created_at,
|
|
233
|
+
cardUri: obj.card_uri,
|
|
234
|
+
isValidArqc: obj.is_valid_arqc,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
class BalanceEntry {
|
|
239
|
+
constructor({
|
|
240
|
+
amount,
|
|
241
|
+
createdAt,
|
|
242
|
+
descriptor,
|
|
243
|
+
entryType,
|
|
244
|
+
fundingInstrumentUri,
|
|
245
|
+
id,
|
|
246
|
+
name,
|
|
247
|
+
relatedTransactionUri,
|
|
248
|
+
rollingBalance,
|
|
249
|
+
}) {
|
|
250
|
+
this.amount = amount;
|
|
251
|
+
this.createdAt = dateToUTC(createdAt);
|
|
252
|
+
this.descriptor = descriptor;
|
|
253
|
+
this.entryType = enumValueFromString(EntryType, entryType);
|
|
254
|
+
this.fundingInstrumentUri = fundingInstrumentUri;
|
|
255
|
+
this.id = id;
|
|
256
|
+
this.name = name;
|
|
257
|
+
this.relatedTransactionUri = relatedTransactionUri;
|
|
258
|
+
this.rollingBalance = rollingBalance;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static fromObject = ({ amount, descriptor, id, name, type, ...obj }) =>
|
|
262
|
+
new BalanceEntry({
|
|
263
|
+
amount,
|
|
264
|
+
descriptor,
|
|
265
|
+
name,
|
|
266
|
+
id,
|
|
267
|
+
createdAt: obj.created_at,
|
|
268
|
+
entryType: type,
|
|
269
|
+
fundingInstrumentUri: obj.funding_instrument_uri,
|
|
270
|
+
relatedTransactionUri: obj.related_transaction_uri,
|
|
271
|
+
rollingBalance: obj.rolling_balance,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
class Transaction {
|
|
276
|
+
constructor({ amount, createdAt, descriptor, status, userId }) {
|
|
277
|
+
this.amount = amount;
|
|
278
|
+
this.createdAt = dateToUTC(createdAt);
|
|
279
|
+
this.descriptor = descriptor;
|
|
280
|
+
this.status = enumValueFromString(TransactionStatus, status);
|
|
281
|
+
this.userId = userId;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
class BillPayment extends Transaction {
|
|
286
|
+
constructor({
|
|
287
|
+
amount,
|
|
288
|
+
accountNumber,
|
|
289
|
+
createdAt,
|
|
290
|
+
descriptor,
|
|
291
|
+
id,
|
|
292
|
+
providerUri,
|
|
293
|
+
status,
|
|
294
|
+
userId,
|
|
295
|
+
}) {
|
|
296
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
297
|
+
this.accountNumber = accountNumber;
|
|
298
|
+
this.id = id;
|
|
299
|
+
this.providerUri = providerUri;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
static fromObject = ({ amount, descriptor, id, status, ...obj }) =>
|
|
303
|
+
new BillPayment({
|
|
304
|
+
amount,
|
|
305
|
+
descriptor,
|
|
306
|
+
id,
|
|
307
|
+
status,
|
|
308
|
+
accountNumber: obj.account_number,
|
|
309
|
+
createdAt: obj.created_at,
|
|
310
|
+
providerUri: obj.provider_uri,
|
|
311
|
+
userId: obj.user_id,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
class Card {
|
|
316
|
+
constructor({
|
|
317
|
+
createdAt,
|
|
318
|
+
cvv2,
|
|
319
|
+
expMonth,
|
|
320
|
+
expYear,
|
|
321
|
+
fundingType,
|
|
322
|
+
id,
|
|
323
|
+
issuer,
|
|
324
|
+
number,
|
|
325
|
+
pin,
|
|
326
|
+
status,
|
|
327
|
+
type,
|
|
328
|
+
updatedAt,
|
|
329
|
+
userId,
|
|
330
|
+
}) {
|
|
331
|
+
this.createdAt = dateToUTC(createdAt);
|
|
332
|
+
this.cvv2 = cvv2;
|
|
333
|
+
this.expMonth = expMonth;
|
|
334
|
+
this.expYear = expYear;
|
|
335
|
+
this.fundingType = enumValueFromString(CardFundingType, fundingType);
|
|
336
|
+
this.id = id;
|
|
337
|
+
this.issuer = enumValueFromString(CardIssuer, issuer);
|
|
338
|
+
this.number = number;
|
|
339
|
+
this.pin = pin;
|
|
340
|
+
this.status = enumValueFromString(CardStatus, status);
|
|
341
|
+
this.type = enumValueFromString(CardType, type);
|
|
342
|
+
this.updatedAt = dateToUTC(updatedAt);
|
|
343
|
+
this.userId = userId;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
static fromObject = ({
|
|
347
|
+
cvv2,
|
|
348
|
+
id,
|
|
349
|
+
issuer,
|
|
350
|
+
number,
|
|
351
|
+
pin,
|
|
352
|
+
status,
|
|
353
|
+
type,
|
|
354
|
+
...obj
|
|
355
|
+
}) =>
|
|
356
|
+
new Card({
|
|
357
|
+
cvv2,
|
|
358
|
+
id,
|
|
359
|
+
issuer,
|
|
360
|
+
number,
|
|
361
|
+
pin,
|
|
362
|
+
status,
|
|
363
|
+
type,
|
|
364
|
+
createdAt: obj.created_at,
|
|
365
|
+
expMonth: obj.exp_month,
|
|
366
|
+
expYear: obj.exp_year,
|
|
367
|
+
fundingType: obj.funding_type,
|
|
368
|
+
updatedAt: obj.updated_at,
|
|
369
|
+
userId: obj.user_id,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
class CardActivation {
|
|
374
|
+
constructor({ cardUri, createdAt, id, ipAddress, success, userId }) {
|
|
375
|
+
this.cardUri = cardUri;
|
|
376
|
+
this.createdAt = dateToUTC(createdAt);
|
|
377
|
+
this.id = id;
|
|
378
|
+
this.ipAddress = ipAddress;
|
|
379
|
+
this.success = success;
|
|
380
|
+
this.userId = userId;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
static fromObject = ({ id, success, ...obj }) =>
|
|
384
|
+
new CardActivation({
|
|
385
|
+
id,
|
|
386
|
+
success,
|
|
387
|
+
cardUri: obj.card_uri,
|
|
388
|
+
createdAt: obj.created_at,
|
|
389
|
+
ipAddress: obj.ip_address,
|
|
390
|
+
userId: obj.user_id,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
class CardTransaction extends Transaction {
|
|
395
|
+
constructor({
|
|
396
|
+
amount,
|
|
397
|
+
cardErrorType,
|
|
398
|
+
cardLastFour,
|
|
399
|
+
cardType,
|
|
400
|
+
cardUri,
|
|
401
|
+
createdAt,
|
|
402
|
+
descriptor,
|
|
403
|
+
metadata,
|
|
404
|
+
network,
|
|
405
|
+
relatedCardTransactionsUris,
|
|
406
|
+
status,
|
|
407
|
+
type,
|
|
408
|
+
userId,
|
|
409
|
+
}) {
|
|
410
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
411
|
+
this.cardErrorType = enumValueFromString(CardErrorType, cardErrorType);
|
|
412
|
+
this.cardLastFour = cardLastFour;
|
|
413
|
+
this.cardType = enumValueFromString(CardType, cardType);
|
|
414
|
+
this.cardUri = cardUri;
|
|
415
|
+
this.metadata = metadata;
|
|
416
|
+
this.network = network;
|
|
417
|
+
this.relatedCardTransactionsUris = relatedCardTransactionsUris;
|
|
418
|
+
this.type = enumValueFromString(CardTransactionType, type);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
static fromObject = ({
|
|
422
|
+
amount,
|
|
423
|
+
descriptor,
|
|
424
|
+
metadata,
|
|
425
|
+
network,
|
|
426
|
+
status,
|
|
427
|
+
type,
|
|
428
|
+
...obj
|
|
429
|
+
}) =>
|
|
430
|
+
new CardTransaction({
|
|
431
|
+
amount,
|
|
432
|
+
descriptor,
|
|
433
|
+
metadata,
|
|
434
|
+
network,
|
|
435
|
+
status,
|
|
436
|
+
type,
|
|
437
|
+
cardErrorType: obj.error_type,
|
|
438
|
+
cardLastFour: obj.card_last4,
|
|
439
|
+
cardType: obj.card_type,
|
|
440
|
+
cardUri: obj.card_uri,
|
|
441
|
+
createdAt: obj.created_at,
|
|
442
|
+
relatedCardTransactionsUris: obj.related_card_transaction,
|
|
443
|
+
userId: obj.user_id,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
class CardValidation {
|
|
448
|
+
constructor({
|
|
449
|
+
cardStatus,
|
|
450
|
+
cardType,
|
|
451
|
+
cardUri,
|
|
452
|
+
createdAt,
|
|
453
|
+
isExpired,
|
|
454
|
+
isPinAttemptsExceeded,
|
|
455
|
+
isValidCvv,
|
|
456
|
+
isValidCvv2,
|
|
457
|
+
isValidExpDate,
|
|
458
|
+
isValidIcvv,
|
|
459
|
+
isValidPinBlock,
|
|
460
|
+
userId,
|
|
461
|
+
}) {
|
|
462
|
+
this.cardStatus = enumValueFromString(CardStatus, cardStatus);
|
|
463
|
+
this.cardType = enumValueFromString(CardType, cardType);
|
|
464
|
+
this.cardUri = cardUri;
|
|
465
|
+
this.createdAt = dateToUTC(createdAt);
|
|
466
|
+
this.isExpired = isExpired;
|
|
467
|
+
this.isPinAttemptsExceeded = isPinAttemptsExceeded;
|
|
468
|
+
this.isValidCvv = isValidCvv;
|
|
469
|
+
this.isValidCvv2 = isValidCvv2;
|
|
470
|
+
this.isValidExpDate = isValidExpDate;
|
|
471
|
+
this.isValidIcvv = isValidIcvv;
|
|
472
|
+
this.isValidPinBlock = isValidPinBlock;
|
|
473
|
+
this.userId = userId;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
static fromObject = ({ ...obj }) =>
|
|
477
|
+
new CardValidation({
|
|
478
|
+
cardStatus: obj.card_status,
|
|
479
|
+
cardType: obj.card_type,
|
|
480
|
+
cardUri: obj.card_uri,
|
|
481
|
+
createdAt: obj.created_at,
|
|
482
|
+
isExpired: obj.is_expired,
|
|
483
|
+
isPinAttemptsExceeded: obj.is_pin_attempts_exceeded,
|
|
484
|
+
isValidCvv: obj.is_valid_cvv,
|
|
485
|
+
isValidCvv2: obj.is_valid_cvv2,
|
|
486
|
+
isValidExpDate: obj.is_valid_exp_date,
|
|
487
|
+
isValidIcvv: obj.is_valid_icvv,
|
|
488
|
+
isValidPinBlock: obj.is_valid_pin_block,
|
|
489
|
+
userId: obj.user_id,
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
get isActive() {
|
|
493
|
+
return this.cardStatus === CardStatus.Active;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
class Commission extends Transaction {
|
|
498
|
+
constructor({
|
|
499
|
+
amount,
|
|
500
|
+
createdAt,
|
|
501
|
+
descriptor,
|
|
502
|
+
relatedTransactionUri,
|
|
503
|
+
status,
|
|
504
|
+
type,
|
|
505
|
+
userId,
|
|
506
|
+
}) {
|
|
507
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
508
|
+
this.relatedTransactionUri = relatedTransactionUri;
|
|
509
|
+
this.type = enumValueFromString(CommissionType, type);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
static fromObject = ({ amount, descriptor, status, type, ...obj }) =>
|
|
513
|
+
new Commission({
|
|
514
|
+
amount,
|
|
515
|
+
descriptor,
|
|
516
|
+
status,
|
|
517
|
+
type,
|
|
518
|
+
createdAt: obj.created_at,
|
|
519
|
+
relatedTransactionUri: obj.related_transaction_uri,
|
|
520
|
+
userId: obj.user_id,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
class Deposit extends Transaction {
|
|
525
|
+
constructor({
|
|
526
|
+
amount,
|
|
527
|
+
createdAt,
|
|
528
|
+
descriptor,
|
|
529
|
+
id,
|
|
530
|
+
network,
|
|
531
|
+
status,
|
|
532
|
+
sourceUri,
|
|
533
|
+
trackingKey,
|
|
534
|
+
userId,
|
|
535
|
+
}) {
|
|
536
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
537
|
+
this.id = id;
|
|
538
|
+
this.network = enumValueFromString(DepositNetwork, network);
|
|
539
|
+
this.sourceUri = sourceUri;
|
|
540
|
+
this.trackingKey = trackingKey;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
static fromObject = ({ amount, descriptor, id, network, status, ...obj }) =>
|
|
544
|
+
new Deposit({
|
|
545
|
+
amount,
|
|
546
|
+
descriptor,
|
|
547
|
+
id,
|
|
548
|
+
network,
|
|
549
|
+
status,
|
|
550
|
+
createdAt: obj.created_at,
|
|
551
|
+
sourceUri: obj.source_uri,
|
|
552
|
+
trackingKey: obj.tracking_key,
|
|
553
|
+
userId: obj.user_id,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
class LoginToken {
|
|
558
|
+
constructor({ id }) {
|
|
559
|
+
this.id = id;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
static fromObject = ({ id }) => new LoginToken({ id });
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
class Wallet {
|
|
566
|
+
constructor({ balance, createdAt, deactivatedAt, id, userId, updatedAt }) {
|
|
567
|
+
this.balance = balance;
|
|
568
|
+
this.createdAt = dateToUTC(createdAt);
|
|
569
|
+
this.deactivatedAt = dateToUTC(deactivatedAt);
|
|
570
|
+
this.id = id;
|
|
571
|
+
this.userId = userId;
|
|
572
|
+
this.updatedAt = dateToUTC(updatedAt);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
class Saving extends Wallet {
|
|
577
|
+
constructor({
|
|
578
|
+
balance,
|
|
579
|
+
category,
|
|
580
|
+
createdAt,
|
|
581
|
+
deactivatedAt,
|
|
582
|
+
goalAmount,
|
|
583
|
+
goalDate,
|
|
584
|
+
id,
|
|
585
|
+
name,
|
|
586
|
+
userId,
|
|
587
|
+
updatedAt,
|
|
588
|
+
}) {
|
|
589
|
+
super({ balance, createdAt, deactivatedAt, id, userId, updatedAt });
|
|
590
|
+
this.category = enumValueFromString(SavingCategory, category);
|
|
591
|
+
this.goalAmount = goalAmount;
|
|
592
|
+
this.goalDate = dateToUTC(goalDate);
|
|
593
|
+
this.name = name;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
static fromObject = ({ balance, category, id, name, ...obj }) =>
|
|
597
|
+
new Saving({
|
|
598
|
+
balance,
|
|
599
|
+
category,
|
|
600
|
+
id,
|
|
601
|
+
name,
|
|
602
|
+
createdAt: obj.created_at,
|
|
603
|
+
deactivatedAt: obj.deactivated_at,
|
|
604
|
+
goalAmount: obj.goal_amount,
|
|
605
|
+
goalDate: obj.goal_date,
|
|
606
|
+
userId: obj.user_id,
|
|
607
|
+
updatedAt: obj.updated_at,
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const categoriesFromString = (categoriesList) => {
|
|
612
|
+
if (categoriesList == null) return [];
|
|
613
|
+
return categoriesList.map((category) =>
|
|
614
|
+
enumValueFromString(ServiceProviderCategory, category),
|
|
615
|
+
);
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
class ServiceProvider {
|
|
619
|
+
constructor({ categories, id, name, providerKey }) {
|
|
620
|
+
this.categories = categoriesFromString(categories);
|
|
621
|
+
this.id = id;
|
|
622
|
+
this.name = name;
|
|
623
|
+
this.providerKey = providerKey;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
static fromObject = ({ categories, id, name, ...obj }) =>
|
|
627
|
+
new ServiceProvider({
|
|
628
|
+
categories,
|
|
629
|
+
id,
|
|
630
|
+
name,
|
|
631
|
+
providerKey: obj.provider_key,
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
class Statement {
|
|
636
|
+
constructor({ createdAt, id, month, year }) {
|
|
637
|
+
this.createdAt = dateToUTC(createdAt);
|
|
638
|
+
this.id = id;
|
|
639
|
+
this.month = month;
|
|
640
|
+
this.year = year;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
static fromObject = ({ id, month, year, ...obj }) =>
|
|
644
|
+
new Statement({
|
|
645
|
+
id,
|
|
646
|
+
month,
|
|
647
|
+
year,
|
|
648
|
+
createdAt: obj.created_at,
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
class Transfer extends Transaction {
|
|
653
|
+
constructor({
|
|
654
|
+
accountNumber,
|
|
655
|
+
amount,
|
|
656
|
+
createdAt,
|
|
657
|
+
descriptor,
|
|
658
|
+
destinationUri,
|
|
659
|
+
id,
|
|
660
|
+
idempotencyKey,
|
|
661
|
+
network,
|
|
662
|
+
recipientName,
|
|
663
|
+
status,
|
|
664
|
+
trackingKey,
|
|
665
|
+
updatedAt,
|
|
666
|
+
userId,
|
|
667
|
+
}) {
|
|
668
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
669
|
+
this.accountNumber = accountNumber;
|
|
670
|
+
this.destinationUri = destinationUri;
|
|
671
|
+
this.id = id;
|
|
672
|
+
this.idempotencyKey = idempotencyKey;
|
|
673
|
+
this.network = enumValueFromString(TransferNetwork, network);
|
|
674
|
+
this.recipientName = recipientName;
|
|
675
|
+
this.trackingKey = trackingKey;
|
|
676
|
+
this.updatedAt = dateToUTC(updatedAt);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
static fromObject = ({ amount, descriptor, id, network, status, ...obj }) =>
|
|
680
|
+
new Transfer({
|
|
681
|
+
amount,
|
|
682
|
+
descriptor,
|
|
683
|
+
id,
|
|
684
|
+
network,
|
|
685
|
+
status,
|
|
686
|
+
accountNumber: obj.account_number,
|
|
687
|
+
createdAt: obj.created_at,
|
|
688
|
+
destinationUri: obj.destination_uri,
|
|
689
|
+
idempotencyKey: obj.idempotency_key,
|
|
690
|
+
recipientName: obj.recipient_name,
|
|
691
|
+
trackingKey: obj.tracking_key,
|
|
692
|
+
updatedAt: obj.updated_at,
|
|
693
|
+
userId: obj.user_id,
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
class UserCredential {
|
|
698
|
+
constructor({ createdAt, id, isActive, updatedAt }) {
|
|
699
|
+
this.createdAt = dateToUTC(createdAt);
|
|
700
|
+
this.id = id;
|
|
701
|
+
this.isActive = isActive;
|
|
702
|
+
this.updatedAt = dateToUTC(updatedAt);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
static fromObject = ({ id, ...obj }) =>
|
|
706
|
+
new UserCredential({
|
|
707
|
+
id,
|
|
708
|
+
createdAt: obj.created_at,
|
|
709
|
+
isActive: obj.is_active,
|
|
710
|
+
updatedAt: obj.updated_at,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
class UserLogin {
|
|
715
|
+
constructor({ id, lastLoginAt, success }) {
|
|
716
|
+
this.id = id;
|
|
717
|
+
this.lastLoginAt = dateToUTC(lastLoginAt);
|
|
718
|
+
this.success = success;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
static fromObject = ({ id, success, ...obj }) =>
|
|
722
|
+
new UserLogin({
|
|
723
|
+
id,
|
|
724
|
+
success,
|
|
725
|
+
lastLoginAt: obj.last_login_at,
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
class WalletTransaction extends Transaction {
|
|
730
|
+
constructor({
|
|
731
|
+
amount,
|
|
732
|
+
createdAt,
|
|
733
|
+
descriptor,
|
|
734
|
+
id,
|
|
735
|
+
status,
|
|
736
|
+
transactionType,
|
|
737
|
+
userId,
|
|
738
|
+
walletUri,
|
|
739
|
+
}) {
|
|
740
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
741
|
+
this.id = id;
|
|
742
|
+
this.transactionType = enumValueFromString(
|
|
743
|
+
WalletTransactionType,
|
|
744
|
+
transactionType,
|
|
745
|
+
);
|
|
746
|
+
this.walletUri = walletUri;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
static fromObject = ({ amount, descriptor, id, status, ...obj }) =>
|
|
750
|
+
new WalletTransaction({
|
|
751
|
+
amount,
|
|
752
|
+
descriptor,
|
|
753
|
+
id,
|
|
754
|
+
status,
|
|
755
|
+
createdAt: obj.created_at,
|
|
756
|
+
transactionType: obj.transaction_type,
|
|
757
|
+
userId: obj.user_id,
|
|
758
|
+
walletUri: obj.wallet_uri,
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
class WhatsAppTransfer extends Transaction {
|
|
763
|
+
constructor({
|
|
764
|
+
amount,
|
|
765
|
+
claimUrl,
|
|
766
|
+
createdAt,
|
|
767
|
+
descriptor,
|
|
768
|
+
destinationUri,
|
|
769
|
+
expiresAt,
|
|
770
|
+
id,
|
|
771
|
+
network,
|
|
772
|
+
phoneNumber,
|
|
773
|
+
recipientName,
|
|
774
|
+
status,
|
|
775
|
+
trackingKey,
|
|
776
|
+
updatedAt,
|
|
777
|
+
userId,
|
|
778
|
+
}) {
|
|
779
|
+
super({ amount, createdAt, descriptor, status, userId });
|
|
780
|
+
this.claimUrl = claimUrl;
|
|
781
|
+
this.destinationUri = destinationUri;
|
|
782
|
+
this.id = id;
|
|
783
|
+
this.expiresAt = dateToUTC(expiresAt);
|
|
784
|
+
this.network = enumValueFromString(TransferNetwork, network);
|
|
785
|
+
this.phoneNumber = phoneNumber;
|
|
786
|
+
this.recipientName = recipientName;
|
|
787
|
+
this.trackingKey = trackingKey;
|
|
788
|
+
this.updatedAt = dateToUTC(updatedAt);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
static fromObject = ({ amount, descriptor, id, network, status, ...obj }) =>
|
|
792
|
+
new WhatsAppTransfer({
|
|
793
|
+
amount,
|
|
794
|
+
descriptor,
|
|
795
|
+
id,
|
|
796
|
+
network,
|
|
797
|
+
status,
|
|
798
|
+
createdAt: obj.created_at,
|
|
799
|
+
claimUrl: obj.claim_url,
|
|
800
|
+
destinationUri: obj.destination_uri,
|
|
801
|
+
expiresAt: obj.expires_at,
|
|
802
|
+
phoneNumber: obj.phone_number,
|
|
803
|
+
recipientName: obj.recipient_name,
|
|
804
|
+
trackingKey: obj.tracking_key,
|
|
805
|
+
updatedAt: obj.updated_at,
|
|
806
|
+
userId: obj.user_id,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
class MixinBuilder {
|
|
811
|
+
constructor(superclass) {
|
|
812
|
+
this.superclass = superclass;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
with(...mixins) {
|
|
816
|
+
return mixins.reduce((c, mixin) => mixin(c), this.superclass);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
const mix = (superclass) => new MixinBuilder(superclass);
|
|
821
|
+
|
|
822
|
+
const getModelFromPath = (path, obj) => {
|
|
823
|
+
const mapper = {
|
|
824
|
+
accounts: () => Account.fromObject(obj),
|
|
825
|
+
api_keys: () => ApiKey.fromObject(obj),
|
|
826
|
+
arpc: () => Arpc.fromObject(obj),
|
|
827
|
+
balance_entries: () => BalanceEntry.fromObject(obj),
|
|
828
|
+
bill_payments: () => BillPayment.fromObject(obj),
|
|
829
|
+
cards: () => Card.fromObject(obj),
|
|
830
|
+
card_activations: () => CardActivation.fromObject(obj),
|
|
831
|
+
card_transactions: () => CardTransaction.fromObject(obj),
|
|
832
|
+
card_validations: () => CardValidation.fromObject(obj),
|
|
833
|
+
commissions: () => Commission.fromObject(obj),
|
|
834
|
+
deposits: () => Deposit.fromObject(obj),
|
|
835
|
+
login_tokens: () => LoginToken.fromObject(obj),
|
|
836
|
+
savings: () => Saving.fromObject(obj),
|
|
837
|
+
service_providers: () => ServiceProvider.fromObject(obj),
|
|
838
|
+
statements: () => Statement.fromObject(obj),
|
|
839
|
+
transfers: () => Transfer.fromObject(obj),
|
|
840
|
+
user_credentials: () => UserCredential.fromObject(obj),
|
|
841
|
+
user_logins: () => UserLogin.fromObject(obj),
|
|
842
|
+
wallet_transactions: () => WalletTransaction.fromObject(obj),
|
|
843
|
+
whatsapp_transfers: () => WhatsAppTransfer.fromObject(obj),
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
return mapper[path]();
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
const getResource = (uri) => {
|
|
850
|
+
if (uri === null || uri === '') return null;
|
|
851
|
+
const regExp = '/(.*?)/';
|
|
852
|
+
const matches = uri.match(regExp);
|
|
853
|
+
if (matches === null) return null;
|
|
854
|
+
return matches[0].replaceAll('/', '');
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
class Resource {
|
|
858
|
+
constructor(path, QueryParams, client) {
|
|
859
|
+
this.path = path;
|
|
860
|
+
this.QueryParams = QueryParams;
|
|
861
|
+
this.client = client;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
const Retrievable = (SuperClass) =>
|
|
866
|
+
class Retrievable extends SuperClass {
|
|
867
|
+
async retrieve(id) {
|
|
868
|
+
const response = await this.client.get({
|
|
869
|
+
endpoint: `/${this.path}/${id}`,
|
|
870
|
+
});
|
|
871
|
+
return getModelFromPath(this.path, response);
|
|
872
|
+
}
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
const Creatable = (SuperClass) =>
|
|
876
|
+
class Creatable extends SuperClass {
|
|
877
|
+
async _create(data) {
|
|
878
|
+
const response = await this.client.post({
|
|
879
|
+
endpoint: `/${this.path}`,
|
|
880
|
+
data,
|
|
881
|
+
});
|
|
882
|
+
return getModelFromPath(this.path, response);
|
|
883
|
+
}
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
const Updateable = (SuperClass) =>
|
|
887
|
+
class Updateable extends SuperClass {
|
|
888
|
+
async _update(id, data) {
|
|
889
|
+
const response = await this.client.patch({
|
|
890
|
+
endpoint: `/${this.path}/${id}`,
|
|
891
|
+
data,
|
|
892
|
+
});
|
|
893
|
+
const model = getModelFromPath(this.path, response);
|
|
894
|
+
return model;
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
|
|
898
|
+
const Deactivable = (SuperClass) =>
|
|
899
|
+
class Deactivable extends SuperClass {
|
|
900
|
+
async _deactivate(id, data) {
|
|
901
|
+
const response = await this.client.delete({
|
|
902
|
+
endpoint: `/${this.path}/${id}`,
|
|
903
|
+
data,
|
|
904
|
+
});
|
|
905
|
+
const model = getModelFromPath(this.path, response);
|
|
906
|
+
return model;
|
|
907
|
+
}
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
const Downlodable = (SuperClass) =>
|
|
911
|
+
class Downlodable extends SuperClass {
|
|
912
|
+
async _download(id, format) {
|
|
913
|
+
const byteString = await this.client.get({
|
|
914
|
+
endpoint: `/${this.path}/${id}`,
|
|
915
|
+
format,
|
|
916
|
+
});
|
|
917
|
+
return byteString;
|
|
918
|
+
}
|
|
919
|
+
};
|
|
920
|
+
|
|
921
|
+
const Queryable = (SuperClass) =>
|
|
922
|
+
class Queryable extends SuperClass {
|
|
923
|
+
async one(queryParams = new this.QueryParams({})) {
|
|
924
|
+
const { items } = await this.client.get({
|
|
925
|
+
endpoint: `/${this.path}`,
|
|
926
|
+
params: queryParams.toParams(),
|
|
927
|
+
});
|
|
928
|
+
|
|
929
|
+
if (!items || !items.length) throw new NoResultFound();
|
|
930
|
+
if (items.length > 1) throw new MultipleResultsFound();
|
|
931
|
+
|
|
932
|
+
const [item] = items;
|
|
933
|
+
const model = getModelFromPath(this.path, item);
|
|
934
|
+
return model;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
async first(queryParams = new this.QueryParams({})) {
|
|
938
|
+
const { items } = await this.client.get({
|
|
939
|
+
endpoint: `/${this.path}`,
|
|
940
|
+
params: queryParams.toParams(),
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
if (!items || !items[0]) return null;
|
|
944
|
+
|
|
945
|
+
const [item] = items;
|
|
946
|
+
const model = getModelFromPath(this.path, item);
|
|
947
|
+
return model;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
async count(queryParams = new this.QueryParams({})) {
|
|
951
|
+
const qP = queryParams;
|
|
952
|
+
qP.count = true;
|
|
953
|
+
const { count } = await this.client.get({
|
|
954
|
+
endpoint: `/${this.path}`,
|
|
955
|
+
params: queryParams.toParams(),
|
|
956
|
+
});
|
|
957
|
+
return count || 0;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
async *all(queryParams = new this.QueryParams({})) {
|
|
961
|
+
let nextPageUri = `/${this.path}?${queryParams.toQueryString()}`;
|
|
962
|
+
while (nextPageUri) {
|
|
963
|
+
// eslint-disable-next-line no-await-in-loop
|
|
964
|
+
const page = await this.client.get({ endpoint: nextPageUri });
|
|
965
|
+
if (page.items) {
|
|
966
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
967
|
+
for (const item of page.items) {
|
|
968
|
+
const model = getModelFromPath(this.path, item);
|
|
969
|
+
yield model;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
nextPageUri = page.next_page_uri;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
class AccountResource extends mix(Resource).with(Queryable, Retrievable) {
|
|
978
|
+
constructor(client) {
|
|
979
|
+
super('accounts', AccountQuery, client);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
class ApiKeyResource extends mix(Resource).with(
|
|
984
|
+
Creatable,
|
|
985
|
+
Deactivable,
|
|
986
|
+
Queryable,
|
|
987
|
+
Retrievable,
|
|
988
|
+
Updateable,
|
|
989
|
+
) {
|
|
990
|
+
constructor(client) {
|
|
991
|
+
super('api_keys', ApiKeyQuery, client);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
async create() {
|
|
995
|
+
const apiKey = await this._create();
|
|
996
|
+
return apiKey;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
async deactivate(apiKeyId, minutes = 0) {
|
|
1000
|
+
const apiKey = await this._deactivate(apiKeyId, { minutes });
|
|
1001
|
+
return apiKey;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
async update(apiKeyId, metadata, userId) {
|
|
1005
|
+
const request = new ApiKeyUpdateRequest(userId, metadata);
|
|
1006
|
+
const apiKey = await this._update(apiKeyId, request.toCleanObject());
|
|
1007
|
+
return apiKey;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
class ArpcResource extends mix(Resource).with(Creatable) {
|
|
1012
|
+
constructor(client) {
|
|
1013
|
+
super('arpc', Object, client);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
async create({
|
|
1017
|
+
number,
|
|
1018
|
+
arqc,
|
|
1019
|
+
arpcMethod,
|
|
1020
|
+
transactionData,
|
|
1021
|
+
responseCode,
|
|
1022
|
+
transactionCounter,
|
|
1023
|
+
panSequence,
|
|
1024
|
+
uniqueNumber,
|
|
1025
|
+
trackDataMethod,
|
|
1026
|
+
}) {
|
|
1027
|
+
const request = new ArpcRequest({
|
|
1028
|
+
number,
|
|
1029
|
+
arqc,
|
|
1030
|
+
arpcMethod,
|
|
1031
|
+
transactionData,
|
|
1032
|
+
responseCode,
|
|
1033
|
+
transactionCounter,
|
|
1034
|
+
panSequence,
|
|
1035
|
+
uniqueNumber,
|
|
1036
|
+
trackDataMethod,
|
|
1037
|
+
});
|
|
1038
|
+
const arpc = await this._create(request.toObject());
|
|
1039
|
+
return arpc;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
class BalanceEntryResource extends mix(Resource).with(Queryable, Retrievable) {
|
|
1044
|
+
constructor(client) {
|
|
1045
|
+
super('balance_entries', BalanceEntryQuery, client);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
async relatedTransaction(relatedTransactionUri) {
|
|
1049
|
+
const resource = getResource(relatedTransactionUri);
|
|
1050
|
+
if (resource == null) return null;
|
|
1051
|
+
|
|
1052
|
+
const response = await this.client.get({
|
|
1053
|
+
endpoint: relatedTransactionUri,
|
|
1054
|
+
});
|
|
1055
|
+
return getModelFromPath(`${resource}`, response);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
async fundingInstrument(fundingInstrumentUri) {
|
|
1059
|
+
const resource = getResource(fundingInstrumentUri);
|
|
1060
|
+
if (resource == null) return null;
|
|
1061
|
+
|
|
1062
|
+
const response = await this.client.get({
|
|
1063
|
+
endpoint: fundingInstrumentUri,
|
|
1064
|
+
});
|
|
1065
|
+
return getModelFromPath(`${resource}`, response);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
class BillPaymentResource extends mix(Resource).with(Queryable, Retrievable) {
|
|
1070
|
+
constructor(client) {
|
|
1071
|
+
super('bill_payments', BillPaymentQuery, client);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
async serviceProvider(providerUri) {
|
|
1075
|
+
const resource = getResource(providerUri);
|
|
1076
|
+
if (resource == null) return null;
|
|
1077
|
+
|
|
1078
|
+
const response = await this.client.get({ endpoint: providerUri });
|
|
1079
|
+
return getModelFromPath(`${resource}`, response);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
class CardActivationResource extends mix(Resource).with(Creatable) {
|
|
1084
|
+
constructor(client) {
|
|
1085
|
+
super('card_activations', Object, client);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
async create({ number, expMonth, expYear, cvv2 }) {
|
|
1089
|
+
const request = new CardActivationRequest(number, expMonth, expYear, cvv2);
|
|
1090
|
+
const activation = await this._create(request.toCleanObject());
|
|
1091
|
+
return activation;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
async card(cardUri) {
|
|
1095
|
+
if (!cardUri) return null;
|
|
1096
|
+
const response = await this.client.get({ endpoint: cardUri });
|
|
1097
|
+
return Card.fromObject(response);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
class CardResource extends mix(Resource).with(
|
|
1102
|
+
Creatable,
|
|
1103
|
+
Deactivable,
|
|
1104
|
+
Queryable,
|
|
1105
|
+
Retrievable,
|
|
1106
|
+
Updateable,
|
|
1107
|
+
) {
|
|
1108
|
+
constructor(client) {
|
|
1109
|
+
super('cards', CardsQuery, client);
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
async create(userId, issuer, fundingType) {
|
|
1113
|
+
const request = new CardRequest(userId, issuer, fundingType);
|
|
1114
|
+
const card = await this._create(request.toCleanObject());
|
|
1115
|
+
return card;
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
async deactivate(cardId) {
|
|
1119
|
+
const response = await this._deactivate(cardId);
|
|
1120
|
+
return response;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
async update(cardId, status, pinBlock) {
|
|
1124
|
+
const request = new CardUpdateRequest(status, pinBlock);
|
|
1125
|
+
const card = await this._update(cardId, request.toCleanObject());
|
|
1126
|
+
return card;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
class CardTransactionResource extends mix(Resource).with(
|
|
1131
|
+
Queryable,
|
|
1132
|
+
Retrievable,
|
|
1133
|
+
) {
|
|
1134
|
+
constructor(client) {
|
|
1135
|
+
super('card_transactions', CardTransactionQuery, client);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
async relatedCard(relatedCardUri) {
|
|
1139
|
+
const resource = getResource(relatedCardUri);
|
|
1140
|
+
if (resource == null) return null;
|
|
1141
|
+
|
|
1142
|
+
const response = await this.client.get({
|
|
1143
|
+
endpoint: relatedCardUri,
|
|
1144
|
+
});
|
|
1145
|
+
return getModelFromPath(`${resource}`, response);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
class CardValidationResource extends mix(Resource).with(Creatable) {
|
|
1150
|
+
constructor(client) {
|
|
1151
|
+
super('card_validations', Object, client);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
async create({
|
|
1155
|
+
cvv,
|
|
1156
|
+
cvv2,
|
|
1157
|
+
expMonth,
|
|
1158
|
+
expYear,
|
|
1159
|
+
icvv,
|
|
1160
|
+
number,
|
|
1161
|
+
pinBlock,
|
|
1162
|
+
pinAttemptsExceeded,
|
|
1163
|
+
}) {
|
|
1164
|
+
const request = new CardValidationRequest({
|
|
1165
|
+
cvv,
|
|
1166
|
+
cvv2,
|
|
1167
|
+
expMonth,
|
|
1168
|
+
expYear,
|
|
1169
|
+
icvv,
|
|
1170
|
+
number,
|
|
1171
|
+
pinBlock,
|
|
1172
|
+
pinAttemptsExceeded,
|
|
1173
|
+
});
|
|
1174
|
+
const validation = await this._create(request.toCleanObject());
|
|
1175
|
+
return validation;
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
async card(cardUri) {
|
|
1179
|
+
if (!cardUri) return null;
|
|
1180
|
+
const response = await this.client.get({ endpoint: cardUri });
|
|
1181
|
+
return Card.fromObject(response);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
class CommissionResource extends mix(Resource).with(Queryable, Retrievable) {
|
|
1186
|
+
constructor(client) {
|
|
1187
|
+
super('commissions', QueryParams, client);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
async relatedTransaction(relatedTransactionUri) {
|
|
1191
|
+
const resource = getResource(relatedTransactionUri);
|
|
1192
|
+
if (resource == null) return null;
|
|
1193
|
+
|
|
1194
|
+
const response = await this.client.get(relatedTransactionUri);
|
|
1195
|
+
return getModelFromPath(`${resource}`, response);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
class DepositResource extends mix(Resource).with(Queryable, Retrievable) {
|
|
1200
|
+
constructor(client) {
|
|
1201
|
+
super('deposits', DepositQuery, client);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
async source(sourceUri) {
|
|
1205
|
+
const response = await this.client.get({ endpoint: sourceUri });
|
|
1206
|
+
return Account.fromObject(response);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
class LoginTokenResource extends mix(Resource).with(Creatable) {
|
|
1211
|
+
constructor(client) {
|
|
1212
|
+
super('login_tokens', Object, client);
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
async create() {
|
|
1216
|
+
const loginToken = await this._create();
|
|
1217
|
+
return loginToken;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
class SavingResource extends mix(Resource).with(
|
|
1222
|
+
Creatable,
|
|
1223
|
+
Deactivable,
|
|
1224
|
+
Queryable,
|
|
1225
|
+
Retrievable,
|
|
1226
|
+
Updateable,
|
|
1227
|
+
) {
|
|
1228
|
+
constructor(client) {
|
|
1229
|
+
super('savings', WalletQuery, client);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
async create(category, goalAmount, goalDate, name) {
|
|
1233
|
+
const request = new SavingRequest(category, goalAmount, goalDate, name);
|
|
1234
|
+
const saving = await this._create(request.toObject());
|
|
1235
|
+
return saving;
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
async deactivate(savingId) {
|
|
1239
|
+
const response = await this._deactivate(savingId);
|
|
1240
|
+
return response;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
async update(savingId, category, goalAmount, goalDate, name) {
|
|
1244
|
+
const request = new SavingRequest(category, goalAmount, goalDate, name);
|
|
1245
|
+
const saving = await this._update(savingId, request.toObject());
|
|
1246
|
+
return saving;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
class ServiceProviderResource extends mix(Resource).with(
|
|
1251
|
+
Queryable,
|
|
1252
|
+
Retrievable,
|
|
1253
|
+
) {
|
|
1254
|
+
constructor(client) {
|
|
1255
|
+
super('service_providers', QueryParams, client);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
class StatementResource extends mix(Resource).with(Downlodable, Queryable) {
|
|
1260
|
+
constructor(client) {
|
|
1261
|
+
super('statements', StatementQuery, client);
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
async pdf(id) {
|
|
1265
|
+
const byteString = await this._download(id, FileFormat.Pdf);
|
|
1266
|
+
return byteString;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
async xml(id) {
|
|
1270
|
+
const byteString = await this._download(id, FileFormat.Xml);
|
|
1271
|
+
return byteString;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
class TransferResource extends mix(Resource).with(
|
|
1276
|
+
Creatable,
|
|
1277
|
+
Queryable,
|
|
1278
|
+
Retrievable,
|
|
1279
|
+
) {
|
|
1280
|
+
constructor(client) {
|
|
1281
|
+
super('transfers', TransferQuery, client);
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
async destination(destinationUri) {
|
|
1285
|
+
const response = await this.client.get({
|
|
1286
|
+
endpoint: destinationUri,
|
|
1287
|
+
});
|
|
1288
|
+
return Account.fromObject(response);
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
async create({
|
|
1292
|
+
accountNumber,
|
|
1293
|
+
amount,
|
|
1294
|
+
descriptor,
|
|
1295
|
+
recipientName,
|
|
1296
|
+
idempotencyKey,
|
|
1297
|
+
}) {
|
|
1298
|
+
const key =
|
|
1299
|
+
idempotencyKey ||
|
|
1300
|
+
this.constructor._genIdempotencyKey(accountNumber, amount);
|
|
1301
|
+
const request = new TransferRequest(
|
|
1302
|
+
accountNumber,
|
|
1303
|
+
amount,
|
|
1304
|
+
descriptor,
|
|
1305
|
+
key,
|
|
1306
|
+
recipientName,
|
|
1307
|
+
);
|
|
1308
|
+
const transfer = await this._create(request.toCleanObject());
|
|
1309
|
+
return transfer;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
async createMany(requests) {
|
|
1313
|
+
if (!requests || !Array.isArray(requests) || !requests.length) return {};
|
|
1314
|
+
const transfers = { submitted: [], errors: [] };
|
|
1315
|
+
|
|
1316
|
+
await Promise.all(
|
|
1317
|
+
requests.map(
|
|
1318
|
+
async ({
|
|
1319
|
+
accountNumber,
|
|
1320
|
+
amount,
|
|
1321
|
+
descriptor,
|
|
1322
|
+
idempotencyKey,
|
|
1323
|
+
recipientName,
|
|
1324
|
+
}) => {
|
|
1325
|
+
const actualRequest = new TransferRequest(
|
|
1326
|
+
accountNumber,
|
|
1327
|
+
amount,
|
|
1328
|
+
descriptor,
|
|
1329
|
+
idempotencyKey ||
|
|
1330
|
+
this.constructor._genIdempotencyKey(accountNumber, amount),
|
|
1331
|
+
recipientName,
|
|
1332
|
+
);
|
|
1333
|
+
let transfer;
|
|
1334
|
+
try {
|
|
1335
|
+
transfer = await this._create(actualRequest.toCleanObject());
|
|
1336
|
+
} catch (error) {
|
|
1337
|
+
transfers.errors.push({ actualRequest, error });
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
transfers.submitted.push(transfer);
|
|
1341
|
+
},
|
|
1342
|
+
),
|
|
1343
|
+
);
|
|
1344
|
+
|
|
1345
|
+
return transfers;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
static _genIdempotencyKey(accountNumber, amount) {
|
|
1349
|
+
const [date] = dateToUTC(Date.now()).toISOString().split('T');
|
|
1350
|
+
return `${date}:${accountNumber}:${amount}`;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
class UserCredentialResource extends mix(Resource).with(Creatable, Updateable) {
|
|
1355
|
+
constructor(client) {
|
|
1356
|
+
super('user_credentials', Object, client);
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
async create(password) {
|
|
1360
|
+
const request = new UserCredentialRequest(password);
|
|
1361
|
+
const credential = await this._create(request.toObject());
|
|
1362
|
+
return credential;
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
async update({ isActive, password, userId = 'me' }) {
|
|
1366
|
+
const request = new UserCredentialUpdateRequest(password, isActive);
|
|
1367
|
+
const credential = await this._update(userId, request.toCleanObject());
|
|
1368
|
+
return credential;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
class UserLoginResource extends mix(Resource).with(Creatable, Deactivable) {
|
|
1373
|
+
constructor(client) {
|
|
1374
|
+
super('user_logins', Object, client);
|
|
1375
|
+
this.loginIdInHeaders = null;
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
async create(password, userId) {
|
|
1379
|
+
const request = new UserLoginRequest(password, userId);
|
|
1380
|
+
const userLogin = await this._create(request.toObject());
|
|
1381
|
+
if (!userLogin.success) throw new InvalidPassword();
|
|
1382
|
+
|
|
1383
|
+
// Set login id to headers
|
|
1384
|
+
this.loginIdInHeaders = this.client.addHeadersToRequest({
|
|
1385
|
+
'X-Cuenca-LoginId': userLogin.id,
|
|
1386
|
+
});
|
|
1387
|
+
|
|
1388
|
+
return userLogin;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
async logOut(userId = 'me') {
|
|
1392
|
+
await this._deactivate(userId, {});
|
|
1393
|
+
|
|
1394
|
+
// Remove current login id from headers
|
|
1395
|
+
this.loginIdInHeaders && this.loginIdInHeaders.eject();
|
|
1396
|
+
|
|
1397
|
+
return true;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
class WalletTransactionsResource extends mix(Resource).with(
|
|
1402
|
+
Creatable,
|
|
1403
|
+
Queryable,
|
|
1404
|
+
Retrievable,
|
|
1405
|
+
) {
|
|
1406
|
+
constructor(client) {
|
|
1407
|
+
super('wallet_transactions', WalletTransactionQuery, client);
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
async create(amount, transactionType, walletUri) {
|
|
1411
|
+
const request = new WalletTransactionRequest(
|
|
1412
|
+
amount,
|
|
1413
|
+
transactionType,
|
|
1414
|
+
walletUri,
|
|
1415
|
+
);
|
|
1416
|
+
const walletTransaction = await this._create(request.toObject());
|
|
1417
|
+
return walletTransaction;
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
async realtedWallet(walletUri) {
|
|
1421
|
+
const resource = getResource(walletUri);
|
|
1422
|
+
if (resource == null) return null;
|
|
1423
|
+
|
|
1424
|
+
const response = await this.client.get(walletUri);
|
|
1425
|
+
return getModelFromPath(`${resource}`, response);
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
class WhatsAppTransferResource extends mix(Resource).with(
|
|
1430
|
+
Queryable,
|
|
1431
|
+
Retrievable,
|
|
1432
|
+
) {
|
|
1433
|
+
constructor(client) {
|
|
1434
|
+
super('whatsapp_transfers', QueryParams, client);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
async accountDestination(destinationUri) {
|
|
1438
|
+
const resource = getResource(destinationUri);
|
|
1439
|
+
if (resource == null) return null;
|
|
1440
|
+
|
|
1441
|
+
const response = await this.client.get(destinationUri);
|
|
1442
|
+
return getModelFromPath(`${resource}`, response);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
class Cuenca {
|
|
1447
|
+
constructor(apiKey, apiSecret, phase = Phase.Sandbox) {
|
|
1448
|
+
this.client = new Client({ apiKey, apiSecret, phase });
|
|
1449
|
+
this.withClient(this.client);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
withClient(client) {
|
|
1453
|
+
this.accounts = new AccountResource(client);
|
|
1454
|
+
this.apiKeys = new ApiKeyResource(client);
|
|
1455
|
+
this.balanceEntries = new BalanceEntryResource(client);
|
|
1456
|
+
this.billPayments = new BillPaymentResource(client);
|
|
1457
|
+
this.cardActivations = new CardActivationResource(client);
|
|
1458
|
+
this.cards = new CardResource(client);
|
|
1459
|
+
this.cardTransactions = new CardTransactionResource(client);
|
|
1460
|
+
this.cardValidations = new CardValidationResource(client);
|
|
1461
|
+
this.commissions = new CommissionResource(client);
|
|
1462
|
+
this.deposits = new DepositResource(client);
|
|
1463
|
+
this.loginTokens = new LoginTokenResource(client);
|
|
1464
|
+
this.savings = new SavingResource(client);
|
|
1465
|
+
this.serviceProviders = new ServiceProviderResource(client);
|
|
1466
|
+
this.statements = new StatementResource(client);
|
|
1467
|
+
this.transfers = new TransferResource(client);
|
|
1468
|
+
this.userCredentials = new UserCredentialResource(client);
|
|
1469
|
+
this.userLogins = new UserLoginResource(client);
|
|
1470
|
+
this.walletTransactions = new WalletTransactionsResource(client);
|
|
1471
|
+
this.whatsAppTransfers = new WhatsAppTransferResource(client);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
export { Cuenca };
|