Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 102 additions & 9 deletions src/CollectWidget.res
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,23 @@ let make = (
getDefaultsAndValidity(payoutDynamicFields, supportedCardBrands)
})
->Option.map(((values, validity)) => {
setFormData(_ => values)
let last_name =
values
->Dict.get(BillingAddress(FullName(LastName))->getPaymentMethodDataFieldKey)
->Option.getOr("")
let first_name =
values
->Dict.get(BillingAddress(FullName(FirstName))->getPaymentMethodDataFieldKey)
->Option.getOr("")
let copy = values->Dict.copy

// If first_name is null and required but last_name is not, use first_name as last_name
// to populate full name properly
if first_name->String.length == 0 {
copy->Dict.set(BillingAddress(FullName(FirstName))->getPaymentMethodDataFieldKey, last_name)
copy->Dict.set(BillingAddress(FullName(LastName))->getPaymentMethodDataFieldKey, "")
}
setFormData(_ => copy)
setValidityDict(_ => validity)
})
->ignore
Expand Down Expand Up @@ -161,6 +177,18 @@ let make = (
}
setFormData(PayoutMethodData(CardBrand)->getPaymentMethodDataFieldKey, newCardBrand)
}
| BillingAddress(FullName(FirstName)) => {
let firstNameKey = BillingAddress(FullName(FirstName))->getPaymentMethodDataFieldKey
let lastNameKey = BillingAddress(FullName(LastName))->getPaymentMethodDataFieldKey
let nameSplits = value->String.trim->String.split(" ")
let firstName = nameSplits->Array.get(0)->Option.getOr("")
let lastName =
nameSplits
->Array.slice(~start=1, ~end=nameSplits->Array.length)
->Array.join(" ")
setFormData(firstNameKey, firstName)
setFormData(lastNameKey, lastName)
}
| _ => ()
}
setFormData(key->getPaymentMethodDataFieldKey, updatedValue)
Expand Down Expand Up @@ -211,6 +239,62 @@ let make = (
| _ => React.null
}
}

let getErrorStringAndClasses = (field: dynamicFieldType) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to some utility file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let key = field->getPaymentMethodDataFieldKey
let value = formData->Dict.get(key)->Option.getOr("")
let isValid = validityDict->Dict.get(key)->Option.flatMap(key => key)

switch field {
| BillingAddress(FullName(FirstName)) => {
// Check both FirstName and LastName validity and merge error messages
let firstNameKey = BillingAddress(FullName(FirstName))->getPaymentMethodDataFieldKey
let lastNameKey = BillingAddress(FullName(LastName))->getPaymentMethodDataFieldKey
let firstNameValue = formData->Dict.get(firstNameKey)->Option.getOr("")
let lastNameValue = formData->Dict.get(lastNameKey)->Option.getOr("")
let firstNameValid = validityDict->Dict.get(firstNameKey)->Option.flatMap(key => key)
let lastNameValid = validityDict->Dict.get(lastNameKey)->Option.flatMap(key => key)

let firstNameError = switch firstNameValid {
| Some(false) =>
BillingAddress(FullName(FirstName))->getPaymentMethodDataErrorString(
firstNameValue,
localeString,
)
| _ => ""
}

let lastNameError = switch lastNameValid {
| Some(false) =>
BillingAddress(FullName(LastName))->getPaymentMethodDataErrorString(
lastNameValue,
localeString,
)
| _ => ""
}

let mergedError = switch (firstNameError, lastNameError) {
| ("", "") => ""
| (firstError, "") => firstError
| ("", lastError) => lastError
| (firstError, lastError) => firstError ++ " " ++ lastError
}

let hasError = mergedError !== ""
(mergedError, hasError ? "text-xs text-red-950" : "")
}
| _ =>
// Default behavior for other fields
switch isValid {
| Some(false) => (
field->getPaymentMethodDataErrorString(value, localeString),
"text-xs text-red-950",
)
| _ => ("", "")
}
}
}

let renderInputTemplate = (field: dynamicFieldType) => {
let labelClasses = "text-sm mt-2.5 text-jp-gray-800"
let inputClasses = "min-w-full border mt-1.5 px-2.5 py-2 rounded-md border-jp-gray-200"
Expand Down Expand Up @@ -241,14 +325,7 @@ let make = (
->Js.Re.source
let key = field->getPaymentMethodDataFieldKey
let value = formData->Dict.get(key)->Option.getOr("")
let isValid = validityDict->Dict.get(key)->Option.flatMap(key => key)
let (errorString, errorStringClasses) = switch isValid {
| Some(false) => (
field->getPaymentMethodDataErrorString(value, localeString),
"text-xs text-red-950",
)
| _ => ("", "")
}
let (errorString, errorStringClasses) = getErrorStringAndClasses(field)
<InputField
id=key
className=inputClasses
Expand Down Expand Up @@ -283,13 +360,29 @@ let make = (
pattern
/>
}
let checkIfLastNameRequiredAndNone = (addressFields: array<dynamicFieldForAddress>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let checkIfLastNameRequiredAndNone = (addressFields: array<dynamicFieldForAddress>) => {
let checkIfNameRequired = (addressFields: array<dynamicFieldForAddress>) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

addressFields->Array.reduce(false, (acc, field) => {
let condition = switch (field.fieldType, field.value) {
| (FullName(LastName), None) => true
| _ => false
}
acc || condition
})
}
let renderAddressForm = (addressFields: array<dynamicFieldForAddress>) =>
addressFields
->Array.mapWithIndex((field, index) =>
<React.Fragment key={index->Int.toString}>
{switch (field.fieldType, field.value) {
| (Email, None) => BillingAddress(Email)->renderInputTemplate
| (FullName(FirstName), None) => BillingAddress(FullName(FirstName))->renderInputTemplate
| (FullName(FirstName), Some(_)) =>
// If first_name exists, last_name is required and has null value, render first_name field to capture last_name
if checkIfLastNameRequiredAndNone(addressFields) {
BillingAddress(FullName(FirstName))->renderInputTemplate
} else {
React.null
}
// first_name and last_name are stored in fullName
| (FullName(LastName), _) => React.null
| (CountryCode, None) => BillingAddress(CountryCode)->renderInputTemplate
Expand Down
60 changes: 35 additions & 25 deletions src/Utilities/PaymentMethodCollectUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ let getPaymentMethodDataFieldKey = (key): string =>
| BillingAddress(b) =>
switch b {
| Email => "billing.address.email"
| FullName(_) => "billing.address.fullName"
| FullName(FirstName) => "billing.address.firstName"
| FullName(LastName) => "billing.address.lastName"
| CountryCode => "billing.address.countryCode"
| PhoneNumber => "billing.address.phoneNumber"
| PhoneCountryCode => "billing.address.phoneCountryCode"
Expand Down Expand Up @@ -425,6 +426,12 @@ let getPaymentMethodDataErrorString = (
| (PayoutMethodData(CardExpDate(_)), false) => localeString.inCompleteExpiryErrorText
| (PayoutMethodData(CardExpDate(_)), true) => localeString.pastExpiryErrorText
| (PayoutMethodData(ACHRoutingNumber), false) => localeString.formFieldInvalidRoutingNumber
| (PayoutMethodData(ACHAccountNumber), _) =>
if value->String.trim->String.length === 0 {
localeString.accountNumberText->localeString.nameEmptyText
} else {
localeString.accountNumberInvalidText
}
| (PayoutMethodData(BacsSortCode), _) =>
if value->String.trim->String.length === 0 {
localeString.sortCodeText->localeString.nameEmptyText
Expand Down Expand Up @@ -922,33 +929,36 @@ let processAddressFields = (
(dataArr, keys)
}
| FullName(LastName) => {
let key = BillingAddress(FullName(FirstName))
let lastNameKey = BillingAddress(FullName(LastName))
let info: dynamicFieldInfo = BillingAddress(dynamicFieldInfo)
let fieldKey = key->getPaymentMethodDataFieldKey
paymentMethodDataDict
->Dict.get(fieldKey)
->Option.map(value => {
let nameSplits = value->String.split(" ")
let lastName =
nameSplits
->Array.slice(~start=1, ~end=nameSplits->Array.length)
->Array.join(" ")
if lastName->String.length > 0 {
dataArr->Array.push((info, lastName))
// Use first name as last name ?
} else {
nameSplits
->Array.get(0)
->Option.map(
firstName => {
dataArr->Array.push((info, firstName))
},
)
let lastNameFieldKey = lastNameKey->getPaymentMethodDataFieldKey

// First try to get lastName from direct lastName field
let lastNameValue = paymentMethodDataDict->Dict.get(lastNameFieldKey)

switch lastNameValue {
| Some(lastName) if lastName->String.trim->String.length > 0 => {
dataArr->Array.push((info, lastName->String.trim))
keys->Array.push(lastNameFieldKey)
}
| _ => {
// Fallback: split firstName field if lastName is empty
let firstNameKey = BillingAddress(FullName(FirstName))
let firstNameFieldKey = firstNameKey->getPaymentMethodDataFieldKey
paymentMethodDataDict
->Dict.get(firstNameFieldKey)
->Option.map(value => {
let nameSplits = value->String.split(" ")
let lastName =
nameSplits
->Array.slice(~start=1, ~end=nameSplits->Array.length)
->Array.join(" ")
dataArr->Array.push((info, lastName))
})
->ignore
keys->Array.push(lastNameFieldKey)
}
})
->ignore
keys->Array.push(fieldKey)
}
Comment on lines +932 to +961
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we invert this logic and check for FirstName first?

Any specific reason to check for lastName?

Copy link
Contributor Author

@aritro2002 aritro2002 Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't invert the logic because suppose I have set first_name: "alex", last_name: "john", in create call, then it will send wrong names in confirm call. As it will check from the first name and will assign last name same as first name. Therefore, I am first checking with LastName.

(dataArr, keys)
}
| _ => {
Expand Down
Loading