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: paze express checkout for multiple iframe #789

Merged
merged 4 commits into from
Nov 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
16 changes: 2 additions & 14 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ let mergeAndFlattenToTuples = (body, requiredFieldsBody) =>
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict

let sendMessageToIframe = (~msg, ~componentName, ~eventSource=None, ~mountedIframeRef=None) => {
let handleIframePostMessageForWallets = (msg, componentName, mountedIframeRef) => {
let isMessageSent = ref(false)
let iframes = Window.querySelectorAll("iframe")

Expand All @@ -1448,18 +1448,6 @@ let sendMessageToIframe = (~msg, ~componentName, ~eventSource=None, ~mountedIfra
})

if !isMessageSent.contents {
switch (eventSource, mountedIframeRef) {
| (Some(source), _) => source->Window.sendPostMessage(msg)
| (None, Some(ref)) => ref->Window.iframePostMessage(msg)
| (None, None) => ()
}
mountedIframeRef->Window.iframePostMessage(msg)
}
}

let handleApplePayIframePostMessage = (msg, componentName, mountedIframeRef) => {
sendMessageToIframe(~msg, ~componentName, ~mountedIframeRef=Some(mountedIframeRef))
}

let handlePazeIframePostMessage = (msg, componentName, eventSource) => {
sendMessageToIframe(~msg, ~componentName, ~eventSource=Some(eventSource))
}
24 changes: 13 additions & 11 deletions src/hyper-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ let make = (
}
}

let onPazeCallback = (event: Types.event) => {
let json = event.data->Identity.anyTypeToJson
let dict = json->getDictFromJson
if dict->getBool("isPaze", false) {
let componentName = dict->getString("componentName", "payment")
let msg = [("data", json)]->Dict.fromArray
handlePazeIframePostMessage(msg, componentName, event.source)
let onPazeCallback = mountedIframeRef => {
(event: Types.event) => {
let json = event.data->Identity.anyTypeToJson
let dict = json->getDictFromJson
if dict->getBool("isPaze", false) {
let componentName = dict->getString("componentName", "payment")
let msg = [("data", json)]->Dict.fromArray
handleIframePostMessageForWallets(msg, componentName, mountedIframeRef)
}
}
}

Expand All @@ -148,7 +150,7 @@ let make = (
isTaxCalculationEnabled.contents =
dict->getDictFromDict("response")->getBool("is_tax_calculation_enabled", false)
addSmartEventListener("message", onPlaidCallback(mountedIframeRef), "onPlaidCallback")
addSmartEventListener("message", onPazeCallback, "onPazeCallback")
addSmartEventListener("message", onPazeCallback(mountedIframeRef), "onPazeCallback")

let json = dict->getJsonFromDict("response", JSON.Encode.null)
let isApplePayPresent = PaymentMethodsRecord.getPaymentMethodTypeFromList(
Expand Down Expand Up @@ -401,7 +403,7 @@ let make = (
try {
let msg = [("applePayCanMakePayments", true->JSON.Encode.bool)]->Dict.fromArray

handleApplePayIframePostMessage(msg, componentName, mountedIframeRef)
handleIframePostMessageForWallets(msg, componentName, mountedIframeRef)
} catch {
| exn => {
let exnString = exn->anyTypeToJson->JSON.stringify
Expand Down Expand Up @@ -895,13 +897,13 @@ let make = (
("applePayShippingContact", shippingContact),
]->Dict.fromArray

handleApplePayIframePostMessage(msg, componentName, mountedIframeRef)
handleIframePostMessageForWallets(msg, componentName, mountedIframeRef)
}

if dict->Dict.get("showApplePayButton")->Option.isSome {
let msg = [("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray

handleApplePayIframePostMessage(msg, componentName, mountedIframeRef)
handleIframePostMessageForWallets(msg, componentName, mountedIframeRef)
}
}

Expand Down
Loading