From b9b503fac9c17410e5493543dacc905a8eebb996 Mon Sep 17 00:00:00 2001 From: Sagnik Mitra Date: Tue, 24 Sep 2024 18:23:29 +0530 Subject: [PATCH 1/2] feat: pass email and phone number to confirm call Pass email and phone number to confirm call --- src/Payments/PaypalSDKHelpers.res | 8 ++++++++ src/Types/PaymentType.res | 23 +++++++++++++++++++++++ src/Utilities/DynamicFieldsUtils.res | 22 +++++++++++++++++----- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 7738c4236..50f2be048 100644 --- a/src/Payments/PaypalSDKHelpers.res +++ b/src/Payments/PaypalSDKHelpers.res @@ -76,8 +76,16 @@ let loadPaypalSDK = ( ->Option.flatMap(JSON.Decode.object) ->Option.getOr(Dict.make()) let details = purchaseUnit->paypalShippingDetails + let payerDetails = + val + ->Utils.getDictFromJson + ->Dict.get("payer") + ->Option.flatMap(JSON.Decode.object) + ->Option.getOr(Dict.make()) + ->PaymentType.itemToPayerDetailsObjectMapper let requiredFieldsBody = DynamicFieldsUtils.getPaypalRequiredFields( ~details, + ~payerDetails, ~paymentMethodTypes, ~statesList=stateJson, ) diff --git a/src/Types/PaymentType.res b/src/Types/PaymentType.res index 5eb0d9065..2737249dc 100644 --- a/src/Types/PaymentType.res +++ b/src/Types/PaymentType.res @@ -173,6 +173,12 @@ type options = { displayDefaultSavedPaymentIcon: bool, hideCardNicknameField: bool, } + +type payerDetails = { + email: option, + phone: option, +} + let defaultCardDetails = { scheme: None, last4Digits: "", @@ -311,6 +317,12 @@ let defaultOptions = { displayDefaultSavedPaymentIcon: true, hideCardNicknameField: false, } + +let defaultPayerDetails = { + email: None, + phone: None, +} + let getLayout = (str, logger) => { switch str { | "tabs" => Tabs @@ -1061,3 +1073,14 @@ type loadType = Loading | Loaded(JSON.t) | SemiLoaded | LoadError(JSON.t) let getIsStoredPaymentMethodHasName = (savedMethod: customerMethods) => { savedMethod.card.cardHolderName->Option.getOr("")->String.length > 0 } + +let itemToPayerDetailsObjectMapper = dict => { + email: dict->Dict.get("email_address")->Option.flatMap(JSON.Decode.string), + phone: dict + ->Dict.get("phone") + ->Option.flatMap(JSON.Decode.object) + ->Option.flatMap(Dict.get(_, "phone_number")) + ->Option.flatMap(JSON.Decode.object) + ->Option.flatMap(Dict.get(_, "national_number")) + ->Option.flatMap(JSON.Decode.string), +} diff --git a/src/Utilities/DynamicFieldsUtils.res b/src/Utilities/DynamicFieldsUtils.res index 8c3d70b9f..7025f9756 100644 --- a/src/Utilities/DynamicFieldsUtils.res +++ b/src/Utilities/DynamicFieldsUtils.res @@ -1051,6 +1051,7 @@ let getGooglePayRequiredFields = ( let getPaypalRequiredFields = ( ~details: PaypalSDKTypes.details, + ~payerDetails: PaymentType.payerDetails=PaymentType.defaultPayerDetails, ~paymentMethodTypes: PaymentMethodsRecord.paymentMethodTypes, ~statesList, ) => { @@ -1072,12 +1073,23 @@ let getPaypalRequiredFields = ( } | ShippingAddressCountry(_) => details.shippingAddress.countryCode->Option.getOr("") | ShippingAddressPincode => details.shippingAddress.postalCode->Option.getOr("") - | Email => details.email + | Email => + switch item.required_field { + | "payment_method_data.billing.email" + | "shipping.email" => + payerDetails.email->Option.getOr("") + | _ => details.email + } | PhoneNumber => - switch (details.phone->Option.getOr(""), details.shippingAddress.phone->Option.getOr("")) { - | (phone, "") => phone - | ("", phone) => phone - | _ => "" + switch item.required_field { + | "payment_method_data.billing.phone.number" | "shipping.phone.number" => + payerDetails.phone->Option.getOr("") + | _ => + switch (details.phone, details.shippingAddress.phone) { + | (Some(phone), None) => phone + | (None, Some(phone)) => phone + | _ => "" + } } | _ => "" } From f8deb8b4b2c00ebebee053d0346ef5259df67342 Mon Sep 17 00:00:00 2001 From: Sagnik Mitra Date: Wed, 25 Sep 2024 16:47:44 +0530 Subject: [PATCH 2/2] refactor: replaced payee's email and phone with payer's, and refactored some code --- src/Payments/PaypalSDKHelpers.res | 4 +-- src/Types/PaymentType.res | 5 ---- src/Types/PaypalSDKTypes.res | 12 +++----- src/Utilities/DynamicFieldsUtils.res | 43 +++++++++------------------- src/Utilities/Utils.res | 22 +++++++------- 5 files changed, 30 insertions(+), 56 deletions(-) diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 50f2be048..6db9a71b5 100644 --- a/src/Payments/PaypalSDKHelpers.res +++ b/src/Payments/PaypalSDKHelpers.res @@ -75,7 +75,6 @@ let loadPaypalSDK = ( ->Array.get(0) ->Option.flatMap(JSON.Decode.object) ->Option.getOr(Dict.make()) - let details = purchaseUnit->paypalShippingDetails let payerDetails = val ->Utils.getDictFromJson @@ -83,9 +82,10 @@ let loadPaypalSDK = ( ->Option.flatMap(JSON.Decode.object) ->Option.getOr(Dict.make()) ->PaymentType.itemToPayerDetailsObjectMapper + + let details = purchaseUnit->paypalShippingDetails(payerDetails) let requiredFieldsBody = DynamicFieldsUtils.getPaypalRequiredFields( ~details, - ~payerDetails, ~paymentMethodTypes, ~statesList=stateJson, ) diff --git a/src/Types/PaymentType.res b/src/Types/PaymentType.res index 2737249dc..9a9566806 100644 --- a/src/Types/PaymentType.res +++ b/src/Types/PaymentType.res @@ -318,11 +318,6 @@ let defaultOptions = { hideCardNicknameField: false, } -let defaultPayerDetails = { - email: None, - phone: None, -} - let getLayout = (str, logger) => { switch str { | "tabs" => Tabs diff --git a/src/Types/PaypalSDKTypes.res b/src/Types/PaypalSDKTypes.res index 854cfae44..48c01ac9c 100644 --- a/src/Types/PaypalSDKTypes.res +++ b/src/Types/PaypalSDKTypes.res @@ -125,9 +125,8 @@ let getShippingDetails = shippingAddressOverrideObj => { } } -let paypalShippingDetails = purchaseUnit => { +let paypalShippingDetails = (purchaseUnit, payerDetails: PaymentType.payerDetails) => { let shippingAddress = purchaseUnit->Utils.getDictFromDict("shipping") - let payee = purchaseUnit->Utils.getDictFromDict("payee") let address = shippingAddress->Utils.getDictFromDict("address") let name = shippingAddress->Utils.getDictFromDict("name") @@ -139,10 +138,7 @@ let paypalShippingDetails = purchaseUnit => { let countryCode = address->Utils.getOptionString("country_code") let postalCode = address->Utils.getOptionString("postal_code") let state = address->Utils.getOptionString("admin_area_1") - let email = payee->Utils.getString("email_address", "") - - let payeePhone = payee->Utils.getOptionString("phone") - let shippingAddressPhone = address->Utils.getOptionString("phone") + let email = payerDetails.email->Option.getOr("") { email, @@ -154,9 +150,9 @@ let paypalShippingDetails = purchaseUnit => { countryCode, postalCode, state, - phone: shippingAddressPhone, + phone: payerDetails.phone, }, - phone: payeePhone, + phone: payerDetails.phone, } } diff --git a/src/Utilities/DynamicFieldsUtils.res b/src/Utilities/DynamicFieldsUtils.res index 7025f9756..f41c10b79 100644 --- a/src/Utilities/DynamicFieldsUtils.res +++ b/src/Utilities/DynamicFieldsUtils.res @@ -1051,7 +1051,6 @@ let getGooglePayRequiredFields = ( let getPaypalRequiredFields = ( ~details: PaypalSDKTypes.details, - ~payerDetails: PaymentType.payerDetails=PaymentType.defaultPayerDetails, ~paymentMethodTypes: PaymentMethodsRecord.paymentMethodTypes, ~statesList, ) => { @@ -1060,43 +1059,27 @@ let getPaypalRequiredFields = ( let fieldVal = switch item.field_type { | ShippingName => { - let name = details.shippingAddress.recipientName->Option.getOr("") - name->getNameFromString(requiredFieldsArr) + let name = details.shippingAddress.recipientName + name->Option.map(getNameFromString(_, requiredFieldsArr)) } - | ShippingAddressLine1 => details.shippingAddress.line1->Option.getOr("") - | ShippingAddressLine2 => details.shippingAddress.line2->Option.getOr("") - | ShippingAddressCity => details.shippingAddress.city->Option.getOr("") + | ShippingAddressLine1 => details.shippingAddress.line1 + | ShippingAddressLine2 => details.shippingAddress.line2 + | ShippingAddressCity => details.shippingAddress.city | ShippingAddressState => { let administrativeArea = details.shippingAddress.state->Option.getOr("") let countryCode = details.shippingAddress.countryCode->Option.getOr("") - Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode) - } - | ShippingAddressCountry(_) => details.shippingAddress.countryCode->Option.getOr("") - | ShippingAddressPincode => details.shippingAddress.postalCode->Option.getOr("") - | Email => - switch item.required_field { - | "payment_method_data.billing.email" - | "shipping.email" => - payerDetails.email->Option.getOr("") - | _ => details.email - } - | PhoneNumber => - switch item.required_field { - | "payment_method_data.billing.phone.number" | "shipping.phone.number" => - payerDetails.phone->Option.getOr("") - | _ => - switch (details.phone, details.shippingAddress.phone) { - | (Some(phone), None) => phone - | (None, Some(phone)) => phone - | _ => "" - } + Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode)->Some } - | _ => "" + | ShippingAddressCountry(_) => details.shippingAddress.countryCode + | ShippingAddressPincode => details.shippingAddress.postalCode + | Email => details.email->Some + | PhoneNumber => details.phone + | _ => None } - if fieldVal !== "" { + fieldVal->Option.mapOr((), fieldVal => acc->Dict.set(item.required_field, fieldVal->JSON.Encode.string) - } + ) acc }) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 0855da98f..016f92d1d 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -1286,21 +1286,21 @@ let getStateNameFromStateCodeAndCountry = (list: JSON.t, stateCode: string, coun list ->getDictFromJson ->getOptionalArrayFromDict(country) - ->Option.getOr([]) - let val = options->Array.find(item => - item - ->getDictFromJson - ->getString("code", "") === stateCode + options + ->Option.flatMap( + Array.find(_, item => + item + ->getDictFromJson + ->getString("code", "") === stateCode + ), ) - - switch val { - | Some(stateObj) => + ->Option.flatMap(stateObj => stateObj ->getDictFromJson - ->getString("name", stateCode) - | None => stateCode - } + ->getOptionString("name") + ) + ->Option.getOr(stateCode) } let removeHyphen = str => str->String.replaceRegExp(%re("/-/g"), "")