-
Notifications
You must be signed in to change notification settings - Fork 108
fix: billing dynamic correction payouts #1276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
7abe1ff
d7ed3b5
2d8db10
0ad364f
5a9360f
3036057
666796c
59bf445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
|
|
@@ -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) | ||||||
|
|
@@ -211,6 +239,62 @@ let make = ( | |||||
| | _ => React.null | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| let getErrorStringAndClasses = (field: dynamicFieldType) => { | ||||||
| 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" | ||||||
|
|
@@ -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 | ||||||
|
|
@@ -283,13 +360,29 @@ let make = ( | |||||
| pattern | ||||||
| /> | ||||||
| } | ||||||
| let checkIfLastNameRequiredAndNone = (addressFields: array<dynamicFieldForAddress>) => { | ||||||
|
||||||
| let checkIfLastNameRequiredAndNone = (addressFields: array<dynamicFieldForAddress>) => { | |
| let checkIfNameRequired = (addressFields: array<dynamicFieldForAddress>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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 | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we invert this logic and check for Any specific reason to check for lastName?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| | _ => { | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done