diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 6c206410c..da5a0f063 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -23,6 +23,16 @@ let make = ( ) => { let handleApplePayMessages = ref(_ => ()) let applePaySessionRef = ref(Nullable.null) + let isReadyToLoadTrustpayPromise = Promise.make((resolve, _) => { + let handleOnReady = (event: Types.event) => { + let json = event.data->anyTypeToJson + let dict = json->getDictFromJson + if dict->Dict.get("preMountLoaderIframeUnMount")->Belt.Option.isSome { + resolve() + } + } + addSmartEventListener("message", handleOnReady, "handleOnReadyToLoadTrustpayScript") + }) try { let iframeRef = [] @@ -144,13 +154,21 @@ let make = ( publishableKey->String.startsWith("pk_prd_") ? "https://tpgw.trustpay.eu/js/v1.js" : "https://test-tpgw.trustpay.eu/js/v1.js" - let trustPayScript = Window.createElement("script") - trustPayScript->Window.elementSrc(trustPayScriptURL) - trustPayScript->Window.elementOnerror(err => { - Utils.logInfo(Console.log2("ERROR DURING LOADING TRUSTPAY APPLE PAY", err)) + isReadyToLoadTrustpayPromise + ->Promise.then(_ => { + let trustPayScript = Window.createElement("script") + logger.setLogInfo(~value="TrustPay Script Loading", ~eventName=TRUSTPAY_SCRIPT, ()) + trustPayScript->Window.elementSrc(trustPayScriptURL) + trustPayScript->Window.elementOnerror(err => { + Utils.logInfo(Console.log2("ERROR DURING LOADING TRUSTPAY APPLE PAY", err)) + }) + trustPayScript->Window.elementOnload(_ => { + logger.setLogInfo(~value="TrustPay Script Loaded", ~eventName=TRUSTPAY_SCRIPT, ()) + }) + Window.body->Window.appendChild(trustPayScript) + Promise.resolve() }) - Window.body->Window.appendChild(trustPayScript) - logger.setLogInfo(~value="TrustPay Script Loaded", ~eventName=TRUSTPAY_SCRIPT, ()) + ->ignore } } let msg = [("paymentMethodList", json)]->Dict.fromArray @@ -619,6 +637,9 @@ let make = ( try { let trustpay = trustPayApi(secrets) + Window.window->alert( + "trustpay: " ++ trustpay->anyTypeToJson->JSON.stringify, + ) trustpay.finishApplePaymentV2(payment, paymentRequest) ->then(res => { Window.window->alert(