From 23582b6c70f687ddffb6098dddf2d68790856a7a Mon Sep 17 00:00:00 2001 From: "aritro.ghosh" Date: Wed, 13 Nov 2024 19:28:02 +0530 Subject: [PATCH] refactor: card Validation update --- src/CardPattern.res | 12 ++++++------ src/CardUtils.res | 22 +++++++++++++++++----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/CardPattern.res b/src/CardPattern.res index 08c3cb63e..bb1d2eb9d 100644 --- a/src/CardPattern.res +++ b/src/CardPattern.res @@ -19,7 +19,7 @@ let cardPatterns = [ { issuer: "Maestro", pattern: %re( - "/^(5018|5081|5044|504681|504993|5020|502260|5038|603845|603123|6304|6759|676[1-3]|6220|504834|504817|504645|504775|600206|627741)/" + "/^(5018|5081|5044|504681|504993|5020|502260|5038|5893|603845|603123|6304|6759|676[1-3]|6220|504834|504817|504645|504775|600206|627741)/" ), cvcLength: [3, 4], length: [12, 13, 14, 15, 16, 17, 18, 19], @@ -29,7 +29,7 @@ let cardPatterns = [ { issuer: "RuPay", pattern: %re( - "/^(508227|508[5-9]|603741|60698[5-9]|60699|607[0-8]|6079[0-7]|60798[0-4]|60800[1-9]|6080[1-9]|608[1-4]|608500|6521[5-9]|652[2-9]|6530|6531[0-4]|817290|817368|817378|353800)/" + "/^(508227|508[5-9]|603741|60698[5-9]|60699|607[0-8]|6079[0-7]|60798[0-4]|60800[1-9]|6080[1-9]|608[1-4]|608500|6521[5-9]|652[2-9]|6530|6531[0-4]|817290|817368|817378|353800|82)/" ), cvcLength: [3], length: [16], @@ -38,7 +38,7 @@ let cardPatterns = [ }, { issuer: "DinersClub", - pattern: %re("/^(36|38|30[0-5])/"), + pattern: %re("/^(36|38|39|30[0-5])/"), cvcLength: [3], maxCVCLenth: 3, length: [14, 15, 16, 17, 18, 19], @@ -46,15 +46,15 @@ let cardPatterns = [ }, { issuer: "Discover", - pattern: %re("/^(6011|65|64[4-9]|622)/"), + pattern: %re("/^(6011|64[4-9]|65|622126|622[1-9][0-9][0-9]|6229[0-1][0-9]|622925)/"), cvcLength: [3], - length: [16], + length: [16, 17, 18, 19], maxCVCLenth: 3, pincodeRequired: true, }, { issuer: "Mastercard", - pattern: %re("/^5[1-5]/"), + pattern: %re("/^(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[0-1][0-9]|2720|5[1-5])/"), cvcLength: [3], maxCVCLenth: 3, length: [16], diff --git a/src/CardUtils.res b/src/CardUtils.res index 4ed5fe889..b7372d752 100644 --- a/src/CardUtils.res +++ b/src/CardUtils.res @@ -177,7 +177,14 @@ let formatCardNumber = (val, cardType) => { let formatedCard = switch cardType { | AMEX => `${clearValue->slice(0, 4)} ${clearValue->slice(4, 10)} ${clearValue->slice(10, 15)}` | DINERSCLUB => - `${clearValue->slice(0, 4)} ${clearValue->slice(4, 10)} ${clearValue->slice(10, 14)}` + if clearValue->String.length > 14 { + `${clearValue->slice(0, 4)} ${clearValue->slice(4, 8)} ${clearValue->slice( + 8, + 12, + )} ${clearValue->slice(12, 16)} ` + } else { + `${clearValue->slice(0, 4)} ${clearValue->slice(4, 10)} ${clearValue->slice(10, 14)}` + } | MASTERCARD | DISCOVER | SODEXO @@ -453,18 +460,23 @@ let generateFontsLink = (fonts: array) => { ->ignore } } + let maxCardLength = cardBrand => { let obj = getobjFromCardPattern(cardBrand) Array.reduce(obj.length, 0, (acc, val) => max(acc, val)) } +let isCardLengthValid = (cardBrand, cardNumberLength) => { + let obj = getobjFromCardPattern(cardBrand) + Array.includes(obj.length, cardNumberLength) +} + let cardValid = (cardNumber, cardBrand) => { let clearValueLength = cardNumber->clearSpaces->String.length if cardBrand == "" && (GlobalVars.isInteg || GlobalVars.isSandbox) { Utils.checkIsTestCardWildcard(cardNumber) } else { - (clearValueLength == maxCardLength(cardBrand) || - (cardBrand === "Visa" && clearValueLength == 16)) && calculateLuhn(cardNumber) + isCardLengthValid(cardBrand, clearValueLength) && calculateLuhn(cardNumber) } } let blurRef = (ref: React.ref>) => { @@ -565,10 +577,10 @@ let setCardValid = (cardnumber, setIsCardValid) => { if cardValid(cardnumber, cardBrand) { setIsCardValid(_ => Some(true)) } else if ( - !cardValid(cardnumber, cardBrand) && cardnumber->String.length == maxCardLength(cardBrand) + !cardValid(cardnumber, cardBrand) && isCardLengthValid(cardBrand, cardnumber->String.length) ) { setIsCardValid(_ => Some(false)) - } else if !(cardnumber->String.length == maxCardLength(cardBrand)) { + } else if !isCardLengthValid(cardBrand, cardnumber->String.length) { setIsCardValid(_ => None) } }