Skip to content

Commit

Permalink
Don't filter countries when they're not supported from Locale.getISOC…
Browse files Browse the repository at this point in the history
…ountries on older Android versions.

Fixes #6501
  • Loading branch information
jaynewstrom-stripe committed Apr 27, 2023
1 parent cb31871 commit 6a888c5
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.ui.core.elements

import androidx.annotation.RestrictTo
import com.stripe.android.core.model.CountryUtils
import com.stripe.android.ui.core.R
import com.stripe.android.uicore.address.AddressRepository
import com.stripe.android.uicore.elements.AddressElement
Expand All @@ -12,7 +13,6 @@ import com.stripe.android.uicore.elements.IdentifierSpec
import com.stripe.android.uicore.elements.SameAsShippingController
import com.stripe.android.uicore.elements.SameAsShippingElement
import com.stripe.android.uicore.elements.SectionElement
import com.stripe.android.uicore.elements.supportedBillingCountries
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
Expand All @@ -30,7 +30,7 @@ data class AddressSpec(
override val apiPath: IdentifierSpec = IdentifierSpec.Generic("billing_details[address]"),

@SerialName("allowed_country_codes")
val allowedCountryCodes: Set<String> = supportedBillingCountries,
val allowedCountryCodes: Set<String> = CountryUtils.supportedBillingCountries,

@SerialName("display_fields")
val displayFields: Set<DisplayField> = emptySet(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.stripe.android.ui.core.elements

import androidx.annotation.RestrictTo
import com.stripe.android.core.model.CountryUtils
import com.stripe.android.ui.core.CardBillingDetailsCollectionConfiguration
import com.stripe.android.ui.core.R
import com.stripe.android.uicore.address.AddressRepository
import com.stripe.android.uicore.elements.IdentifierSpec
import com.stripe.android.uicore.elements.SameAsShippingController
import com.stripe.android.uicore.elements.SameAsShippingElement
import com.stripe.android.uicore.elements.SectionElement
import com.stripe.android.uicore.elements.supportedBillingCountries
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -18,7 +18,7 @@ data class CardBillingSpec(
@SerialName("api_path")
override val apiPath: IdentifierSpec = IdentifierSpec.Generic("card_billing"),
@SerialName("allowed_country_codes")
val allowedCountryCodes: Set<String> = supportedBillingCountries,
val allowedCountryCodes: Set<String> = CountryUtils.supportedBillingCountries,
@SerialName("collection_mode")
val collectionMode: CardBillingDetailsCollectionConfiguration.AddressCollectionMode =
CardBillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.stripe.android.ui.core.elements

import androidx.annotation.RestrictTo
import com.stripe.android.core.model.CountryUtils
import com.stripe.android.uicore.elements.CountryConfig
import com.stripe.android.uicore.elements.CountryElement
import com.stripe.android.uicore.elements.DropdownFieldController
import com.stripe.android.uicore.elements.IdentifierSpec
import com.stripe.android.uicore.elements.supportedBillingCountries
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -21,7 +21,7 @@ data class CountrySpec(
override val apiPath: IdentifierSpec = IdentifierSpec.Country,

@SerialName("allowed_country_codes")
val allowedCountryCodes: Set<String> = supportedBillingCountries
val allowedCountryCodes: Set<String> = CountryUtils.supportedBillingCountries
) : FormItemSpec() {
fun transform(
initialValues: Map<IdentifierSpec, String?>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.stripe.android.ui.core.forms

import androidx.annotation.RestrictTo
import com.stripe.android.core.model.CountryUtils
import com.stripe.android.ui.core.elements.CardBillingSpec
import com.stripe.android.ui.core.elements.CardDetailsSectionSpec
import com.stripe.android.ui.core.elements.LayoutSpec
import com.stripe.android.uicore.elements.IdentifierSpec
import com.stripe.android.uicore.elements.supportedBillingCountries

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
val LinkCardForm = LayoutSpec(
listOf(
CardDetailsSectionSpec(IdentifierSpec.Generic("card_details_section")),
CardBillingSpec(allowedCountryCodes = supportedBillingCountries)
CardBillingSpec(allowedCountryCodes = CountryUtils.supportedBillingCountries)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,41 @@ import java.util.Locale

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) // For paymentsheet -- this still auto-completes
object CountryUtils {

// This comes from: stripe-js-v3/blob/master/src/lib/shared/checkoutSupportedCountries.js
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
val supportedBillingCountries = setOf(
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AT", "AU", "AW", "AX",
"AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO",
"BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CD", "CF", "CG", "CH", "CI",
"CK", "CL", "CM", "CN", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM",
"DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FO", "FR",
"GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR",
"GS", "GT", "GU", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM",
"IN", "IO", "IQ", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM",
"KN", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU",
"LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MK", "ML", "MM", "MN", "MO", "MQ",
"MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NG", "NI",
"NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL",
"PM", "PN", "PR", "PS", "PT", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB",
"SC", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST",
"SV", "SX", "SZ", "TA", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN",
"TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VA", "VC", "VE",
"VG", "VN", "VU", "WF", "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW"
)

private val CARD_POSTAL_CODE_COUNTRIES = setOf(
"US",
"GB",
"CA"
)

private fun localizedCountries(currentLocale: Locale) =
Locale.getISOCountries().map { code ->
Country(
CountryCode.create(code),
Locale("", code).getDisplayCountry(currentLocale)
)
}
private fun localizedCountries(currentLocale: Locale) = supportedBillingCountries.map { code ->
Country(
CountryCode.create(code),
Locale("", code).getDisplayCountry(currentLocale)
)
}

@JvmSynthetic
fun getDisplayCountry(countryCode: CountryCode, currentLocale: Locale): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ class CountryUtilsTest {
.isEqualTo("Deutschland")
}

@Test
// https://github.com/stripe/stripe-android/issues/6501
fun countryIsAvailableEvenWhenNotReturnedFrom_getISOCountries() {
// We used to use Locale.getISOCountries instead of our hardcoded set.
// Some countries (notably Kosovo) wasn't available on some older Android versions.
assertThat(CountryUtils.getDisplayCountry(CountryCode.create("XK"), Locale.US))
.isEqualTo("Kosovo")
val country = CountryUtils.getCountryByCode(CountryCode.create("XK"), Locale.US)
assertThat(country).isEqualTo(Country("XK", "Kosovo"))
}

@Test
fun `getOrderedCountriesLocaleLanguage() in the language of the current locale`() {
val currentLocale = Locale("de", "DE")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.uicore.elements

import com.google.common.truth.Truth.assertThat
import com.stripe.android.core.model.CountryUtils
import com.stripe.android.uicore.R
import org.junit.Test
import java.util.Locale
Expand Down Expand Up @@ -62,7 +63,7 @@ class CountryConfigTest {
locale = Locale.US
).displayItems
val supportedCountries = CountryConfig(
onlyShowCountryCodes = supportedBillingCountries,
onlyShowCountryCodes = CountryUtils.supportedBillingCountries,
locale = Locale.US
).displayItems

Expand All @@ -79,10 +80,10 @@ class CountryConfigTest {

assertThat(
defaultCountries.size
).isEqualTo(249)
).isEqualTo(235)

assertThat(
supportedCountries.size
).isEqualTo(233)
).isEqualTo(235)
}
}

0 comments on commit 6a888c5

Please sign in to comment.