diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 50f2be04..6db9a71b 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 2737249d..9a956680 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 854cfae4..48c01ac9 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 7025f975..f41c10b7 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 0855da98..016f92d1 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"), "")