Skip to content

Commit

Permalink
fix: switched GPay and ApplePay to oneClickButtons in case required f…
Browse files Browse the repository at this point in the history
…ields are prefilled
  • Loading branch information
ArushKapoorJuspay committed Mar 12, 2024
1 parent 456e51d commit 7e29872
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
15 changes: 5 additions & 10 deletions src/Components/DynamicFields.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ let make = (
let {billingAddress} = Recoil.useRecoilValueFromAtom(optionAtom)

//<...>//
let paymentMethodTypes = React.useMemo3(() => {
PaymentMethodsRecord.getPaymentMethodTypeFromList(
~list,
~paymentMethod,
~paymentMethodType=PaymentUtils.getPaymentMethodName(
~paymentMethodType=paymentMethod,
~paymentMethodName=paymentMethodType,
),
)->Belt.Option.getWithDefault(PaymentMethodsRecord.defaultPaymentMethodType)
}, (list, paymentMethod, paymentMethodType))
let paymentMethodTypes = DynamicFieldsUtils.usePaymentMethodTypeFromList(
~list,
~paymentMethod,
~paymentMethodType,
)

let requiredFieldsWithBillingDetails = React.useMemo3(() => {
if paymentMethod === "card" {
Expand Down
25 changes: 23 additions & 2 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,19 @@ let make = (
None
}, [savedMethods])

let (walletList, paymentOptionsList, actualList) = React.useMemo4(() => {
let areAllGooglePayRequiredFieldsPrefilled = DynamicFieldsUtils.areAllRequiredFieldsPrefilled(
~list,
~paymentMethod="wallet",
~paymentMethodType="google_pay",
)

let areAllApplePayRequiredFieldsPrefilled = DynamicFieldsUtils.areAllRequiredFieldsPrefilled(
~list,
~paymentMethod="wallet",
~paymentMethodType="apple_pay",
)

let (walletList, paymentOptionsList, actualList) = React.useMemo6(() => {
switch methodslist {
| Loaded(paymentlist) =>
let paymentOrder =
Expand All @@ -119,6 +131,8 @@ let make = (
~order=paymentOrder,
~showApplePay=isApplePayReady,
~showGooglePay=isGooglePayReady,
~areAllGooglePayRequiredFieldsPrefilled,
~areAllApplePayRequiredFieldsPrefilled,
)
(
wallets->Utils.removeDuplicate,
Expand All @@ -131,7 +145,14 @@ let make = (
: ([], [], [])
| _ => ([], [], [])
}
}, (methodslist, paymentMethodOrder, isApplePayReady, isGooglePayReady))
}, (
methodslist,
paymentMethodOrder,
isApplePayReady,
isGooglePayReady,
areAllGooglePayRequiredFieldsPrefilled,
areAllApplePayRequiredFieldsPrefilled,
))

React.useEffect4(() => {
switch methodslist {
Expand Down
21 changes: 21 additions & 0 deletions src/Utilities/DynamicFieldsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,24 @@ let useSubmitCallback = () => {
}
}, (line1, line2, state, city, postalCode))
}

let usePaymentMethodTypeFromList = (~list, ~paymentMethod, ~paymentMethodType) => {
React.useMemo3(() => {
PaymentMethodsRecord.getPaymentMethodTypeFromList(
~list,
~paymentMethod,
~paymentMethodType=PaymentUtils.getPaymentMethodName(
~paymentMethodType=paymentMethod,
~paymentMethodName=paymentMethodType,
),
)->Belt.Option.getWithDefault(PaymentMethodsRecord.defaultPaymentMethodType)
}, (list, paymentMethod, paymentMethodType))
}

let areAllRequiredFieldsPrefilled = (~list, ~paymentMethod, ~paymentMethodType) => {
let paymentMethodTypes = usePaymentMethodTypeFromList(~list, ~paymentMethod, ~paymentMethodType)

paymentMethodTypes.required_fields->Js.Array2.reduce((acc, requiredField) => {
acc && requiredField.value != ""
}, true)
}
8 changes: 6 additions & 2 deletions src/Utilities/PaymentUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ let paymentListLookupNew = (
~order,
~showApplePay,
~showGooglePay,
~areAllGooglePayRequiredFieldsPrefilled,
~areAllApplePayRequiredFieldsPrefilled,
) => {
let pmList = list->PaymentMethodsRecord.buildFromPaymentList
let walletsList = []
Expand All @@ -26,14 +28,16 @@ let paymentListLookupNew = (
let applePayFields = pmList->Js.Array2.find(item => item.paymentMethodName === "apple_pay")
switch googlePayFields {
| Some(val) =>
if val.fields->Js.Array2.length > 0 && showGooglePay {
if (
val.fields->Js.Array2.length > 0 && showGooglePay && !areAllGooglePayRequiredFieldsPrefilled
) {
walletToBeDisplayedInTabs->Js.Array2.push("google_pay")->ignore
}
| None => ()
}
switch applePayFields {
| Some(val) =>
if val.fields->Js.Array2.length > 0 && showApplePay {
if val.fields->Js.Array2.length > 0 && showApplePay && !areAllApplePayRequiredFieldsPrefilled {
walletToBeDisplayedInTabs->Js.Array2.push("apple_pay")->ignore
}
| None => ()
Expand Down

0 comments on commit 7e29872

Please sign in to comment.