|
| 1 | +package com.stripe.android.lpmfoundations.paymentmethod |
| 2 | + |
| 3 | +import com.stripe.android.core.strings.resolvableString |
| 4 | +import com.stripe.android.model.PaymentMethod |
| 5 | +import com.stripe.android.paymentsheet.PaymentSheet |
| 6 | +import com.stripe.android.ui.core.BillingDetailsCollectionConfiguration |
| 7 | +import com.stripe.android.ui.core.R |
| 8 | +import com.stripe.android.ui.core.elements.BillingAddressCollectionMode |
| 9 | +import com.stripe.android.ui.core.elements.BillingAddressElement |
| 10 | +import com.stripe.android.ui.core.elements.additionalAutomaticTaxFieldsByCountry |
| 11 | +import com.stripe.android.uicore.elements.AddressFieldsElement |
| 12 | +import com.stripe.android.uicore.elements.CountryConfig |
| 13 | +import com.stripe.android.uicore.elements.DropdownFieldController |
| 14 | +import com.stripe.android.uicore.elements.FormElement |
| 15 | +import com.stripe.android.uicore.elements.IdentifierSpec |
| 16 | +import com.stripe.android.uicore.elements.SameAsShippingController |
| 17 | +import com.stripe.android.uicore.elements.SameAsShippingElement |
| 18 | +import com.stripe.android.uicore.elements.SectionController |
| 19 | +import com.stripe.android.uicore.elements.SectionElement |
| 20 | + |
| 21 | +/** |
| 22 | + * Widens an existing shared address for automatic tax, or appends a country-first billing |
| 23 | + * address when the payment method does not already collect one. |
| 24 | + */ |
| 25 | +internal fun List<FormElement>.withAutomaticTaxBillingAddressIfNecessary( |
| 26 | + paymentMethodCode: String, |
| 27 | + arguments: UiDefinitionFactory.Arguments, |
| 28 | +): List<FormElement> { |
| 29 | + val shouldCollectTaxAddress = arguments.requiresBillingAddressForAutomaticTax && |
| 30 | + arguments.billingDetailsCollectionConfiguration.address == |
| 31 | + PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic |
| 32 | + if (!shouldCollectTaxAddress || paymentMethodCode in separatelyRenderedPaymentMethodCodes) { |
| 33 | + return this |
| 34 | + } |
| 35 | + |
| 36 | + val addressField = filterIsInstance<SectionElement>() |
| 37 | + .flatMap { it.fields } |
| 38 | + .filterIsInstance<AddressFieldsElement>() |
| 39 | + .firstOrNull() |
| 40 | + if (addressField != null && addressField !is BillingAddressElement) { |
| 41 | + return this |
| 42 | + } |
| 43 | + |
| 44 | + val existingSameAsShippingElement = filterIsInstance<SameAsShippingElement>().firstOrNull() |
| 45 | + val sameAsShippingElement = existingSameAsShippingElement ?: createSameAsShippingElement(arguments) |
| 46 | + |
| 47 | + if (addressField is BillingAddressElement) { |
| 48 | + return widenBillingAddress( |
| 49 | + addressField = addressField, |
| 50 | + arguments = arguments, |
| 51 | + sameAsShippingElement = sameAsShippingElement, |
| 52 | + appendSameAsShippingElement = existingSameAsShippingElement == null, |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + val addressElement = createTaxBillingAddressElement( |
| 57 | + arguments = arguments, |
| 58 | + countryDropdownFieldController = DropdownFieldController( |
| 59 | + config = CountryConfig(arguments.billingDetailsCollectionConfiguration.allowedBillingCountries), |
| 60 | + initialValue = arguments.initialValues[IdentifierSpec.Country], |
| 61 | + ), |
| 62 | + sameAsShippingElement = sameAsShippingElement, |
| 63 | + ) |
| 64 | + |
| 65 | + return this + listOfNotNull( |
| 66 | + SectionElement.wrap(addressElement, R.string.stripe_billing_details.resolvableString), |
| 67 | + sameAsShippingElement, |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +private fun List<FormElement>.widenBillingAddress( |
| 72 | + addressField: BillingAddressElement, |
| 73 | + arguments: UiDefinitionFactory.Arguments, |
| 74 | + sameAsShippingElement: SameAsShippingElement?, |
| 75 | + appendSameAsShippingElement: Boolean, |
| 76 | +): List<FormElement> { |
| 77 | + val addressSectionIndex = indexOfFirst { element -> |
| 78 | + element is SectionElement && addressField in element.fields |
| 79 | + } |
| 80 | + val addressSection = this[addressSectionIndex] as SectionElement |
| 81 | + val widenedFields = addressSection.fields.map { field -> |
| 82 | + if (field === addressField) { |
| 83 | + addressField.withAdditionalFields( |
| 84 | + additionalFieldsByCountry = additionalAutomaticTaxFieldsByCountry, |
| 85 | + sameAsShippingElement = sameAsShippingElement, |
| 86 | + shippingValuesMap = arguments.shippingValues, |
| 87 | + ) |
| 88 | + } else { |
| 89 | + field |
| 90 | + } |
| 91 | + } |
| 92 | + val widenedSection = SectionElement( |
| 93 | + identifier = addressSection.identifier, |
| 94 | + fields = widenedFields, |
| 95 | + controller = SectionController( |
| 96 | + label = addressSection.controller.label, |
| 97 | + sectionFieldValidationControllers = widenedFields.map { it.sectionFieldErrorController() }, |
| 98 | + ), |
| 99 | + ) |
| 100 | + return flatMapIndexed { index, element -> |
| 101 | + if (index == addressSectionIndex) { |
| 102 | + listOfNotNull( |
| 103 | + widenedSection, |
| 104 | + sameAsShippingElement.takeIf { appendSameAsShippingElement }, |
| 105 | + ) |
| 106 | + } else { |
| 107 | + listOf(element) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +private fun createTaxBillingAddressElement( |
| 113 | + arguments: UiDefinitionFactory.Arguments, |
| 114 | + countryDropdownFieldController: DropdownFieldController, |
| 115 | + sameAsShippingElement: SameAsShippingElement?, |
| 116 | +): BillingAddressElement { |
| 117 | + return BillingAddressElement( |
| 118 | + identifier = IdentifierSpec.Generic("billing_details[address]"), |
| 119 | + rawValuesMap = arguments.initialValues, |
| 120 | + countryCodes = arguments.billingDetailsCollectionConfiguration.allowedBillingCountries, |
| 121 | + countryDropdownFieldController = countryDropdownFieldController, |
| 122 | + countryElementIdentifier = IdentifierSpec.Country, |
| 123 | + autocompleteAddressInteractorFactory = null, |
| 124 | + sameAsShippingElement = sameAsShippingElement, |
| 125 | + shippingValuesMap = arguments.shippingValues, |
| 126 | + addressCollectionMode = BillingAddressCollectionMode.Country( |
| 127 | + additionalFieldsByCountry = additionalAutomaticTaxFieldsByCountry, |
| 128 | + ), |
| 129 | + collectionConfiguration = BillingDetailsCollectionConfiguration( |
| 130 | + collectName = false, |
| 131 | + collectEmail = false, |
| 132 | + collectPhone = false, |
| 133 | + address = BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic, |
| 134 | + allowedCountries = arguments.billingDetailsCollectionConfiguration.allowedBillingCountries, |
| 135 | + ), |
| 136 | + shouldHideCountryOnNoAddressCollection = false, |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +private fun createSameAsShippingElement( |
| 141 | + arguments: UiDefinitionFactory.Arguments, |
| 142 | +): SameAsShippingElement? { |
| 143 | + return arguments.shippingValues |
| 144 | + ?.get(IdentifierSpec.SameAsShipping) |
| 145 | + ?.toBooleanStrictOrNull() |
| 146 | + ?.let { isSameAsShipping -> |
| 147 | + SameAsShippingElement( |
| 148 | + identifier = IdentifierSpec.SameAsShipping, |
| 149 | + controller = SameAsShippingController(isSameAsShipping), |
| 150 | + ) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +private val separatelyRenderedPaymentMethodCodes = setOf( |
| 155 | + PaymentMethod.Type.USBankAccount.code, |
| 156 | + PaymentMethod.Type.Link.code, |
| 157 | +) |
0 commit comments