Skip to content

Commit

Permalink
Limit the sofort country list. (#3961)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelleb-stripe authored Jul 9, 2021
1 parent 8ff2d32 commit 9d7fdc5
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat
import com.stripe.android.paymentsheet.ElementType
import com.stripe.android.paymentsheet.forms.FormFieldEntry
import com.stripe.android.paymentsheet.forms.FormFieldValues
import com.stripe.android.paymentsheet.specifications.IdentifierSpec
import com.stripe.android.paymentsheet.specifications.SectionFieldSpec
import com.stripe.android.paymentsheet.specifications.SectionFieldSpec.Email
import com.stripe.android.paymentsheet.specifications.SectionFieldSpec.Name
Expand Down Expand Up @@ -56,7 +57,7 @@ class TransformToPaymentMethodCreateParamMapTest {
"joe@gmail.com",
true
),
SectionFieldSpec.Country.identifier to FormFieldEntry(
IdentifierSpec("country") to FormFieldEntry(
ElementType.Country,
"US",
true
Expand Down
11 changes: 10 additions & 1 deletion paymentsheet/api/paymentsheet.api
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,16 @@ public abstract class com/stripe/android/paymentsheet/specifications/SectionFiel

public final class com/stripe/android/paymentsheet/specifications/SectionFieldSpec$Country : com/stripe/android/paymentsheet/specifications/SectionFieldSpec {
public static final field $stable I
public static final field INSTANCE Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$Country;
public fun <init> ()V
public fun <init> (Ljava/util/Set;)V
public synthetic fun <init> (Ljava/util/Set;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ljava/util/Set;
public final fun copy (Ljava/util/Set;)Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$Country;
public static synthetic fun copy$default (Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$Country;Ljava/util/Set;ILjava/lang/Object;)Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$Country;
public fun equals (Ljava/lang/Object;)Z
public final fun getOnlyShowCountryCodes ()Ljava/util/Set;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/stripe/android/paymentsheet/specifications/SectionFieldSpec$Email : com/stripe/android/paymentsheet/specifications/SectionFieldSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ import com.stripe.android.paymentsheet.ElementType
import com.stripe.android.paymentsheet.R
import java.util.Locale

internal class CountryConfig(val locale: Locale = Locale.getDefault()) : DropdownConfig {
/**
* This is the configuration for a country dropdown.
*
* @property onlyShowCountryCodes: a list of country code that should be shown. If empty all
* countries will be shown.
* @property locale: this is the locale used to display the country names.
*/
internal class CountryConfig(
val onlyShowCountryCodes: Set<String> = emptySet(),
val locale: Locale = Locale.getDefault()
) : DropdownConfig {
override val debugLabel = "country"

@StringRes
override val label = R.string.address_label_country

override val elementType = ElementType.Country

override fun getDisplayItems(): List<String> =
CountryUtils.getOrderedCountries(locale).map { it.name }
override fun getDisplayItems(): List<String> = CountryUtils.getOrderedCountries(locale)
.filter {
onlyShowCountryCodes.isEmpty() ||
(onlyShowCountryCodes.isNotEmpty() && onlyShowCountryCodes.contains(it.code.value))
}.map { it.name }

override fun convertFromRaw(rawValue: String) =
CountryUtils.getCountryByCode(CountryCode.create(rawValue), Locale.getDefault())?.name
?: getDisplayItems()[0]

override fun convertToRaw(it: String) =
CountryUtils.getCountryCodeByName(it, Locale.getDefault())?.value
?: null
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ internal sealed interface DropdownConfig {
/**
* This will convert from a raw value used in the parameter map to a disiplayValue
*/
fun convertToRaw(it: String): String?
fun convertToRaw(displayName: String): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ internal class IdealBankConfig : DropdownConfig {
.firstOrNull { it.paymentMethodParamFieldValue == rawValue }
?.paymentMethodParamFieldValue ?: DISPLAY_TO_PARAM[0].displayName

override fun convertToRaw(bankDisplayName: String) = DISPLAY_TO_PARAM
.firstOrNull { it.displayName == bankDisplayName }
override fun convertToRaw(displayName: String) = DISPLAY_TO_PARAM
.firstOrNull { it.displayName == displayName }
?.paymentMethodParamFieldValue

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal sealed interface TextFieldConfig {
/**
* This will convert the field to a raw value to use in the parameter map
*/
fun convertToRaw(it: String): String
fun convertToRaw(displayName: String): String

/**
* This will convert from a raw value used in the parameter map to a disiplayValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ internal class TransformSpecToElement {
spec.field as SectionFieldSpec.Name,
focusRequesterCount
)
SectionFieldSpec.Country -> transform(
spec.field as SectionFieldSpec.Country
is SectionFieldSpec.Country -> transform(
spec.field
)
SectionFieldSpec.IdealBank -> transform(
spec.field as SectionFieldSpec.IdealBank
Expand Down Expand Up @@ -97,7 +97,7 @@ internal class TransformSpecToElement {
private fun transform(spec: SectionFieldSpec.Country) =
SectionFieldElement.Country(
spec.identifier,
DropdownFieldController(CountryConfig())
DropdownFieldController(CountryConfig(spec.onlyShowCountryCodes))
)

private fun transform(spec: SectionFieldSpec.IdealBank) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.stripe.android.paymentsheet.specifications

import androidx.compose.ui.graphics.Color
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.specifications.FormItemSpec.MandateTextSpec
import com.stripe.android.paymentsheet.specifications.FormItemSpec.SaveForFutureUseSpec
import com.stripe.android.paymentsheet.specifications.FormItemSpec.SectionSpec
import com.stripe.android.paymentsheet.specifications.FormItemSpec.MandateTextSpec
import com.stripe.android.paymentsheet.specifications.SectionFieldSpec.Country
import com.stripe.android.paymentsheet.specifications.SectionFieldSpec.Email
import com.stripe.android.paymentsheet.specifications.SectionFieldSpec.Name
Expand All @@ -21,7 +21,8 @@ internal val sofortParamKey: MutableMap<String, Any?> = mutableMapOf(

internal val sofortNameSection = SectionSpec(IdentifierSpec("name"), Name)
internal val sofortEmailSection = SectionSpec(IdentifierSpec("email"), Email)
internal val sofortCountrySection = SectionSpec(IdentifierSpec("country"), Country)
internal val sofortCountrySection =
SectionSpec(IdentifierSpec("country"), Country(setOf("AT", "BE", "DE", "ES", "IT", "NL")))
internal val sofortMandate = MandateTextSpec(
IdentifierSpec("mandate"),
R.string.sofort_mandate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ sealed class FormItemSpec {
) : FormItemSpec(), OptionalItemSpec

/**
* This is an element that will make elements (as specified by identifer hidden
* This is an element that will make elements (as specified by identifier hidden
* when save for future use is unchecked)
*/
data class SaveForFutureUseSpec(
Expand All @@ -66,7 +66,13 @@ sealed class SectionFieldSpec(val identifier: IdentifierSpec) {

object Email : SectionFieldSpec(IdentifierSpec("email"))

object Country : SectionFieldSpec(IdentifierSpec("country"))
/**
* This is the specification for a country field.
* @property onlyShowCountryCodes: a list of country code that should be shown. If empty all
* countries will be shown.
*/
data class Country(val onlyShowCountryCodes: Set<String> = emptySet()) :
SectionFieldSpec(IdentifierSpec("country"))

object IdealBank : SectionFieldSpec(IdentifierSpec("bank"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ class CountryConfigTest {

@Test
fun `Verify the displayed country list`() {
assertThat(CountryConfig(Locale.US).getDisplayItems()[0]).isEqualTo("United States")
assertThat(CountryConfig(locale = Locale.US).getDisplayItems()[0])
.isEqualTo("United States")
}

@Test
fun `Verify the label`() {
CountryConfig().label
assertThat(CountryConfig(Locale.US).label).isEqualTo(R.string.address_label_country)
assertThat(CountryConfig(locale = Locale.US).label)
.isEqualTo(R.string.address_label_country)
}

@Test
fun `Verify only show countries requested`() {
assertThat(
CountryConfig(
onlyShowCountryCodes = setOf("AT"),
locale = Locale.US
).getDisplayItems()[0]
).isEqualTo("Austria")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.Test
import java.util.Locale

class DropdownFieldControllerTest {
private val countryConfig = CountryConfig(Locale.US)
private val countryConfig = CountryConfig(locale = Locale.US)
private val controller = DropdownFieldController(countryConfig)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FormViewModelTest {
private val emailSection = FormItemSpec.SectionSpec(IdentifierSpec("emailSection"), Email)
private val countrySection = FormItemSpec.SectionSpec(
IdentifierSpec("countrySection"),
Country
Country()
)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TransformSpecToElementTest {
fun `Adding a country section sets up the section and country elements correctly`() {
val countrySection = FormItemSpec.SectionSpec(
IdentifierSpec("countrySection"),
SectionFieldSpec.Country
SectionFieldSpec.Country(onlyShowCountryCodes = setOf("AT"))
)
val formElement = transformSpecToElement.transform(
LayoutSpec(
Expand All @@ -58,6 +58,9 @@ class TransformSpecToElementTest {
// of the section field controller
assertThat(countrySectionElement.controller).isEqualTo(countryElement.controller)

assertThat(countryElement.controller.displayItems).hasSize(1)
assertThat(countryElement.controller.displayItems[0]).isEqualTo("Austria")

// Verify the correct config is setup for the controller
assertThat(countryElement.controller.label).isEqualTo(CountryConfig().label)

Expand Down

0 comments on commit 9d7fdc5

Please sign in to comment.