@@ -534,24 +534,26 @@ const updateCartByPaymentId = async (cartId, paymentId, cartVersion, visaCheckou
534534} ;
535535
536536const setCustomerTokens = async ( tokenCustomerId , paymentInstrumentId , instrumentIdentifier , updatePaymentObj , addressId ) => {
537- let tokenResponse : any ;
537+ let tokenResponse = null ;
538538 let customerInfo : any ;
539539 let client : any ;
540540 let tokenData : any ;
541541 let isvTokens : any ;
542- let mappedTokens : any ;
542+ let mappedTokens : string [ ] = [ ] ;
543543 let exceptionData : any ;
544- let failedTokens : any ;
544+ let failedTokens : string [ ] = [ ] ;
545545 let stringTokenData : string ;
546546 let length : number ;
547547 let tokenArray : Array < string > ;
548548 let customerId = null ;
549+ let customTypePresent = false ;
549550 try {
550551 if ( null != paymentInstrumentId && null != instrumentIdentifier && null != updatePaymentObj && Constants . STRING_CUSTOMER in updatePaymentObj && Constants . STRING_ID in updatePaymentObj . customer ) {
551552 customerId = updatePaymentObj . customer . id ;
552553 client = getClient ( ) ;
553554 if ( null != client && null != customerId ) {
554555 customerInfo = await getCustomer ( customerId ) ;
556+ customTypePresent = ( customerInfo ?. custom ?. type ?. id ) ? true : false ;
555557 tokenData = {
556558 alias : updatePaymentObj . custom . fields . isv_tokenAlias ,
557559 value : tokenCustomerId ,
@@ -566,24 +568,27 @@ const setCustomerTokens = async (tokenCustomerId, paymentInstrumentId, instrumen
566568 timeStamp : new Date ( Date . now ( ) ) . toISOString ( ) ,
567569 } ;
568570 stringTokenData = JSON . stringify ( tokenData ) ;
569- if (
570- null != customerInfo &&
571- Constants . STRING_CUSTOM in customerInfo &&
572- Constants . STRING_FIELDS in customerInfo . custom &&
573- Constants . ISV_TOKENS in customerInfo . custom . fields &&
574- Constants . STRING_EMPTY != customerInfo . custom . fields . isv_tokens &&
575- Constants . VAL_ZERO < customerInfo . custom . fields . isv_tokens . length
576- ) {
577- failedTokens = customerInfo . custom . fields . isv_failedTokens ;
578- isvTokens = customerInfo . custom . fields . isv_tokens ;
579- mappedTokens = isvTokens . map ( ( item ) => item ) ;
580- length = mappedTokens . length ;
581- mappedTokens [ length ] = stringTokenData ;
582- tokenResponse = await setCustomType ( customerId , mappedTokens , failedTokens ) ;
583- } else {
571+ if ( customTypePresent ) {
572+ if (
573+ null != customerInfo &&
574+ Constants . STRING_CUSTOM in customerInfo &&
575+ Constants . STRING_FIELDS in customerInfo . custom &&
576+ Constants . ISV_TOKENS in customerInfo . custom . fields &&
577+ Constants . STRING_EMPTY != customerInfo . custom . fields . isv_tokens &&
578+ Constants . VAL_ZERO < customerInfo . custom . fields . isv_tokens . length
579+ ) {
580+ isvTokens = customerInfo . custom . fields . isv_tokens ;
581+ mappedTokens = isvTokens . map ( ( item ) => item ) ;
582+ length = mappedTokens . length ;
583+ mappedTokens [ length ] = stringTokenData ;
584+ } else {
585+ mappedTokens [ 0 ] = stringTokenData ;
586+ }
584587 if ( null != customerInfo && Constants . STRING_CUSTOM in customerInfo && Constants . STRING_FIELDS in customerInfo . custom ) {
585588 failedTokens = customerInfo . custom . fields . isv_failedTokens ;
586589 }
590+ tokenResponse = await updateCustomerToken ( mappedTokens , customerInfo , failedTokens ) ;
591+ } else {
587592 tokenArray = [ stringTokenData ] ;
588593 tokenResponse = await setCustomType ( customerId , tokenArray , failedTokens ) ;
589594 }
@@ -1274,6 +1279,68 @@ const addCustomerAddress = async (customerId, addressObj) => {
12741279 return customerResponse ;
12751280} ;
12761281
1282+ const updateCustomerToken = async ( updateObject , customerObject , failedTokens ) => {
1283+ let setCustomFieldResponse ;
1284+ let actions = [ ] as any ;
1285+ let exceptionData ;
1286+ let customerId = '' ;
1287+ try {
1288+ if ( customerObject ) {
1289+ customerId = customerObject . id
1290+ const client = getClient ( ) ;
1291+ if ( null !== client ) {
1292+ const requestBuilder = createRequestBuilder ( {
1293+ projectKey : process . env . CT_PROJECT_KEY ,
1294+ } ) ;
1295+ const uri = requestBuilder . customers . byId ( customerObject . id ) . build ( ) ;
1296+ if ( failedTokens ) {
1297+ actions . push ( {
1298+ action : "setCustomField" ,
1299+ name : "isv_failedTokens" ,
1300+ value : failedTokens
1301+ } ) ;
1302+ }
1303+ if ( updateObject ) {
1304+ actions . push ( {
1305+ action : "setCustomField" ,
1306+ name : "isv_tokens" ,
1307+ value : updateObject
1308+ } )
1309+ }
1310+ if ( customerObject ?. custom ?. fields ?. isv_tokenAction ) {
1311+ actions . push ( {
1312+ action : "setCustomField" ,
1313+ name : "isv_tokenAction" ,
1314+ value : null
1315+ } )
1316+ }
1317+ const channelsRequest = {
1318+ uri : uri ,
1319+ method : 'POST' ,
1320+ body : JSON . stringify ( {
1321+ version : customerObject . version ,
1322+ actions : actions
1323+ } )
1324+ } ;
1325+ setCustomFieldResponse = await client . execute ( channelsRequest ) ;
1326+ } else {
1327+ paymentService . logData ( path . parse ( path . basename ( __filename ) ) . name , 'FuncUpdateCustomerToken' , Constants . LOG_INFO , '' , Constants . ERROR_MSG_COMMERCETOOLS_CONNECT ) ;
1328+ }
1329+ }
1330+ } catch ( exception ) {
1331+ if ( typeof exception === 'string' ) {
1332+ exceptionData = Constants . EXCEPTION_MSG_CUSTOMER_UPDATE + Constants . STRING_HYPHEN + exception . toUpperCase ( ) ;
1333+ } else if ( exception instanceof Error ) {
1334+ exceptionData = Constants . EXCEPTION_MSG_CUSTOMER_UPDATE + Constants . STRING_HYPHEN + exception . message ;
1335+ } else {
1336+ exceptionData = Constants . EXCEPTION_MSG_CUSTOMER_UPDATE + Constants . STRING_HYPHEN + exception ;
1337+ }
1338+ paymentService . logData ( path . parse ( path . basename ( __filename ) ) . name , 'FuncUpdateCustomerTokens' , Constants . LOG_ERROR , 'CustomerId : ' + customerId , exceptionData ) ;
1339+ setCustomFieldResponse = exception ;
1340+ }
1341+ return setCustomFieldResponse ;
1342+ }
1343+
12771344
12781345
12791346export default {
@@ -1299,5 +1366,6 @@ export default {
12991366 addCustomField,
13001367 updateAvailableAmount,
13011368 getCartById,
1302- addCustomerAddress
1369+ addCustomerAddress,
1370+ updateCustomerToken
13031371} ;
0 commit comments