-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: klarna Checkout SDK added (#851)
Co-authored-by: Pritish Budhiraja <pritish.budhiraja@gmail.com>
- Loading branch information
1 parent
19acc32
commit 3011a1c
Showing
7 changed files
with
148 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
open RecoilAtoms | ||
open Promise | ||
|
||
let klarnaIcon = <Icon size=35 width=90 name="klarna" /> | ||
|
||
@react.component | ||
let make = () => { | ||
let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom) | ||
let sdkHandleIsThere = Recoil.useRecoilValueFromAtom(isPaymentButtonHandlerProvidedAtom) | ||
let {publishableKey} = Recoil.useRecoilValueFromAtom(keys) | ||
let options = Recoil.useRecoilValueFromAtom(optionAtom) | ||
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(isManualRetryEnabled) | ||
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Other) | ||
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue) | ||
let (klarnaClicked, setKlarnaClicked) = React.useState(_ => false) | ||
|
||
let (_, _, _, heightType, _) = options.wallets.style.height | ||
let height = switch heightType { | ||
| Klarna(val) => val | ||
| _ => 48 | ||
} | ||
let (buttonColor, textColor) = | ||
options.wallets.style.theme == Light ? ("#0070ba", "#ffffff") : ("#0b051d", "#000000") | ||
|
||
let onKlarnaClick = async _ev => { | ||
try { | ||
loggerState.setLogInfo( | ||
~value="Klarna Checkout Button Clicked", | ||
~eventName=KLARNA_CHECKOUT_FLOW, | ||
~paymentMethod="KLARNA", | ||
) | ||
|
||
setKlarnaClicked(_ => true) | ||
|
||
let result = await Utils.makeOneClickHandlerPromise(sdkHandleIsThere) | ||
let decodedResult = result->JSON.Decode.bool->Option.getOr(false) | ||
|
||
if decodedResult { | ||
let (connectors, _) = | ||
paymentMethodListValue->PaymentUtils.getConnectors(PayLater(Klarna(Redirect))) | ||
let body = PaymentBody.klarnaCheckoutBody(~connectors) | ||
|
||
intent( | ||
~bodyArr=body, | ||
~confirmParam={ | ||
return_url: options.wallets.walletReturnUrl, | ||
publishableKey, | ||
}, | ||
~handleUserError=true, | ||
~manualRetry=isManualRetryEnabled, | ||
) | ||
} else { | ||
setKlarnaClicked(_ => false) | ||
} | ||
resolve() | ||
} catch { | ||
| _ => resolve() | ||
} | ||
} | ||
|
||
<button | ||
style={ | ||
display: "inline-block", | ||
color: textColor, | ||
height: `${height->Int.toString}px`, | ||
borderRadius: `${options.wallets.style.buttonRadius->Int.toString}px`, | ||
width: "100%", | ||
backgroundColor: buttonColor, | ||
} | ||
onClick={_ => | ||
if !options.readOnly { | ||
onKlarnaClick()->ignore | ||
}}> | ||
<div className="justify-center" style={display: "flex", flexDirection: "row", color: textColor}> | ||
{if klarnaClicked { | ||
<Loader /> | ||
} else { | ||
klarnaIcon | ||
}} | ||
</div> | ||
</button> | ||
} | ||
|
||
let default = make |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
let make = React.lazy_(() => Js.import(KlarnaCheckout.default)) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
open PaymentMethodsRecord | ||
open SessionsType | ||
|
||
type klarnaExperienceData = { | ||
klarnaTokenObj: optionalTokenType, | ||
isKlarnaSDKFlow: bool, | ||
isKlarnaCheckoutFlow: bool, | ||
} | ||
|
||
let usePaymentMethodExperience = (~paymentMethodListValue, ~sessionObj: sessions) => { | ||
let klarnaPaymentMethodExperience = React.useMemo(() => { | ||
getPaymentExperienceTypeFromPML( | ||
~paymentMethodList=paymentMethodListValue, | ||
~paymentMethodName="pay_later", | ||
~paymentMethodType="klarna", | ||
) | ||
}, [paymentMethodListValue]) | ||
|
||
let klarnaTokenObj = React.useMemo( | ||
() => getPaymentSessionObj(sessionObj.sessionsToken, Klarna), | ||
[sessionObj], | ||
) | ||
|
||
let isKlarnaSDKFlow = klarnaPaymentMethodExperience->Array.includes(InvokeSDK) | ||
let isKlarnaCheckoutFlow = klarnaPaymentMethodExperience->Array.includes(RedirectToURL) | ||
|
||
{ | ||
klarnaTokenObj, | ||
isKlarnaSDKFlow, | ||
isKlarnaCheckoutFlow, | ||
} | ||
} |
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