Skip to content

Commit

Permalink
Merged in klarna/sdk/safari/fix (pull request #127)
Browse files Browse the repository at this point in the history
Removed SDK Loading

* Removed SDK Loading

* Removed logs


Approved-by: Narayana Aaditya G
  • Loading branch information
Bopanna M J committed Feb 9, 2023
1 parent d083784 commit dc6795d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Components/PaymentLoader.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@react.component
let make = () => {
let loaderUI = <div className={`flex flex-col justify-center m-auto visible`}> <Loader /> </div>
<div className="h-screen w-screen bg-black/70 flex m-auto items-center backdrop-blur-sm">
<div className="h-screen w-screen bg-black/80 flex m-auto items-center backdrop-blur-md">
{loaderUI}
</div>
}
105 changes: 81 additions & 24 deletions src/Payments/KlarnaSDK.res
Original file line number Diff line number Diff line change
@@ -1,39 +1,96 @@
open RecoilAtoms
type token = {client_token: string}
type loadType = {
container: option<string>,
color_text: option<string>,
payment_method_category: option<string>,
}
type init
type res = {
approved: bool,
show_form: bool,
authorization_token: string,
finalize_required: bool,
}
type some = {
init: (. token) => unit,
load: (. loadType, res => unit) => unit,
authorize: (. loadType, Js.Json.t, res => unit) => unit,
loadPaymentReview: (. loadType, res => unit) => unit,
}

@val external klarnaInit: some = "Klarna.Payments"

@react.component
let make = (~sessionObj: SessionsType.token) => {
open Utils
let {clientSecret, iframeId} = Recoil.useRecoilValueFromAtom(keys)
let handlePopUp = confirm => {
switch clientSecret {
| Some(key) =>
Utils.handlePostMessage([
("fullscreen", true->Js.Json.boolean),
("param", "klarna"->Js.Json.string),
("iframeId", iframeId->Js.Json.string),
(
"metadata",
[
("token", sessionObj.token->Js.Json.string),
("clientSecret", key->Js.Json.string),
("confirmParam", confirm->toJson),
]
->Js.Dict.fromArray
->Js.Json.object_,
),
])
| None => ()
}
}
let loggerState = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState))
let {iframeId} = Recoil.useRecoilValueFromAtom(keys)
let status = CommonHooks.useScript("https://x.klarnacdn.net/kp/lib/v1/api.js")

let handleCloseLoader = () => {
Utils.handlePostMessage([("fullscreen", false->Js.Json.boolean)])
Utils.postFailedSubmitResponse(
~errortype="confirm_payment_failed",
~message="An unknown error has occured",
)
}
let submitCallback = React.useCallback((ev: Window.event) => {
let json = ev.data->Js.Json.parseExn
let confirm = json->Utils.getDictFromJson->ConfirmType.itemToObjMapper

if confirm.doSubmit {
handlePopUp(confirm.confirmParams)
Utils.handlePostMessage([
("fullscreen", true->Js.Json.boolean),
("param", "paymentloader"->Js.Json.string),
("iframeId", iframeId->Js.Json.string),
])
klarnaInit.authorize(.
{
container: None,
color_text: None,
payment_method_category: Some("klarna"),
},
Js.Dict.empty()->Js.Json.object_,
(res: res) => {
let body = PaymentBody.klarnaSDKbody(~token=res.authorization_token)
res.approved
? intent(
~bodyArr=body,
~clientSecret=confirm.clientSecret,
~confirmParam=confirm.confirmParams,
~handleUserError=false,
)
: handleCloseLoader()
},
)
}
})
submitPaymentData(submitCallback)

React.useEffect1(() => {
if status == "ready" {
klarnaInit.init(. {
client_token: sessionObj.token,
})

klarnaInit.load(.
{
container: Some("#klarna-payments"),
color_text: Some("#E51515"),
payment_method_category: Some("pay_later"),
},
(_res: res) => {
()
},
)
}
None
}, [status])

let bottomElement = <RedirectElement />
<div className="p-1 animate-slowShow"> <Block bottomElement /> </div>
<div className="p-1 animate-slowShow">
<div id="klarna-payments" className="m-3 hidden" /> <Block bottomElement />
</div>
}
15 changes: 2 additions & 13 deletions src/orca-loader/HyperLoader.res
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ module Elements = {
})
->then(res => {
let (json, applePayPresent) = res
Js.log2("APPLE PAY PRESENT", applePayPresent)
if componentType === "payment" && applePayPresent->Belt.Option.isSome {
let scriptURL = "https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js"
let script = Window.createElement("script")
Expand All @@ -448,7 +447,6 @@ module Elements = {
let dict = json->getDictFromJson

if dict->Js.Dict.get("applePayMounted")->Belt.Option.isSome {
Js.log("APPLE PAY MOUNTED COMPONENT")
switch sessionForApplePay->Js.Nullable.toOption {
| Some(session) =>
if session.canMakePayments(.) {
Expand Down Expand Up @@ -480,10 +478,7 @@ module Elements = {
if val->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false) {
let ssn = applePaySession(3, paymentRequest->objToJson)

Js.log2("SESSION", ssn)

ssn.onvalidatemerchant = event => {
Js.log2(">>>onvalidatemerchant", event)
ssn.onvalidatemerchant = _event => {
let merchantSession =
applePayPresent
->Belt.Option.getWithDefault(Js.Dict.empty()->Js.Json.object_)
Expand All @@ -492,18 +487,12 @@ module Elements = {
}

ssn.onpaymentauthorized = event => {
Js.log(">>>onpaymentauthorized")
ssn.completePayment(. {"status": ssn.\"STATUS_SUCCESS"}->objToJson)
Js.log2("Confirm", ssn.\"STATUS_SUCCESS")

Js.log2("EVENT", event)
Js.log2("SIGNATURE", event.payment.token.paymentData->Js.Json.stringify->btoa)

processPayment(event.payment.token.paymentData->Js.Json.stringify->btoa)
Js.log("processPayment Done")
}
ssn.oncancel = () => {
Js.log(">>cancelled")
Js.log("cancelled")
}

ssn.begin(.)
Expand Down

0 comments on commit dc6795d

Please sign in to comment.