Skip to content

Commit

Permalink
refactor: similar code move to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
sakksham7 committed Nov 20, 2024
1 parent 67ef9f8 commit a1a732e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
21 changes: 6 additions & 15 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
displaySavedPaymentMethods,
sdkHandleConfirmPayment,
} = Recoil.useRecoilValueFromAtom(optionAtom)
let logger = HyperLogger.make(~source=Elements(Payment))
let {themeObj, localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let optionAtomValue = Recoil.useRecoilValueFromAtom(optionAtom)
let paymentMethodList = Recoil.useRecoilValueFromAtom(paymentMethodList)
Expand Down Expand Up @@ -158,19 +157,11 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
googlePayThirdPartySessionObj.sessionsToken,
Gpay,
)
let paypalPaymentMethodExperience = React.useMemo(() => {
PaymentMethodsRecord.getPaymentExperienceTypeFromPML(
~paymentMethodList=paymentMethodListValue,
~paymentMethodName="wallet",
~paymentMethodType="paypal",
)
}, [paymentMethodListValue])
let paypalToken = React.useMemo(
() => SessionsType.getPaymentSessionObj(sessionObj.sessionsToken, Paypal),
[sessionObj],
)
let isPaypalSDKFlow = paypalPaymentMethodExperience->Array.includes(InvokeSDK)
let isPaypalRedirectFlow = paypalPaymentMethodExperience->Array.includes(RedirectToURL)
let {
paypalToken,
isPaypalSDKFlow,
isPaypalRedirectFlow,
} = PayPalHelpers.usePaymentMethodExperience(~paymentMethodListValue, ~sessionObj)

React.useEffect(() => {
switch paymentMethodList {
Expand Down Expand Up @@ -374,7 +365,7 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
| OtherTokenOptional(optToken) =>
switch (optToken, isPaypalSDKFlow, isPaypalRedirectFlow) {
| (Some(_token), true, _) => {
logger.setLogInfo(
loggerState.setLogInfo(
~value="PayPal Invoke SDK Flow in Tabs",
~eventName=PAYPAL_SDK_FLOW,
)
Expand Down
31 changes: 31 additions & 0 deletions src/Payments/PayPalHelpers.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
open PaymentMethodsRecord
open SessionsType

type paypalExperienceData = {
paypalToken: optionalTokenType,
isPaypalSDKFlow: bool,
isPaypalRedirectFlow: bool,
}

let usePaymentMethodExperience = (~paymentMethodListValue, ~sessionObj: sessions) => {
let paypalPaymentMethodExperience = React.useMemo(() => {
getPaymentExperienceTypeFromPML(
~paymentMethodList=paymentMethodListValue,
~paymentMethodName="wallet",
~paymentMethodType="paypal",
)
}, [paymentMethodListValue])

let paypalToken = React.useMemo(
() => getPaymentSessionObj(sessionObj.sessionsToken, Paypal),
[sessionObj],
)
let isPaypalSDKFlow = paypalPaymentMethodExperience->Array.includes(InvokeSDK)
let isPaypalRedirectFlow = paypalPaymentMethodExperience->Array.includes(RedirectToURL)

{
paypalToken,
isPaypalSDKFlow,
isPaypalRedirectFlow,
}
}
20 changes: 7 additions & 13 deletions src/Payments/PaymentRequestButtonElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ let make = (~sessions, ~walletOptions, ~paymentType) => {
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)

let sessionObj = React.useMemo(() => itemToObjMapper(dict, Others), [dict])
let paypalToken = React.useMemo(
() => getPaymentSessionObj(sessionObj.sessionsToken, Paypal),
[sessionObj],
)
let paypalPaymentMethodExperience = React.useMemo(() => {
PaymentMethodsRecord.getPaymentExperienceTypeFromPML(
~paymentMethodList=paymentMethodListValue,
~paymentMethodName="wallet",
~paymentMethodType="paypal",
)
}, [paymentMethodListValue])

let {
paypalToken,
isPaypalSDKFlow,
isPaypalRedirectFlow,
} = PayPalHelpers.usePaymentMethodExperience(~paymentMethodListValue, ~sessionObj)

let gPayToken = getPaymentSessionObj(sessionObj.sessionsToken, Gpay)
let applePaySessionObj = itemToObjMapper(dict, ApplePayObject)
let applePayToken = getPaymentSessionObj(applePaySessionObj.sessionsToken, ApplePay)
Expand All @@ -70,8 +66,6 @@ let make = (~sessions, ~walletOptions, ~paymentType) => {

let {clientSecret} = Recoil.useRecoilValueFromAtom(RecoilAtoms.keys)
let options = Recoil.useRecoilValueFromAtom(RecoilAtoms.optionAtom)
let isPaypalSDKFlow = paypalPaymentMethodExperience->Array.includes(InvokeSDK)
let isPaypalRedirectFlow = paypalPaymentMethodExperience->Array.includes(RedirectToURL)

<div className="flex flex-col gap-2 h-auto w-full">
{walletOptions
Expand Down
20 changes: 5 additions & 15 deletions src/Utilities/PaymentUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,11 @@ let usePaypalFlowStatus = (~sessions, ~paymentMethodListValue) => {
->getDictFromJson
->SessionsType.itemToObjMapper(Others)

let paypalPaymentMethodExperience = React.useMemo(() => {
PaymentMethodsRecord.getPaymentExperienceTypeFromPML(
~paymentMethodList=paymentMethodListValue,
~paymentMethodName="wallet",
~paymentMethodType="paypal",
)
}, [paymentMethodListValue])

let paypalToken = React.useMemo(
() => SessionsType.getPaymentSessionObj(sessionObj.sessionsToken, Paypal),
[sessionObj],
)

let isPaypalSDKFlow = paypalPaymentMethodExperience->Array.includes(InvokeSDK)
let isPaypalRedirectFlow = paypalPaymentMethodExperience->Array.includes(RedirectToURL)
let {
paypalToken,
isPaypalSDKFlow,
isPaypalRedirectFlow,
} = PayPalHelpers.usePaymentMethodExperience(~paymentMethodListValue, ~sessionObj)

let isPaypalTokenExist = switch paypalToken {
| OtherTokenOptional(optToken) =>
Expand Down

0 comments on commit a1a732e

Please sign in to comment.