Skip to content

Commit

Permalink
Merge branch 'main' into cypress-automation-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanskar2001 authored Nov 7, 2024
2 parents 2bce06c + efc7820 commit 157750c
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## [0.101.4](https://github.com/juspay/hyperswitch-web/compare/v0.101.3...v0.101.4) (2024-11-07)

## [0.101.3](https://github.com/juspay/hyperswitch-web/compare/v0.101.2...v0.101.3) (2024-11-07)

## [0.101.2](https://github.com/juspay/hyperswitch-web/compare/v0.101.1...v0.101.2) (2024-11-07)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orca-payment-page",
"version": "0.101.3",
"version": "0.101.4",
"main": "index.js",
"private": true,
"dependencies": {
Expand Down
14 changes: 14 additions & 0 deletions src/Hooks/UtilityHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ let useSendEventsToParent = eventsToSendToParent => {
Some(() => {Window.removeEventListener("message", handle)})
})
}

let useUpdateShouldUseTopRedirection = () => {
let setShouldUseTopRedirection = Recoil.useSetRecoilState(RecoilAtoms.shouldUseTopRedirectionAtom)
let updateTopRedirectionAtom = paymentOptions => {
paymentOptions
->Dict.get("shouldUseTopRedirection")
->Option.flatMap(JSON.Decode.bool)
->Option.map(useTop => {
setShouldUseTopRedirection(_ => useTop)
})
->ignore
}
updateTopRedirectionAtom
}
8 changes: 8 additions & 0 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
}
}

let updateShouldUseTopRedirection = UtilityHooks.useUpdateShouldUseTopRedirection()

React.useEffect0(() => {
messageParentWindow([("iframeMounted", true->JSON.Encode.bool)])
messageParentWindow([
Expand Down Expand Up @@ -275,6 +277,9 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
})
logger.setClientSecret(clientSecret)

// Update top redirection atom
updateShouldUseTopRedirection(paymentOptions)

switch getThemePromise(paymentOptions) {
| Some(promise) =>
promise->then(res => {
Expand Down Expand Up @@ -322,6 +327,9 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
})
logger.setClientSecret(clientSecret)

// Update top redirection atom
updateShouldUseTopRedirection(paymentOptions)

switch getThemePromise(paymentOptions) {
| Some(promise) =>
promise->then(res => {
Expand Down
15 changes: 14 additions & 1 deletion src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ let rec intentCall = (
~isPaymentSession=false,
~isCallbackUsedVal=?,
~componentName="payment",
~shouldUseTopRedirection,
) => {
open Promise
let isConfirm = uri->String.includes("/confirm")
Expand Down Expand Up @@ -322,7 +323,7 @@ let rec intentCall = (
)
let handleOpenUrl = url => {
if isPaymentSession {
Window.replaceRootHref(url)
Window.replaceRootHref(url, shouldUseTopRedirection)
} else {
openUrl(url)
}
Expand Down Expand Up @@ -448,6 +449,7 @@ let rec intentCall = (
~sdkHandleOneClickConfirmPayment,
~counter=counter + 1,
~componentName,
~shouldUseTopRedirection,
)
->then(
res => {
Expand Down Expand Up @@ -875,6 +877,7 @@ let rec intentCall = (
~counter=counter + 1,
~isPaymentSession,
~componentName,
~shouldUseTopRedirection,
)
->then(
res => {
Expand Down Expand Up @@ -905,6 +908,7 @@ let usePaymentSync = (optLogger: option<HyperLogger.loggerMake>, paymentType: pa
let keys = Recoil.useRecoilValueFromAtom(keys)
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)
let customPodUri = Recoil.useRecoilValueFromAtom(customPodUri)
let shouldUseTopRedirection = Recoil.useRecoilValueFromAtom(shouldUseTopRedirectionAtom)
let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
(~handleUserError=false, ~confirmParam: ConfirmType.confirmParams, ~iframeId="") => {
switch keys.clientSecret {
Expand Down Expand Up @@ -932,6 +936,7 @@ let usePaymentSync = (optLogger: option<HyperLogger.loggerMake>, paymentType: pa
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
~isCallbackUsedVal,
~shouldUseTopRedirection,
)->ignore
}
switch paymentMethodList {
Expand Down Expand Up @@ -979,6 +984,7 @@ let usePaymentIntent = (optLogger, paymentType) => {
let paymentMethodList = Recoil.useRecoilValueFromAtom(paymentMethodList)
let keys = Recoil.useRecoilValueFromAtom(keys)
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)
let shouldUseTopRedirection = Recoil.useRecoilValueFromAtom(shouldUseTopRedirectionAtom)

let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
(
Expand Down Expand Up @@ -1074,6 +1080,7 @@ let usePaymentIntent = (optLogger, paymentType) => {
~counter=0,
~isCallbackUsedVal,
~componentName,
~shouldUseTopRedirection,
)
->then(val => {
intentCallback(val)
Expand Down Expand Up @@ -1159,6 +1166,7 @@ let useCompleteAuthorize = (optLogger: option<HyperLogger.loggerMake>, paymentTy
let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
let url = RescriptReactRouter.useUrl()
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)
let shouldUseTopRedirection = Recoil.useRecoilValueFromAtom(shouldUseTopRedirectionAtom)
let paymentTypeFromUrl =
CardUtils.getQueryParamsDictforKey(url.search, "componentName")->CardThemeType.getPaymentMode
(
Expand Down Expand Up @@ -1203,6 +1211,7 @@ let useCompleteAuthorize = (optLogger: option<HyperLogger.loggerMake>, paymentTy
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
~isCallbackUsedVal,
~shouldUseTopRedirection,
)->ignore
}
switch paymentMethodList {
Expand Down Expand Up @@ -1595,6 +1604,7 @@ let paymentIntentForPaymentSession = (
~clientSecret,
~logger,
~customPodUri,
~shouldUseTopRedirection,
) => {
let confirmParams =
payload
Expand Down Expand Up @@ -1651,6 +1661,7 @@ let paymentIntentForPaymentSession = (
~sdkHandleOneClickConfirmPayment=false,
~counter=0,
~isPaymentSession=true,
~shouldUseTopRedirection,
)
}

Expand Down Expand Up @@ -2087,6 +2098,7 @@ let usePostSessionTokens = (
let customPodUri = Recoil.useRecoilValueFromAtom(customPodUri)
let paymentMethodList = Recoil.useRecoilValueFromAtom(paymentMethodList)
let keys = Recoil.useRecoilValueFromAtom(keys)
let shouldUseTopRedirection = Recoil.useRecoilValueFromAtom(RecoilAtoms.shouldUseTopRedirectionAtom)

let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
(
Expand Down Expand Up @@ -2178,6 +2190,7 @@ let usePostSessionTokens = (
~customPodUri,
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
~shouldUseTopRedirection,
)
->then(val => {
intentCallback(val)
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/RecoilAtoms.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let sessionId = Recoil.atom("sessionId", "")
let isConfirmBlocked = Recoil.atom("isConfirmBlocked", false)
let customPodUri = Recoil.atom("customPodUri", "")
let selectedOptionAtom = Recoil.atom("selectedOption", "")
let shouldUseTopRedirectionAtom = Recoil.atom("shouldUseTopRedirection", false)
let paymentTokenAtom = Recoil.atom(
"paymentToken",
{
Expand Down
19 changes: 18 additions & 1 deletion src/Window.res
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,21 @@ let getRootHostName = () =>
}

/* Redirect Handling */
let replaceRootHref = (href: string) => Location.replace(href)
let replaceRootHref = (href: string, shouldUseTopRedirection: bool) => {
switch shouldUseTopRedirection {
| true =>
try {
Top.Location.replace(href)
} catch {
| e => {
Js.Console.error3(
"Failed to redirect root document",
e,
`Using [window.location.replace] for redirection`,
)
Location.replace(href)
}
}
| false => Location.replace(href)
}
}
12 changes: 9 additions & 3 deletions src/hyper-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let make = (
~logger: option<HyperLogger.loggerMake>,
~analyticsMetadata,
~customBackendUrl,
~shouldUseTopRedirection,
) => {
try {
let iframeRef = []
Expand Down Expand Up @@ -310,6 +311,7 @@ let make = (
("locale", locale),
("loader", loader),
("fonts", fonts),
("shouldUseTopRedirection", shouldUseTopRedirection->JSON.Encode.bool),
]->getJsonFromArrayOfJson
let message = [
(
Expand Down Expand Up @@ -667,7 +669,7 @@ let make = (
let returnUrl = dict->getString("return_url", "")
let redirectUrl = `${returnUrl}?payment_intent_client_secret=${clientSecret}&status=${status}`
if redirect.contents === "always" {
Window.replaceRootHref(redirectUrl)
Window.replaceRootHref(redirectUrl, shouldUseTopRedirection)
resolve(JSON.Encode.null)
} else {
messageCurrentWindow([
Expand Down Expand Up @@ -695,7 +697,7 @@ let make = (

let handleErrorResponse = err => {
if redirect.contents === "always" {
Window.replaceRootHref(url)
Window.replaceRootHref(url, shouldUseTopRedirection)
}
messageCurrentWindow([
("submitSuccessful", false->JSON.Encode.bool),
Expand Down Expand Up @@ -756,7 +758,10 @@ let make = (
->then(json => json->handleRetrievePaymentResponse)
->catch(err => {
if redirect.contents === "always" {
Window.replaceRootHref(redirectUrl->JSON.Decode.string->Option.getOr(""))
Window.replaceRootHref(
redirectUrl->JSON.Decode.string->Option.getOr(""),
shouldUseTopRedirection,
)
resolve(JSON.Encode.null)
} else {
messageCurrentWindow([
Expand Down Expand Up @@ -1157,6 +1162,7 @@ let make = (
setElementIframeRef,
iframeRef,
mountPostMessage,
~shouldUseTopRedirection,
)
savedPaymentElement->Dict.set(componentType, paymentElement)
paymentElement
Expand Down
13 changes: 10 additions & 3 deletions src/hyper-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
->Option.getOr(JSON.Encode.null)
->Utils.getDictFromJson
->Utils.getBool("isPreloadEnabled", true)
let shouldUseTopRedirection =
options
->Option.getOr(JSON.Encode.null)
->Utils.getDictFromJson
->Utils.getBool("shouldUseTopRedirection", false)
let analyticsMetadata =
options
->Option.getOr(JSON.Encode.null)
Expand Down Expand Up @@ -393,9 +398,9 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
let submitSuccessfulValue = val->JSON.Decode.bool->Option.getOr(false)

if isSdkButton && submitSuccessfulValue {
Window.replaceRootHref(returnUrl)
Window.replaceRootHref(returnUrl, shouldUseTopRedirection)
} else if submitSuccessfulValue && redirect === "always" {
Window.replaceRootHref(returnUrl)
Window.replaceRootHref(returnUrl, shouldUseTopRedirection)
} else if !submitSuccessfulValue {
resolve1(json)
} else {
Expand Down Expand Up @@ -487,6 +492,7 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
->Option.getOr(JSON.Encode.null)
->getDictFromJson
->getString("customBackendUrl", ""),
~shouldUseTopRedirection,
)
}

Expand Down Expand Up @@ -572,7 +578,7 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
)
let url = decodedData->getString("return_url", "/")
if val->JSON.Decode.bool->Option.getOr(false) && url !== "/" {
Window.replaceRootHref(url)
Window.replaceRootHref(url, shouldUseTopRedirection)
} else {
resolve(json)
}
Expand Down Expand Up @@ -669,6 +675,7 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
~publishableKey,
~logger=Some(logger),
~ephemeralKey=ephemeralKey.contents,
~shouldUseTopRedirection,
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/hyper-loader/LoaderPaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let make = (
iframeRef,
mountPostMessage,
~isPaymentManagementElement=false,
~shouldUseTopRedirection,
) => {
try {
let mountId = ref("")
Expand Down Expand Up @@ -243,7 +244,7 @@ let make = (
switch eventDataObject->getOptionalJsonFromJson("openurl") {
| Some(val) => {
let url = val->getStringFromJson("")
Window.replaceRootHref(url)
Window.replaceRootHref(url, shouldUseTopRedirection)
}
| None => ()
}
Expand Down
1 change: 1 addition & 0 deletions src/hyper-loader/PaymentMethodsManagementElements.res
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ let make = (
iframeRef,
mountPostMessage,
~isPaymentManagementElement=true,
~shouldUseTopRedirection=false,
)
savedPaymentElement->Dict.set(componentType, paymentElement)
paymentElement
Expand Down
2 changes: 2 additions & 0 deletions src/hyper-loader/PaymentSession.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let make = (
~publishableKey,
~logger: option<HyperLogger.loggerMake>,
~ephemeralKey,
~shouldUseTopRedirection,
) => {
let logger = logger->Option.getOr(HyperLogger.defaultLoggerConfig)
let customPodUri =
Expand All @@ -24,6 +25,7 @@ let make = (
~endpoint,
~logger,
~customPodUri,
~shouldUseTopRedirection,
),
getPaymentManagementMethods: _ =>
PaymentSessionMethods.getPaymentManagementMethods(
Expand Down
Loading

0 comments on commit 157750c

Please sign in to comment.