Skip to content

Commit

Permalink
fix: validation rules for card holder's name and nickname (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam25092001 authored and 25harsh committed Nov 20, 2024
1 parent 3230e5e commit 5e9a5f4
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion android
Submodule android updated 38 files
+865 −866 app/src/main/assets/hyperswitch.bundle
+5 −53 app/src/main/java/io/hyperswitch/view/GooglePayButtonView.kt
+0 −16 app/src/main/res/values/theme.xml
+3 −38 demo-app/build.gradle
+6 −5 demo-app/src/main/AndroidManifest.xml
+5 −3 demo-app/src/main/java/io/hyperswitch/demoapp/MainActivity.kt
+71 −61 demo-app/src/main/res/layout/main_activity.xml
+6 −0 demo-app/src/main/res/values/colors.xml
+4 −0 demo-app/src/main/res/values/strings.xml
+4 −12 demo-app/src/main/res/values/styles.xml
+2 −2 gradle.properties
+1 −1 hyperswitch-gradle-plugin/build.gradle
+2 −1 hyperswitch-gradle-plugin/src/main/groovy/io/hyperswitch/HyperPlugin.groovy
+32 −1 hyperswitch-sdk-android-lite/build.gradle
+0 −0 hyperswitch-sdk-android-lite/consumer-rules.pro
+1 −1 hyperswitch-sdk-android-lite/src/main/AndroidManifest.xml
+1 −1 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/PaymentConfiguration.kt
+0 −23 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/lite/WebViewFragment.kt
+2 −2 ...h-sdk-android-lite/src/main/java/io/hyperswitch/payments/expresscheckoutlauncher/ExpressCheckoutLauncher.kt
+2 −2 ...-lite/src/main/java/io/hyperswitch/payments/expresscheckoutlauncher/ExpressCheckoutPaymentMethodLauncher.kt
+11 −8 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/googlepaylauncher/GooglePayActivity.kt
+2 −2 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/googlepaylauncher/GooglePayLauncher.kt
+2 −2 ...-sdk-android-lite/src/main/java/io/hyperswitch/payments/googlepaylauncher/GooglePayPaymentMethodLauncher.kt
+7 −14 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/googlepaylauncher/GooglePayViewModel.kt
+2 −2 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/paymentlauncher/PaymentLauncher.kt
+2 −4 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/paymentlauncher/PaymentResult.kt
+2 −2 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/paypallauncher/PayPalLauncher.kt
+2 −2 ...switch-sdk-android-lite/src/main/java/io/hyperswitch/payments/paypallauncher/PayPalPaymentMethodLauncher.kt
+62 −0 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/payments/view/GooglePayButtonView.kt
+29 −0 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/AddressElementActivityContract.kt
+20 −21 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/AddressLauncher.kt
+2 −2 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/DefaultFlowController.kt
+40 −10 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/DefaultPaymentSheetLauncher.kt
+4 −4 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/FlowControllerFactory.kt
+1 −1 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/PaymentOption.kt
+8 −12 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/PaymentSheet.kt
+9 −8 hyperswitch-sdk-android-lite/src/main/java/io/hyperswitch/paymentsheet/PaymentSheetContract.kt
+6 −3 hyperswitch-sdk-android-lite/src/main/res/values/theme.xml
14 changes: 12 additions & 2 deletions src/components/common/DynamicFields.res
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,21 @@ module RenderField = {
let (firstNameVal, firstNameErrorMessage) =
firstNameVal === ""
? (JSON.Encode.null, Some(localeObject.cardHolderNameRequiredText))
: (JSON.Encode.string(firstNameVal), None)
: (
JSON.Encode.string(firstNameVal),
firstNameVal->ValidationFunctions.containsDigit
? Some(localeObject.invalidDigitsCardHolderNameError)
: None,
)
let (lastNameVal, lastNameErrorMessage) =
lastNameVal === ""
? (JSON.Encode.null, Some(localeObject.lastNameRequiredText))
: (JSON.Encode.string(lastNameVal), None)
: (
JSON.Encode.string(lastNameVal),
lastNameVal->ValidationFunctions.containsDigit
? Some(localeObject.invalidDigitsCardHolderNameError)
: None,
)

setErrorMesage(_ =>
switch firstNameErrorMessage {
Expand Down
14 changes: 14 additions & 0 deletions src/components/common/ValidationFunctions.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ let isValidZip = (~zipCode, ~country) => {
let isZipCodeValid = RegExp.test(postalCode.regex->Js.Re.fromString, zipCode)
zipCode->String.length > 0 && isZipCodeValid
}

let containsDigit = text => {
switch text->String.match(%re("/\d/")) {
| Some(_) => true
| None => false
}
}

let containsMoreThanTwoDigits = text => {
switch text->String.match(%re("/\d/g")) {
| Some(matches) => matches->Array.length > 2
| None => false
}
}
7 changes: 4 additions & 3 deletions src/pages/payment/Card.res
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ let make = (
// Validity Hooks
let (isAllCardValuesValid, setIsAllCardValuesValid) = React.useState(_ => false)
let (isAllDynamicFieldValid, setIsAllDynamicFieldValid) = React.useState(_ => true)
let (isNicknameValid, setIsNicknameValid) = React.useState(_ => true)
let requiredFields = cardVal.required_field->Array.filter(val => {
switch val.field_type {
| RequiredFieldsTypes.UnKnownField(_) => false
Expand All @@ -45,7 +46,7 @@ let make = (
)> => [])
let (error, setError) = React.useState(_ => None)

let isConfirmButtonValid = isAllCardValuesValid && isAllDynamicFieldValid
let isConfirmButtonValid = isAllCardValuesValid && isAllDynamicFieldValid && (isNicknameSelected ? isNicknameValid : true)

let initialiseNetcetera = NetceteraThreeDsHooks.useInitNetcetera()
let (isInitialised, setIsInitialised) = React.useState(_ => false)
Expand Down Expand Up @@ -185,9 +186,9 @@ let make = (
allApiData.additionalPMLData.mandateType,
) {
| (false, _, true, NEW_MANDATE | NORMAL) =>
<NickNameElement nickname setNickname isNicknameSelected />
<NickNameElement nickname setNickname isNicknameSelected setIsNicknameValid />
| (false, _, false, NEW_MANDATE) | (false, _, _, SETUP_MANDATE) =>
<NickNameElement nickname setNickname isNicknameSelected=true />
<NickNameElement nickname setNickname isNicknameSelected=true setIsNicknameValid />
| _ => React.null
}}
</View>
Expand Down
38 changes: 32 additions & 6 deletions src/pages/payment/NickNameElement.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
@react.component
let make = (~nickname, ~setNickname, ~isNicknameSelected) => {
let {component, borderWidth, borderRadius} = ThemebasedStyle.useThemeBasedStyle()
let make = (~nickname, ~setNickname, ~isNicknameSelected, ~setIsNicknameValid) => {
let {component, borderWidth, borderRadius, dangerColor} = ThemebasedStyle.useThemeBasedStyle()
let localeObject = GetLocale.useGetLocalObj()
let (isFocus, setisFocus) = React.useState(_ => false)
let (errorMessage, setErrorMesage) = React.useState(_ => None)

let onChange = text => {
setNickname(_ => Some(text))

if text->String.length > 12 {
setErrorMesage(_ => Some(localeObject.nickNameLengthExceedError))
setIsNicknameValid(_ => false)
} else {
switch text->ValidationFunctions.containsMoreThanTwoDigits {
| true => {
setErrorMesage(_ => Some(localeObject.invalidDigitsNickNameError))
setIsNicknameValid(_ => false)
}
| false => {
setErrorMesage(_ => None)
setIsNicknameValid(_ => true)
}
}
}
}

isNicknameSelected
? <>
Expand All @@ -10,13 +32,13 @@ let make = (~nickname, ~setNickname, ~isNicknameSelected) => {
// <Space height=5. />
<CustomInput
state={nickname->Option.getOr("")}
setState={str => setNickname(_ => Some(str))}
setState={str => onChange(str)}
placeholder={`${localeObject.cardNickname}${" (Optional)"}`}
keyboardType=#default
isValid=true
onFocus={_ => ()}
onBlur={_ => ()}
textColor=component.color
onFocus={_ => setisFocus(_ => true)}
onBlur={_ => setisFocus(_ => false)}
textColor={isFocus || errorMessage->Option.isNone ? component.color : dangerColor}
borderBottomLeftRadius=borderRadius
borderBottomRightRadius=borderRadius
borderTopLeftRadius=borderRadius
Expand All @@ -27,6 +49,10 @@ let make = (~nickname, ~setNickname, ~isNicknameSelected) => {
borderRightWidth=borderWidth
animateLabel=localeObject.cardNickname
/>
{switch errorMessage {
| Some(text) => <ErrorText text=Some(text) />
| None => React.null
}}
<Space height=5. />
</>
: React.null
Expand Down
Loading

0 comments on commit 5e9a5f4

Please sign in to comment.