From f86f7dcdc8312ac385bff5d45f12f211adf0b5a7 Mon Sep 17 00:00:00 2001 From: Saksham Sharma Date: Thu, 7 Nov 2024 14:33:08 +0530 Subject: [PATCH] refactor: added utility for mergeAndFlattenToTuples --- src/Components/SavedMethods.res | 18 +++--------------- src/Payments/ACHBankTransfer.res | 8 +++----- src/Payments/CardPayment.res | 6 +----- src/Payments/KlarnaSDK.res | 6 +----- src/Payments/PaymentMethodsWrapper.res | 7 +------ src/Payments/PaypalSDKHelpers.res | 7 +------ src/Payments/SepaBankTransfer.res | 8 +++----- src/Utilities/ApplePayHelpers.res | 12 ++---------- src/Utilities/GooglePayHelpers.res | 12 ++---------- src/Utilities/Utils.res | 7 +++++++ 10 files changed, 24 insertions(+), 67 deletions(-) diff --git a/src/Components/SavedMethods.res b/src/Components/SavedMethods.res index ae2dbdf1..377deb88 100644 --- a/src/Components/SavedMethods.res +++ b/src/Components/SavedMethods.res @@ -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, @@ -168,11 +164,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, @@ -180,11 +172,7 @@ let make = ( } | _ => intent( - ~bodyArr=savedPaymentMethodBody - ->getJsonFromArrayOfJson - ->flattenObject(true) - ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) - ->getArrayOfTupleFromDict, + ~bodyArr=savedPaymentMethodBody->mergeAndFlattenToTuples(requiredFieldsBody), ~confirmParam=confirm.confirmParams, ~handleUserError=false, ~manualRetry=isManualRetryEnabled, diff --git a/src/Payments/ACHBankTransfer.res b/src/Payments/ACHBankTransfer.res index 98a91da3..c7fc5b91 100644 --- a/src/Payments/ACHBankTransfer.res +++ b/src/Payments/ACHBankTransfer.res @@ -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, diff --git a/src/Payments/CardPayment.res b/src/Payments/CardPayment.res index da4da5c9..05c54066 100644 --- a/src/Payments/CardPayment.res +++ b/src/Payments/CardPayment.res @@ -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, diff --git a/src/Payments/KlarnaSDK.res b/src/Payments/KlarnaSDK.res index a53771a7..ea7ab530 100644 --- a/src/Payments/KlarnaSDK.res +++ b/src/Payments/KlarnaSDK.res @@ -100,11 +100,7 @@ let make = (~sessionObj: SessionsType.token) => { ) let body = { - klarnaSDKBody - ->getJsonFromArrayOfJson - ->flattenObject(true) - ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) - ->getArrayOfTupleFromDict + klarnaSDKBody->mergeAndFlattenToTuples(requiredFieldsBody) } res.approved diff --git a/src/Payments/PaymentMethodsWrapper.res b/src/Payments/PaymentMethodsWrapper.res index 857a796e..cb4cf062 100644 --- a/src/Payments/PaymentMethodsWrapper.res +++ b/src/Payments/PaymentMethodsWrapper.res @@ -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, diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 2b22e4cc..d50d0a33 100644 --- a/src/Payments/PaypalSDKHelpers.res +++ b/src/Payments/PaypalSDKHelpers.res @@ -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, diff --git a/src/Payments/SepaBankTransfer.res b/src/Payments/SepaBankTransfer.res index 8f9c4bd2..ca543425 100644 --- a/src/Payments/SepaBankTransfer.res +++ b/src/Payments/SepaBankTransfer.res @@ -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, diff --git a/src/Utilities/ApplePayHelpers.res b/src/Utilities/ApplePayHelpers.res index 71b92d0d..3a342cac 100644 --- a/src/Utilities/ApplePayHelpers.res +++ b/src/Utilities/ApplePayHelpers.res @@ -61,11 +61,7 @@ let getApplePayFromResponse = ( let bodyDict = PaymentBody.applePayBody(~token, ~connectors) - bodyDict - ->getJsonFromArrayOfJson - ->flattenObject(true) - ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) - ->getArrayOfTupleFromDict + bodyDict->mergeAndFlattenToTuples(requiredFieldsBody) } let startApplePaySession = ( @@ -261,11 +257,7 @@ let useHandleApplePayResponse = ( let bodyArr = if isWallet { applePayBody } else { - applePayBody - ->getJsonFromArrayOfJson - ->flattenObject(true) - ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) - ->getArrayOfTupleFromDict + applePayBody->mergeAndFlattenToTuples(requiredFieldsBody) } processPayment( diff --git a/src/Utilities/GooglePayHelpers.res b/src/Utilities/GooglePayHelpers.res index 4f4e961e..8552941d 100644 --- a/src/Utilities/GooglePayHelpers.res +++ b/src/Utilities/GooglePayHelpers.res @@ -53,11 +53,7 @@ let getGooglePayBodyFromResponse = ( ) } - gPayBody - ->getJsonFromArrayOfJson - ->flattenObject(true) - ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) - ->getArrayOfTupleFromDict + gPayBody->mergeAndFlattenToTuples(requiredFieldsBody) } let processPayment = ( @@ -123,11 +119,7 @@ let useHandleGooglePayResponse = ( let googlePayBody = if isWallet { body } else { - body - ->getJsonFromArrayOfJson - ->flattenObject(true) - ->mergeTwoFlattenedJsonDicts(requiredFieldsBody) - ->getArrayOfTupleFromDict + body->mergeAndFlattenToTuples(requiredFieldsBody) } processPayment( diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 49b177a6..45c43cc0 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -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