From f8fb987d7355ded166f4ae4ebcd9f83f451dec3a Mon Sep 17 00:00:00 2001 From: Till Hellmund Date: Fri, 18 Oct 2024 10:46:57 -0400 Subject: [PATCH] Consider `attachDefaultsToPaymentMethod` --- .../ach/USBankAccountFormViewModel.kt | 52 +++++++++++++------ .../ach/USBankAccountFormViewModelTest.kt | 4 +- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt index a3f700512ca..674ba645a40 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModel.kt @@ -76,8 +76,11 @@ internal class USBankAccountFormViewModel @Inject internal constructor( private val collectingPhone = args.formArgs.billingDetailsCollectionConfiguration.phone == CollectionMode.Always - private val collectingName = + private val collectingName = if (args.instantDebits) { + args.formArgs.billingDetailsCollectionConfiguration.name == CollectionMode.Always + } else { args.formArgs.billingDetailsCollectionConfiguration.name != CollectionMode.Never + } private val collectingEmail = args.formArgs.billingDetailsCollectionConfiguration.email != CollectionMode.Never @@ -249,8 +252,14 @@ internal class USBankAccountFormViewModel @Inject internal constructor( val hasDefaultEmail = args.formArgs.billingDetails?.email != null && args.formArgs.billingDetailsCollectionConfiguration.attachDefaultsToPaymentMethod - assert((hasDefaultName || collectingName) && (hasDefaultEmail || collectingEmail)) { - "If name or email are not collected, they must be provided through defaults" + if (args.instantDebits) { + assert(hasDefaultEmail || collectingEmail) { + "If email is not collected, it must be provided through defaults" + } + } else { + assert((hasDefaultName || collectingName) && (hasDefaultEmail || collectingEmail)) { + "If name or email are not collected, they must be provided through defaults" + } } } @@ -508,24 +517,33 @@ internal class USBankAccountFormViewModel @Inject internal constructor( amount = args.formArgs.amount?.value, currency = args.formArgs.amount?.currencyCode, linkMode = args.linkMode, - billingAddress = ElementsSessionContext.BillingAddress( - name = name.value, - phone = phone.value?.let { phoneController.getE164PhoneNumber(it) }, - address = address.value?.let { - ElementsSessionContext.BillingAddress.Address( - line1 = it.line1, - line2 = it.line2, - postalCode = it.postalCode, - city = it.city, - state = it.state, - country = it.country, - ) - }, - ), + billingAddress = makeElementsSessionContextBillingAddress(), ), ) } + private fun makeElementsSessionContextBillingAddress(): ElementsSessionContext.BillingAddress { + val attachDefaultsToPaymentMethod = collectionConfiguration.attachDefaultsToPaymentMethod + val name = name.value.takeIf { collectingName || attachDefaultsToPaymentMethod } + val phone = phone.value.takeIf { collectingPhone || attachDefaultsToPaymentMethod } + val address = address.value.takeIf { collectingAddress || attachDefaultsToPaymentMethod } + + return ElementsSessionContext.BillingAddress( + name = name, + phone = phone?.let { phoneController.getE164PhoneNumber(it) }, + address = address?.let { + ElementsSessionContext.BillingAddress.Address( + line1 = it.line1, + line2 = it.line2, + postalCode = it.postalCode, + city = it.city, + state = it.state, + country = it.country, + ) + }, + ) + } + private fun createUSBankAccountConfiguration(): CollectBankAccountConfiguration.USBankAccount { return CollectBankAccountConfiguration.USBankAccount( name = name.value, diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt index 03d1be657a4..405d6084b7e 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/paymentdatacollection/ach/USBankAccountFormViewModelTest.kt @@ -1086,9 +1086,7 @@ class USBankAccountFormViewModelTest { amount = 5099, currency = "usd", linkMode = LinkMode.LinkCardBrand, - billingAddress = ElementsSessionContext.BillingAddress( - name = "Jenny Rose", - ), + billingAddress = ElementsSessionContext.BillingAddress(), ), ) ),