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

fix: disable and enable Pay now button #221

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/Components/PayNowButton.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,41 @@ let make = (
~cardProps: CardUtils.cardProps,
~expiryProps: CardUtils.expiryProps,
~selectedOption: PaymentModeType.payment,
~savedMethods: array<PaymentType.customerMethods>,
~paymentToken,
) => {
open RecoilAtoms
let {themeObj, localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let (isDisabled, setIsDisabled) = React.useState(() => false)
let (showLoader, setShowLoader) = React.useState(() => false)
let areRequiredFieldsValidValue = Recoil.useRecoilValueFromAtom(areRequiredFieldsValid)
let {sdkHandleConfirmPayment} = Recoil.useRecoilValueFromAtom(optionAtom)
let showFields = Recoil.useRecoilValueFromAtom(showCardFieldsAtom)

let (isCVCValid, _, _, _, _, _, _, _, _, _) = cvcProps
let (isCVCValid, _, cvcNumber, _, _, _, _, _, _, _) = cvcProps
let (isCardValid, _, _, _, _, _, _, _, _, _) = cardProps
let (isExpiryValid, _, _, _, _, _, _, _, _) = expiryProps

let (token, _) = paymentToken
let customerMethod =
savedMethods
->Array.filter(savedMethod => {
savedMethod.paymentToken === token
})
->Array.get(0)
->Option.getOr(PaymentType.defaultCustomerMethods)
let isCardPaymentMethod = customerMethod.paymentMethod === "card"
let complete = switch isCVCValid {
| Some(val) => token !== "" && val
| _ => false
}
let empty = cvcNumber == ""
let isSavedMethodCheck =
areRequiredFieldsValidValue && (!isCardPaymentMethod || (complete && !empty))

let validFormat =
isCVCValid->Option.getOr(false) &&
isCardValid->Option.getOr(false) &&
isCVCValid->Option.getOr(false) &&
isExpiryValid->Option.getOr(false) &&
areRequiredFieldsValidValue

Expand All @@ -44,14 +64,19 @@ let make = (
setShowLoader(_ => true)
Utils.handlePostMessage([("handleSdkConfirm", confirmPayload)])
}
React.useEffect3(() => {
if selectedOption === Card {
setIsDisabled(_ => !validFormat)

React.useEffect4(() => {
if showFields {
if selectedOption === Card {
setIsDisabled(_ => !validFormat)
} else {
setIsDisabled(_ => !areRequiredFieldsValidValue)
}
} else {
setIsDisabled(_ => !areRequiredFieldsValidValue)
setIsDisabled(_ => !isSavedMethodCheck)
}
None
}, (validFormat, areRequiredFieldsValidValue, selectedOption))
}, (validFormat, areRequiredFieldsValidValue, selectedOption, isSavedMethodCheck))

<div className="flex flex-col gap-1 h-auto w-full items-center">
<button
Expand Down
6 changes: 6 additions & 0 deletions src/Payment.res
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ let make = (~paymentMode, ~integrateError, ~logger) => {
if cvc->Js.String2.length > 0 && cvcNumberInRange(cvc, cardBrand)->Js.Array2.includes(true) {
zipRef.current->Js.Nullable.toOption->Belt.Option.forEach(input => input->focus)->ignore
}

if cvc->Js.String2.length > 0 && cvcNumberInRange(cvc, cardBrand)->Js.Array2.includes(true) {
setIsCVCValid(_ => Some(true))
} else {
setIsCVCValid(_ => None)
}
}

let changeZipCode = ev => {
Expand Down
5 changes: 3 additions & 2 deletions src/PaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ let make = (

React.useEffect1(() => {
let defaultPaymentMethod =
savedMethods
->Js.Array2.find(savedMethod => savedMethod.defaultPaymentMethodSet)
savedMethods->Js.Array2.find(savedMethod => savedMethod.defaultPaymentMethodSet)

let isSavedMethodsEmpty = savedMethods->Js.Array2.length === 0

Expand Down Expand Up @@ -400,6 +399,8 @@ let make = (
cardProps
expiryProps
selectedOption={selectedOption->PaymentModeType.paymentMode}
savedMethods
paymentToken
/>
</div>
</RenderIf>
Expand Down
4 changes: 2 additions & 2 deletions src/Payments/ApplePay.res
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ let make = (
->ignore
}

React.useEffect3(() => {
React.useEffect4(() => {
let handleApplePayMessages = (ev: Window.event) => {
let json = try {
ev.data->Js.Json.parseExn
Expand Down Expand Up @@ -317,7 +317,7 @@ let make = (
Window.removeEventListener("message", handleApplePayMessages)
},
)
}, (isInvokeSDKFlow, requiredFieldsBody, isWallet))
}, (isInvokeSDKFlow, requiredFieldsBody, isWallet, isGuestCustomer))
vsrivatsa-edinburgh marked this conversation as resolved.
Show resolved Hide resolved

React.useEffect4(() => {
if (
Expand Down
6 changes: 3 additions & 3 deletions src/Payments/CardPayment.res
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ let make = (
)
let banContactBody = PaymentBody.bancontactBody()
let cardBody = if displaySavedPaymentMethodsCheckbox {
if isSaveCardsChecked || list.payment_type === "setup_mandate" {
if isSaveCardsChecked || list.payment_type === SETUP_MANDATE {
defaultCardBody->Js.Array2.concat(onSessionBody)
} else {
defaultCardBody
}
} else if isGuestCustomer || list.payment_type === "normal" {
} else if isGuestCustomer || list.payment_type === NORMAL {
defaultCardBody
} else {
defaultCardBody->Js.Array2.concat(onSessionBody)
Expand Down Expand Up @@ -178,7 +178,7 @@ let make = (
let paymentMethodType = isBancontact ? "bancontact_card" : "debit"
let conditionsForShowingSaveCardCheckbox =
!isGuestCustomer &&
list.payment_type !== "setup_mandate" &&
list.payment_type !== SETUP_MANDATE &&
options.displaySavedPaymentMethodsCheckbox &&
!isBancontact

Expand Down
25 changes: 22 additions & 3 deletions src/Payments/PaymentMethodsRecord.res
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,14 @@ type mandate = {
single_use: option<mandateType>,
multi_use: option<mandateType>,
}
type payment_type = NORMAL | NEW_MANDATE | SETUP_MANDATE | NONE

type list = {
redirect_url: string,
currency: string,
payment_methods: array<methods>,
mandate_payment: option<mandate>,
payment_type: string,
payment_type: payment_type,
}

open Utils
Expand All @@ -733,7 +734,7 @@ let defaultList = {
currency: "",
payment_methods: [],
mandate_payment: None,
payment_type: "",
payment_type: NONE,
}
let getMethod = str => {
switch str {
Expand Down Expand Up @@ -927,13 +928,31 @@ let getMandate = (dict, str) => {
})
}

let paymentTypeMapper = payment_type => {
switch payment_type {
| "normal" => NORMAL
| "new_mandate" => NEW_MANDATE
| "setup_mandate" => SETUP_MANDATE
| _ => NONE
}
}

let paymentTypeToStringMapper = payment_type => {
switch payment_type {
| NORMAL => "normal"
| NEW_MANDATE => "new_mandate"
| SETUP_MANDATE => "setup_mandate"
| NONE => ""
}
}

let itemToObjMapper = dict => {
{
redirect_url: getString(dict, "redirect_url", ""),
currency: getString(dict, "currency", ""),
payment_methods: getMethodsArr(dict, "payment_methods"),
mandate_payment: getMandate(dict, "mandate_payment"),
payment_type: getString(dict, "payment_type", ""),
payment_type: getString(dict, "payment_type", "")->paymentTypeMapper,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/PaymentBody.res
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ let achBankDebitBody = (
->Js.Dict.fromArray
->Js.Json.object_,
),
]->Js.Array2.concat(mandateBody(paymentType))
]->Array.concat(mandateBody(paymentType->PaymentMethodsRecord.paymentTypeToStringMapper))

let sepaBankDebitBody = (
~fullName,
Expand Down
4 changes: 3 additions & 1 deletion src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,9 @@ let usePaymentIntent = (optLogger: option<OrcaLogger.loggerMake>, paymentType: p
body
->Js.Array2.concat(
bodyArr->Js.Array2.concatMany([
PaymentBody.mandateBody(mandatePaymentType),
PaymentBody.mandateBody(
mandatePaymentType->PaymentMethodsRecord.paymentTypeToStringMapper,
),
broswerInfo(),
]),
)
Expand Down
7 changes: 5 additions & 2 deletions src/Utilities/PaymentUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,11 @@ let getPaymentMethodName = (~paymentMethodType, ~paymentMethodName) => {
}
}

let isAppendingCustomerAcceptance = (~isGuestCustomer, ~paymentType) => {
!isGuestCustomer && (paymentType === "new_mandate" || paymentType === "setup_mandate")
let isAppendingCustomerAcceptance = (
~isGuestCustomer,
~paymentType: PaymentMethodsRecord.payment_type,
) => {
!isGuestCustomer && (paymentType === NEW_MANDATE || paymentType === SETUP_MANDATE)
}

let appendedCustomerAcceptance = (~isGuestCustomer, ~paymentType, ~body) => {
Expand Down
Loading