Skip to content

Commit

Permalink
Add prefix to form field value in PhoneNumberController
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe committed Nov 7, 2024
1 parent 10d1a34 commit 7310d31
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,11 @@ internal class InlineSignupViewModel @AssistedInject constructor(

return if (email != null && phoneNumber != null && signUpMode != null) {
val isNameValid = !requiresNameCollection || !name.isNullOrBlank()

val phone = phoneController.getE164PhoneNumber(phoneNumber)
val country = phoneController.getCountryCode()

UserInput.SignUp(
email = email,
phone = phone,
phone = phoneNumber,
country = country,
name = name,
consentAction = signUpMode.toConsentAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,17 @@ internal class PhoneNumberControllerTest {
assertThat(awaitItem()).isTrue()
}
}

@Test
fun `when phone number is entered, form field value should contain prefix`() = runTest {
val phoneNumberController = PhoneNumberController.createPhoneNumberController(
initiallySelectedCountryCode = "CA",
)

phoneNumberController.onValueChange("(122) 252-5252")

phoneNumberController.formFieldValue.test {
assertThat(awaitItem().value).isEqualTo("+11222525252")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal fun billingDetailsParams(): RequestMatcher {
internal fun fullBillingDetailsParams(): RequestMatcher {
return RequestMatchers.composite(
bodyPart(urlEncode("billing_details[name]"), urlEncode(CustomerSheetPage.NAME)),
bodyPart(urlEncode("billing_details[phone]"), CustomerSheetPage.PHONE_NUMBER),
bodyPart(urlEncode("billing_details[phone]"), urlEncode("+1${CustomerSheetPage.PHONE_NUMBER}")),
bodyPart(urlEncode("billing_details[email]"), urlEncode(CustomerSheetPage.EMAIL)),
bodyPart(urlEncode("billing_details[address][line1]"), urlEncode(CustomerSheetPage.ADDRESS_LINE_ONE)),
bodyPart(urlEncode("billing_details[address][line2]"), urlEncode(CustomerSheetPage.ADDRESS_LINE_TWO)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
name = name,
// The createPaymentDetails endpoint does not accept uppercase characters.
email = email?.lowercase(),
phone = phone?.let { phoneController.getE164PhoneNumber(it) },
phone = phone,
address = address?.let {
ElementsSessionContext.BillingDetails.Address(
line1 = it.line1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class USBankAccountFormViewModelTest {
val input = PaymentSelection.New.USBankAccount.Input(
name = "Some One",
email = "[email protected]",
phone = "1112223456",
phone = "+11112223456",
address = Address(
line1 = "123 Not Main Street",
line2 = "Apt 123",
Expand Down Expand Up @@ -331,7 +331,7 @@ class USBankAccountFormViewModelTest {
val input = PaymentSelection.New.USBankAccount.Input(
name = "Some One",
email = "[email protected]",
phone = "1112223456",
phone = "+11112223456",
address = Address(
line1 = "123 Not Main Street",
line2 = "Apt 123",
Expand Down Expand Up @@ -386,7 +386,7 @@ class USBankAccountFormViewModelTest {
billingDetails = PaymentSheet.BillingDetails(
name = CUSTOMER_NAME,
email = CUSTOMER_EMAIL,
phone = CUSTOMER_PHONE_COUNTRY_CODE + CUSTOMER_PHONE,
phone = CUSTOMER_PHONE,
address = CUSTOMER_ADDRESS,
),
billingDetailsCollectionConfiguration = PaymentSheet.BillingDetailsCollectionConfiguration(
Expand Down Expand Up @@ -528,7 +528,7 @@ class USBankAccountFormViewModelTest {
billingDetails = PaymentSheet.BillingDetails(
name = CUSTOMER_NAME,
email = CUSTOMER_EMAIL,
phone = CUSTOMER_PHONE_COUNTRY_CODE + CUSTOMER_PHONE,
phone = CUSTOMER_PHONE,
address = CUSTOMER_ADDRESS,
),
billingDetailsCollectionConfiguration = PaymentSheet.BillingDetailsCollectionConfiguration(
Expand Down Expand Up @@ -1638,8 +1638,7 @@ class USBankAccountFormViewModelTest {
const val CUSTOMER_EMAIL = "[email protected]"
const val STRIPE_ACCOUNT_ID = "stripe_account_id"
const val CUSTOMER_COUNTRY = "US"
const val CUSTOMER_PHONE_COUNTRY_CODE = "+1"
const val CUSTOMER_PHONE = "3105551234"
const val CUSTOMER_PHONE = "+13105551234"
val CUSTOMER_ADDRESS = PaymentSheet.Address(
line1 = "123 Main Street",
line2 = "Apt 456",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class PhoneNumberController private constructor(
override val isComplete = combineAsStateFlow(fieldValue, phoneNumberMinimumLength) { value, minLength ->
value.length >= (minLength ?: 0) || acceptAnyInput
}
override val formFieldValue = combineAsStateFlow(fieldValue, isComplete) { fieldValue, isComplete ->
FormFieldEntry(fieldValue, isComplete)
override val formFieldValue = combineAsStateFlow(rawFieldValue, isComplete) { rawFieldValue, isComplete ->
FormFieldEntry(rawFieldValue, isComplete)
}

override val error: StateFlow<FieldError?> = combineAsStateFlow(
Expand Down

0 comments on commit 7310d31

Please sign in to comment.