From a4f753353d4d3d9f08720e207e3f460fbf739b8a Mon Sep 17 00:00:00 2001 From: Michael Shafrir Date: Wed, 29 Apr 2020 14:53:57 -0400 Subject: [PATCH] Capitalize GooglePayJsonFactory's allowedCountryCodes --- .../stripe/android/GooglePayJsonFactory.kt | 26 +++++++++++++----- .../android/GooglePayJsonFactoryTest.kt | 27 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/stripe/src/main/java/com/stripe/android/GooglePayJsonFactory.kt b/stripe/src/main/java/com/stripe/android/GooglePayJsonFactory.kt index 75fc1165f46..20404b296c6 100644 --- a/stripe/src/main/java/com/stripe/android/GooglePayJsonFactory.kt +++ b/stripe/src/main/java/com/stripe/android/GooglePayJsonFactory.kt @@ -162,10 +162,14 @@ class GooglePayJsonFactory constructor( shippingAddressParameters: ShippingAddressParameters ): JSONObject { return JSONObject() - .put("allowedCountryCodes", - JSONArray(shippingAddressParameters.allowedCountryCodes)) - .put("phoneNumberRequired", - shippingAddressParameters.phoneNumberRequired) + .put( + "allowedCountryCodes", + JSONArray(shippingAddressParameters.normalizedAllowedCountryCodes) + ) + .put( + "phoneNumberRequired", + shippingAddressParameters.phoneNumberRequired + ) } private fun createCardPaymentMethod( @@ -335,16 +339,26 @@ class GooglePayJsonFactory constructor( * ISO 3166-1 alpha-2 country code values of the countries where shipping is allowed. * If this object isn't specified, all shipping address countries are allowed. */ - internal val allowedCountryCodes: Set = emptySet(), + private val allowedCountryCodes: Set = emptySet(), /** * Set to true if a phone number is required for the provided shipping address. */ internal val phoneNumberRequired: Boolean = false ) : Parcelable { + /** + * Normalized form of [allowedCountryCodes] (i.e. capitalized country codes) + */ + internal val normalizedAllowedCountryCodes: Set + get() { + return allowedCountryCodes.map { + it.toUpperCase(Locale.ROOT) + }.toSet() + } + init { val countryCodes = Locale.getISOCountries() - allowedCountryCodes.forEach { allowedShippingCountryCode -> + normalizedAllowedCountryCodes.forEach { allowedShippingCountryCode -> require( countryCodes.any { allowedShippingCountryCode == it } ) { diff --git a/stripe/src/test/java/com/stripe/android/GooglePayJsonFactoryTest.kt b/stripe/src/test/java/com/stripe/android/GooglePayJsonFactoryTest.kt index f26df795e62..c745fdede22 100644 --- a/stripe/src/test/java/com/stripe/android/GooglePayJsonFactoryTest.kt +++ b/stripe/src/test/java/com/stripe/android/GooglePayJsonFactoryTest.kt @@ -175,6 +175,33 @@ class GooglePayJsonFactoryTest { .isEqualTo("US") } + @Test + fun shippingAddressAllowedCountryCodes_shouldBeCapitalized() { + val createPaymentDataRequestJson = factory.createPaymentDataRequest( + transactionInfo = GooglePayJsonFactory.TransactionInfo( + currencyCode = "USD", + totalPriceStatus = GooglePayJsonFactory.TransactionInfo.TotalPriceStatus.Estimated, + totalPrice = 500, + countryCode = "US", + totalPriceLabel = "Your total price" + ), + shippingAddressParameters = GooglePayJsonFactory.ShippingAddressParameters( + isRequired = true, + allowedCountryCodes = setOf("us", "de") + ) + ) + + val allowedCountryCodes = createPaymentDataRequestJson + .getJSONObject("shippingAddressParameters") + .getJSONArray("allowedCountryCodes") + .let { + StripeJsonUtils.jsonArrayToList(it) + } + + assertThat(allowedCountryCodes) + .containsExactly("US", "DE") + } + @Test fun allowedCardNetworks_whenJcbDisabled_shouldNotIncludeJcb() { val allowedCardNetworks = factory.createIsReadyToPayRequest()