Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removed fallback in case of sessions call fail based on payment experience #440

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
setLoadSavedCards: (savedCardsLoadState => savedCardsLoadState) => unit,
) = React.useState(_ => LoadingSavedCards)

let isKlarnaRedirectFlow = PaymentUtils.getIsKlarnaRedirectFlow(sessions)

React.useEffect(() => {
switch (displaySavedPaymentMethods, customerPaymentMethods) {
| (false, _) => {
Expand Down Expand Up @@ -246,11 +244,9 @@ let make = (~cardProps, ~expiryProps, ~cvcProps, ~paymentType: CardThemeType.mod
{switch selectedOption->PaymentModeType.paymentMode {
| Card => <CardPayment cardProps expiryProps cvcProps paymentType />
| Klarna =>
<RenderIf condition={isKlarnaRedirectFlow}>
<React.Suspense fallback={loader()}>
<KlarnaPaymentLazy paymentType />
</React.Suspense>
</RenderIf>
<React.Suspense fallback={loader()}>
<KlarnaPaymentLazy paymentType />
</React.Suspense>
| ACHTransfer =>
<React.Suspense fallback={loader()}>
<ACHBankTransferLazy paymentType />
Expand Down
5 changes: 1 addition & 4 deletions src/Payments/CardPayment.res
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ let make = (
let cardPaymentMethod =
paymentMethodListValue.payment_methods
->Array.find(ele => ele.payment_method === "card")
->Option.getOr({
payment_method: "card",
payment_method_types: [],
})
->Option.getOr(PaymentMethodsRecord.defaultMethods)

let cardNetworks = cardPaymentMethod.payment_method_types->Array.map(ele => ele.card_networks)

Expand Down
31 changes: 27 additions & 4 deletions src/Payments/PaymentMethodsRecord.res
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,11 @@ type methods = {
payment_method_types: array<paymentMethodTypes>,
}

let defaultMethods = {
payment_method: "card",
payment_method_types: [],
}

type mandateType = {
amount: int,
currency: string,
Expand Down Expand Up @@ -1049,10 +1054,7 @@ let getPaymentMethodTypeFromList = (
->Array.find(item => {
item.payment_method == paymentMethod
})
->Option.getOr({
payment_method: "card",
payment_method_types: [],
})
->Option.getOr(defaultMethods)
).payment_method_types->Array.find(item => {
item.payment_method_type == paymentMethodType
})
Expand All @@ -1064,3 +1066,24 @@ let getCardNetwork = (~paymentMethodType, ~cardBrand) => {
->Array.get(0)
->Option.getOr(defaultCardNetworks)
}

let getPaymentExperienceTypeFromPML = (
~paymentMethodList: paymentMethodList,
~paymentMethodName,
~paymentMethodType,
) => {
paymentMethodList.payment_methods
->Array.filter(paymentMethod => paymentMethod.payment_method === paymentMethodName)
->Array.get(0)
->Option.flatMap(method =>
method.payment_method_types
->Array.filter(methodTypes => methodTypes.payment_method_type === paymentMethodType)
->Array.get(0)
)
->Option.flatMap(paymentMethodTypes =>
paymentMethodTypes.payment_experience
->Array.map(paymentExperience => paymentExperience.payment_experience_type)
->Some
)
->Option.getOr([])
}
56 changes: 40 additions & 16 deletions src/Utilities/PaymentUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ let paymentListLookupNew = (
~order,
~isShowPaypal,
~isShowKlarnaOneClick,
~isKlarnaRedirectFlow,
~isKlarnaSDKFlow,
~paymentMethodListValue: PaymentMethodsRecord.paymentMethodList,
) => {
let pmList = list->PaymentMethodsRecord.buildFromPaymentList
let walletsList = []
Expand Down Expand Up @@ -43,8 +44,21 @@ let paymentListLookupNew = (
} else if item.methodType == "reward" {
otherPaymentList->Array.push(item.paymentMethodName)->ignore
} else if item.methodType == "pay_later" {
if item.paymentMethodName === "klarna" && !isKlarnaRedirectFlow && isShowKlarnaOneClick {
walletsList->Array.push(item.paymentMethodName)->ignore
if item.paymentMethodName === "klarna" {
let klarnaPaymentMethodExperience = PaymentMethodsRecord.getPaymentExperienceTypeFromPML(
~paymentMethodList=paymentMethodListValue,
~paymentMethodName=item.methodType,
~paymentMethodType=item.paymentMethodName,
)

let isInvokeSDKExperience = klarnaPaymentMethodExperience->Array.includes(InvokeSDK)
let isRedirectExperience = klarnaPaymentMethodExperience->Array.includes(RedirectToURL)

if isKlarnaSDKFlow && isShowKlarnaOneClick && isInvokeSDKExperience {
walletsList->Array.push(item.paymentMethodName)->ignore
} else if isRedirectExperience {
otherPaymentList->Array.push(item.paymentMethodName)->ignore
}
} else {
otherPaymentList->Array.push(item.paymentMethodName)->ignore
}
Expand Down Expand Up @@ -265,13 +279,13 @@ let useAreAllRequiredFieldsPrefilled = (
})
}

let getIsKlarnaRedirectFlow = sessions => {
let getIsKlarnaSDKFlow = sessions => {
let dict = sessions->Utils.getDictFromJson
let sessionObj = SessionsType.itemToObjMapper(dict, Others)
let klarnaTokenObj = SessionsType.getPaymentSessionObj(sessionObj.sessionsToken, Klarna)
switch klarnaTokenObj {
| OtherTokenOptional(optToken) => optToken->Option.isNone
| _ => true
| OtherTokenOptional(optToken) => optToken->Option.isSome
| _ => false
}
}

Expand All @@ -286,13 +300,7 @@ let useGetPaymentMethodList = (~paymentOptions, ~paymentType, ~sessions) => {

let paymentOrder = paymentMethodOrder->getOptionalArr->removeDuplicate

let isKlarnaRedirectFlow = getIsKlarnaRedirectFlow(sessions)

let filterPaymentMethods = (paymentOptionsList: array<string>, ~isKlarnaRedirectFlow) => {
paymentOptionsList->Array.filter(paymentOptionsName =>
!(paymentOptionsName === "klarna" && !isKlarnaRedirectFlow)
)
}
let isKlarnaSDKFlow = getIsKlarnaSDKFlow(sessions)

React.useMemo(() => {
switch methodslist {
Expand All @@ -305,14 +313,30 @@ let useGetPaymentMethodList = (~paymentOptions, ~paymentType, ~sessions) => {
~order=paymentOrder,
~isShowPaypal=optionAtomValue.wallets.payPal === Auto,
~isShowKlarnaOneClick=optionAtomValue.wallets.klarna === Auto,
~isKlarnaRedirectFlow,
~isKlarnaSDKFlow,
~paymentMethodListValue=plist,
)

let klarnaPaymentMethodExperience = PaymentMethodsRecord.getPaymentExperienceTypeFromPML(
~paymentMethodList=plist,
~paymentMethodName="pay_later",
~paymentMethodType="klarna",
)

let isKlarnaInvokeSDKExperience = klarnaPaymentMethodExperience->Array.includes(InvokeSDK)

let filterPaymentMethods = (paymentOptionsList: array<string>) => {
paymentOptionsList->Array.filter(paymentOptionsName =>
!(paymentOptionsName === "klarna" && isKlarnaSDKFlow && isKlarnaInvokeSDKExperience)
)
}

(
wallets->removeDuplicate->Utils.getWalletPaymentMethod(paymentType),
paymentOptions
->Array.concat(otherOptions)
->removeDuplicate
->filterPaymentMethods(~isKlarnaRedirectFlow),
->filterPaymentMethods,
otherOptions,
)
| SemiLoaded =>
Expand All @@ -327,7 +351,7 @@ let useGetPaymentMethodList = (~paymentOptions, ~paymentType, ~sessions) => {
optionAtomValue.wallets.payPal,
optionAtomValue.wallets.klarna,
paymentType,
isKlarnaRedirectFlow,
isKlarnaSDKFlow,
))
}

Expand Down
Loading