Skip to content

Commit

Permalink
feat: HS-1111: Moved HTTP requests to preMountLoader iframe"
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrishab Srivatsa committed Feb 26, 2024
1 parent e8a512c commit 4d2ac42
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 520 deletions.
6 changes: 3 additions & 3 deletions aws/hyperswitch_web_aws_production_deployment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ echo $( (aws s3api put-bucket-policy --bucket $MY_AWS_S3_BUCKET_NAME --policy "$

echo "Bucket configuration updated"

echo "Enter the backend endpoint your Hyperswitch Client will hit (hosted Hyperswitch Backend, https://sandbox.hyperswitch.io is taken by default):"
echo "Enter the backend endpoint your Hyperswitch Client will hit (hosted Hyperswitch Backend, https://beta.hyperswitch.io/api is taken by default):"
read AWS_BACKEND_URL </dev/tty

if [ -z $AWS_BACKEND_URL ]; then
echo "Setting backend URL value to https://sandbox.hyperswitch.io by default"
AWS_BACKEND_URL="https://sandbox.hyperswitch.io"
echo "Setting backend URL value to https://beta.hyperswitch.io/api by default"
AWS_BACKEND_URL="https://beta.hyperswitch.io/api"

fi

Expand Down
12 changes: 6 additions & 6 deletions aws/hyperswitch_web_aws_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ command_discovery() {
command_discovery curl
command_discovery aws

echo "Please enter the URL where you have hosted Hyperswitch Backend (https://sandbox.hyperswitch.io) "
echo "Please enter the URL where you have hosted Hyperswitch Backend (https://beta.hyperswitch.io/api) "
read HYPERSWITCH_SERVER_URL < /dev/tty

if [ -z "$HYPERSWITCH_SERVER_URL" ]; then
echo "Using default api url: https://sandbox.hyperswitch.io"
HYPERSWITCH_SERVER_URL="https://sandbox.hyperswitch.io"
echo "Using default api url: https://beta.hyperswitch.io/api"
HYPERSWITCH_SERVER_URL="https://beta.hyperswitch.io/api"
fi

# echo "Please enter the api url (https://sandbox.hyperswitch.io) "
# echo "Please enter the api url (https://beta.hyperswitch.io/api) "
# read apiBaseUrl < /dev/tty

# if [ -z "$apiBaseUrl" ]; then
# echo "Using default api url: https://sandbox.hyperswitch.io"
# apiBaseUrl="https://sandbox.hyperswitch.io"
# echo "Using default api url: https://beta.hyperswitch.io/api"
# apiBaseUrl="https://beta.hyperswitch.io/api"
# fi

echo "Please enter the URL where you have hosted Hyperswitch Client SDK (https://beta.hyperswitch.io/v1) "
Expand Down
11 changes: 7 additions & 4 deletions src/App.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ let make = () => {
log
})

React.useEffect1(()=>{
setLoggerState(._=>logger)
React.useEffect1(() => {
setLoggerState(._ => logger)
None
},[logger])
}, [logger])

let renderFullscreen = {
switch fullscreenMode {
| "paymentloader" => <PaymentLoader />
| "fullscreen" => <div id="fullscreen"> <FullScreenDivDriver /> </div>
| "fullscreen" =>
<div id="fullscreen">
<FullScreenDivDriver />
</div>
| "qrData" => <QRCodeDisplay />
| "preMountLoader" => {
let clientSecret = CardUtils.getQueryParamsDictforKey(url.search, "clientSecret")
Expand Down
173 changes: 98 additions & 75 deletions src/Payments/PreMountLoader.res
Original file line number Diff line number Diff line change
@@ -1,83 +1,106 @@
@react.component
let make = (~sessionId, ~publishableKey, ~clientSecret, ~endpoint) => {
// open Utils
// let logger = OrcaLogger.make(
// ~sessionId,
// ~source=Loader,
// ~merchantId=publishableKey,
// ~clientSecret,
// (),
// )
open Utils
let (paymentMethodsResponseSent, setPaymentMethodsResponseSent) = React.useState(_ => false)
let (
customerPaymentMethodsResponseSent,
setCustomerPaymentMethodsResponseSent,
) = React.useState(_ => false)
let (sessionTokensResponseSent, setSessionTokensResponseSent) = React.useState(_ => false)
let logger = OrcaLogger.make(
~sessionId,
~source=Loader,
~merchantId=publishableKey,
~clientSecret,
(),
)

// // let (
// // paymentMethodsResponse,
// // customerPaymentMethodsResponse,
// // sessionTokensResponse,
// // ) = React.useMemo0(() => {
// // (
// // PaymentHelpers.usePaymentMethodList(
// // ~clientSecret,
// // ~publishableKey,
// // ~logger,
// // ~switchToCustomPod=false,
// // ~endpoint,
// // ),
// // PaymentHelpers.useCustomerDetails(
// // ~clientSecret,
// // ~publishableKey,
// // ~optLogger=Some(logger),
// // ~switchToCustomPod=false,
// // ~endpoint,
// // ),
// // PaymentHelpers.useSessions(
// // ~clientSecret,
// // ~publishableKey,
// // ~optLogger=Some(logger),
// // ~switchToCustomPod=false,
// // ~endpoint,
// // (),
// // ),
// // )
// // })
let (
paymentMethodsResponse,
customerPaymentMethodsResponse,
sessionTokensResponse,
) = React.useMemo0(() => {
(
PaymentHelpers.usePaymentMethodList(
~clientSecret,
~publishableKey,
~logger,
~switchToCustomPod=false,
~endpoint,
),
PaymentHelpers.useCustomerDetails(
~clientSecret,
~publishableKey,
~optLogger=Some(logger),
~switchToCustomPod=false,
~endpoint,
),
PaymentHelpers.useSessions(
~clientSecret,
~publishableKey,
~optLogger=Some(logger),
~switchToCustomPod=false,
~endpoint,
(),
),
)
})

// // let sendPromiseData = (promise, key) => {
// // open Promise
// // promise
// // ->then(res => {
// // handlePostMessage([("response", res), ("data", key->Js.Json.string)])
// // resolve()
// // })
// // ->catch(_err => {
// // handlePostMessage([("response", Js.Json.null), ("data", key->Js.Json.string)])
// // resolve()
// // })
// // ->ignore
// // }
let sendPromiseData = (promise, key) => {
open Promise
promise
->then(res => {
handlePostMessage([("response", res), ("data", key->Js.Json.string)])
resolve()
})
->catch(_err => {
handlePostMessage([("response", Js.Json.null), ("data", key->Js.Json.string)])
resolve()
})
->ignore
}

// // React.useEffect0(() => {
// // let handle = (ev: Window.event) => {
// // let json = try {
// // ev.data->Js.Json.parseExn
// // } catch {
// // | _ => Js.Json.null
// // }
// // let dict = json->Utils.getDictFromJson
// // if dict->Js.Dict.get("sendPaymentMethodsResponse")->Belt.Option.isSome {
// // paymentMethodsResponse->sendPromiseData("payment_methods")
// // } else if dict->Js.Dict.get("sendCustomerPaymentMethodsResponse")->Belt.Option.isSome {
// // customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods")
// // } else if dict->Js.Dict.get("sendSessionTokensResponse")->Belt.Option.isSome {
// // sessionTokensResponse->sendPromiseData("session_tokens")
// // }
// // }
// // Window.addEventListener("message", handle)
// // handlePostMessage([("preMountLoaderInitCallback", true->Js.Json.boolean)])
// // Some(
// // () => {
// // Window.removeEventListener("message", handle)
// // },
// // )
// // })
let handle = (ev: Window.event) => {
let json = try {
ev.data->Js.Json.parseExn
} catch {
| _ => Js.Json.null
}
Js.log("preMountHandlerEventHandler")
let dict = json->Utils.getDictFromJson
if dict->Js.Dict.get("sendPaymentMethodsResponse")->Belt.Option.isSome {
paymentMethodsResponse->sendPromiseData("payment_methods")
setPaymentMethodsResponseSent(_ => true)
} else if dict->Js.Dict.get("sendCustomerPaymentMethodsResponse")->Belt.Option.isSome {
if dict->Utils.getBool("sendCustomerPaymentMethodsResponse", false) {
customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods")
}
setCustomerPaymentMethodsResponseSent(_ => true)
} else if dict->Js.Dict.get("sendSessionTokensResponse")->Belt.Option.isSome {
sessionTokensResponse->sendPromiseData("session_tokens")
setSessionTokensResponseSent(_ => true)
}
}

React.useEffect0(() => {
Window.addEventListener("message", handle)
handlePostMessage([("preMountLoaderIframeMountedCallback", true->Js.Json.boolean)])
Some(
() => {
Window.removeEventListener("message", handle)
},
)
})

React.useEffect3(() => {
if (
paymentMethodsResponseSent && customerPaymentMethodsResponseSent && sessionTokensResponseSent
) {
Js.log("Removing event listener")
Window.removeEventListener("message", handle)
}
None
}, (paymentMethodsResponseSent, customerPaymentMethodsResponseSent, sessionTokensResponseSent))

React.null
}
2 changes: 1 addition & 1 deletion src/Utilities/ApiEndpoint.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let getApiEndPoint = (~publishableKey="", ~isConfirmCall=false, ()) => {
| None =>
let backendEndPoint = isConfirmCall ? GlobalVars.confirmEndPoint : GlobalVars.backendEndPoint
if GlobalVars.isProd {
testMode ? "https://sandbox.hyperswitch.io" : backendEndPoint
testMode ? "https://beta.hyperswitch.io/api" : backendEndPoint
} else {
backendEndPoint
}
Expand Down
Loading

0 comments on commit 4d2ac42

Please sign in to comment.