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: mandate changes for Saved Card flow #309

Merged
merged 16 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
@react.component
let make = (~isChecked, ~setIsChecked) => {
let make = (~isChecked, ~setIsChecked, ~list: PaymentMethodsRecord.list) => {
let {themeObj} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom)
let showFields = Recoil.useRecoilValueFromAtom(RecoilAtoms.showCardFieldsAtom)
let {business} = Recoil.useRecoilValueFromAtom(RecoilAtoms.optionAtom)

let css = `.container {
display: flex;
cursor: pointer;
position: relative;
justify-content: center;
align-items: center;
}

.container input {
Expand Down Expand Up @@ -46,13 +48,21 @@ let make = (~isChecked, ~setIsChecked) => {
? ("Checkbox--checked", "CheckboxInput--checked", "CheckboxLabel--checked")
: ("", "", "")

let valueToBeRendered = if showFields {
vsrivatsa-edinburgh marked this conversation as resolved.
Show resolved Hide resolved
localeString.saveCardDetails
} else if business.name !== "" {
prafulkoppalkar marked this conversation as resolved.
Show resolved Hide resolved
localeString.cardTerms(business.name)
} else {
localeString.cardTerms(list.merchant_name)
}

<div className={`Checkbox ${checkboxState} flex flex-row gap-2 items-center`}>
<style> {React.string(css)} </style>
<label className={`container CheckboxInput ${checkedState}`}>
<input type_={`checkbox`} onChange />
<div className={`checkmark CheckboxInput ${checkedState}`} />
<div className={`CheckboxLabel ${checkBoxLabelSate} ml-2`}>
{React.string(localeString.saveCardDetails)}
<div className={`checkmark CheckboxInput ${checkedState} mt-1`} />
<div className={`CheckboxLabel ${checkBoxLabelSate} ml-2 w-11/12`}>
vsrivatsa-edinburgh marked this conversation as resolved.
Show resolved Hide resolved
{React.string(valueToBeRendered)}
vsrivatsa-edinburgh marked this conversation as resolved.
Show resolved Hide resolved
</div>
</label>
</div>
Expand Down
48 changes: 44 additions & 4 deletions src/Components/SavedMethods.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open CardUtils
open Utils
@react.component
let make = (
~paymentToken,
Expand All @@ -10,6 +8,9 @@ let make = (
~paymentType,
~list,
) => {
open CardUtils
open Utils
open UtilityHooks
let {themeObj, localeString} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom)
let (showFields, setShowFields) = Recoil.useRecoilState(RecoilAtoms.showCardFieldsAtom)
let areRequiredFieldsValid = Recoil.useRecoilValueFromAtom(RecoilAtoms.areRequiredFieldsValid)
Expand All @@ -19,6 +20,9 @@ let make = (
postFailedSubmitResponse(~errortype="validation_error", ~message)
loggerState.setLogError(~value=message, ~eventName=INVALID_FORMAT, ())
}
let (isSaveCardsChecked, setIsSaveCardsChecked) = React.useState(_ => false)
let {displaySavedPaymentMethodsCheckbox} = Recoil.useRecoilValueFromAtom(RecoilAtoms.optionAtom)
let isGuestCustomer = useIsGuestCustomer()

let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Card)
let (token, _) = paymentToken
Expand All @@ -33,6 +37,13 @@ let make = (
}
}

let isCustomerAcceptanceRequired = useIsCustomerAcceptanceRequired(
~displaySavedPaymentMethodsCheckbox,
~isSaveCardsChecked,
~list,
~isGuestCustomer,
)

let bottomElement = {
savedMethods
->Array.mapWithIndex((obj, i) => {
Expand Down Expand Up @@ -90,7 +101,7 @@ let make = (

let paymentType = customerMethod.paymentMethodType->Option.getOr(customerMethod.paymentMethod)

UtilityHooks.useHandlePostMessages(~complete, ~empty, ~paymentType, ~savedMethod=true)
useHandlePostMessages(~complete, ~empty, ~paymentType, ~savedMethod=true)

let submitCallback = React.useCallback((ev: Window.event) => {
let json = ev.data->JSON.parseExn
Expand All @@ -103,6 +114,7 @@ let make = (
~customerId,
~cvcNumber,
~requiresCvv=customerMethod.requiresCvv,
~isCustomerAcceptanceRequired,
)
| _ => {
let paymentMethodType = switch customerMethod.paymentMethodType {
Expand All @@ -115,6 +127,7 @@ let make = (
~customerId,
~paymentMethod=customerMethod.paymentMethod,
~paymentMethodType,
~isCustomerAcceptanceRequired,
)
}
}
Expand Down Expand Up @@ -153,9 +166,20 @@ let make = (
}
}
}
}, (areRequiredFieldsValid, requiredFieldsBody, empty, complete, customerMethod))
}, (
areRequiredFieldsValid,
requiredFieldsBody,
empty,
complete,
customerMethod,
isCustomerAcceptanceRequired,
))
useSubmitPaymentData(submitCallback)

let conditionsForShowingSaveCardCheckbox = React.useMemo(() => {
!isGuestCustomer && list.payment_type !== SETUP_MANDATE && displaySavedPaymentMethodsCheckbox
}, (isGuestCustomer, list.payment_type, displaySavedPaymentMethodsCheckbox))

<div className="flex flex-col overflow-auto h-auto no-scrollbar animate-slowShow">
{if savedCardlength === 0 && (loadSavedCards === PaymentType.LoadingSavedCards || !showFields) {
<div
Expand All @@ -179,6 +203,22 @@ let make = (
} else {
<RenderIf condition={!showFields}> {bottomElement} </RenderIf>
}}
<RenderIf condition={conditionsForShowingSaveCardCheckbox}>
<div className="pt-4 pb-2 flex items-center justify-start">
<SaveDetailsCheckbox isChecked=isSaveCardsChecked setIsChecked=setIsSaveCardsChecked list />
</div>
</RenderIf>
<RenderIf condition={list.payment_type === SETUP_MANDATE}>
<div
className="opacity-50 text-xs mb-2 text-left"
style={ReactDOMStyle.make(
~color=themeObj.colorText,
~marginTop=themeObj.spacingGridColumn,
(),
)}>
<Terms mode={Card} />
</div>
</RenderIf>
<RenderIf condition={!showFields}>
<div
className="Label flex flex-row gap-3 items-end cursor-pointer"
Expand Down
13 changes: 13 additions & 0 deletions src/Hooks/UtilityHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ let useHandlePostMessages = (~complete, ~empty, ~paymentType, ~savedMethod=false
None
}, (complete, empty, paymentType))
}

let useIsCustomerAcceptanceRequired = (
~displaySavedPaymentMethodsCheckbox,
~isSaveCardsChecked,
~list: PaymentMethodsRecord.list,
~isGuestCustomer,
) => React.useMemo(() => {
if displaySavedPaymentMethodsCheckbox {
isSaveCardsChecked || list.payment_type === SETUP_MANDATE
} else {
!(isGuestCustomer || list.payment_type === NORMAL)
}
}, (isSaveCardsChecked, list.payment_type))
31 changes: 15 additions & 16 deletions src/Payments/CardPayment.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
type target = {checked: bool}
type event = {target: target}
open PaymentType
open PaymentModeType

@react.component
let make = (
Expand All @@ -12,7 +10,11 @@ let make = (
~paymentType: CardThemeType.mode,
~list: PaymentMethodsRecord.list,
) => {
open PaymentType
open PaymentModeType
open Utils
open UtilityHooks

let {config, themeObj, localeString} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom)
let options = Recoil.useRecoilValueFromAtom(RecoilAtoms.optionAtom)
let loggerState = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom)
Expand Down Expand Up @@ -81,27 +83,22 @@ let make = (
None
}, [complete])

UtilityHooks.useHandlePostMessages(
~complete=complete && areRequiredFieldsValid,
~empty,
~paymentType="card",
)
useHandlePostMessages(~complete=complete && areRequiredFieldsValid, ~empty, ~paymentType="card")

let isGuestCustomer = UtilityHooks.useIsGuestCustomer()
let isGuestCustomer = useIsGuestCustomer()
let isCvcValidValue = CardUtils.getBoolOptionVal(isCVCValid)
let (cardEmpty, cardComplete, cardInvalid) = CardUtils.useCardDetails(
~cvcNumber,
~isCVCValid,
~isCvcValidValue,
)

let isCustomerAcceptanceRequired = React.useMemo(() => {
if displaySavedPaymentMethodsCheckbox {
isSaveCardsChecked || list.payment_type === SETUP_MANDATE
} else {
!(isGuestCustomer || list.payment_type === NORMAL)
}
}, (isSaveCardsChecked, list.payment_type))
let isCustomerAcceptanceRequired = useIsCustomerAcceptanceRequired(
~displaySavedPaymentMethodsCheckbox,
~isSaveCardsChecked,
~list,
~isGuestCustomer,
)

let submitCallback = React.useCallback((ev: Window.event) => {
let json = ev.data->JSON.parseExn
Expand Down Expand Up @@ -277,7 +274,9 @@ let make = (
/>
<RenderIf condition={conditionsForShowingSaveCardCheckbox}>
<div className="pt-4 pb-2 flex items-center justify-start">
<AnimatedCheckbox isChecked=isSaveCardsChecked setIsChecked=setIsSaveCardsChecked />
<SaveDetailsCheckbox
isChecked=isSaveCardsChecked setIsChecked=setIsSaveCardsChecked list
/>
</div>
</RenderIf>
<RenderIf condition={isCustomerAcceptanceRequired}>
Expand Down
62 changes: 43 additions & 19 deletions src/Utilities/PaymentBody.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,27 @@ let boletoBody = (~socialSecurityNumber) => [
),
]

let savedCardBody = (~paymentToken, ~customerId, ~cvcNumber, ~requiresCvv) => {
let customerAcceptanceBody =
[
("acceptance_type", "online"->JSON.Encode.string),
("accepted_at", Date.now()->Js.Date.fromFloat->Date.toISOString->JSON.Encode.string),
(
"online",
[("user_agent", BrowserSpec.navigator.userAgent->JSON.Encode.string)]
->Dict.fromArray
->JSON.Encode.object,
),
]
->Dict.fromArray
->JSON.Encode.object

let savedCardBody = (
~paymentToken,
~customerId,
~cvcNumber,
~requiresCvv,
~isCustomerAcceptanceRequired,
) => {
let savedCardBody = [
("payment_method", "card"->JSON.Encode.string),
("payment_token", paymentToken->JSON.Encode.string),
Expand All @@ -75,29 +95,33 @@ let savedCardBody = (~paymentToken, ~customerId, ~cvcNumber, ~requiresCvv) => {
savedCardBody->Array.push(("card_cvc", cvcNumber->JSON.Encode.string))->ignore
}

if isCustomerAcceptanceRequired {
savedCardBody->Array.push(("customer_acceptance", customerAcceptanceBody))->ignore
}

savedCardBody
}

let customerAcceptanceBody =
[
("acceptance_type", "online"->JSON.Encode.string),
("accepted_at", Date.now()->Js.Date.fromFloat->Date.toISOString->JSON.Encode.string),
(
"online",
[("user_agent", BrowserSpec.navigator.userAgent->JSON.Encode.string)]
->Dict.fromArray
->JSON.Encode.object,
),
let savedPaymentMethodBody = (
~paymentToken,
~customerId,
~paymentMethod,
~paymentMethodType,
~isCustomerAcceptanceRequired,
) => {
let savedPaymentMethodBody = [
("payment_method", paymentMethod->JSON.Encode.string),
("payment_token", paymentToken->JSON.Encode.string),
("customer_id", customerId->JSON.Encode.string),
("payment_method_type", paymentMethodType),
]
->Dict.fromArray
->JSON.Encode.object

let savedPaymentMethodBody = (~paymentToken, ~customerId, ~paymentMethod, ~paymentMethodType) => [
("payment_method", paymentMethod->JSON.Encode.string),
("payment_token", paymentToken->JSON.Encode.string),
("customer_id", customerId->JSON.Encode.string),
("payment_method_type", paymentMethodType),
]
if isCustomerAcceptanceRequired {
savedPaymentMethodBody->Array.push(("customer_acceptance", customerAcceptanceBody))->ignore
}

savedPaymentMethodBody
}

let mandateBody = paymentType => {
[
Expand Down
Loading