Skip to content

Commit

Permalink
fix: added support for ShippingAddress enums for Dynamic Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushKapoorJuspay committed May 15, 2024
1 parent f0136a6 commit 7588298
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 76 deletions.
16 changes: 15 additions & 1 deletion src/Components/DynamicFields.res
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,14 @@ let make = (
| BlikCode
| SpecialField(_)
| CountryAndPincode(_)
| AddressCountry(_) => React.null
| AddressCountry(_)
| ShippingName // Shipping Details are currently supported by only one click widgets
| ShippingAddressLine1
| ShippingAddressLine2
| ShippingAddressCity
| ShippingAddressPincode
| ShippingAddressState
| ShippingAddressCountry(_) => React.null
}}
</div>
})
Expand Down Expand Up @@ -757,6 +764,13 @@ let make = (
| CardExpiryAndCvc
| Currency(_)
| FullName
| ShippingName // Shipping Details are currently supported by only one click widgets
| ShippingAddressLine1
| ShippingAddressLine2
| ShippingAddressCity
| ShippingAddressPincode
| ShippingAddressState
| ShippingAddressCountry(_)
| None => React.null
}}
</div>
Expand Down
45 changes: 37 additions & 8 deletions src/Payments/PaymentMethodsRecord.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ type paymentMethodsFields =
| CardExpiryMonthAndYear
| CardCvc
| CardExpiryAndCvc
| ShippingName
| ShippingAddressLine1
| ShippingAddressLine2
| ShippingAddressCity
| ShippingAddressPincode
| ShippingAddressState
| ShippingAddressCountry(array<string>)

let getPaymentMethodsFieldsOrder = paymentMethodField => {
switch paymentMethodField {
Expand Down Expand Up @@ -526,21 +533,31 @@ let getPaymentMethodsFieldTypeFromString = (str, isBancontact) => {
| ("user_card_expiry_month", true) => CardExpiryMonth
| ("user_card_expiry_year", true) => CardExpiryYear
| ("user_card_cvc", true) => CardCvc
| ("user_shipping_name", _) => ShippingName
| ("user_shipping_address_line1", _) => ShippingAddressLine1
| ("user_shipping_address_line2", _) => ShippingAddressLine2
| ("user_shipping_address_city", _) => ShippingAddressCity
| ("user_shipping_address_pincode", _) => ShippingAddressPincode
| ("user_shipping_address_state", _) => ShippingAddressState
| _ => None
}
}

let getOptionsFromPaymentMethodFieldType = (dict, key) => {
let getOptionsFromPaymentMethodFieldType = (dict, key, ~isAddressCountry=true) => {
let options = dict->Utils.getArrayValFromJsonDict(key, "options")
switch options->Array.get(0)->Option.getOr("") {
| "" => None
| "ALL" => AddressCountry(Country.country->Array.map(item => item.countryName))
| _ =>
AddressCountry(
Country.country
->Array.filter(item => options->Array.includes(item.isoAlpha2))
->Array.map(item => item.countryName),
)
| "ALL" => {
let countryArr = Country.country->Array.map(item => item.countryName)
isAddressCountry ? AddressCountry(countryArr) : ShippingAddressCountry(countryArr)
}
| _ => {
let countryArr =
Country.country
->Array.filter(item => options->Array.includes(item.isoAlpha2))
->Array.map(item => item.countryName)
isAddressCountry ? AddressCountry(countryArr) : ShippingAddressCountry(countryArr)
}
}
}

Expand All @@ -554,6 +571,11 @@ let getPaymentMethodsFieldTypeFromDict = dict => {
}
| "user_country" => dict->getOptionsFromPaymentMethodFieldType("user_country")
| "user_address_country" => dict->getOptionsFromPaymentMethodFieldType("user_address_country")
| "user_shipping_address_country" =>
dict->getOptionsFromPaymentMethodFieldType(
"user_shipping_address_country",
~isAddressCountry=false,
)
| _ => None
}
}
Expand Down Expand Up @@ -1057,5 +1079,12 @@ let paymentMethodFieldToStrMapper = (field: paymentMethodsFields) => {
| CardExpiryMonthAndYear => "CardExpiryMonthAndYear"
| CardCvc => "CardCvc"
| CardExpiryAndCvc => "CardExpiryAndCvc"
| ShippingName => "ShippingName"
| ShippingAddressLine1 => "ShippingAddressLine1"
| ShippingAddressLine2 => "ShippingAddressLine2"
| ShippingAddressCity => "ShippingAddressCity"
| ShippingAddressPincode => "ShippingAddressPincode"
| ShippingAddressState => "ShippingAddressState"
| ShippingAddressCountry(_) => "ShippingAddressCountry"
}
}
145 changes: 78 additions & 67 deletions src/Utilities/DynamicFieldsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ let useSetInitialRequiredFields = (
| CardExpiryMonthAndYear
| CardCvc
| CardExpiryAndCvc
| ShippingName // Shipping Details are currently supported by only one click widgets
| ShippingAddressLine1
| ShippingAddressLine2
| ShippingAddressCity
| ShippingAddressPincode
| ShippingAddressState
| ShippingAddressCountry(_)
| None => ()
}
})
Expand Down Expand Up @@ -435,6 +442,13 @@ let useRequiredFieldsBody = (
| CardExpiryMonthAndYear
| CardExpiryAndCvc
| FullName
| ShippingName // Shipping Details are currently supported by only one click widgets
| ShippingAddressLine1
| ShippingAddressLine2
| ShippingAddressCity
| ShippingAddressPincode
| ShippingAddressState
| ShippingAddressCountry(_)
| None => ""
}
}
Expand Down Expand Up @@ -748,14 +762,7 @@ let getApplePayRequiredFields = (
paymentMethodTypes.required_fields->Array.reduce(Dict.make(), (acc, item) => {
let requiredFieldsArr = item.required_field->String.split(".")

let getBillingOrShipping = (billingVal, shippingVal) => {
requiredFieldsArr->Array.includes("billing") ? billingVal : shippingVal
}

let getName = {
let firstName = billingContact.givenName->getBillingOrShipping(shippingContact.givenName)
let lastName = billingContact.familyName->getBillingOrShipping(shippingContact.familyName)

let getName = (firstName, lastName) => {
switch requiredFieldsArr->Array.get(requiredFieldsArr->Array.length - 1)->Option.getOr("") {
| "first_name" => firstName
| "last_name" => lastName
Expand All @@ -768,29 +775,36 @@ let getApplePayRequiredFields = (
}

let fieldVal = switch item.field_type {
| FullName => getName
| BillingName => getName
| AddressLine1 =>
let billingAddressLine1 = billingContact.addressLines->getAddressLine(0)
let shippingAddressLine1 = shippingContact.addressLines->getAddressLine(0)
billingAddressLine1->getBillingOrShipping(shippingAddressLine1)
| AddressLine2 =>
let billingAddressLine1 = billingContact.addressLines->getAddressLine(1)
let shippingAddressLine1 = shippingContact.addressLines->getAddressLine(1)
billingAddressLine1->getBillingOrShipping(shippingAddressLine1)
| AddressCity => billingContact.locality->getBillingOrShipping(shippingContact.locality)
| FullName
| BillingName =>
getName(billingContact.givenName, billingContact.familyName)
| AddressLine1 => billingContact.addressLines->getAddressLine(0)
| AddressLine2 => billingContact.addressLines->getAddressLine(1)
| AddressCity => billingContact.locality
| AddressState =>
let administrativeArea =
billingContact.administrativeArea->getBillingOrShipping(shippingContact.administrativeArea)
let countryCode =
billingContact.countryCode->getBillingOrShipping(shippingContact.countryCode)
Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode)
Utils.getStateNameFromStateCodeAndCountry(
statesList,
billingContact.administrativeArea,
billingContact.countryCode,
)
| Country
| AddressCountry(_) =>
billingContact.countryCode->getBillingOrShipping(shippingContact.countryCode)
| AddressPincode => billingContact.postalCode->getBillingOrShipping(shippingContact.postalCode)
billingContact.countryCode
| AddressPincode => billingContact.postalCode
| Email => shippingContact.emailAddress
| PhoneNumber => shippingContact.phoneNumber
| ShippingName => getName(shippingContact.givenName, shippingContact.familyName)
| ShippingAddressLine1 => shippingContact.addressLines->getAddressLine(0)
| ShippingAddressLine2 => shippingContact.addressLines->getAddressLine(1)
| ShippingAddressCity => shippingContact.locality
| ShippingAddressState =>
Utils.getStateNameFromStateCodeAndCountry(
statesList,
shippingContact.administrativeArea,
shippingContact.countryCode,
)
| ShippingAddressCountry(_) => shippingContact.countryCode
| ShippingAddressPincode => shippingContact.postalCode
| _ => ""
}

Expand Down Expand Up @@ -834,35 +848,37 @@ let getGooglePayRequiredFields = (
paymentMethodTypes.required_fields->Array.reduce(Dict.make(), (acc, item) => {
let requiredFieldsArr = item.required_field->String.split(".")

let getBillingOrShipping = (billingVal, shippingVal) => {
requiredFieldsArr->Array.includes("billing") ? billingVal : shippingVal
}

let getName = {
let name = billingContact.name->getBillingOrShipping(shippingContact.name)

name->getNameFromString(requiredFieldsArr)
}

let fieldVal = switch item.field_type {
| FullName => getName
| BillingName => getName
| AddressLine1 => billingContact.address1->getBillingOrShipping(shippingContact.address1)
| AddressLine2 => billingContact.address2->getBillingOrShipping(shippingContact.address2)
| AddressCity => billingContact.locality->getBillingOrShipping(shippingContact.locality)
| FullName => billingContact.name->getNameFromString(requiredFieldsArr)
| BillingName => billingContact.name->getNameFromString(requiredFieldsArr)
| AddressLine1 => billingContact.address1
| AddressLine2 => billingContact.address2
| AddressCity => billingContact.locality
| AddressState =>
let administrativeArea =
billingContact.administrativeArea->getBillingOrShipping(shippingContact.administrativeArea)
let countryCode =
billingContact.countryCode->getBillingOrShipping(shippingContact.countryCode)
Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode)
Utils.getStateNameFromStateCodeAndCountry(
statesList,
billingContact.administrativeArea,
billingContact.countryCode,
)
| Country
| AddressCountry(_) =>
billingContact.countryCode->getBillingOrShipping(shippingContact.countryCode)
| AddressPincode => billingContact.postalCode->getBillingOrShipping(shippingContact.postalCode)
billingContact.countryCode
| AddressPincode => billingContact.postalCode
| Email => email
| PhoneNumber =>
shippingContact.phoneNumber->String.replaceAll(" ", "")->String.replaceAll("-", "")
| ShippingName => shippingContact.name->getNameFromString(requiredFieldsArr)
| ShippingAddressLine1 => shippingContact.address1
| ShippingAddressLine2 => shippingContact.address2
| ShippingAddressCity => shippingContact.locality
| ShippingAddressState =>
Utils.getStateNameFromStateCodeAndCountry(
statesList,
shippingContact.administrativeArea,
shippingContact.countryCode,
)
| ShippingAddressCountry(_) => shippingContact.countryCode
| ShippingAddressPincode => shippingContact.postalCode
| _ => ""
}

Expand All @@ -882,26 +898,21 @@ let getPaypalRequiredFields = (
paymentMethodTypes.required_fields->Array.reduce(Dict.make(), (acc, item) => {
let requiredFieldsArr = item.required_field->String.split(".")

let getName = {
let name = details.shippingAddress.recipientName->Option.getOr("")

name->getNameFromString(requiredFieldsArr)
}

let fieldVal = switch item.field_type {
| FullName => getName
| BillingName => getName
| AddressLine1 => details.shippingAddress.line1->Option.getOr("")
| AddressLine2 => details.shippingAddress.line2->Option.getOr("")
| AddressCity => details.shippingAddress.city->Option.getOr("")
| AddressState =>
let administrativeArea = details.shippingAddress.state->Option.getOr("")
let countryCode = details.shippingAddress.countryCode->Option.getOr("")
Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode)
| Country
| AddressCountry(_) =>
details.shippingAddress.countryCode->Option.getOr("")
| AddressPincode => details.shippingAddress.postalCode->Option.getOr("")
| ShippingName => {
let name = details.shippingAddress.recipientName->Option.getOr("")
name->getNameFromString(requiredFieldsArr)
}
| ShippingAddressLine1 => details.shippingAddress.line1->Option.getOr("")
| ShippingAddressLine2 => details.shippingAddress.line2->Option.getOr("")
| ShippingAddressCity => details.shippingAddress.city->Option.getOr("")
| 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("")) {
Expand Down

0 comments on commit 7588298

Please sign in to comment.