@@ -3,13 +3,19 @@ import common from '../common';
3
3
import { handleTypedError } from '../common/message' ;
4
4
import PayTheoryHostedField from '../components/pay-theory-hosted-field' ;
5
5
import {
6
+ BillingInfo ,
6
7
ErrorResponse ,
7
8
ErrorType ,
9
+ Level3DataSummary ,
8
10
PayorInfo ,
9
11
TaxIndicatorType ,
10
12
HealthExpenseType ,
11
13
} from '../common/pay_theory_types' ;
12
- import { ModifiedCheckoutDetails , ModifiedTransactProps } from '../common/format' ;
14
+ import {
15
+ ModifiedCheckoutDetails ,
16
+ ModifiedTransactProps ,
17
+ PayTheoryDataObject ,
18
+ } from '../common/format' ;
13
19
import { ElementTypes } from '../common/data' ;
14
20
15
21
const findField =
@@ -26,8 +32,8 @@ const findAccountType = findField('account-type');
26
32
const findAccountName = findField ( 'account-name' ) ;
27
33
28
34
// partner mode is used to indicate migration builds
29
- const checkApiKey = ( key : any ) => {
30
- if ( ! validate ( key , 'string' ) ) {
35
+ const checkApiKey = ( key : unknown ) => {
36
+ if ( ! validate < string > ( key , 'string' ) ) {
31
37
throw Error ( `Valid API Key not found. Please provide a valid API Key` ) ;
32
38
}
33
39
const keyParts = key . split ( '-' ) ;
@@ -42,41 +48,50 @@ const checkApiKey = (key: any) => {
42
48
}
43
49
} ;
44
50
45
- const validate = ( value : any , type : string ) => {
46
- return typeof value === type && value ;
51
+ const validate = < T > ( value : unknown , type : string ) : value is T => {
52
+ return typeof value === type && Boolean ( value ) ;
47
53
} ;
48
54
49
- const checkFeeMode = ( mode : string ) => {
50
- if ( ! validate ( mode , 'string' ) || ! [ common . MERCHANT_FEE , common . SERVICE_FEE ] . includes ( mode ) ) {
55
+ const checkFeeMode = ( mode : unknown ) => {
56
+ if (
57
+ ! validate < string > ( mode , 'string' ) ||
58
+ ! [ common . MERCHANT_FEE , common . SERVICE_FEE ] . includes ( mode )
59
+ ) {
51
60
console . error (
52
61
`Fee Mode should be either 'merchant_fee' or 'service_fee' which are also available as constants at window.paytheory.MERCHANT_FEE and window.paytheory.SERVICE_FEE` ,
53
62
) ;
54
63
}
55
64
} ;
56
65
57
- const checkMetadata = ( metadata : any ) => {
66
+ const checkMetadata = ( metadata : unknown ) => {
58
67
if ( ! validate ( metadata , 'object' ) ) {
59
68
throw Error ( `Metadata should be a JSON Object` ) ;
60
69
}
61
70
} ;
62
71
63
- const checkStyles = ( styles : any ) => {
72
+ const checkStyles = ( styles : unknown ) => {
64
73
if ( ! validate ( styles , 'object' ) ) {
65
74
throw Error (
66
75
`Styles should be a JSON Object. An example of the object is at https://github.com/pay-theory/payment-components` ,
67
76
) ;
68
77
}
69
78
} ;
70
79
71
- const checkAmount = ( amount : any ) => {
80
+ const checkAmount = ( amount : unknown ) => {
72
81
const error = isValidAmount ( amount ) ;
73
82
74
83
if ( error ) {
75
84
throw Error ( error . error ) ;
76
85
}
77
86
} ;
78
87
79
- const checkInitialParams = ( key : any , mode : any , metadata : any , styles : any , amount : any ) => {
88
+ const checkInitialParams = (
89
+ key : unknown ,
90
+ mode : unknown ,
91
+ metadata : unknown ,
92
+ styles : unknown ,
93
+ amount : unknown ,
94
+ ) => {
80
95
checkApiKey ( key ) ;
81
96
if ( mode ) checkFeeMode ( mode ) ;
82
97
checkMetadata ( metadata ) ;
@@ -269,25 +284,25 @@ const nullifyEmptyStrings = (params: object) => {
269
284
const formatPayorObject = ( payorInfo : payorInfo ) : PayorInfo => {
270
285
// Make a deep copy of the payorInfo object
271
286
let payorCopy = JSON . parse ( JSON . stringify ( payorInfo ) ) ;
272
- // Nullify any empty strings
287
+ // Nullify unknown empty strings
273
288
payorCopy = nullifyEmptyStrings ( payorCopy ) ;
274
- // Strip out any non-numeric characters from the phone number
289
+ // Strip out unknown non-numeric characters from the phone number
275
290
if ( payorCopy . phone ) {
276
291
payorCopy . phone = payorCopy . phone . replace ( / \D / g, '' ) ;
277
292
}
278
293
return payorCopy ;
279
294
} ;
280
295
281
296
const isvalidTransactParams = (
282
- amount : any ,
283
- payorInfo : any ,
284
- metadata : any ,
297
+ amount : unknown ,
298
+ payorInfo : unknown ,
299
+ metadata : unknown ,
285
300
) : ErrorResponse | null => {
286
301
//Make sure that we have the base required settings
287
302
if (
288
- ! validate ( amount , 'number' ) ||
289
- ! validate ( metadata , 'object' ) ||
290
- ! validate ( payorInfo , 'object' )
303
+ ! validate < number > ( amount , 'number' ) ||
304
+ ! validate < object > ( metadata , 'object' ) ||
305
+ ! validate < object > ( payorInfo , 'object' )
291
306
) {
292
307
const missing = `${ ! validate ( amount , 'number' ) ? 'amount ' : '' } ${ ! validate ( metadata , 'object' ) ? 'metadata ' : '' } ${ ! validate ( payorInfo , 'object' ) ? 'payorInfo ' : '' } ` ;
293
308
return handleTypedError (
@@ -301,7 +316,7 @@ const isvalidTransactParams = (
301
316
return null ;
302
317
} ;
303
318
304
- const isValidTokenizeParams = ( payorInfo : any , metadata : any ) : ErrorResponse | null => {
319
+ const isValidTokenizeParams = ( payorInfo : unknown , metadata : unknown ) : ErrorResponse | null => {
305
320
//Make sure that we have the base required settings
306
321
if ( ! validate ( metadata , 'object' ) || ! validate ( payorInfo , 'object' ) ) {
307
322
const missing = `${ ! validate ( metadata , 'object' ) ? 'metadata ' : '' } ${ ! validate ( payorInfo , 'object' ) ? 'payorInfo ' : '' } ` ;
@@ -313,8 +328,8 @@ const isValidTokenizeParams = (payorInfo: any, metadata: any): ErrorResponse | n
313
328
return null ;
314
329
} ;
315
330
316
- const isValidAmount = ( amount : any ) : ErrorResponse | null => {
317
- if ( ! validate ( amount , 'number' ) ) {
331
+ const isValidAmount = ( amount : unknown ) : ErrorResponse | null => {
332
+ if ( ! validate < number > ( amount , 'number' ) ) {
318
333
return handleTypedError ( ErrorType . INVALID_PARAM , 'amount must be a positive integer' ) ;
319
334
}
320
335
if ( amount % 1 !== 0 ) {
@@ -323,14 +338,14 @@ const isValidAmount = (amount: any): ErrorResponse | null => {
323
338
return null ;
324
339
} ;
325
340
326
- const isValidDeviceId = ( deviceId : any ) : ErrorResponse | null => {
327
- if ( ! validate ( deviceId , 'string' ) ) {
328
- return handleTypedError ( ErrorType . INVALID_PARAM , 'deviceId is required and must be a string' ) ;
329
- }
330
- return null ;
331
- } ;
341
+ // const isValidDeviceId = (deviceId: unknown ): ErrorResponse | null => {
342
+ // if (!validate(deviceId, 'string')) {
343
+ // return handleTypedError(ErrorType.INVALID_PARAM, 'deviceId is required and must be a string');
344
+ // }
345
+ // return null;
346
+ // };
332
347
333
- const isValidPayorDetails = ( payorInfo : any , payorId : any ) : ErrorResponse | null => {
348
+ const isValidPayorDetails = ( payorInfo : unknown , payorId : unknown ) : ErrorResponse | null => {
334
349
const keys = Object . keys ( payorInfo ) ;
335
350
// Verify both id and info aren't passed in
336
351
if ( payorId && keys . length > 0 ) {
@@ -345,15 +360,15 @@ const isValidPayorDetails = (payorInfo: any, payorId: any): ErrorResponse | null
345
360
return null ;
346
361
} ;
347
362
348
- const isValidInvoiceAndRecurringId = ( payTheoryInfo : any ) : ErrorResponse | null => {
349
- const { invoiceId , recurringId } = payTheoryInfo ;
350
- if ( invoiceId && ! validate ( invoiceId , 'string' ) ) {
363
+ const isValidInvoiceAndRecurringId = ( payTheoryInfo : PayTheoryDataObject ) : ErrorResponse | null => {
364
+ const { invoice_id , recurring_id } = payTheoryInfo ;
365
+ if ( invoice_id && ! validate ( invoice_id , 'string' ) ) {
351
366
return handleTypedError ( ErrorType . INVALID_PARAM , 'invoiceId must be a string' ) ;
352
367
}
353
- if ( recurringId && ! validate ( recurringId , 'string' ) ) {
368
+ if ( recurring_id && ! validate ( recurring_id , 'string' ) ) {
354
369
return handleTypedError ( ErrorType . INVALID_PARAM , 'recurringId must be a string' ) ;
355
370
}
356
- if ( invoiceId && recurringId ) {
371
+ if ( invoice_id && recurring_id ) {
357
372
return handleTypedError (
358
373
ErrorType . INVALID_PARAM ,
359
374
'invoiceId and recurringId cannot both be present' ,
@@ -362,7 +377,7 @@ const isValidInvoiceAndRecurringId = (payTheoryInfo: any): ErrorResponse | null
362
377
return null ;
363
378
} ;
364
379
365
- const isValidFeeMode = ( feeMode : any ) : ErrorResponse | null => {
380
+ const isValidFeeMode = ( feeMode : string ) : ErrorResponse | null => {
366
381
if ( ! [ common . MERCHANT_FEE , common . SERVICE_FEE ] . includes ( feeMode ) ) {
367
382
return handleTypedError (
368
383
ErrorType . INVALID_PARAM ,
@@ -372,34 +387,36 @@ const isValidFeeMode = (feeMode: any): ErrorResponse | null => {
372
387
return null ;
373
388
} ;
374
389
375
- const isValidFeeAmount = ( fee : any ) : ErrorResponse | null => {
390
+ const isValidFeeAmount = ( fee : unknown ) : ErrorResponse | null => {
376
391
if ( ( fee || typeof fee === 'number' ) && ! validate ( fee , 'number' ) ) {
377
392
return handleTypedError ( ErrorType . INVALID_PARAM , 'fee must be a positive integer' ) ;
378
393
}
379
394
return null ;
380
395
} ;
381
396
382
- const isValidBillingInfo = ( billingInfo : any ) : ErrorResponse | null => {
383
- if ( billingInfo && ! validate ( billingInfo , 'object' ) ) {
384
- return handleTypedError ( ErrorType . INVALID_PARAM , 'billingInfo must be an object' ) ;
385
- }
386
- if ( billingInfo && billingInfo . address && ! validate ( billingInfo . address , 'object' ) ) {
387
- return handleTypedError ( ErrorType . INVALID_PARAM , 'billingInfo.address must be an object' ) ;
388
- }
389
- if ( billingInfo && billingInfo . address && ! validate ( billingInfo . address . postal_code , 'string' ) ) {
390
- return handleTypedError (
391
- ErrorType . INVALID_PARAM ,
392
- 'billingInfo.address.postal_code is required when passing in billingInfo' ,
393
- ) ;
397
+ const isValidBillingInfo = ( billingInfo : unknown ) : ErrorResponse | null => {
398
+ if ( billingInfo ) {
399
+ if ( ! validate < BillingInfo > ( billingInfo , 'object' ) ) {
400
+ return handleTypedError ( ErrorType . INVALID_PARAM , 'billingInfo must be an object' ) ;
401
+ }
402
+ if ( billingInfo . address && ! validate ( billingInfo . address , 'object' ) ) {
403
+ return handleTypedError ( ErrorType . INVALID_PARAM , 'billingInfo.address must be an object' ) ;
404
+ }
405
+ if ( billingInfo . address && ! validate ( billingInfo . address . postal_code , 'string' ) ) {
406
+ return handleTypedError (
407
+ ErrorType . INVALID_PARAM ,
408
+ 'billingInfo.address.postal_code is required when passing in billingInfo' ,
409
+ ) ;
410
+ }
394
411
}
395
412
return null ;
396
413
} ;
397
414
398
- const isValidL3DataSummary = ( l3DataSummary : any ) : ErrorResponse | null => {
415
+ const isValidL3DataSummary = ( l3DataSummary : unknown ) : ErrorResponse | null => {
399
416
// If null or undefined then return null
400
417
if ( l3DataSummary === null || l3DataSummary === undefined ) return null ;
401
418
402
- if ( ! validate ( l3DataSummary , 'object' ) ) {
419
+ if ( ! validate < Level3DataSummary > ( l3DataSummary , 'object' ) ) {
403
420
return handleTypedError ( ErrorType . INVALID_PARAM , 'l3DataSummary must be an object' ) ;
404
421
}
405
422
@@ -459,11 +476,11 @@ const isValidL3DataSummary = (l3DataSummary: any): ErrorResponse | null => {
459
476
return null ;
460
477
} ;
461
478
462
- const isValidHealthExpenseType = ( healthExpenseType : any ) : ErrorResponse | null => {
479
+ const isValidHealthExpenseType = ( healthExpenseType : unknown ) : ErrorResponse | null => {
463
480
if ( healthExpenseType === null || healthExpenseType === undefined ) return null ;
464
- if ( ! validate ( healthExpenseType , 'string' ) ) {
481
+ if ( ! validate < string > ( healthExpenseType , 'string' ) ) {
465
482
return handleTypedError ( ErrorType . INVALID_PARAM , 'healthExpenseType must be a string' ) ;
466
- }
483
+ } // @ts -expect-error
467
484
if ( ! Object . values ( HealthExpenseType ) . includes ( healthExpenseType ) ) {
468
485
return handleTypedError (
469
486
ErrorType . INVALID_PARAM ,
@@ -475,9 +492,9 @@ const isValidHealthExpenseType = (healthExpenseType: any): ErrorResponse | null
475
492
} ;
476
493
477
494
const validateHostedCheckoutParams = (
478
- callToAction : any ,
479
- acceptedPaymentMethods : any ,
480
- paymentName : any ,
495
+ callToAction : string ,
496
+ acceptedPaymentMethods : string ,
497
+ paymentName : unknown ,
481
498
) : ErrorResponse | null => {
482
499
if ( callToAction && ! common . CTA_TYPES . includes ( callToAction ) ) {
483
500
return handleTypedError (
@@ -533,7 +550,7 @@ const validTransactionParams = (
533
550
return isValidFeeAmount ( payTheoryData ?. fee ) ;
534
551
} ;
535
552
536
- const validQRSize = ( size : any ) : ErrorResponse | null => {
553
+ const validQRSize = ( size : unknown ) : ErrorResponse | null => {
537
554
if ( ! validate ( size , 'number' ) ) {
538
555
return handleTypedError ( ErrorType . INVALID_PARAM , 'size must be a number' ) ;
539
556
}
@@ -550,7 +567,6 @@ export {
550
567
formatPayorObject ,
551
568
validate ,
552
569
isValidAmount ,
553
- isValidDeviceId ,
554
570
isvalidTransactParams ,
555
571
isValidTokenizeParams ,
556
572
isValidPayorInfo ,
0 commit comments