diff --git a/src/CardSchemeComponent.res b/src/CardSchemeComponent.res new file mode 100644 index 000000000..dee03adf6 --- /dev/null +++ b/src/CardSchemeComponent.res @@ -0,0 +1,51 @@ +module CoBadgeCardSchemeDropDown = { + @react.component + let make = (~eligibleCardSchemes, ~setCardBrand) => { + + } +} + +@react.component +let make = (~cardNumber, ~paymentType, ~cardBrand, ~setCardBrand) => { + let cardType = React.useMemo1(_ => cardBrand->CardUtils.getCardType, [cardBrand]) + let animate = cardType == NOTFOUND ? "animate-slideLeft" : "animate-slideRight" + let cardBrandIcon = React.useMemo1( + _ => CardUtils.getCardBrandIcon(cardType, paymentType), + [cardBrand], + ) + + let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue) + let enabledCardSchemes = + paymentMethodListValue->PaymentUtils.getSupportedCardBrands->Option.getOr([]) + + let matchedCardSchemes = cardNumber->CardUtils.getAllMatchedCardSchemes + + let eligibleCardSchemes = CardUtils.getEligibleCoBadgedCardSchemes( + ~matchedCardSchemes, + ~enabledCardSchemes, + ) + + let isCardCoBadged = eligibleCardSchemes->Array.length > 1 + + let marginLeft = isCardCoBadged ? "-ml-2" : "" + +
+ cardBrandIcon + + + +
+} diff --git a/src/CardUtils.res b/src/CardUtils.res index a20967fdc..02a4bd55f 100644 --- a/src/CardUtils.res +++ b/src/CardUtils.res @@ -26,6 +26,7 @@ type cardProps = ( string, (string => string) => unit, int, + string, ) type expiryProps = ( @@ -655,3 +656,18 @@ let getPaymentMethodBrand = (customerMethod: PaymentType.customerMethods) => { ) } } + +let getAllMatchedCardSchemes = cardNumber => { + CardPattern.cardPatterns->Array.reduce([], (acc, item) => { + if String.match(cardNumber, item.pattern)->Option.isSome { + acc->Array.push(item.issuer) + } + acc + }) +} + +let getEligibleCoBadgedCardSchemes = (~matchedCardSchemes, ~enabledCardSchemes) => { + matchedCardSchemes->Array.filter(ele => { + enabledCardSchemes->Array.includes(ele->String.toLowerCase) + }) +} diff --git a/src/Components/DynamicFields.res b/src/Components/DynamicFields.res index 3fc1d3b90..f12709881 100644 --- a/src/Components/DynamicFields.res +++ b/src/Components/DynamicFields.res @@ -109,6 +109,7 @@ let make = ( "", _ => (), 0, + "", ) let defaultExpiryProps = ( @@ -163,6 +164,7 @@ let make = ( cardError, _, maxCardLength, + cardScheme, ) = switch cardProps { | Some(cardProps) => cardProps | None => defaultCardProps diff --git a/src/Components/PaymentInputField.res b/src/Components/PaymentInputField.res index 98ba1809d..7c8b5540b 100644 --- a/src/Components/PaymentInputField.res +++ b/src/Components/PaymentInputField.res @@ -131,7 +131,7 @@ let make = ( -
{rightIcon}
+
{rightIcon}
{switch errorString { diff --git a/src/Payment.res b/src/Payment.res index 13dd3d733..9aa7ba63b 100644 --- a/src/Payment.res +++ b/src/Payment.res @@ -47,13 +47,15 @@ let make = (~paymentMode, ~integrateError, ~logger) => { let (isZipValid, setIsZipValid) = React.useState(_ => None) let (isCardSupported, setIsCardSupported) = React.useState(_ => None) - let (cardBrand, maxCardLength) = React.useMemo(() => { - let brand = getCardBrand(cardNumber) - let maxLength = getMaxLength(cardNumber) - let isNotBancontact = selectedOption !== "bancontact_card" && brand == "" - !showFields && isNotBancontact ? (cardScheme, maxLength) : (brand, maxLength) + let maxCardLength = React.useMemo(() => { + getMaxLength(cardNumber) }, (cardNumber, cardScheme, showFields)) + let cardBrand = getCardBrand(cardNumber) + let isNotBancontact = selectedOption !== "bancontact_card" && cardBrand == "" + let (cardBrand, setCardBrand) = React.useState(_ => + !showFields && isNotBancontact ? cardScheme : cardBrand + ) let supportedCardBrands = React.useMemo(() => { paymentMethodListValue->PaymentUtils.getSupportedCardBrands }, [paymentMethodListValue]) @@ -219,7 +221,7 @@ let make = (~paymentMode, ~integrateError, ~logger) => { } let cardNetwork = { if cardBrand != "" { - [("card_network", cardNumber->CardUtils.getCardBrand->JSON.Encode.string)] + [("card_network", cardBrand->JSON.Encode.string)] } else { [] } @@ -350,11 +352,14 @@ let make = (~paymentMode, ~integrateError, ~logger) => { None }, (isExpiryValid, isExpiryComplete(cardExpiry))) + React.useEffect(() => { + setCardBrand(_ => cardNumber->CardUtils.getCardBrand) + None + }, [cardNumber]) + let icon = React.useMemo(() => { - let animate = cardType == NOTFOUND ? "animate-slideLeft" : "animate-slideRight" - let cardBrandIcon = getCardBrandIcon(cardType, paymentType) -
cardBrandIcon
- }, (cardType, paymentType)) + + }, (cardType, paymentType, cardBrand, cardNumber)) let cardProps: CardUtils.cardProps = ( isCardValid, @@ -368,6 +373,7 @@ let make = (~paymentMode, ~integrateError, ~logger) => { cardError, setCardError, maxCardLength, + cardBrand, ) let expiryProps: CardUtils.expiryProps = ( diff --git a/src/Payments/CardPayment.res b/src/Payments/CardPayment.res index 7e1e811fd..bdb3a5071 100644 --- a/src/Payments/CardPayment.res +++ b/src/Payments/CardPayment.res @@ -35,12 +35,9 @@ let make = ( cardError, setCardError, maxCardLength, + cardBrand, ) = cardProps - let cardBrand = React.useMemo(() => { - cardNumber->CardUtils.getCardBrand - }, [cardNumber]) - let ( isExpiryValid, setIsExpiryValid, @@ -207,6 +204,7 @@ let make = ( nickname, isCardBrandValid, isManualRetryEnabled, + cardProps, )) useSubmitPaymentData(submitCallback) diff --git a/src/RenderPaymentMethods.res b/src/RenderPaymentMethods.res index 842e77d7b..a5bf5a21f 100644 --- a/src/RenderPaymentMethods.res +++ b/src/RenderPaymentMethods.res @@ -24,6 +24,7 @@ let make = ( _, _, maxCardLength, + _, ) = cardProps let ( diff --git a/src/SingleLineCardPayment.res b/src/SingleLineCardPayment.res index 5a118442c..7e4c3cc35 100644 --- a/src/SingleLineCardPayment.res +++ b/src/SingleLineCardPayment.res @@ -26,6 +26,7 @@ let make = ( _, _, maxCardLength, + _, ) = cardProps let (