Skip to content

Commit

Permalink
Consider attachDefaultsToPaymentMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Oct 18, 2024
1 parent 74c3f3b commit f8fb987
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
}
}

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,9 +1086,7 @@ class USBankAccountFormViewModelTest {
amount = 5099,
currency = "usd",
linkMode = LinkMode.LinkCardBrand,
billingAddress = ElementsSessionContext.BillingAddress(
name = "Jenny Rose",
),
billingAddress = ElementsSessionContext.BillingAddress(),
),
)
),
Expand Down

0 comments on commit f8fb987

Please sign in to comment.