From 384e63a3a6737d2dcaa2a5cf21b8a873dd9e9248 Mon Sep 17 00:00:00 2001 From: Arush Date: Fri, 22 Mar 2024 16:20:08 +0530 Subject: [PATCH] fix: applePay Dynamic Fields Error Handling and Dynamic Fields Postal Code Error --- src/Payments/ApplePay.res | 4 +- src/Utilities/DynamicFieldsUtils.res | 4 +- src/orca-loader/Elements.res | 93 +++++++++++++++++----------- 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/src/Payments/ApplePay.res b/src/Payments/ApplePay.res index 2dc6b59e6..634d2d324 100644 --- a/src/Payments/ApplePay.res +++ b/src/Payments/ApplePay.res @@ -333,7 +333,7 @@ let make = ( None }, (isApplePayReady, isInvokeSDKFlow, paymentExperience, isWallet)) - let submitCallback = (ev: Window.event) => { + let submitCallback = React.useCallback2((ev: Window.event) => { if !isWallet { let json = ev.data->JSON.parseExn let confirm = json->getDictFromJson->ConfirmType.itemToObjMapper @@ -353,7 +353,7 @@ let make = ( ) } } - } + }, (areRequiredFieldsValid, areRequiredFieldsEmpty)) useSubmitPaymentData(submitCallback) { diff --git a/src/Utilities/DynamicFieldsUtils.res b/src/Utilities/DynamicFieldsUtils.res index f09bce1e8..0c569d6c2 100644 --- a/src/Utilities/DynamicFieldsUtils.res +++ b/src/Utilities/DynamicFieldsUtils.res @@ -142,10 +142,10 @@ let useRequiredFieldsEmptyAndValid = ( | StateAndCity => state.value !== "" && city.value !== "" | CountryAndPincode(countryArr) => (country !== "" || countryArr->Array.length === 0) && - postalCode.isValid->Option.getOr(false) + postalCode.value !== "" | AddressCity => city.value !== "" - | AddressPincode => postalCode.isValid->Option.getOr(false) + | AddressPincode => postalCode.value !== "" | AddressState => state.value !== "" | BlikCode => blikCode.value !== "" | Currency(currencyArr) => currency !== "" || currencyArr->Array.length === 0 diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 6700963e1..ea15add96 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -529,49 +529,68 @@ let make = ( | _ => () } } else { - let paymentRequest = - applePayPresent - ->Option.flatMap(JSON.Decode.object) - ->Option.getOr(Dict.make()) - ->Dict.get("payment_request_data") - ->Option.getOr(Dict.make()->JSON.Encode.object) - ->Utils.transformKeys(Utils.CamelCase) - - let ssn = applePaySession(3, paymentRequest) - switch applePaySessionRef.contents->Nullable.toOption { - | Some(session) => - try { - session.abort() - } catch { - | error => Console.log2("Abort fail", error) - } - | None => () - } - - ssn.begin() - applePaySessionRef := ssn->Nullable.make - - ssn.onvalidatemerchant = _event => { - let merchantSession = + try { + let paymentRequest = applePayPresent ->Option.flatMap(JSON.Decode.object) ->Option.getOr(Dict.make()) - ->Dict.get("session_token_data") + ->Dict.get("payment_request_data") ->Option.getOr(Dict.make()->JSON.Encode.object) ->Utils.transformKeys(Utils.CamelCase) - ssn.completeMerchantValidation(merchantSession) - } - ssn.onpaymentauthorized = event => { - ssn.completePayment({"status": ssn.\"STATUS_SUCCESS"}->objToJson) - applePaySessionRef := Nullable.null - processPayment(event.payment.token) - } - ssn.oncancel = _ev => { - let msg = [("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray - mountedIframeRef->Window.iframePostMessage(msg) - applePaySessionRef := Nullable.null - Utils.logInfo(Console.log("Apple Pay payment cancelled")) + let ssn = applePaySession(3, paymentRequest) + switch applePaySessionRef.contents->Nullable.toOption { + | Some(session) => + try { + session.abort() + } catch { + | error => Console.log2("Abort fail", error) + } + | None => () + } + + applePaySessionRef := ssn->Nullable.make + + ssn.onvalidatemerchant = _event => { + let merchantSession = + applePayPresent + ->Option.flatMap(JSON.Decode.object) + ->Option.getOr(Dict.make()) + ->Dict.get("session_token_data") + ->Option.getOr(Dict.make()->JSON.Encode.object) + ->Utils.transformKeys(Utils.CamelCase) + ssn.completeMerchantValidation(merchantSession) + } + + ssn.onpaymentauthorized = event => { + ssn.completePayment({"status": ssn.\"STATUS_SUCCESS"}->objToJson) + applePaySessionRef := Nullable.null + processPayment(event.payment.token) + } + ssn.oncancel = _ev => { + let msg = + [("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray + mountedIframeRef->Window.iframePostMessage(msg) + applePaySessionRef := Nullable.null + Utils.logInfo(Console.log("Apple Pay payment cancelled")) + } + + ssn.begin() + } catch { + | exn => { + logger.setLogInfo( + ~value=exn->Utils.formatException->JSON.stringify, + ~eventName=APPLE_PAY_FLOW, + ~paymentMethod="APPLE_PAY", + (), + ) + Utils.logInfo(Console.error2("Apple Pay Error", exn)) + + let msg = + [("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray + mountedIframeRef->Window.iframePostMessage(msg) + applePaySessionRef := Nullable.null + } } } } else {