From 81d3c8e04a090f52c9071ed3e75f6cfbee5aedef Mon Sep 17 00:00:00 2001 From: Vrishab Srivatsa Date: Wed, 24 Apr 2024 19:36:01 +0530 Subject: [PATCH 1/5] fix: added breakpoints to debug --- src/Payments/ApplePay.res | 3 ++- src/orca-loader/Elements.res | 49 ++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/Payments/ApplePay.res b/src/Payments/ApplePay.res index a3a985a23..c10026aa5 100644 --- a/src/Payments/ApplePay.res +++ b/src/Payments/ApplePay.res @@ -309,7 +309,8 @@ let make = ( postFailedSubmitResponse(~errortype="server_error", ~message="Something went wrong") } } else if dict->Dict.get("applePaySyncPayment")->Option.isSome { - syncPayment() + () + // syncPayment(breakpoint) } } catch { | _ => Utils.logInfo(Console.log("Error in parsing Apple Pay Data")) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 37716fc43..6c206410c 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -4,6 +4,7 @@ open Identity open Utils open EventListenerManager open ApplePayTypes +@send external alert: ('t, string) => unit = "alert" type trustPayFunctions = { finishApplePaymentV2: (string, paymentRequestData) => Promise.t, @@ -462,7 +463,6 @@ let make = ( addSmartEventListener("message", handleApplePayMounted, "onApplePayMount") addSmartEventListener("message", handlePollStatusMessage, "onPollStatusMsg") addSmartEventListener("message", handleGooglePayThirdPartyFlow, "onGooglePayThirdParty") - Window.removeEventListener("message", handleApplePayMessages.contents) let fetchSessionTokens = mountedIframeRef => { let handleSessionTokensLoaded = (event: Types.event) => { @@ -536,6 +536,11 @@ let make = ( ->JSON.Decode.bool ->Belt.Option.getWithDefault(false) + Window.window->alert( + "isDelayedSessionToken: " ++ + isDelayedSessionToken->anyTypeToJson->JSON.stringify, + ) + if isDelayedSessionToken { logger.setLogInfo( ~value="Delayed Session Token Flow", @@ -544,19 +549,28 @@ let make = ( (), ) - let applePayPresent = + let applePaySessionTokenData = dict ->Dict.get("applePayPresent") ->Belt.Option.flatMap(JSON.Decode.object) ->Belt.Option.getWithDefault(Dict.make()) + Window.window->alert( + "applePaySessionTokenData: " ++ + applePaySessionTokenData->anyTypeToJson->JSON.stringify, + ) + let connector = - applePayPresent + applePaySessionTokenData ->Dict.get("connector") ->Belt.Option.getWithDefault(JSON.Encode.null) ->JSON.Decode.string ->Belt.Option.getWithDefault("") + Window.window->alert( + "connector: " ++ connector->anyTypeToJson->JSON.stringify, + ) + switch connector { | "trustpay" => logger.setLogInfo( @@ -566,7 +580,7 @@ let make = ( (), ) let secrets = - applePayPresent + applePaySessionTokenData ->Dict.get("session_token_data") ->Belt.Option.getWithDefault(JSON.Encode.null) ->JSON.Decode.object @@ -574,13 +588,22 @@ let make = ( ->Dict.get("secrets") ->Belt.Option.getWithDefault(JSON.Encode.null) + Window.window->alert( + "secrets: " ++ secrets->anyTypeToJson->JSON.stringify, + ) + let paymentRequest = - applePayPresent + applePaySessionTokenData ->Dict.get("payment_request_data") ->Belt.Option.flatMap(JSON.Decode.object) ->Belt.Option.getWithDefault(Dict.make()) ->ApplePayTypes.jsonToPaymentRequestDataType + Window.window->alert( + "paymentRequest: " ++ + paymentRequest->anyTypeToJson->JSON.stringify, + ) + let payment = secrets ->JSON.Decode.object @@ -590,10 +613,17 @@ let make = ( ->JSON.Decode.string ->Belt.Option.getWithDefault("") + Window.window->alert( + "payment: " ++ payment->anyTypeToJson->JSON.stringify, + ) + try { let trustpay = trustPayApi(secrets) trustpay.finishApplePaymentV2(payment, paymentRequest) ->then(res => { + Window.window->alert( + "res: " ++ res->anyTypeToJson->JSON.stringify, + ) logger.setLogInfo( ~value="TrustPay ApplePay Success Response", ~internalMetadata=res->JSON.stringify, @@ -604,6 +634,7 @@ let make = ( let msg = [ ("applePaySyncPayment", true->JSON.Encode.bool), + ("breakpoint", "1"->JSON.Encode.string), ]->Dict.fromArray mountedIframeRef->Window.iframePostMessage(msg) logger.setLogInfo( @@ -617,6 +648,9 @@ let make = ( ->catch(err => { let exceptionMessage = err->Utils.formatException->JSON.stringify + Window.window->alert( + "err: " ++ err->anyTypeToJson->JSON.stringify, + ) logger.setLogInfo( ~eventName=APPLE_PAY_FLOW, ~paymentMethod="APPLE_PAY", @@ -626,6 +660,7 @@ let make = ( let msg = [ ("applePaySyncPayment", true->JSON.Encode.bool), + ("breakpoint", "2"->JSON.Encode.string), ]->Dict.fromArray mountedIframeRef->Window.iframePostMessage(msg) resolve() @@ -639,9 +674,13 @@ let make = ( ~paymentMethod="APPLE_PAY", (), ) + Window.window->alert( + "exn: " ++ exn->Utils.formatException->JSON.stringify, + ) let msg = [ ("applePaySyncPayment", true->JSON.Encode.bool), + ("breakpoint", "3"->JSON.Encode.string), ]->Dict.fromArray mountedIframeRef->Window.iframePostMessage(msg) } From 0482811d10fa20c90552b7a7143b856054da4529 Mon Sep 17 00:00:00 2001 From: Vrishab Srivatsa Date: Thu, 25 Apr 2024 01:00:43 +0530 Subject: [PATCH 2/5] fix: load Trustpay Script only after all API data is sent to Elements from PremountLoader --- src/orca-loader/Elements.res | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 6c206410c..4f80dc523 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -23,6 +23,17 @@ 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 { + Console.log("Lets load Trustpay script") + resolve() + } + } + addSmartEventListener("message", handleOnReady, "handleOnReadyToLoadTrustpayScript") + }) try { let iframeRef = [] @@ -144,13 +155,18 @@ 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") + trustPayScript->Window.elementSrc(trustPayScriptURL) + trustPayScript->Window.elementOnerror(err => { + Utils.logInfo(Console.log2("ERROR DURING LOADING TRUSTPAY APPLE PAY", err)) + }) + Window.body->Window.appendChild(trustPayScript) + logger.setLogInfo(~value="TrustPay Script Loaded", ~eventName=TRUSTPAY_SCRIPT, ()) + Promise.resolve() }) - Window.body->Window.appendChild(trustPayScript) - logger.setLogInfo(~value="TrustPay Script Loaded", ~eventName=TRUSTPAY_SCRIPT, ()) + ->ignore } } let msg = [("paymentMethodList", json)]->Dict.fromArray From 35d17a877a6c628578a354e099ce4cdabbec50e9 Mon Sep 17 00:00:00 2001 From: Vrishab Srivatsa Date: Thu, 25 Apr 2024 01:19:18 +0530 Subject: [PATCH 3/5] fix: load Trustpay Script only after all API data is sent to Elements --- src/orca-loader/Elements.res | 1 - 1 file changed, 1 deletion(-) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 4f80dc523..350e60592 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -28,7 +28,6 @@ let make = ( let json = event.data->anyTypeToJson let dict = json->getDictFromJson if dict->Dict.get("preMountLoaderIframeUnMount")->Belt.Option.isSome { - Console.log("Lets load Trustpay script") resolve() } } From 07a7495ecbf7ec28bb609b38cf06503c4761ac66 Mon Sep 17 00:00:00 2001 From: Vrishab Srivatsa Date: Thu, 25 Apr 2024 13:51:15 +0530 Subject: [PATCH 4/5] fix: added alert on trustpayapi init, log onload of script --- src/orca-loader/Elements.res | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 350e60592..b738cbc4f 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -151,18 +151,21 @@ let make = ( Window.querySelectorAll(`script[src="https://test-tpgw.trustpay.eu/js/v1.js"]`)->Array.length === 0 ) { let trustPayScriptURL = - publishableKey->String.startsWith("pk_prd_") + publishableKey->String.startsWith("pk_prd_")->not ? "https://tpgw.trustpay.eu/js/v1.js" : "https://test-tpgw.trustpay.eu/js/v1.js" 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) - logger.setLogInfo(~value="TrustPay Script Loaded", ~eventName=TRUSTPAY_SCRIPT, ()) Promise.resolve() }) ->ignore @@ -634,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( From ae7b13b684f626a2356f632b9e6e312bf7af681f Mon Sep 17 00:00:00 2001 From: Vrishab Srivatsa <136090360+vsrivatsa-juspay@users.noreply.github.com> Date: Thu, 25 Apr 2024 13:56:09 +0530 Subject: [PATCH 5/5] fix: Update src/orca-loader/Elements.res --- src/orca-loader/Elements.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index b738cbc4f..da5a0f063 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -151,7 +151,7 @@ let make = ( Window.querySelectorAll(`script[src="https://test-tpgw.trustpay.eu/js/v1.js"]`)->Array.length === 0 ) { let trustPayScriptURL = - publishableKey->String.startsWith("pk_prd_")->not + publishableKey->String.startsWith("pk_prd_") ? "https://tpgw.trustpay.eu/js/v1.js" : "https://test-tpgw.trustpay.eu/js/v1.js" isReadyToLoadTrustpayPromise