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
16 changes: 10 additions & 6 deletions src/orca-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,16 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
}
postSubmitMessage(dict)

if (
isSdkButton ||
(val->JSON.Decode.bool->Option.getOr(false) && redirect === "always")
) {
if isSdkButton {
if !(val->getBoolFromJson(false)) {
resolve1(json)
} else {
resolve1(data)
sakksham7 marked this conversation as resolved.
Show resolved Hide resolved
Window.replace(returnUrl)
}
} else if val->getBoolFromJson(false) && redirect === "always" {
Window.replace(returnUrl)
} else if !(val->JSON.Decode.bool->Option.getOr(false)) {
} else if !(val->getBoolFromJson(false)) {
resolve1(json)
} else {
resolve1(data)
Expand Down Expand Up @@ -496,7 +500,7 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
~eventName=CONFIRM_CARD_PAYMENT,
)
let url = decodedData->getString("return_url", "/")
if val->JSON.Decode.bool->Option.getOr(false) && url !== "/" {
if val->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