-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Moved HTTP requests to within iframe (#128)
Co-authored-by: Praful Koppalkar <[email protected]>
- Loading branch information
1 parent
dc7c372
commit 4dacb73
Showing
12 changed files
with
616 additions
and
535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,108 @@ | ||
@react.component | ||
let make = (~sessionId as _, ~publishableKey as _, ~clientSecret as _, ~endpoint as _) => { | ||
// open Utils | ||
// let logger = OrcaLogger.make( | ||
// ~sessionId, | ||
// ~source=Loader, | ||
// ~merchantId=publishableKey, | ||
// ~clientSecret, | ||
// (), | ||
// ) | ||
let make = (~sessionId, ~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.fetchPaymentMethodList( | ||
// // ~clientSecret, | ||
// // ~publishableKey, | ||
// // ~logger, | ||
// // ~switchToCustomPod=false, | ||
// // ~endpoint, | ||
// // ), | ||
// // PaymentHelpers.fetchCustomerDetails( | ||
// // ~clientSecret, | ||
// // ~publishableKey, | ||
// // ~optLogger=Some(logger), | ||
// // ~switchToCustomPod=false, | ||
// // ~endpoint, | ||
// // ), | ||
// // PaymentHelpers.fetchSessions( | ||
// // ~clientSecret, | ||
// // ~publishableKey, | ||
// // ~optLogger=Some(logger), | ||
// // ~switchToCustomPod=false, | ||
// // ~endpoint, | ||
// // (), | ||
// // ), | ||
// // ) | ||
// // }) | ||
let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey, ()) | ||
|
||
// // let sendPromiseData = (promise, key) => { | ||
// // open Promise | ||
// // promise | ||
// // ->then(res => { | ||
// // handlePostMessage([("response", res), ("data", key->JSON.Encode.string)]) | ||
// // resolve() | ||
// // }) | ||
// // ->catch(_err => { | ||
// // handlePostMessage([("response", JSON.Encode.null), ("data", key->JSON.Encode.string)]) | ||
// // resolve() | ||
// // }) | ||
// // ->ignore | ||
// // } | ||
let paymentMethodsResponse = React.useMemo0(() => | ||
PaymentHelpers.fetchPaymentMethodList( | ||
~clientSecret, | ||
~publishableKey, | ||
~logger, | ||
~switchToCustomPod=false, | ||
~endpoint, | ||
) | ||
) | ||
|
||
// // React.useEffect0(() => { | ||
// // let handle = (ev: Window.event) => { | ||
// // let json = try { | ||
// // ev.data->JSON.parseExn | ||
// // } catch { | ||
// // | _ => JSON.Encode.null | ||
// // } | ||
// // let dict = json->Utils.getDictFromJson | ||
// // if dict->Dict.get("sendPaymentMethodsResponse")->Option.isSome { | ||
// // paymentMethodsResponse->sendPromiseData("payment_methods") | ||
// // } else if dict->Dict.get("sendCustomerPaymentMethodsResponse")->Option.isSome { | ||
// // customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods") | ||
// // } else if dict->Dict.get("sendSessionTokensResponse")->Option.isSome { | ||
// // sessionTokensResponse->sendPromiseData("session_tokens") | ||
// // } | ||
// // } | ||
// // Window.addEventListener("message", handle) | ||
// // handlePostMessage([("preMountLoaderInitCallback", true->JSON.Encode.bool)]) | ||
// // Some( | ||
// // () => { | ||
// // Window.removeEventListener("message", handle) | ||
// // }, | ||
// // ) | ||
// // }) | ||
let customerPaymentMethodsResponse = React.useMemo0(() => | ||
PaymentHelpers.fetchCustomerPaymentMethodList( | ||
~clientSecret, | ||
~publishableKey, | ||
~optLogger=Some(logger), | ||
~switchToCustomPod=false, | ||
~endpoint, | ||
) | ||
) | ||
|
||
let sessionTokensResponse = React.useMemo0(() => | ||
PaymentHelpers.fetchSessions( | ||
~clientSecret, | ||
~publishableKey, | ||
~optLogger=Some(logger), | ||
~switchToCustomPod=false, | ||
~endpoint, | ||
(), | ||
) | ||
) | ||
|
||
let sendPromiseData = (promise, key) => { | ||
open Promise | ||
promise | ||
->then(res => { | ||
handlePostMessage([("response", res), ("data", key->JSON.Encode.string)]) | ||
switch key { | ||
| "payment_methods" => setPaymentMethodsResponseSent(_ => true) | ||
| "session_tokens" => setSessionTokensResponseSent(_ => true) | ||
| "customer_payment_methods" => setCustomerPaymentMethodsResponseSent(_ => true) | ||
| _ => () | ||
} | ||
resolve() | ||
}) | ||
->catch(_err => { | ||
handlePostMessage([("response", JSON.Encode.null), ("data", key->JSON.Encode.string)]) | ||
resolve() | ||
}) | ||
->ignore | ||
} | ||
|
||
let handle = (ev: Window.event) => { | ||
let json = try { | ||
ev.data->JSON.parseExn | ||
} catch { | ||
| _ => JSON.Encode.null | ||
} | ||
let dict = json->Utils.getDictFromJson | ||
if dict->Dict.get("sendPaymentMethodsResponse")->Belt.Option.isSome { | ||
paymentMethodsResponse->sendPromiseData("payment_methods") | ||
} else if dict->Dict.get("sendCustomerPaymentMethodsResponse")->Belt.Option.isSome { | ||
customerPaymentMethodsResponse->sendPromiseData("customer_payment_methods") | ||
} else if dict->Dict.get("sendSessionTokensResponse")->Belt.Option.isSome { | ||
sessionTokensResponse->sendPromiseData("session_tokens") | ||
} | ||
} | ||
|
||
React.useEffect0(() => { | ||
Window.addEventListener("message", handle) | ||
handlePostMessage([("preMountLoaderIframeMountedCallback", true->JSON.Encode.bool)]) | ||
Some( | ||
() => { | ||
Window.removeEventListener("message", handle) | ||
}, | ||
) | ||
}) | ||
|
||
React.useEffect3(() => { | ||
if ( | ||
paymentMethodsResponseSent && customerPaymentMethodsResponseSent && sessionTokensResponseSent | ||
) { | ||
handlePostMessage([("preMountLoaderIframeUnMount", true->JSON.Encode.bool)]) | ||
Window.removeEventListener("message", handle) | ||
} | ||
None | ||
}, (paymentMethodsResponseSent, customerPaymentMethodsResponseSent, sessionTokensResponseSent)) | ||
|
||
React.null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.