Skip to content

Commit

Permalink
refactor: replaced payee's email and phone with payer's, and refactor…
Browse files Browse the repository at this point in the history
…ed some code
  • Loading branch information
ImSagnik007 committed Sep 25, 2024
1 parent b9b503f commit f8deb8b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ 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,
~payerDetails,
~paymentMethodTypes,
~statesList=stateJson,
)
Expand Down
5 changes: 0 additions & 5 deletions src/Types/PaymentType.res
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ let defaultOptions = {
hideCardNicknameField: false,
}

let defaultPayerDetails = {
email: None,
phone: None,
}

let getLayout = (str, logger) => {
switch str {
| "tabs" => Tabs
Expand Down
12 changes: 4 additions & 8 deletions src/Types/PaypalSDKTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -154,9 +150,9 @@ let paypalShippingDetails = purchaseUnit => {
countryCode,
postalCode,
state,
phone: shippingAddressPhone,
phone: payerDetails.phone,
},
phone: payeePhone,
phone: payerDetails.phone,
}
}

Expand Down
43 changes: 13 additions & 30 deletions src/Utilities/DynamicFieldsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,6 @@ let getGooglePayRequiredFields = (

let getPaypalRequiredFields = (
~details: PaypalSDKTypes.details,
~payerDetails: PaymentType.payerDetails=PaymentType.defaultPayerDetails,
~paymentMethodTypes: PaymentMethodsRecord.paymentMethodTypes,
~statesList,
) => {
Expand All @@ -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
})
Expand Down
22 changes: 11 additions & 11 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"), "")
Expand Down

0 comments on commit f8deb8b

Please sign in to comment.