Skip to content

Commit

Permalink
refactor: made common function for reusable code
Browse files Browse the repository at this point in the history
  • Loading branch information
PritishBudhiraja committed Nov 7, 2024
1 parent 514c7da commit 8a2aef0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
34 changes: 0 additions & 34 deletions src/Types/ApplePayTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
29 changes: 29 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
7 changes: 3 additions & 4 deletions src/hyper-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ open ErrorUtils
open Identity
open Utils
open EventListenerManager
open ApplePayTypes

type trustPayFunctions = {
finishApplePaymentV2: (string, paymentRequestData) => promise<JSON.t>,
finishApplePaymentV2: (string, ApplePayTypes.paymentRequestData) => promise<JSON.t>,
executeGooglePayment: (string, GooglePayType.paymentDataRequest) => promise<JSON.t>,
}
@new external trustPayApi: JSON.t => trustPayFunctions = "TrustPayApi"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -938,7 +937,7 @@ let make = (
intermediatePaymentData
->getDictFromJson
->getDictFromDict("shippingAddress")
->billingContactItemToObjMapper
->ApplePayTypes.billingContactItemToObjMapper
let newShippingAddress =
[
("state", shippingAddress.administrativeArea->JSON.Encode.string),
Expand Down

0 comments on commit 8a2aef0

Please sign in to comment.