Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capitalize GooglePayJsonFactory's allowedCountryCodes #2442

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions stripe/src/main/java/com/stripe/android/GooglePayJsonFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<String> = emptySet(),
private val allowedCountryCodes: Set<String> = 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<String>
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 }
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
mshafrir-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
fun allowedCardNetworks_whenJcbDisabled_shouldNotIncludeJcb() {
val allowedCardNetworks = factory.createIsReadyToPayRequest()
Expand Down