@@ -7,6 +7,7 @@ import com.stripe.android.lpmfoundations.paymentmethod.definitions.BlikDefinitio
77import com.stripe.android.lpmfoundations.paymentmethod.definitions.KlarnaDefinition
88import com.stripe.android.lpmfoundations.paymentmethod.definitions.WeroDefinition
99import com.stripe.android.model.PaymentMethod
10+ import com.stripe.android.model.PaymentMethodCreateParams
1011import com.stripe.android.model.PaymentMethodFixtures
1112import com.stripe.android.paymentsheet.PaymentSheet
1213import com.stripe.android.paymentsheet.addresselement.AddressDetails
@@ -18,6 +19,7 @@ import com.stripe.android.testing.FeatureFlagTestRule
1819import com.stripe.android.testing.PaymentIntentFactory
1920import com.stripe.android.ui.core.FieldValuesToParamsMapConverter
2021import com.stripe.android.ui.core.elements.BillingAddressElement
22+ import com.stripe.android.ui.core.elements.additionalAutomaticTaxFieldsByCountry
2123import com.stripe.android.uicore.elements.AddressElement
2224import com.stripe.android.uicore.elements.AddressFieldsElement
2325import com.stripe.android.uicore.elements.FormElement
@@ -92,21 +94,41 @@ class AutomaticTaxBillingAddressTest {
9294 .sectionFields()
9395 .filterIsInstance<BillingAddressElement >()
9496 .single()
95- val formValues = requireNotNull(
96- CompleteFormFieldValueFilter (
97- currentFieldValueMap = addressElement.getFormFieldValueFlow().map { it.toMap() },
98- hiddenIdentifiers = addressElement.hiddenIdentifiers,
99- userRequestedReuse = flowOf(PaymentSelection .CustomerRequestedSave .NoRequest ),
100- defaultValues = emptyMap(),
101- ).filterFlow().first()
97+ val createParams = addressElement.toCreateParams(metadata, PaymentMethod .Type .Blik .code)
98+ val billingDetails = createParams.toParamMap()[" billing_details" ] as Map <* , * >
99+
100+ assertThat(billingDetails[" address" ]).isEqualTo(
101+ mapOf (
102+ " line1" to " 510 Townsend Street" ,
103+ " city" to " San Francisco" ,
104+ " state" to " CA" ,
105+ " postal_code" to " 94103" ,
106+ " country" to " US" ,
107+ )
102108 )
109+ }
103110
104- val createParams = FieldValuesToParamsMapConverter .transformToPaymentMethodCreateParams(
105- fieldValuePairs = formValues.fieldValuePairs,
106- code = PaymentMethod .Type .Blik .code,
107- requiresMandate = false ,
108- clientAttributionMetadata = metadata.clientAttributionMetadata,
111+ @Test
112+ fun `widened Klarna address is included in create params` () = runTest {
113+ val metadata = createMetadata(
114+ paymentMethodCode = PaymentMethod .Type .Klarna .code,
115+ checkoutSessionResponse = automaticTaxCheckoutSessionResponse,
116+ defaultBillingDetails = PaymentSheet .BillingDetails (
117+ address = PaymentSheet .Address (
118+ line1 = " 510 Townsend Street" ,
119+ city = " San Francisco" ,
120+ state = " CA" ,
121+ postalCode = " 94103" ,
122+ country = " US" ,
123+ ),
124+ ),
109125 )
126+ val addressElement = KlarnaDefinition .formElements(metadata)
127+ .sectionFields()
128+ .filterIsInstance<BillingAddressElement >()
129+ .single()
130+
131+ val createParams = addressElement.toCreateParams(metadata, PaymentMethod .Type .Klarna .code)
110132 val billingDetails = createParams.toParamMap()[" billing_details" ] as Map <* , * >
111133
112134 assertThat(billingDetails[" address" ]).isEqualTo(
@@ -278,9 +300,33 @@ class AutomaticTaxBillingAddressTest {
278300 uiDefinitionFactoryArgumentsFactory = TestUiDefinitionFactoryArgumentsFactory .create(),
279301 )
280302 )
281- assertWithMessage(" $code should contain exactly one address" )
282- .that(formElements.sectionFields().filterIsInstance<AddressFieldsElement >())
283- .hasSize(1 )
303+ val addressFields = formElements.sectionFields().filterIsInstance<AddressFieldsElement >()
304+ assertWithMessage(" $code should contain exactly one address" ).that(addressFields).hasSize(1 )
305+
306+ val addressElement = addressFields.single()
307+ if (addressElement !is BillingAddressElement ) {
308+ assertWithMessage(" $code should already contain a full address" )
309+ .that(addressElement)
310+ .isInstanceOf(AddressElement ::class .java)
311+ return @forEach
312+ }
313+
314+ if (code == PaymentMethod .Type .Wero .code) {
315+ val controller = addressElement.countryElement.controller
316+ listOf (" DE" , " BE" , " FR" ).forEach { allowedCountry ->
317+ controller.onRawValueChange(allowedCountry)
318+ assertThat(controller.rawFieldValue.value).isEqualTo(allowedCountry)
319+ }
320+ controller.onRawValueChange(" US" )
321+ assertThat(controller.rawFieldValue.value).isNotEqualTo(" US" )
322+ }
323+
324+ val selectedCountry = requireNotNull(addressElement.countryElement.controller.rawFieldValue.value)
325+ additionalAutomaticTaxFieldsByCountry[selectedCountry].orEmpty().forEach { requiredField ->
326+ assertWithMessage(" $code should show $requiredField for $selectedCountry " )
327+ .that(addressElement.hiddenIdentifiers.value)
328+ .doesNotContain(requiredField)
329+ }
284330 }
285331 }
286332
@@ -296,6 +342,27 @@ class AutomaticTaxBillingAddressTest {
296342 return addressElement
297343 }
298344
345+ private suspend fun BillingAddressElement.toCreateParams (
346+ metadata : PaymentMethodMetadata ,
347+ paymentMethodCode : String ,
348+ ): PaymentMethodCreateParams {
349+ val formValues = requireNotNull(
350+ CompleteFormFieldValueFilter (
351+ currentFieldValueMap = getFormFieldValueFlow().map { it.toMap() },
352+ hiddenIdentifiers = hiddenIdentifiers,
353+ userRequestedReuse = flowOf(PaymentSelection .CustomerRequestedSave .NoRequest ),
354+ defaultValues = emptyMap(),
355+ ).filterFlow().first()
356+ )
357+
358+ return FieldValuesToParamsMapConverter .transformToPaymentMethodCreateParams(
359+ fieldValuePairs = formValues.fieldValuePairs,
360+ code = paymentMethodCode,
361+ requiresMandate = false ,
362+ clientAttributionMetadata = metadata.clientAttributionMetadata,
363+ )
364+ }
365+
299366 private fun createMetadata (
300367 paymentMethodCode : String ,
301368 checkoutSessionResponse : CheckoutSessionResponse ? ,
0 commit comments