From 412ba68b0c32698cc6de8b0e5e2dcfeec2741d15 Mon Sep 17 00:00:00 2001 From: Sagnik Mitra <83326850+ImSagnik007@users.noreply.github.com> Date: Thu, 26 Sep 2024 18:06:24 +0530 Subject: [PATCH] feat: pass email and phone number to confirm call (#681) Co-authored-by: Pritish Budhiraja --- src/Payments/PaypalSDKHelpers.res | 10 ++++++++- src/Types/PaymentType.res | 18 ++++++++++++++++ src/Types/PaypalSDKTypes.res | 12 ++++------- src/Utilities/DynamicFieldsUtils.res | 31 ++++++++++++---------------- src/Utilities/Utils.res | 22 ++++++++++---------- 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 7738c4236..6db9a71b5 100644 --- a/src/Payments/PaypalSDKHelpers.res +++ b/src/Payments/PaypalSDKHelpers.res @@ -75,7 +75,15 @@ let loadPaypalSDK = ( ->Array.get(0) ->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 details = purchaseUnit->paypalShippingDetails(payerDetails) let requiredFieldsBody = DynamicFieldsUtils.getPaypalRequiredFields( ~details, ~paymentMethodTypes, diff --git a/src/Types/PaymentType.res b/src/Types/PaymentType.res index 29c0b522a..86c0b06c1 100644 --- a/src/Types/PaymentType.res +++ b/src/Types/PaymentType.res @@ -174,6 +174,12 @@ type options = { hideCardNicknameField: bool, customMessageForCardTerms: string, } + +type payerDetails = { + email: option, + phone: option, +} + let defaultCardDetails = { scheme: None, last4Digits: "", @@ -313,6 +319,7 @@ let defaultOptions = { hideCardNicknameField: false, customMessageForCardTerms: "", } + let getLayout = (str, logger) => { switch str { | "tabs" => Tabs @@ -1065,3 +1072,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/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 8c3d70b9f..f41c10b79 100644 --- a/src/Utilities/DynamicFieldsUtils.res +++ b/src/Utilities/DynamicFieldsUtils.res @@ -1059,32 +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 => details.email - | PhoneNumber => - switch (details.phone->Option.getOr(""), details.shippingAddress.phone->Option.getOr("")) { - | (phone, "") => phone - | ("", 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"), "")