Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sdk pay now button enable prop added #543

Merged
merged 14 commits into from
Aug 7, 2024
5 changes: 5 additions & 0 deletions src/Components/PayNowButton.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ let make = () => {
let confirmPayload = sdkHandleConfirmPayment->PaymentBody.confirmPayloadForSDKButton
let buttonText = sdkHandleConfirmPayment.buttonText->Option.getOr(localeString.payNowButton)

React.useEffect1(() => {
setIsPayNowButtonDisable(_ => !sdkHandleConfirmPayment.allowButtonBeforeValidation)
None
}, [sdkHandleConfirmPayment.allowButtonBeforeValidation])

let handleMessage = (event: Types.event) => {
let json = event.data->Identity.anyTypeToJson->getStringFromJson("")->safeParse
let dict = json->getDictFromJson
Expand Down
13 changes: 9 additions & 4 deletions src/Hooks/UtilityHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ let useIsGuestCustomer = () => {
}

let useHandlePostMessages = (~complete, ~empty, ~paymentType, ~savedMethod=false) => {
let loggerState = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom)
let setIsPayNowButtonDisable = RecoilAtoms.payNowButtonDisable->Recoil.useSetRecoilState
open RecoilAtoms

let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom)
let setIsPayNowButtonDisable = Recoil.useSetRecoilState(payNowButtonDisable)
let {sdkHandleConfirmPayment} = Recoil.useRecoilValueFromAtom(optionAtom)

React.useEffect(() => {
let isCompletelyFilled = complete && paymentType !== ""
setIsPayNowButtonDisable(_ => !isCompletelyFilled)
if !sdkHandleConfirmPayment.allowButtonBeforeValidation {
let isCompletelyFilled = complete && paymentType !== ""
setIsPayNowButtonDisable(_ => !isCompletelyFilled)
}
Utils.handlePostMessageEvents(~complete, ~empty, ~paymentType, ~loggerState, ~savedMethod)
None
}, (complete, empty, paymentType))
Expand Down
3 changes: 2 additions & 1 deletion src/TabCard.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let make = (~paymentOption: PaymentMethodsRecord.paymentFieldsInfo, ~isActive: b
let {themeObj, localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let {readOnly, customMethodNames} = Recoil.useRecoilValueFromAtom(optionAtom)
let setSelectedOption = Recoil.useSetRecoilState(selectedOptionAtom)
let {sdkHandleConfirmPayment} = optionAtom->Recoil.useRecoilValueFromAtom
let setIsPayNowButtonDisable = Recoil.useSetRecoilState(payNowButtonDisable)
let (tabClass, tabLabelClass, tabIconClass) = React.useMemo(
() => isActive ? ("Tab--selected", "TabLabel--selected", "TabIcon--selected") : ("", "", ""),
Expand All @@ -16,7 +17,7 @@ let make = (~paymentOption: PaymentMethodsRecord.paymentFieldsInfo, ~isActive: b
paymentOption.icon,
)
let onClick = _ => {
setIsPayNowButtonDisable(_ => true)
setIsPayNowButtonDisable(_ => !sdkHandleConfirmPayment.allowButtonBeforeValidation)
setSelectedOption(_ => paymentOption.paymentMethodName)
}
<button
Expand Down
3 changes: 3 additions & 0 deletions src/Types/PaymentType.res
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type sdkHandleConfirmPayment = {
handleConfirm: bool,
buttonText?: string,
confirmParams: ConfirmType.confirmParams,
allowButtonBeforeValidation: bool,
}

type options = {
Expand Down Expand Up @@ -286,6 +287,7 @@ let defaultBillingAddress = {
let defaultSdkHandleConfirmPayment = {
handleConfirm: false,
confirmParams: ConfirmType.defaultConfirm,
allowButtonBeforeValidation: false,
}

let defaultOptions = {
Expand Down Expand Up @@ -983,6 +985,7 @@ let getSdkHandleConfirmPaymentProps = dict => {
handleConfirm: dict->getBool("handleConfirm", false),
buttonText: ?dict->getOptionString("buttonText"),
confirmParams: dict->getDictFromDict("confirmParams")->getConfirmParams,
allowButtonBeforeValidation: dict->getBool("allowButtonBeforeValidation", false),
}

let itemToObjMapper = (dict, logger) => {
Expand Down
6 changes: 5 additions & 1 deletion src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -918,10 +918,14 @@ let getStringFromOptionalJson = (json, default) => {
json->Option.flatMap(JSON.Decode.string)->Option.getOr(default)
}

let getBoolFromJson = (json, default) => {
let getBoolFromOptionalJson = (json, default) => {
json->Option.flatMap(JSON.Decode.bool)->Option.getOr(default)
}

let getBoolFromJson = (json, default) => {
json->JSON.Decode.bool->Option.getOr(default)
}

let getOptionalJson = (json, str) => {
json
->JSON.Decode.object
Expand Down
2 changes: 1 addition & 1 deletion src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ let make = (
let gpayClicked =
evJson
->getOptionalJsonFromJson("GpayClicked")
->getBoolFromJson(false)
->getBoolFromOptionalJson(false)

let paymentDataRequest =
evJson
Expand Down
69 changes: 35 additions & 34 deletions src/orca-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -291,42 +291,43 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
let handleMessage = (event: Types.event) => {
let json = event.data->anyTypeToJson
let dict = json->getDictFromJson
switch dict->Dict.get("submitSuccessful") {
| Some(val) =>
logApi(
~apiLogType=Method,
~optLogger=Some(logger),
~result=val,
~paymentMethod="confirmPayment",
~eventName=CONFIRM_PAYMENT,
let submitSuccessful =
dict->Dict.get("submitSuccessful")->getBoolFromOptionalJson(false)

logApi(
~apiLogType=Method,
~optLogger=Some(logger),
~result=JSON.Encode.bool(submitSuccessful),
~paymentMethod="confirmPayment",
~eventName=CONFIRM_PAYMENT,
)
let data = dict->getDictFromDict("data")->JSON.Encode.object
let returnUrl = dict->getString("url", url)

if isOneClick {
iframeRef.contents->Array.forEach(
ifR => {
// to unset one click button loader
ifR->Window.iframePostMessage(
[("oneClickDoSubmit", false->JSON.Encode.bool)]->Dict.fromArray,
)
},
)
let data = dict->Dict.get("data")->Option.getOr(Dict.make()->JSON.Encode.object)
let returnUrl =
dict->Dict.get("url")->Option.flatMap(JSON.Decode.string)->Option.getOr(url)

if isOneClick {
iframeRef.contents->Array.forEach(
ifR => {
// to unset one click button loader
ifR->Window.iframePostMessage(
[("oneClickDoSubmit", false->JSON.Encode.bool)]->Dict.fromArray,
)
},
)
}
postSubmitMessage(dict)
}
postSubmitMessage(dict)

if (
isSdkButton ||
(val->JSON.Decode.bool->Option.getOr(false) && redirect === "always")
) {
Window.replace(returnUrl)
} else if !(val->JSON.Decode.bool->Option.getOr(false)) {
if isSdkButton {
if !submitSuccessful {
resolve1(json)
} else {
resolve1(data)
Window.replace(returnUrl)
}
| None => ()
} else if submitSuccessful && redirect === "always" {
Window.replace(returnUrl)
} else if !submitSuccessful {
resolve1(json)
} else {
resolve1(data)
}
}
let message = isOneClick
Expand Down Expand Up @@ -487,16 +488,16 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
let json = event.data->anyTypeToJson
let dict = json->getDictFromJson
switch dict->Dict.get("submitSuccessful") {
| Some(val) =>
| Some(submitSuccessful) =>
logApi(
~apiLogType=Method,
~optLogger=Some(logger),
~result=val,
~result=submitSuccessful,
~paymentMethod="confirmCardPayment",
~eventName=CONFIRM_CARD_PAYMENT,
)
let url = decodedData->getString("return_url", "/")
if val->JSON.Decode.bool->Option.getOr(false) && url !== "/" {
if submitSuccessful->getBoolFromJson(false) && url !== "/" {
Window.replace(url)
} else {
resolve(json)
Expand Down
5 changes: 3 additions & 2 deletions src/orca-loader/LoaderPaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ let make = (
| None => ()
}

let isCopy = eventDataObject->getOptionalJsonFromJson("copy")->getBoolFromJson(false)
let isCopy =
eventDataObject->getOptionalJsonFromJson("copy")->getBoolFromOptionalJson(false)
let text =
eventDataObject->getOptionalJsonFromJson("copyDetails")->getStringFromOptionalJson("")
if isCopy {
Expand Down Expand Up @@ -235,7 +236,7 @@ let make = (
eventDataObject->getOptionalJsonFromJson("iframeId")->getStringFromOptionalJson("")

if fullscreenIframe->Option.isSome {
fullscreen := fullscreenIframe->getBoolFromJson(false)
fullscreen := fullscreenIframe->getBoolFromOptionalJson(false)
fullscreenParam := param->getStringFromOptionalJson("")
fullscreenMetadata :=
metadata
Expand Down
Loading