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