diff --git a/src/Types/ApplePayTypes.res b/src/Types/ApplePayTypes.res index f8e07330..f644e938 100644 --- a/src/Types/ApplePayTypes.res +++ b/src/Types/ApplePayTypes.res @@ -185,37 +185,3 @@ let getPaymentRequestFromSession = (~sessionObj, ~componentName) => { paymentRequest } - -let handleApplePayIframePostMessage = (msg, componentName, mountedIframeRef) => { - let isApplePayMessageSent = ref(false) - - let iframes = Window.querySelectorAll("iframe") - - iframes->Array.forEach(iframe => { - let iframeSrc = iframe->Window.getAttribute("src")->Option.getOr("") - if iframeSrc->String.includes(`componentName=${componentName}`) { - iframe->Js.Nullable.return->Window.iframePostMessage(msg) - isApplePayMessageSent := true - } - }) - - if !isApplePayMessageSent.contents { - mountedIframeRef->Window.iframePostMessage(msg) - } -} - -let handlePazeIframePostMessage = (msg, componentName, eventSource) => { - let isPazePresent = ref(false) - let iframes = Window.querySelectorAll("iframe") - iframes->Array.forEach(iframe => { - let iframeSrc = iframe->Window.getAttribute("src")->Option.getOr("") - if iframeSrc->String.includes(`componentName=${componentName}`) { - iframe->Js.Nullable.return->Window.iframePostMessage(msg) - isPazePresent := true - } - }) - - if !isPazePresent.contents { - eventSource->Window.sendPostMessage(msg) - } -} diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index db5e7a18..ad9b32ac 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -1439,3 +1439,32 @@ let mergeAndFlattenToTuples = (body, requiredFieldsBody) => ->flattenObject(true) ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) ->getArrayOfTupleFromDict + +let sendMessageToIframe = (~msg, ~componentName, ~eventSource=None, ~mountedIframeRef=None) => { + let isMessageSent = ref(false) + let iframes = Window.querySelectorAll("iframe") + + iframes->Array.forEach(iframe => { + let iframeSrc = iframe->Window.getAttribute("src")->Option.getOr("") + if iframeSrc->String.includes(`componentName=${componentName}`) { + iframe->Js.Nullable.return->Window.iframePostMessage(msg) + isMessageSent := true + } + }) + + if !isMessageSent.contents { + switch (eventSource, mountedIframeRef) { + | (Some(source), _) => source->Window.sendPostMessage(msg) + | (None, Some(ref)) => ref->Window.iframePostMessage(msg) + | (None, None) => () + } + } +} + +let handleApplePayIframePostMessage = (msg, componentName, mountedIframeRef) => { + sendMessageToIframe(~msg, ~componentName, ~mountedIframeRef=Some(mountedIframeRef)) +} + +let handlePazeIframePostMessage = (msg, componentName, eventSource) => { + sendMessageToIframe(~msg, ~componentName, ~eventSource=Some(eventSource)) +} diff --git a/src/hyper-loader/Elements.res b/src/hyper-loader/Elements.res index c850cab3..bd1cedcf 100644 --- a/src/hyper-loader/Elements.res +++ b/src/hyper-loader/Elements.res @@ -3,10 +3,9 @@ open ErrorUtils open Identity open Utils open EventListenerManager -open ApplePayTypes type trustPayFunctions = { - finishApplePaymentV2: (string, paymentRequestData) => promise, + finishApplePaymentV2: (string, ApplePayTypes.paymentRequestData) => promise, executeGooglePayment: (string, GooglePayType.paymentDataRequest) => promise, } @new external trustPayApi: JSON.t => trustPayFunctions = "TrustPayApi" @@ -353,7 +352,7 @@ let make = ( if dict->Dict.get("applePayMounted")->Option.isSome { if wallets.applePay === Auto { - switch sessionForApplePay->Nullable.toOption { + switch ApplePayTypes.sessionForApplePay->Nullable.toOption { | Some(session) => try { if session.canMakePayments() { @@ -938,7 +937,7 @@ let make = ( intermediatePaymentData ->getDictFromJson ->getDictFromDict("shippingAddress") - ->billingContactItemToObjMapper + ->ApplePayTypes.billingContactItemToObjMapper let newShippingAddress = [ ("state", shippingAddress.administrativeArea->JSON.Encode.string),