@cuenca-mx/cuenca-js 0.0.1-dev.1

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.
@@ -0,0 +1,498 @@
1
+ 'use strict';
2
+
3
+ var errors = require('./errors.js');
4
+ var data = require('./data-c53f1052.js');
5
+
6
+ class BaseRequest {
7
+ toObject() {
8
+ return {};
9
+ }
10
+
11
+ toCleanObject() {
12
+ const obj = this.toObject();
13
+ Object.keys(obj).forEach((k) => {
14
+ if (obj[k] == null) delete obj[k];
15
+ });
16
+ return obj;
17
+ }
18
+ }
19
+
20
+ class ApiKeyUpdateRequest extends BaseRequest {
21
+ constructor(userId, metadata) {
22
+ super();
23
+ this.userId = userId;
24
+ this.metadata = metadata;
25
+ }
26
+
27
+ toObject() {
28
+ return {
29
+ user_id: this.userId,
30
+ metadata: this.metadata,
31
+ };
32
+ }
33
+ }
34
+
35
+ class ArpcRequest extends BaseRequest {
36
+ constructor({
37
+ arpcMethod,
38
+ arqc,
39
+ number,
40
+ panSequence,
41
+ responseCode,
42
+ transactionCounter,
43
+ transactionData,
44
+ trackDataMethod,
45
+ uniqueNumber,
46
+ }) {
47
+ super();
48
+ this.arpcM = arpcMethod;
49
+ this.arqc = arqc;
50
+ this.num = number;
51
+ this.panSequence = panSequence;
52
+ this.responseCode = responseCode;
53
+ this.transactionCounter = transactionCounter;
54
+ this.transactionData = transactionData;
55
+ this.trackDataMethod = trackDataMethod;
56
+ this.uniqueNumber = uniqueNumber;
57
+ }
58
+
59
+ get number() {
60
+ return this._number;
61
+ }
62
+
63
+ set num(value) {
64
+ if (!value) return;
65
+ const validations = [!!value, value.length === 16, /^\d{16}/.test(value)];
66
+ if (validations.some((x) => !x)) {
67
+ throw new errors.ValidationError('Invalid number');
68
+ }
69
+ this._number = value;
70
+ }
71
+
72
+ get arpcMethod() {
73
+ return this._arpcMethod;
74
+ }
75
+
76
+ set arpcM(value) {
77
+ if (!value) return;
78
+ const validations = [value.length === 1];
79
+ if (validations.some((x) => !x)) {
80
+ throw new errors.ValidationError('Invalid method');
81
+ }
82
+ this._arpcMethod = value;
83
+ }
84
+
85
+ toObject() {
86
+ return {
87
+ arpc_method: this.arpcMethod,
88
+ arqc: this.arqc,
89
+ number: this.number,
90
+ pan_sequence: this.panSequence,
91
+ response_code: this.responseCode,
92
+ transaction_counter: this.transactionCounter,
93
+ transaction_data: this.transactionData,
94
+ track_data_method: this.trackDataMethod,
95
+ unique_number: this.uniqueNumber,
96
+ };
97
+ }
98
+ }
99
+
100
+ class CardRequest extends BaseRequest {
101
+ constructor(userId, issuer, fundingType) {
102
+ super();
103
+ this.userId = userId;
104
+ this.issuer = issuer;
105
+ this.fundingType = fundingType;
106
+ }
107
+
108
+ toObject() {
109
+ return {
110
+ user_id: this.userId,
111
+ issuer: this.issuer,
112
+ funding_type: this.fundingType,
113
+ };
114
+ }
115
+ }
116
+
117
+ class CardUpdateRequest extends BaseRequest {
118
+ constructor(status, pinBlock) {
119
+ super();
120
+ this.status = status;
121
+ this.pinBlock = pinBlock;
122
+ }
123
+
124
+ toObject() {
125
+ return {
126
+ status: this.status,
127
+ pin_block: this.pinBlock,
128
+ };
129
+ }
130
+ }
131
+
132
+ class CardActivationRequest extends BaseRequest {
133
+ constructor(number, expMonth, expYear, cvv2) {
134
+ super();
135
+ this.n = number;
136
+ this.eM = expMonth;
137
+ this.eY = expYear;
138
+ this.c2 = cvv2;
139
+ }
140
+
141
+ get number() {
142
+ return this._number;
143
+ }
144
+
145
+ set n(value) {
146
+ const validations = [!!value, value.length === 16, /^\d{16}/.test(value)];
147
+ if (validations.some((x) => !x)) {
148
+ throw new errors.ValidationError('Invalid number');
149
+ }
150
+ this._number = value.trim();
151
+ }
152
+
153
+ get expMonth() {
154
+ return this._expMonth;
155
+ }
156
+
157
+ set eM(value) {
158
+ const validations = [!!value, value >= 1, value <= 12];
159
+ if (validations.some((x) => !x)) {
160
+ throw new errors.ValidationError('Invalid expiration month');
161
+ }
162
+ this._expMonth = value;
163
+ }
164
+
165
+ get expYear() {
166
+ return this._expYear;
167
+ }
168
+
169
+ set eY(value) {
170
+ const validations = [!!value, value >= 18, value <= 99];
171
+ if (validations.some((x) => !x)) {
172
+ throw new errors.ValidationError('Invalid expiration year');
173
+ }
174
+ this._expYear = value;
175
+ }
176
+
177
+ get cvv2() {
178
+ return this._cvv2;
179
+ }
180
+
181
+ set c2(value) {
182
+ const validations = [!!value, value.length === 3, /^\d{3}/.test(value)];
183
+ if (validations.some((x) => !x)) {
184
+ throw new errors.ValidationError('Invalid cvv2');
185
+ }
186
+ this._cvv2 = value;
187
+ }
188
+
189
+ toObject() {
190
+ return {
191
+ number: this.number,
192
+ exp_month: this.expMonth,
193
+ exp_year: this.expYear,
194
+ cvv2: this.cvv2,
195
+ };
196
+ }
197
+ }
198
+
199
+ class CardValidationRequest extends BaseRequest {
200
+ constructor({
201
+ cvv,
202
+ cvv2,
203
+ expMonth,
204
+ expYear,
205
+ icvv,
206
+ number,
207
+ pinBlock,
208
+ pinAttemptsExceeded,
209
+ }) {
210
+ super();
211
+ this.c = cvv;
212
+ this.c2 = cvv2;
213
+ this.em = expMonth;
214
+ this.ey = expYear;
215
+ this.ic = icvv;
216
+ this.n = number;
217
+ this.pinBloc = pinBlock;
218
+ this.pinAttemptsExceeded = pinAttemptsExceeded;
219
+ }
220
+
221
+ get cvv() {
222
+ return this._cvv;
223
+ }
224
+
225
+ set c(value) {
226
+ if (!value) return;
227
+ const validations = [value.length === 3];
228
+ if (validations.some((x) => !x)) {
229
+ throw new errors.ValidationError('Invalid cvv');
230
+ }
231
+ this._cvv = value;
232
+ }
233
+
234
+ get cvv2() {
235
+ return this._cvv2;
236
+ }
237
+
238
+ set c2(value) {
239
+ if (!value) return;
240
+ const validations = [value.length === 3];
241
+ if (validations.some((x) => !x)) {
242
+ throw new errors.ValidationError('Invalid cvv2');
243
+ }
244
+ this._cvv2 = value;
245
+ }
246
+
247
+ get expMonth() {
248
+ return this._expMonth;
249
+ }
250
+
251
+ set em(value) {
252
+ if (!value) return;
253
+ const validations = [value >= 1, value <= 12];
254
+ if (validations.some((x) => !x)) {
255
+ throw new errors.ValidationError('Invalid expiration month');
256
+ }
257
+ this._expMonth = value;
258
+ }
259
+
260
+ get expYear() {
261
+ return this._expYear;
262
+ }
263
+
264
+ set ey(value) {
265
+ if (!value) return;
266
+ const validations = [value >= 18, value <= 99];
267
+ if (validations.some((x) => !x)) {
268
+ throw new errors.ValidationError('Invalid expiration year');
269
+ }
270
+ this._expYear = value;
271
+ }
272
+
273
+ get icvv() {
274
+ return this._icvv;
275
+ }
276
+
277
+ set ic(value) {
278
+ if (!value) return;
279
+ const validations = [value.length === 3];
280
+ if (validations.some((x) => !x)) {
281
+ throw new errors.ValidationError('Invalid icvv');
282
+ }
283
+ this._icvv = value;
284
+ }
285
+
286
+ get number() {
287
+ return this._number;
288
+ }
289
+
290
+ set n(value) {
291
+ const validations = [!!value, value.length === 16, /^\d{16}/.test(value)];
292
+ if (validations.some((x) => !x)) {
293
+ throw new errors.ValidationError('Invalid number');
294
+ }
295
+ this._number = value;
296
+ }
297
+
298
+ toObject() {
299
+ return {
300
+ cvv: this.cvv,
301
+ cvv2: this.cvv2,
302
+ exp_month: this.expMonth,
303
+ exp_year: this.expYear,
304
+ icvv: this.icvv,
305
+ number: this.number,
306
+ pin_block: this.pinBloc,
307
+ pin_attempts_exceeded: this.pinAttemptsExceeded,
308
+ };
309
+ }
310
+ }
311
+
312
+ class SavingRequest extends BaseRequest {
313
+ constructor(category, goalAmount, goalDate, name) {
314
+ super();
315
+ this.category = category;
316
+ this.name = name;
317
+ this.goalAmount = goalAmount;
318
+ this.validDate = goalDate;
319
+ }
320
+
321
+ get goalDate() {
322
+ return this._goalDate;
323
+ }
324
+
325
+ set validDate(value) {
326
+ if (!value) return;
327
+ if (data.dateToUTC(value).getTime() <= data.dateToUTC(Date.now()).getTime()) {
328
+ throw new errors.ValidationError(
329
+ 'The goal_date always need to be higher than now',
330
+ );
331
+ }
332
+ this._goalDate = value;
333
+ }
334
+
335
+ toObject() {
336
+ return {
337
+ category: this.category,
338
+ goal_amount: this.goalAmount,
339
+ goal_date: this.goalDate,
340
+ name: this.name,
341
+ };
342
+ }
343
+ }
344
+
345
+ class TransferRequest extends BaseRequest {
346
+ constructor(
347
+ accountNumber,
348
+ amount,
349
+ descriptor,
350
+ idempotencyKey,
351
+ recipientName,
352
+ ) {
353
+ super();
354
+ this.accountNumber = accountNumber;
355
+ this.amount = amount;
356
+ this.descriptor = descriptor;
357
+ this.idempotencyKey = idempotencyKey;
358
+ this.recipientName = recipientName;
359
+ }
360
+
361
+ toObject() {
362
+ return {
363
+ account_number: this.accountNumber,
364
+ amount: this.amount,
365
+ descriptor: this.descriptor,
366
+ idempotency_key: this.idempotencyKey,
367
+ recipient_name: this.recipientName,
368
+ };
369
+ }
370
+ }
371
+
372
+ class UserCredentialRequest extends BaseRequest {
373
+ constructor(password) {
374
+ super();
375
+ this.pwd = password;
376
+ }
377
+
378
+ get password() {
379
+ return this._password;
380
+ }
381
+
382
+ set pwd(value) {
383
+ const validations = [!!value, value.length === 6, /^\d{6}$/.test(value)];
384
+ if (validations.some((x) => !x)) {
385
+ throw new errors.ValidationError('Invalid password');
386
+ }
387
+ this._password = value;
388
+ }
389
+
390
+ toObject() {
391
+ return {
392
+ password: this.password,
393
+ };
394
+ }
395
+ }
396
+
397
+ class UserCredentialUpdateRequest extends BaseRequest {
398
+ constructor(password, isActive) {
399
+ super();
400
+ this.pwd = password;
401
+ this.isActive = isActive;
402
+ this.req = {
403
+ password: this.password,
404
+ isActive: this.isActive,
405
+ };
406
+ }
407
+
408
+ get password() {
409
+ return this._password;
410
+ }
411
+
412
+ get request() {
413
+ return this._request;
414
+ }
415
+
416
+ set pwd(value) {
417
+ if (!value) {
418
+ this._password = value;
419
+ return;
420
+ }
421
+ const validations = [value.length === 6, /^\d{6}$/.test(value)];
422
+ if (validations.some((x) => !x)) {
423
+ throw new errors.ValidationError('Invalid password');
424
+ }
425
+ this._password = value;
426
+ }
427
+
428
+ set req(value) {
429
+ if (value.password && value.isActive != null) {
430
+ throw new errors.ValidationError('Only one property can be updated at a time');
431
+ }
432
+ this._request = value;
433
+ }
434
+
435
+ toObject() {
436
+ return {
437
+ password: this.request.password,
438
+ is_active: this.request.isActive,
439
+ };
440
+ }
441
+ }
442
+
443
+ class UserLoginRequest extends BaseRequest {
444
+ constructor(password, userId = 'me') {
445
+ super();
446
+ this.pwd = password;
447
+ this.userId = userId;
448
+ }
449
+
450
+ get password() {
451
+ return this._password;
452
+ }
453
+
454
+ set pwd(value) {
455
+ const validations = [!!value, value.length === 6, /^\d{6}$/.test(value)];
456
+ if (validations.some((x) => !x)) {
457
+ throw new errors.ValidationError('Invalid password');
458
+ }
459
+ this._password = value;
460
+ }
461
+
462
+ toObject() {
463
+ return {
464
+ password: this.password,
465
+ user_id: this.userId,
466
+ };
467
+ }
468
+ }
469
+
470
+ class WalletTransactionRequest extends BaseRequest {
471
+ constructor(amount, transactionType, walletUri) {
472
+ super();
473
+ this.amount = amount;
474
+ this.transactionType = transactionType;
475
+ this.walletUri = walletUri;
476
+ }
477
+
478
+ toObject() {
479
+ return {
480
+ amount: this.amount,
481
+ transaction_type: this.transactionType,
482
+ wallet_uri: this.walletUri,
483
+ };
484
+ }
485
+ }
486
+
487
+ exports.ApiKeyUpdateRequest = ApiKeyUpdateRequest;
488
+ exports.ArpcRequest = ArpcRequest;
489
+ exports.CardActivationRequest = CardActivationRequest;
490
+ exports.CardRequest = CardRequest;
491
+ exports.CardUpdateRequest = CardUpdateRequest;
492
+ exports.CardValidationRequest = CardValidationRequest;
493
+ exports.SavingRequest = SavingRequest;
494
+ exports.TransferRequest = TransferRequest;
495
+ exports.UserCredentialRequest = UserCredentialRequest;
496
+ exports.UserCredentialUpdateRequest = UserCredentialUpdateRequest;
497
+ exports.UserLoginRequest = UserLoginRequest;
498
+ exports.WalletTransactionRequest = WalletTransactionRequest;
@@ -0,0 +1,11 @@
1
+ const dateToUTC = (date) => {
2
+ if (!date) return null;
3
+ const dateObj = new Date(date);
4
+ return new Date(dateObj.getTime());
5
+ };
6
+
7
+ const enumValueFromString = (enumValue, value) => {
8
+ return Object.values(enumValue).find((enumV) => enumV.value === value);
9
+ };
10
+
11
+ export { dateToUTC as d, enumValueFromString as e };
@@ -0,0 +1,64 @@
1
+ class CuencaException extends Error {
2
+ constructor(msg) {
3
+ super(msg);
4
+
5
+ Object.setPrototypeOf(this, CuencaException.prototype);
6
+ }
7
+ }
8
+
9
+ class CuencaResponseException extends CuencaException {
10
+ constructor(data, status) {
11
+ super(`Cuenca Response Error: ${status}`);
12
+ this.name = 'CuencaResponseError';
13
+ this.data = data;
14
+ this.status = status;
15
+
16
+ Object.setPrototypeOf(this, CuencaResponseException.prototype);
17
+ }
18
+ }
19
+
20
+ class NoResultFound extends CuencaException {
21
+ constructor() {
22
+ super('No results were found');
23
+ this.name = 'NoResultFound';
24
+
25
+ Object.setPrototypeOf(this, NoResultFound.prototype);
26
+ }
27
+ }
28
+
29
+ class MultipleResultsFound extends CuencaException {
30
+ constructor() {
31
+ super('One result was expected but multiple were found');
32
+ this.name = 'MultipleResultsFound';
33
+
34
+ Object.setPrototypeOf(this, MultipleResultsFound.prototype);
35
+ }
36
+ }
37
+
38
+ class MalformedJwtToken extends CuencaException {
39
+ constructor() {
40
+ super('An invalid JWT token was obtained during authentication');
41
+ this.name = 'MalformedJwtToken';
42
+
43
+ Object.setPrototypeOf(this, MalformedJwtToken.prototype);
44
+ }
45
+ }
46
+
47
+ class InvalidPassword extends CuencaException {
48
+ constructor() {
49
+ super('Invalid password');
50
+ this.name = 'InvalidPassword';
51
+
52
+ Object.setPrototypeOf(this, InvalidPassword.prototype);
53
+ }
54
+ }
55
+
56
+ class ValidationError extends Error {
57
+ constructor(msg) {
58
+ super(msg);
59
+
60
+ Object.setPrototypeOf(this, ValidationError.prototype);
61
+ }
62
+ }
63
+
64
+ export { CuencaException, CuencaResponseException, InvalidPassword, MalformedJwtToken, MultipleResultsFound, NoResultFound, ValidationError };