Skip to content

Commit

Permalink
refactor: card Validation update
Browse files Browse the repository at this point in the history
  • Loading branch information
aritro2002 committed Nov 13, 2024
1 parent 324b556 commit 23582b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/CardPattern.res
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -38,23 +38,23 @@ 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],
pincodeRequired: false,
},
{
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],
Expand Down
22 changes: 17 additions & 5 deletions src/CardUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -453,18 +460,23 @@ let generateFontsLink = (fonts: array<CardThemeType.fonts>) => {
->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<Nullable.t<Dom.element>>) => {
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 23582b6

Please sign in to comment.