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

refactor: added utility for mergeAndFlattenToTuples #778

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 12 additions & 15 deletions src/Components/DynamicFields.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open RecoilAtoms
@react.component
let make = (
~paymentType,
Expand All @@ -12,7 +11,9 @@ let make = (
~cvcProps=None,
~isBancontact=false,
) => {
open DynamicFieldsUtils
open Utils
open RecoilAtoms
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)

React.useEffect(() => {
Expand Down Expand Up @@ -41,20 +42,16 @@ let make = (

paymentMethodTypes.required_fields
->Array.concat(creditRequiredFields)
->DynamicFieldsUtils.removeRequiredFieldsDuplicates
} else if (
PaymentMethodsRecord.dynamicFieldsEnabledPaymentMethods->Array.includes(paymentMethodType)
) {
->removeRequiredFieldsDuplicates
} else if dynamicFieldsEnabledPaymentMethods->Array.includes(paymentMethodType) {
paymentMethodTypes.required_fields
} else {
[]
}
}, (paymentMethod, paymentMethodTypes.required_fields, paymentMethodType))

let requiredFields = React.useMemo(() => {
requiredFieldsWithBillingDetails->DynamicFieldsUtils.removeBillingDetailsIfUseBillingAddress(
billingAddress,
)
requiredFieldsWithBillingDetails->removeBillingDetailsIfUseBillingAddress(billingAddress)
}, [requiredFieldsWithBillingDetails])

let isAllStoredCardsHaveName = React.useMemo(() => {
Expand All @@ -69,7 +66,7 @@ let make = (
~isSavedCardFlow,
~isAllStoredCardsHaveName,
)
->DynamicFieldsUtils.updateDynamicFields(billingAddress)
->updateDynamicFields(billingAddress)
->Belt.SortArray.stableSortBy(PaymentMethodsRecord.sortPaymentMethodFields)
//<...>//
}, (requiredFields, isAllStoredCardsHaveName, isSavedCardFlow))
Expand Down Expand Up @@ -255,7 +252,7 @@ let make = (
}
}

DynamicFieldsUtils.useRequiredFieldsEmptyAndValid(
useRequiredFieldsEmptyAndValid(
~requiredFields,
~fieldsArr,
~countryNames,
Expand All @@ -268,14 +265,14 @@ let make = (
~cvcNumber,
)

DynamicFieldsUtils.useSetInitialRequiredFields(
useSetInitialRequiredFields(
~requiredFields={
billingAddress.usePrefilledValues === Auto ? requiredFieldsWithBillingDetails : requiredFields
},
~paymentMethodType,
)

DynamicFieldsUtils.useRequiredFieldsBody(
useRequiredFieldsBody(
~requiredFields,
~paymentMethodType,
~cardNumber,
Expand All @@ -286,7 +283,7 @@ let make = (
~setRequiredFieldsBody,
)

let submitCallback = DynamicFieldsUtils.useSubmitCallback()
let submitCallback = useSubmitCallback()
useSubmitPaymentData(submitCallback)

let bottomElement = <InfoElement />
Expand All @@ -307,11 +304,11 @@ let make = (
}

let dynamicFieldsToRenderOutsideBilling = React.useMemo(() => {
fieldsArr->Array.filter(DynamicFieldsUtils.isFieldTypeToRenderOutsideBilling)
fieldsArr->Array.filter(isFieldTypeToRenderOutsideBilling)
}, [fieldsArr])

let dynamicFieldsToRenderInsideBilling = React.useMemo(() => {
fieldsArr->Array.filter(field => !(field->DynamicFieldsUtils.isFieldTypeToRenderOutsideBilling))
fieldsArr->Array.filter(field => !(field->isFieldTypeToRenderOutsideBilling))
}, [fieldsArr])

let isInfoElementPresent = dynamicFieldsToRenderInsideBilling->Array.includes(InfoElement)
Expand Down
18 changes: 3 additions & 15 deletions src/Components/SavedMethods.res
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ let make = (
| _ =>
// TODO - To be replaced with proper error message
intent(
~bodyArr=savedPaymentMethodBody
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict,
~bodyArr=savedPaymentMethodBody->mergeAndFlattenToTuples(requiredFieldsBody),
~confirmParam=confirm.confirmParams,
~handleUserError=false,
~manualRetry=isManualRetryEnabled,
Expand All @@ -168,23 +164,15 @@ let make = (
| _ =>
// TODO - To be replaced with proper error message
intent(
~bodyArr=savedPaymentMethodBody
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict,
~bodyArr=savedPaymentMethodBody->mergeAndFlattenToTuples(requiredFieldsBody),
~confirmParam=confirm.confirmParams,
~handleUserError=false,
~manualRetry=isManualRetryEnabled,
)
}
| _ =>
intent(
~bodyArr=savedPaymentMethodBody
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict,
~bodyArr=savedPaymentMethodBody->mergeAndFlattenToTuples(requiredFieldsBody),
~confirmParam=confirm.confirmParams,
~handleUserError=false,
~manualRetry=isManualRetryEnabled,
Expand Down
8 changes: 3 additions & 5 deletions src/Payments/ACHBankTransfer.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ let make = (~paymentType: CardThemeType.mode) => {
if confirm.doSubmit {
if complete {
let bodyArr =
PaymentBody.dynamicPaymentBody("bank_transfer", "ach")
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
PaymentBody.dynamicPaymentBody("bank_transfer", "ach")->mergeAndFlattenToTuples(
requiredFieldsBody,
)
intent(
~bodyArr,
~confirmParam=confirm.confirmParams,
Expand Down
6 changes: 1 addition & 5 deletions src/Payments/CardPayment.res
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ let make = (
if validFormat && (showFields || isBancontact) {
intent(
~bodyArr={
(isBancontact ? banContactBody : cardBody)
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
(isBancontact ? banContactBody : cardBody)->mergeAndFlattenToTuples(requiredFieldsBody)
},
~confirmParam=confirm.confirmParams,
~handleUserError=false,
Expand Down
6 changes: 1 addition & 5 deletions src/Payments/KlarnaSDK.res
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ let make = (~sessionObj: SessionsType.token) => {
)

let body = {
klarnaSDKBody
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
klarnaSDKBody->mergeAndFlattenToTuples(requiredFieldsBody)
}

res.approved
Expand Down
23 changes: 0 additions & 23 deletions src/Payments/PaymentMethodsRecord.res
Original file line number Diff line number Diff line change
Expand Up @@ -661,29 +661,6 @@ let getFieldType = (dict, isBancontact) => {
}
}

let dynamicFieldsEnabledPaymentMethods = [
"crypto_currency",
"debit",
"credit",
"blik",
"google_pay",
"apple_pay",
"bancontact_card",
"open_banking_uk",
"eps",
"ideal",
"sofort",
"pix_transfer",
"giropay",
"local_bank_transfer_transfer",
"afterpay_clearpay",
"mifinity",
"upi_collect",
"sepa",
"affirm",
"ach",
]

let getIsBillingField = requiredFieldType => {
switch requiredFieldType {
| AddressLine1
Expand Down
7 changes: 1 addition & 6 deletions src/Payments/PaymentMethodsWrapper.res
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@ let make = (~paymentType: CardThemeType.mode, ~paymentMethodName: string) => {
phoneNumber.countryCode->Option.getOr("") ++ phoneNumber.value,
),
~paymentExperience=paymentFlow,
)
->Dict.fromArray
->JSON.Encode.object
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
)->mergeAndFlattenToTuples(requiredFieldsBody)

intent(
~bodyArr=body,
Expand Down
7 changes: 1 addition & 6 deletions src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,7 @@ let loadBraintreePaypalSdk = (
~statesList=stateJson,
)

let paypalBody =
body
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
let paypalBody = body->mergeAndFlattenToTuples(requiredFieldsBody)

let modifiedPaymentBody = PaymentUtils.appendedCustomerAcceptance(
~isGuestCustomer,
Expand Down
8 changes: 3 additions & 5 deletions src/Payments/SepaBankTransfer.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ let make = (~paymentType) => {
if confirm.doSubmit {
if complete {
let bodyArr =
PaymentBody.dynamicPaymentBody("bank_transfer", "sepa")
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
PaymentBody.dynamicPaymentBody("bank_transfer", "sepa")->mergeAndFlattenToTuples(
requiredFieldsBody,
)

intent(
~bodyArr,
Expand Down
12 changes: 2 additions & 10 deletions src/Utilities/ApplePayHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ let getApplePayFromResponse = (

let bodyDict = PaymentBody.applePayBody(~token, ~connectors)

bodyDict
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
bodyDict->mergeAndFlattenToTuples(requiredFieldsBody)
}

let startApplePaySession = (
Expand Down Expand Up @@ -261,11 +257,7 @@ let useHandleApplePayResponse = (
let bodyArr = if isWallet {
applePayBody
} else {
applePayBody
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
applePayBody->mergeAndFlattenToTuples(requiredFieldsBody)
}

processPayment(
Expand Down
23 changes: 23 additions & 0 deletions src/Utilities/DynamicFieldsUtils.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
open RecoilAtoms

let dynamicFieldsEnabledPaymentMethods = [
"crypto_currency",
"debit",
"credit",
"blik",
"google_pay",
"apple_pay",
"bancontact_card",
"open_banking_uk",
"eps",
"ideal",
"sofort",
"pix_transfer",
"giropay",
"local_bank_transfer_transfer",
"afterpay_clearpay",
"mifinity",
"upi_collect",
"sepa",
"affirm",
"ach",
]

let getName = (item: PaymentMethodsRecord.required_fields, field: RecoilAtomTypes.field) => {
let fieldNameArr = field.value->String.split(" ")
let requiredFieldsArr = item.required_field->String.split(".")
Expand Down
12 changes: 2 additions & 10 deletions src/Utilities/GooglePayHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ let getGooglePayBodyFromResponse = (
)
}

gPayBody
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
gPayBody->mergeAndFlattenToTuples(requiredFieldsBody)
}

let processPayment = (
Expand Down Expand Up @@ -123,11 +119,7 @@ let useHandleGooglePayResponse = (
let googlePayBody = if isWallet {
body
} else {
body
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
body->mergeAndFlattenToTuples(requiredFieldsBody)
}

processPayment(
Expand Down
7 changes: 7 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1423,3 +1423,10 @@ let isKeyPresentInDict = (dict, key) => dict->Dict.get(key)->Option.isSome
let checkIsTestCardWildcard = val => ["1111222233334444"]->Array.includes(val)

let minorUnitToString = val => (val->Int.toFloat /. 100.)->Float.toString

let mergeAndFlattenToTuples = (body, requiredFieldsBody) =>
body
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict
Loading