Skip to content

Commit

Permalink
refactor: HS-111: Resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrishab Srivatsa committed Feb 19, 2024
1 parent ec7f6cd commit e47561d
Showing 1 changed file with 35 additions and 89 deletions.
124 changes: 35 additions & 89 deletions src/orca-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ let make = (publishableKey, options: option<Js.Json.t>, analyticsInfo: option<Js
[("paymentIntent", data)]->Js.Dict.fromArray->Js.Json.object_->Promise.resolve
})
}
let confirmPayment = payload => {

let confirmPaymentWrapper = (payload, isOneClick, result) => {
let confirmParams =
payload
->Js.Json.decodeObject
Expand All @@ -267,24 +268,6 @@ let make = (publishableKey, options: option<Js.Json.t>, analyticsInfo: option<Js
->Belt.Option.getWithDefault("")

Js.Promise.make((~resolve, ~reject as _) => {
iframeRef.contents->Js.Array2.forEach(ifR => {
ifR->Window.iframePostMessage(
[
("doSubmit", true->Js.Json.boolean),
("clientSecret", clientSecret.contents->Js.Json.string),
(
"confirmParams",
[
("return_url", url->Js.Json.string),
("publishableKey", publishableKey->Js.Json.string),
]
->Js.Dict.fromArray
->Js.Json.object_,
),
]->Js.Dict.fromArray,
)
})

let handleMessage = (event: Types.event) => {
let json = event.data->eventToJson
let dict = json->getDictFromJson
Expand All @@ -308,73 +291,15 @@ let make = (publishableKey, options: option<Js.Json.t>, analyticsInfo: option<Js
->Js.Dict.get("url")
->Belt.Option.flatMap(Js.Json.decodeString)
->Belt.Option.getWithDefault(url)
if (
val->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false) &&
redirect === "always"
) {
Window.replace(returnUrl)
} else if !(val->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false)) {
resolve(. json)
} else {
resolve(. data)
}
| None => ()
}
}

addSmartEventListener("message", handleMessage, "onSubmit")
})
}

let confirmOneClickPayment = (payload, result: bool) => {
let confirmParams =
payload
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("confirmParams"))
->Belt.Option.getWithDefault(Js.Dict.empty()->Js.Json.object_)
let redirect =
payload
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("redirect"))
->Belt.Option.flatMap(Js.Json.decodeString)
->Belt.Option.getWithDefault("if_required")
let url =
confirmParams
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("return_url"))
->Belt.Option.flatMap(Js.Json.decodeString)
->Belt.Option.getWithDefault("")
Js.Promise.make((~resolve, ~reject as _) => {
let handleMessage = (event: Types.event) => {
let json = event.data->eventToJson
let dict = json->getDictFromJson

switch dict->Js.Dict.get("submitSuccessful") {
| Some(val) =>
logApi(
~type_="method",
~optLogger=Some(logger),
~result=val,
~paymentMethod="confirmPayment",
~eventName=CONFIRM_PAYMENT,
(),
)
let data =
dict
->Js.Dict.get("data")
->Belt.Option.getWithDefault(Js.Dict.empty()->Js.Json.object_)
let returnUrl =
dict
->Js.Dict.get("url")
->Belt.Option.flatMap(Js.Json.decodeString)
->Belt.Option.getWithDefault(url)

iframeRef.contents->Js.Array2.forEach(ifR => {
// to unset loader
ifR->Window.iframePostMessage(
[("oneClickDoSubmit", false->Js.Json.boolean)]->Js.Dict.fromArray,
)
})
if isOneClick {
iframeRef.contents->Js.Array2.forEach(ifR => {
// to unset one click button loader
ifR->Window.iframePostMessage(
[("oneClickDoSubmit", false->Js.Json.boolean)]->Js.Dict.fromArray,
)
})
}

if (
val->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false) &&
Expand All @@ -389,15 +314,36 @@ let make = (publishableKey, options: option<Js.Json.t>, analyticsInfo: option<Js
| None => ()
}
}
let message = isOneClick
? [("oneClickDoSubmit", result->Js.Json.boolean)]->Js.Dict.fromArray
: [
("doSubmit", true->Js.Json.boolean),
("clientSecret", clientSecret.contents->Js.Json.string),
(
"confirmParams",
[
("return_url", url->Js.Json.string),
("publishableKey", publishableKey->Js.Json.string),
]
->Js.Dict.fromArray
->Js.Json.object_,
),
]->Js.Dict.fromArray
addSmartEventListener("message", handleMessage, "onSubmit")
iframeRef.contents->Js.Array2.forEach(ifR => {
ifR->Window.iframePostMessage(
[("oneClickDoSubmit", result->Js.Json.boolean)]->Js.Dict.fromArray,
)
ifR->Window.iframePostMessage(message)
})
addSmartEventListener("message", handleMessage, "onSubmit")
})
}

let confirmPayment = payload => {
confirmPaymentWrapper(payload, false, true)
}

let confirmOneClickPayment = (payload, result: bool) => {
confirmPaymentWrapper(payload, true, result)
}

let elements = elementsOptions => {
logger.setLogInfo(~value="orca.elements called", ~eventName=ORCA_ELEMENTS_CALLED, ())
let clientSecretId =
Expand Down

0 comments on commit e47561d

Please sign in to comment.