@@ -22,6 +22,24 @@ function luhnCheck(cardNumber) {
2222 }
2323 return sum % 10 === 0 ;
2424}
25+ function getInputValidationDetails ( cardBinResponse ) {
26+ if ( ! cardBinResponse || ! cardBinResponse . scheme ) {
27+ return null ;
28+ }
29+ let schemeType = cardBinResponse . scheme . toLowerCase ( ) ;
30+ let inputValidationDetails ;
31+ switch ( schemeType ) {
32+ case 'amex' :
33+ inputValidationDetails = { max_input_length : 15 , cvv_length : 4 } ;
34+ break ;
35+ case 'diners' :
36+ inputValidationDetails = { max_input_length : 14 , cvv_length : 3 } ;
37+ break ;
38+ default : // Covers visa, mastercard, rupay, jcb, discover, and unknown schemes
39+ inputValidationDetails = { max_input_length : 16 , cvv_length : 3 } ;
40+ }
41+ return inputValidationDetails ;
42+ }
2543/**
2644 * Fetching Tdr info with card bin data & cfSession object
2745 * @param session : for payment sessionId & env
@@ -73,9 +91,9 @@ const CardInput = forwardRef(({ cfSession, cardListener, style, ...props }, ref)
7391 doPayment,
7492 doPaymentWithPaymentSessionId,
7593 } ) ) ;
76- let tdrJson = null ;
77- let cardBinJson = null ;
78- let firstEightDigits = '' ;
94+ const tdrJsonRef = React . useRef ( null ) ;
95+ const cardBinJsonRef = React . useRef ( null ) ;
96+ const firstEightDigitsRef = React . useRef ( '' ) ;
7997 const handleChange = React . useCallback ( async ( cardNumber ) => {
8098 let completeResponse = { } ;
8199 const textWithoutSpaces = cardNumber . replaceAll ( ' ' , '' ) ;
@@ -95,7 +113,7 @@ const CardInput = forwardRef(({ cfSession, cardListener, style, ...props }, ref)
95113 formattedText += ' ' ;
96114 }
97115 inputNumberRef . current = formattedText ;
98- setInputNumber ( formattedText ) ;
116+ setInputNumber ( ( prev ) => prev === formattedText ? prev : formattedText ) ;
99117 }
100118 let tdrResponse = null ;
101119 let cardBinResponse = null ;
@@ -106,63 +124,65 @@ const CardInput = forwardRef(({ cfSession, cardListener, style, ...props }, ref)
106124 await getTDR ( cfSession , textWithoutSpaces )
107125 . then ( ( response ) => {
108126 tdrResponse = response ;
127+ firstEightDigitsRef . current = textWithoutSpaces . substring ( 0 , 8 ) ;
109128 } )
110129 . catch ( ( ) => {
111130 tdrResponse = null ;
112131 } ) ;
113132 await getCardBin ( cfSession , textWithoutSpaces )
114133 . then ( ( response ) => {
115134 cardBinResponse = response ;
135+ firstEightDigitsRef . current = textWithoutSpaces . substring ( 0 , 8 ) ;
116136 } )
117137 . catch ( ( ) => {
118138 cardBinResponse = null ;
119139 } ) ;
120140 if ( tdrResponse ) {
121- tdrJson = tdrResponse ;
122- completeResponse [ ' tdr_info' ] = tdrJson ;
141+ tdrJsonRef . current = tdrResponse ;
142+ completeResponse . tdr_info = tdrJsonRef . current ;
123143 }
124144 if ( cardBinResponse ) {
125- cardBinJson = cardBinResponse ;
126- completeResponse [ 'card_bin_info' ] = cardBinJson ;
145+ cardBinJsonRef . current = cardBinResponse ;
146+ completeResponse . card_bin_info = cardBinJsonRef . current ;
147+ completeResponse . input_validation = getInputValidationDetails ( cardBinJsonRef . current ) ;
127148 }
128149 }
129150 if ( textWithoutSpaces . length === 8 ) {
130- firstEightDigits = textWithoutSpaces ;
131151 await fetchDataAndSet ( ) ;
132152 }
133153 else if ( textWithoutSpaces . length > 8 ) {
134- if ( firstEightDigits === textWithoutSpaces . substring ( 0 , 8 ) ) {
135- completeResponse [ 'tdr_info' ] = tdrJson ;
136- completeResponse [ 'card_bin_info' ] = cardBinJson ;
154+ if ( firstEightDigitsRef . current === textWithoutSpaces . substring ( 0 , 8 ) ) {
155+ completeResponse . tdr_info = tdrJsonRef . current ;
156+ completeResponse . card_bin_info = cardBinJsonRef . current ;
157+ completeResponse . input_validation = getInputValidationDetails ( cardBinJsonRef . current ) ;
137158 }
138159 else {
139- firstEightDigits = textWithoutSpaces . substring ( 0 , 8 ) ;
140- tdrJson = null ;
141- cardBinJson = null ;
160+ tdrJsonRef . current = null ;
161+ cardBinJsonRef . current = null ;
142162 await fetchDataAndSet ( ) ;
143163 }
144164 }
145165 if ( textWithoutSpaces . length < 8 ) {
146- cardBinJson = null ;
147- tdrJson = null ;
148- firstEightDigits = '' ;
166+ tdrJsonRef . current = null ;
167+ cardBinJsonRef . current = null ;
168+ firstEightDigitsRef . current = '' ;
149169 }
150- if ( cardBinJson !== null ) {
151- completeResponse [ ' card_network' ] = cardBinJson [ ' scheme' ] ;
170+ if ( cardBinJsonRef . current !== null ) {
171+ completeResponse . card_network = cardBinJsonRef . current . scheme ;
152172 }
153173 let luhnStatus = luhnCheck ( textWithoutSpaces ) ;
154174 if ( luhnStatus ) {
155- completeResponse [ ' luhn_check_info' ] = 'SUCCESS' ;
175+ completeResponse . luhn_check_info = 'SUCCESS' ;
156176 if ( textWithoutSpaces && textWithoutSpaces . length > 4 ) {
157- completeResponse [ ' last_four_digit' ] = textWithoutSpaces . substring ( textWithoutSpaces . length - 4 ) ;
177+ completeResponse . last_four_digit = textWithoutSpaces . substring ( textWithoutSpaces . length - 4 ) ;
158178 }
159179 }
160180 else {
161- completeResponse [ ' luhn_check_info' ] = 'FAIL' ;
181+ completeResponse . luhn_check_info = 'FAIL' ;
162182 }
163- completeResponse [ ' card_length' ] = textWithoutSpaces . length ;
183+ completeResponse . card_length = textWithoutSpaces . length ;
164184 return cardListener ( JSON . stringify ( completeResponse ) ) ;
165- } , [ cardListener ] ) ;
185+ } , [ cfSession , cardListener ] ) ;
166186 const doPayment = ( cardInfo ) => {
167187 try {
168188 let cfCardNumber = inputNumberRef . current ;
0 commit comments