Skip to content

Commit

Permalink
feat: added confirm handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sakksham7 committed Oct 14, 2024
1 parent 81f1776 commit 5ac4cd4
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
let setUserAddressState = Recoil.useLoggedSetRecoilState(userAddressState, "state", logger)
let setUserAddressCountry = Recoil.useLoggedSetRecoilState(userAddressCountry, "country", logger)
let (_country, setCountry) = Recoil.useRecoilState(userCountry)
let (isCompleteCallbackUsed, setIsCompleteCallbackUsed) = Recoil.useRecoilState(
isCompleteCallbackUsed,
)

let optionsCallback = (optionsPayment: PaymentType.options) => {
[
Expand Down Expand Up @@ -245,6 +248,10 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
let metadata = dict->getJsonObjectFromDict("analyticsMetadata")
logger.setMetadata(metadata)
}
if dict->getDictIsSome("onCompleteDoThisUsed") {
let isCallbackUsedVal = dict->Utils.getBool("onCompleteDoThisUsed", false)
setIsCompleteCallbackUsed(_ => isCallbackUsedVal)
}
if dict->getDictIsSome("paymentOptions") {
let paymentOptions = dict->getDictFromObj("paymentOptions")

Expand Down
2 changes: 2 additions & 0 deletions src/Payments/PaypalSDK.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
let areOneClickWalletsRendered = Recoil.useSetRecoilState(RecoilAtoms.areOneClickWalletsRendered)
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)
let (isCompleted, setIsCompleted) = React.useState(_ => false)
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)

let token = sessionObj.token
let orderDetails = sessionObj.orderDetails->getOrderDetails(paymentType)
Expand Down Expand Up @@ -90,6 +91,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
~handleCloseLoader,
~areOneClickWalletsRendered,
~setIsCompleted,
~isCallbackUsedVal,
)
})
Window.body->Window.appendChild(paypalScript)
Expand Down
1 change: 1 addition & 0 deletions src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let loadPaypalSDK = (
RecoilAtoms.areOneClickWalletsRendered => RecoilAtoms.areOneClickWalletsRendered
) => unit,
~setIsCompleted,
~isCallbackUsedVal: bool,
) => {
loggerState.setLogInfo(
~value="Paypal SDK Button Clicked",
Expand Down
28 changes: 25 additions & 3 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,11 @@ let rec intentCall = (
~sdkHandleOneClickConfirmPayment,
~counter,
~isPaymentSession=false,
~isCallbackUsedVal=?,
) => {
open Promise
let isConfirm = uri->String.includes("/confirm")

let isCompleteAuthorize = uri->String.includes("/complete_authorize")
let (eventName: OrcaLogger.eventName, initEventName: OrcaLogger.eventName) = switch (
isConfirm,
Expand Down Expand Up @@ -506,14 +508,28 @@ let rec intentCall = (
| (Applepay, false)
| (Paypal, false) =>
if !isPaymentSession {
closePaymentLoaderIfAny()
if isCallbackUsedVal->Option.getOr(false) {
Utils.handleOnCompleteDoThisMessage()
} else {
closePaymentLoaderIfAny()
}

postSubmitResponse(~jsonData=data, ~url=url.href)
} else if confirmParam.redirect === Some("always") {
handleOpenUrl(url.href)
if isCallbackUsedVal->Option.getOr(false) {
Utils.handleOnCompleteDoThisMessage()
} else {
handleOpenUrl(url.href)
}
} else {
resolve(data)
}
| _ => handleOpenUrl(url.href)
| _ =>
if isCallbackUsedVal->Option.getOr(false) {
Utils.handleOnCompleteDoThisMessage()
} else {
handleOpenUrl(url.href)
}
}
}

Expand Down Expand Up @@ -890,6 +906,7 @@ let usePaymentSync = (optLogger: option<OrcaLogger.loggerMake>, paymentType: pay
open RecoilAtoms
let paymentMethodList = Recoil.useRecoilValueFromAtom(paymentMethodList)
let keys = Recoil.useRecoilValueFromAtom(keys)
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)
let customPodUri = Recoil.useRecoilValueFromAtom(customPodUri)
let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
(~handleUserError=false, ~confirmParam: ConfirmType.confirmParams, ~iframeId="") => {
Expand Down Expand Up @@ -917,6 +934,7 @@ let usePaymentSync = (optLogger: option<OrcaLogger.loggerMake>, paymentType: pay
~customPodUri,
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
~isCallbackUsedVal,
)->ignore
}
switch paymentMethodList {
Expand Down Expand Up @@ -963,6 +981,7 @@ let usePaymentIntent = (optLogger, paymentType) => {
let customPodUri = Recoil.useRecoilValueFromAtom(customPodUri)
let paymentMethodList = Recoil.useRecoilValueFromAtom(paymentMethodList)
let keys = Recoil.useRecoilValueFromAtom(keys)
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)

let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
(
Expand Down Expand Up @@ -1056,6 +1075,7 @@ let usePaymentIntent = (optLogger, paymentType) => {
~customPodUri,
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
~isCallbackUsedVal,
)
->then(val => {
intentCallback(val)
Expand Down Expand Up @@ -1140,6 +1160,7 @@ let useCompleteAuthorize = (optLogger: option<OrcaLogger.loggerMake>, paymentTyp
let customPodUri = Recoil.useRecoilValueFromAtom(customPodUri)
let setIsManualRetryEnabled = Recoil.useSetRecoilState(isManualRetryEnabled)
let url = RescriptReactRouter.useUrl()
let isCallbackUsedVal = Recoil.useRecoilValueFromAtom(RecoilAtoms.isCompleteCallbackUsed)
let paymentTypeFromUrl =
CardUtils.getQueryParamsDictforKey(url.search, "componentName")->CardThemeType.getPaymentMode
(
Expand Down Expand Up @@ -1183,6 +1204,7 @@ let useCompleteAuthorize = (optLogger: option<OrcaLogger.loggerMake>, paymentTyp
~customPodUri,
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
~isCallbackUsedVal,
)->ignore
}
switch paymentMethodList {
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/RecoilAtoms.res
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ let userVpaId = Recoil.atom("userVpaId", defaultFieldValues)
let userPixKey = Recoil.atom("userPixKey", defaultFieldValues)
let userPixCPF = Recoil.atom("userPixCPF", defaultFieldValues)
let userPixCNPJ = Recoil.atom("userPixCNPJ", defaultFieldValues)
let isCompleteCallbackUsed = Recoil.atom("isCompleteCallbackUsed", false)

type areOneClickWalletsRendered = {
isGooglePay: bool,
Expand Down
5 changes: 5 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ let handleOnFocusPostMessage = (~targetOrigin="*") => {
messageParentWindow([("focus", true->JSON.Encode.bool)], ~targetOrigin)
}

let handleOnCompleteDoThisMessage = (~targetOrigin="*") => {
messageParentWindow([("completeDoThis", true->JSON.Encode.bool)], ~targetOrigin)
}

let handleOnBlurPostMessage = (~targetOrigin="*") => {
messageParentWindow([("blur", true->JSON.Encode.bool)], ~targetOrigin)
}
Expand Down Expand Up @@ -1148,6 +1152,7 @@ let eventHandlerFunc = (
| Click
| Ready
| Focus
| CompleteDoThis
| ConfirmPayment
| OneClickConfirmPayment
| Blur =>
Expand Down
4 changes: 4 additions & 0 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ let make = (
("analyticsMetadata", analyticsMetadata),
("launchTime", launchTime->JSON.Encode.float),
("customBackendUrl", customBackendUrl->JSON.Encode.string),
(
"onCompleteDoThisUsed",
LoaderPaymentElement.onCompleteDoThisUsed.contents->JSON.Encode.bool,
),
]->Dict.fromArray

let wallets = PaymentType.getWallets(newOptions->getDictFromJson, "wallets", logger)
Expand Down
10 changes: 10 additions & 0 deletions src/orca-loader/LoaderPaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open Identity
@val @scope(("navigator", "clipboard"))
external writeText: string => promise<'a> = "writeText"

let onCompleteDoThisUsed = ref(false)
let make = (
componentType,
options,
Expand Down Expand Up @@ -47,6 +48,15 @@ let make = (
},
"onEscape",
)
| CompleteDoThis => {
onCompleteDoThisUsed := true
eventHandlerFunc(
ev => ev.data.completeDoThis,
eventHandler,
CompleteDoThis,
"onCompleteDoThis",
)
}
| Change =>
eventHandlerFunc(
ev => ev.data.elementType === componentType,
Expand Down
13 changes: 12 additions & 1 deletion src/orca-loader/Types.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type eventData = {
blur: bool,
ready: bool,
clickTriggered: bool,
completeDoThis: bool,
elementType: string,
classChange: bool,
newClassType: string,
Expand Down Expand Up @@ -184,14 +185,24 @@ let defaultHyperInstance = {
}

type eventType =
Escape | Change | Click | Ready | Focus | Blur | ConfirmPayment | OneClickConfirmPayment | None
| Escape
| Change
| Click
| Ready
| Focus
| Blur
| CompleteDoThis
| ConfirmPayment
| OneClickConfirmPayment
| None

let eventTypeMapper = event => {
switch event {
| "escape" => Escape
| "change" => Change
| "clickTriggered" => Click
| "ready" => Ready
| "completeDoThis" => CompleteDoThis
| "focus" => Focus
| "blur" => Blur
| "confirmTriggered" => ConfirmPayment
Expand Down

0 comments on commit 5ac4cd4

Please sign in to comment.