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: samsungpay script mount condition correction #897

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 52 additions & 17 deletions src/hyper-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,35 @@ let make = (
addSmartEventListener("message", handleGooglePayThirdPartyFlow, "onGooglePayThirdParty")
addSmartEventListener("message", handleApplePayThirdPartyFlow, "onApplePayThirdParty")

let mountSamsungPayScript = samsungPayPresent => {
Promise.make((resolve, _) => {
if (
samsungPayPresent->Option.isSome &&
Window.querySelectorAll(`script[src="https://img.mpay.samsung.com/gsmpi/sdk/samsungpay_web_sdk.js"]`)->Array.length === 0
) {
let samsungPayScriptUrl = "https://img.mpay.samsung.com/gsmpi/sdk/samsungpay_web_sdk.js"
let samsungPayScript = Window.createElement("script")
samsungPayScript->Window.elementSrc(samsungPayScriptUrl)
samsungPayScript->Window.elementOnerror(err => {
logger.setLogError(
~value="ERROR DURING LOADING SAMSUNG PAY SCRIPT",
~eventName=SAMSUNG_PAY_SCRIPT,
~internalMetadata=err->formatException->JSON.stringify,
~paymentMethod="SAMSUNG_PAY",
)
resolve()
})
Window.body->Window.appendChild(samsungPayScript)
samsungPayScript->Window.elementOnload(_ => {
logger.setLogInfo(~value="SamsungPay Script Loaded", ~eventName=SAMSUNG_PAY_SCRIPT)
resolve()
})
} else {
resolve()
}
})
}

let fetchSessionTokens = mountedIframeRef => {
let handleSessionTokensLoaded = (event: Types.event) => {
let json = event.data->anyTypeToJson
Expand All @@ -817,20 +846,28 @@ let make = (
if sessionTokensData {
let json = dict->getJsonFromDict("response", JSON.Encode.null)

{
let sessionsArr =
json
->JSON.Decode.object
->Option.getOr(Dict.make())
->SessionsType.getSessionsTokenJson("session_token")
let sessionsArr =
json
->JSON.Decode.object
->Option.getOr(Dict.make())
->SessionsType.getSessionsTokenJson("session_token")

let samsungPayPresent = sessionsArr->Array.find(item => {
let walletName = item->getDictFromJson->getString("wallet_name", "")
walletName === "samsung_pay" || walletName === "samsungpay"
})

mountSamsungPayScript(samsungPayPresent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if samsung pay is present then only call this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I call mountSamsungPayScript inside the if block when Samsung Pay is present, I will need to duplicate the same logic in both the if and else blocks. Additionally, since a similar implementation would be required for Google Pay, it would lead to even more repeated code.

To keep the implementation clean and avoid redundancy, I believe the current approach is more maintainable. Let me know if you have any suggestions on structuring it better without introducing duplication.

Screenshot 2025-02-13 at 11 21 06 AM

->then(_ => {
let applePayPresent = sessionsArr->Array.find(item => {
let x =
item
->JSON.Decode.object
->Belt.Option.flatMap(x => {
x->Dict.get("wallet_name")
})
->Belt.Option.flatMap(
x => {
x->Dict.get("wallet_name")
},
)
->Belt.Option.flatMap(JSON.Decode.string)
->Option.getOr("")
x === "apple_pay" || x === "applepay"
Expand All @@ -844,20 +881,18 @@ let make = (
let x =
item
->JSON.Decode.object
->Belt.Option.flatMap(x => {
x->Dict.get("wallet_name")
})
->Belt.Option.flatMap(
x => {
x->Dict.get("wallet_name")
},
)
->Belt.Option.flatMap(JSON.Decode.string)
->Option.getOr("")
x === "google_pay" || x === "googlepay"
})
let samsungPayPresent = sessionsArr->Array.find(item => {
let walletName = item->getDictFromJson->getString("wallet_name", "")
walletName === "samsung_pay" || walletName === "samsungpay"
})

(json, applePayPresent, googlePayPresent, samsungPayPresent)->resolve
}
})
->then(res => {
let (json, applePayPresent, googlePayPresent, samsungPayPresent) = res
if (
Expand Down
20 changes: 0 additions & 20 deletions src/hyper-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,6 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
logger.setLogInfo(~value="GooglePay Script Loaded", ~eventName=GOOGLE_PAY_SCRIPT)
}

if (
Window.querySelectorAll(`script[src="https://img.mpay.samsung.com/gsmpi/sdk/samsungpay_web_sdk.js"]`)->Array.length === 0
) {
let samsungPayScriptUrl = "https://img.mpay.samsung.com/gsmpi/sdk/samsungpay_web_sdk.js"
let samsungPayScript = Window.createElement("script")
samsungPayScript->Window.elementSrc(samsungPayScriptUrl)
samsungPayScript->Window.elementOnerror(err => {
logger.setLogError(
~value="ERROR DURING LOADING SAMSUNG PAY SCRIPT",
~eventName=SAMSUNG_PAY_SCRIPT,
~internalMetadata=err->formatException->JSON.stringify,
~paymentMethod="SAMSUNG_PAY",
)
})
Window.body->Window.appendChild(samsungPayScript)
samsungPayScript->Window.elementOnload(_ =>
logger.setLogInfo(~value="SamsungPay Script Loaded", ~eventName=SAMSUNG_PAY_SCRIPT)
)
}

let iframeRef = ref([])
let clientSecret = ref("")
let ephemeralKey = ref("")
Expand Down
Loading