Skip to content

Commit

Permalink
Feature - one click confirm handler (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Praful Koppalkar <[email protected]>
  • Loading branch information
vsrivatsa-edinburgh and prafulkoppalkar authored Feb 20, 2024
1 parent ce8b7b6 commit c93e44c
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 162 deletions.
11 changes: 9 additions & 2 deletions src/Hooks/CommonHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type keys = {
iframeId: string,
parentURL: string,
sdkHandleConfirmPayment: bool,
sdkHandleOneClickConfirmPayment: bool,
}
@val @scope("document") external querySelector: string => Js.Nullable.t<element> = "querySelector"

Expand Down Expand Up @@ -67,7 +68,7 @@ let useScript = (src: string) => {
let updateKeys = (dict, keyPair, setKeys) => {
let (key, value) = keyPair
let valueStr = value->Js.Json.decodeString->Belt.Option.getWithDefault("")
let valueBool = value->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false)
let valueBool = default => value->Js.Json.decodeBoolean->Belt.Option.getWithDefault(default)
if dict->Utils.getDictIsSome(key) {
switch key {
| "iframeId" =>
Expand All @@ -88,7 +89,12 @@ let updateKeys = (dict, keyPair, setKeys) => {
| "sdkHandleConfirmPayment" =>
setKeys(.prev => {
...prev,
sdkHandleConfirmPayment: dict->Utils.getBool(key, valueBool),
sdkHandleConfirmPayment: dict->Utils.getBool(key, valueBool(false)),
})
| "sdkHandleOneClickConfirmPayment" =>
setKeys(.prev => {
...prev,
sdkHandleOneClickConfirmPayment: dict->Utils.getBool(key, valueBool(true)),
})
| _ => ()
}
Expand All @@ -100,4 +106,5 @@ let defaultkeys = {
iframeId: "",
parentURL: "*",
sdkHandleConfirmPayment: false,
sdkHandleOneClickConfirmPayment: true,
}
1 change: 1 addition & 0 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => {
("publishableKey", ""->Js.Json.string),
("parentURL", "*"->Js.Json.string),
("sdkHandleConfirmPayment", false->Js.Json.boolean),
("sdkHandleOneClickConfirmPayment", true->Js.Json.boolean),
]->Js.Array2.forEach(keyPair => {
dict->CommonHooks.updateKeys(keyPair, setKeys)
})
Expand Down
56 changes: 34 additions & 22 deletions src/Payments/ApplePay.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ let make = (
~walletOptions: array<string>,
) => {
let loggerState = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom)
let {publishableKey} = Recoil.useRecoilValueFromAtom(RecoilAtoms.keys)
let {publishableKey, sdkHandleOneClickConfirmPayment} = Recoil.useRecoilValueFromAtom(
RecoilAtoms.keys,
)
let isApplePayReady = Recoil.useRecoilValueFromAtom(RecoilAtoms.isApplePayReady)
let setIsShowOrPayUsing = Recoil.useSetRecoilState(RecoilAtoms.isShowOrPayUsing)
let (showApplePay, setShowApplePay) = React.useState(() => false)
Expand Down Expand Up @@ -234,30 +236,40 @@ let make = (
(),
)
setApplePayClicked(_ => true)
open Promise
OrcaUtils.makeOneClickHandlerPromise(sdkHandleOneClickConfirmPayment)
->then(result => {
let result = result->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false)
if result {
if isInvokeSDKFlow {
let isDelayedSessionToken =
sessionObj
->Belt.Option.getWithDefault(Js.Json.null)
->Js.Json.decodeObject
->Belt.Option.getWithDefault(Js.Dict.empty())
->Js.Dict.get("delayed_session_token")
->Belt.Option.getWithDefault(Js.Json.null)
->Js.Json.decodeBoolean
->Belt.Option.getWithDefault(false)

if isInvokeSDKFlow {
let isDelayedSessionToken =
sessionObj
->Belt.Option.getWithDefault(Js.Json.null)
->Js.Json.decodeObject
->Belt.Option.getWithDefault(Js.Dict.empty())
->Js.Dict.get("delayed_session_token")
->Belt.Option.getWithDefault(Js.Json.null)
->Js.Json.decodeBoolean
->Belt.Option.getWithDefault(false)

if isDelayedSessionToken {
setShowApplePayLoader(_ => true)
let bodyDict = PaymentBody.applePayThirdPartySdkBody(~connectors)
processPayment(bodyDict)
if isDelayedSessionToken {
setShowApplePayLoader(_ => true)
let bodyDict = PaymentBody.applePayThirdPartySdkBody(~connectors)
processPayment(bodyDict)
} else {
let message = [("applePayButtonClicked", true->Js.Json.boolean)]
Utils.handlePostMessage(message)
}
} else {
let bodyDict = PaymentBody.applePayRedirectBody(~connectors)
processPayment(bodyDict)
}
} else {
let message = [("applePayButtonClicked", true->Js.Json.boolean)]
Utils.handlePostMessage(message)
setApplePayClicked(_ => false)
}
} else {
let bodyDict = PaymentBody.applePayRedirectBody(~connectors)
processPayment(bodyDict)
}
resolve()
})
->ignore
}

React.useEffect3(() => {
Expand Down
51 changes: 29 additions & 22 deletions src/Payments/GPay.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let make = (
let (requiredFieldsBody, setRequiredFieldsBody) = React.useState(_ => Js.Dict.empty())
let (loggerState, _setLoggerState) = Recoil.useRecoilState(loggerAtom)
let {iframeId} = Recoil.useRecoilValueFromAtom(keys)
let {publishableKey} = Recoil.useRecoilValueFromAtom(keys)
let {publishableKey, sdkHandleOneClickConfirmPayment} = Recoil.useRecoilValueFromAtom(keys)
let {localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let options = Recoil.useRecoilValueFromAtom(optionAtom)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Gpay)
Expand Down Expand Up @@ -137,28 +137,35 @@ let make = (
~paymentMethod="GOOGLE_PAY",
(),
)
if isInvokeSDKFlow {
if isDelayedSessionToken {
let bodyDict = PaymentBody.gPayThirdPartySdkBody(~connectors)
processPayment(bodyDict)
} else {
handlePostMessage([
("fullscreen", true->Js.Json.boolean),
("param", "paymentloader"->Js.Json.string),
("iframeId", iframeId->Js.Json.string),
])
options.readOnly ? () : handlePostMessage([("GpayClicked", true->Js.Json.boolean)])
open Promise
OrcaUtils.makeOneClickHandlerPromise(sdkHandleOneClickConfirmPayment)->then(result => {
let result = result->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false)
if result {
if isInvokeSDKFlow {
if isDelayedSessionToken {
let bodyDict = PaymentBody.gPayThirdPartySdkBody(~connectors)
processPayment(bodyDict)
} else {
handlePostMessage([
("fullscreen", true->Js.Json.boolean),
("param", "paymentloader"->Js.Json.string),
("iframeId", iframeId->Js.Json.string),
])
options.readOnly ? () : handlePostMessage([("GpayClicked", true->Js.Json.boolean)])
}
} else {
let bodyDict = PaymentBody.gpayRedirectBody(~connectors)
processPayment(bodyDict)
}
loggerState.setLogInfo(
~value="",
~eventName=PAYMENT_DATA_FILLED,
~paymentMethod="GOOGLE_PAY",
(),
)
}
} else {
let bodyDict = PaymentBody.gpayRedirectBody(~connectors)
processPayment(bodyDict)
}
loggerState.setLogInfo(
~value="",
~eventName=PAYMENT_DATA_FILLED,
~paymentMethod="GOOGLE_PAY",
(),
)
resolve()
})
}

let buttonStyle = {
Expand Down
35 changes: 23 additions & 12 deletions src/Payments/PayPal.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let payPalIcon = <Icon size=35 width=90 name="paypal" />
let make = (~list: PaymentMethodsRecord.list) => {
let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom)
let (paypalClicked, setPaypalClicked) = React.useState(_ => false)
let {publishableKey} = Recoil.useRecoilValueFromAtom(keys)
let {publishableKey, sdkHandleOneClickConfirmPayment} = Recoil.useRecoilValueFromAtom(keys)
let options = Recoil.useRecoilValueFromAtom(optionAtom)
let areOneClickWalletsRendered = Recoil.useSetRecoilState(RecoilAtoms.areOneClickWalletsRendered)
let (_, _, labelType) = options.wallets.style.type_
Expand All @@ -38,17 +38,28 @@ let make = (~list: PaymentMethodsRecord.list) => {
(),
)
setPaypalClicked(_ => true)
let (connectors, _) = list->PaymentUtils.getConnectors(Wallets(Paypal(Redirect)))
let body = PaymentBody.paypalRedirectionBody(~connectors)
intent(
~bodyArr=body,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
(),
)
open Promise
OrcaUtils.makeOneClickHandlerPromise(sdkHandleOneClickConfirmPayment)
->then(result => {
let result = result->Js.Json.decodeBoolean->Belt.Option.getWithDefault(false)
if result {
let (connectors, _) = list->PaymentUtils.getConnectors(Wallets(Paypal(Redirect)))
let body = PaymentBody.paypalRedirectionBody(~connectors)
intent(
~bodyArr=body,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
(),
)
} else {
setPaypalClicked(_ => false)
}
resolve()
})
->ignore
}

React.useEffect0(() => {
Expand Down
Loading

0 comments on commit c93e44c

Please sign in to comment.