Skip to content

Commit

Permalink
refactor: moved card cvc, card expiry validations and respective util…
Browse files Browse the repository at this point in the history
… functions to hyperswitch-sdk-utils
  • Loading branch information
ChiragKV-Juspay committed Dec 18, 2024
1 parent 01e66aa commit de63808
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 191 deletions.
4 changes: 4 additions & 0 deletions rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
{
"dir": "reactNativeWeb",
"subdirs": true
},
{
"dir": "hyperswitch-sdk-utils",
"subdirs": true
}
],
"bs-dependencies": [
Expand Down
4 changes: 3 additions & 1 deletion src/components/elements/CardElement.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
open Validation
open CardExpiryValidation

type cardFormType = {isZipAvailable: bool}
type viewType = PaymentSheet | CardForm(cardFormType)
@react.component
Expand Down Expand Up @@ -83,7 +85,7 @@ let make = (
}
let onChangeCvv = (text, cvvOrZipRef: React.ref<Nullable.t<ReactNative.TextInput.element>>) => {
let cvvData = formatCVCNumber(text, getCardBrand(cardData.cardNumber))
let isthisValid = checkCardCVC(cvvData, getCardBrand(cardData.cardNumber))
let isthisValid = CardCvcValidation.checkCardCVC(cvvData, getCardBrand(cardData.cardNumber))
if isthisValid {
switch cvvOrZipRef.current->Nullable.toOption {
| None => ()
Expand Down
3 changes: 3 additions & 0 deletions src/components/elements/CardFormUi.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
open ReactNative
open Style
open Validation
open CardCvcValidation
open ValidationUtils

@react.component
let make = (
~cardNumber,
Expand Down
2 changes: 2 additions & 0 deletions src/components/elements/PaymentSheetUi.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
open ReactNative
open Style
open Validation
open CardCvcValidation
open ValidationUtils

@react.component
let make = (
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/AllPaymentHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -866,13 +866,13 @@ let useSavePaymentMethod = () => {
let (nativeProp, _) = React.useContext(NativePropContext.nativePropContext)
let (cardData, _) = React.useContext(CardDataContext.cardDataContext)

let (month, year) = Validation.getExpiryDates(cardData.expireDate)
let (month, year) = CardExpiryValidation.getExpiryDates(cardData.expireDate)
let payment_method_data =
[
(
"card",
[
("card_number", cardData.cardNumber->Validation.clearSpaces->JSON.Encode.string),
("card_number", cardData.cardNumber->ValidationUtils.clearSpaces->JSON.Encode.string),
("card_exp_month", month->JSON.Encode.string),
("card_exp_year", year->JSON.Encode.string),
]
Expand Down
7 changes: 4 additions & 3 deletions src/pages/payment/SaveCardsList.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open ReactNative
open Style
open CardCvcValidation

module CVVComponent = {
@react.component
Expand All @@ -17,7 +18,7 @@ module CVVComponent = {
isCvcFocus || savedCardCvv->Option.isNone
? true
: savedCardCvv->Option.getOr("")->String.length > 0 &&
Validation.cvcNumberInRange(savedCardCvv->Option.getOr(""), cardScheme)
cvcNumberInRange(savedCardCvv->Option.getOr(""), cardScheme)

let localeObject = GetLocale.useGetLocalObj()

Expand Down Expand Up @@ -59,7 +60,7 @@ module CVVComponent = {
secureTextEntry=true
textColor={isCvcValid ? component.color : dangerColor}
iconRight=CustomIcon({
Validation.checkCardCVC(savedCardCvv->Option.getOr(""), cardScheme)
checkCardCVC(savedCardCvv->Option.getOr(""), cardScheme)
? <Icon name="cvvfilled" height=35. width=35. fill="black" />
: <Icon name="cvvempty" height=35. width=35. fill="black" />
})
Expand Down Expand Up @@ -230,7 +231,7 @@ module PaymentMethodListView = {
| "NotCard" => true
| _ =>
switch savedCardCvv {
| Some(cvv) => cvv->String.length > 0 && Validation.cvcNumberInRange(cvv, cardScheme)
| Some(cvv) => cvv->String.length > 0 && cvcNumberInRange(cvv, cardScheme)
| None => !(pmObject->PaymentUtils.checkIsCVCRequired)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/widgets/CardWidget.res
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ let make = () => {
| _ => handleSuccessFailure(~apiResStatus=status, ())
}
}
let (month, year) = Validation.getExpiryDates(expireDate)
let (month, year) = CardExpiryValidation.getExpiryDates(expireDate)
let payment_method_data =
[
(
prop.payment_method,
[
("card_number", cardNumber->Validation.clearSpaces->JSON.Encode.string),
("card_number", cardNumber->ValidationUtils.clearSpaces->JSON.Encode.string),
("card_exp_month", month->JSON.Encode.string),
("card_exp_year", year->JSON.Encode.string),
("card_holder_name", ""->JSON.Encode.string),
Expand Down
4 changes: 2 additions & 2 deletions src/utility/logics/PaymentUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ let generatePaymentMethodData = (
~cardHolderName: option<'a>,
~nickname: option<'a>,
) => {
let (month, year) = Validation.getExpiryDates(cardData.expireDate)
let (month, year) = CardExpiryValidation.getExpiryDates(cardData.expireDate)

[
(
prop.payment_method,
[
("card_number", cardData.cardNumber->Validation.clearSpaces->JSON.Encode.string),
("card_number", cardData.cardNumber->ValidationUtils.clearSpaces->JSON.Encode.string),
("card_exp_month", month->JSON.Encode.string),
("card_exp_year", year->JSON.Encode.string),
(
Expand Down
103 changes: 0 additions & 103 deletions src/utility/reusableCodeFromWeb/CardPattern.res

This file was deleted.

81 changes: 3 additions & 78 deletions src/utility/reusableCodeFromWeb/Validation.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open ValidationUtils

type cardIssuer =
| VISA
| MASTERCARD
Expand All @@ -11,8 +13,6 @@ type cardIssuer =
| JCB
| NOTFOUND

let toInt = val => val->Int.fromString->Option.getOr(0)

let cardType = val => {
switch val->String.toUpperCase {
| "VISA" => VISA
Expand All @@ -29,20 +29,6 @@ let cardType = val => {
}
}

let getobjFromCardPattern = cardBrand => {
let patternsDict = CardPattern.cardPatterns
patternsDict
->Array.filter(item => {
cardBrand === item.issuer
})
->Array.get(0)
->Option.getOr(CardPattern.defaultCardPattern)
}

let clearSpaces = value => {
value->String.replaceRegExp(%re("/\D+/g"), "")
}

let slice = (val, start: int, end: int) => {
val->String.slice(~start, ~end)
}
Expand All @@ -54,20 +40,7 @@ let getStrFromIndex = (arr: array<string>, index) => {
let formatCVCNumber = (val, cardType) => {
let clearValue = val->clearSpaces
let obj = getobjFromCardPattern(cardType)
clearValue->slice(0, obj.maxCVCLenth)
}

let getCurrentMonthAndYear = (dateTimeIsoString: string) => {
let tempTimeDateString = dateTimeIsoString->String.replace("Z", "")
let tempTimeDate = tempTimeDateString->String.split("T")

let date = tempTimeDate->Array.get(0)->Option.getOr("")
let dateComponents = date->String.split("-")

let currentMonth = dateComponents->Array.get(1)->Option.getOr("")
let currentYear = dateComponents->Array.get(0)->Option.getOr("")

(currentMonth->toInt, currentYear->toInt)
clearValue->slice(0, obj.maxCVCLength)
}

let formatCardNumber = (val, cardType) => {
Expand Down Expand Up @@ -95,20 +68,6 @@ let formatCardNumber = (val, cardType) => {

formatedCard->String.trim
}
let splitExpiryDates = val => {
let split = val->String.split("/")
let value = split->Array.map(item => item->String.trim)
let month = value->Array.get(0)->Option.getOr("")
let year = value->Array.get(1)->Option.getOr("")
(month, year)
}
let getExpiryDates = val => {
let date = Date.make()->Date.toISOString
let (month, year) = splitExpiryDates(val)
let (_, currentYear) = getCurrentMonthAndYear(date)
let prefix = currentYear->Int.toString->String.slice(~start=0, ~end=2)
(month, `${prefix}${year}`)
}

let formatCardExpiryNumber = val => {
let clearValue = val->clearSpaces
Expand Down Expand Up @@ -250,22 +209,6 @@ let calculateLuhn = value => {
// }
// }

let getExpiryValidity = cardExpiry => {
let date = Date.make()->Date.toISOString
let (month, year) = getExpiryDates(cardExpiry)
let (currentMonth, currentYear) = getCurrentMonthAndYear(date)
let valid = if currentYear == year->toInt && month->toInt >= currentMonth && month->toInt <= 12 {
true
} else if (
year->toInt > currentYear && year->toInt < 2075 && month->toInt >= 1 && month->toInt <= 12
) {
true
} else {
false
}
valid
}

// let max = (a, b) => {
// a > b ? a : b
// }
Expand All @@ -284,17 +227,6 @@ let getExpiryValidity = cardExpiry => {
// }
// }

let cvcNumberInRange = (val, cardBrand) => {
let clearValue = val->clearSpaces
let obj = getobjFromCardPattern(cardBrand)
let cvcLengthInRange =
obj.cvcLength
->Array.find(item => {
clearValue->String.length == item
})
->Option.isSome
cvcLengthInRange
}
// let genreateFontsLink = (fonts: array<CardThemeType.fonts>) => {
// if fonts->Array.length > 0 {
// fonts
Expand Down Expand Up @@ -391,13 +323,6 @@ let cardValid = (cardNumber, cardBrand) => {
// thirdIframeVal === "" ? secondIframeVal === "" ? firstIframeVal : secondIframeVal : thirdIframeVal
// }

let checkCardCVC = (cvcNumber, cardBrand) => {
cvcNumber->String.length > 0 && cvcNumberInRange(cvcNumber, cardBrand)
}
let checkCardExpiry = expiry => {
expiry->String.length > 0 && getExpiryValidity(expiry)
}

// let commonKeyDownEvent = (ev, srcRef, destRef, srcEle, destEle, setEle) => {
// let key = ReactEvent.Keyboard.keyCode(ev)
// if key == 8 && srcEle == "" {
Expand Down

0 comments on commit de63808

Please sign in to comment.