From a94135507fe66b23efd78599fc8bb89d8247363d Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 21 Jun 2021 12:31:50 +1000 Subject: [PATCH 1/3] Fixes issue with Google Pay returning code 10, developer error, because totalPrice is being displayed in Eastern Arabic numerals when the locale is set to Arabic --- .../main/java/com/stripe/android/PayWithGoogleUtils.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt b/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt index c60f9ecd6c4..edd5d1ddc3b 100644 --- a/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt +++ b/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt @@ -3,6 +3,7 @@ package com.stripe.android import java.text.DecimalFormat import java.text.DecimalFormatSymbols import java.util.Currency +import java.util.Locale import kotlin.math.pow /** @@ -27,7 +28,7 @@ object PayWithGoogleUtils { for (i in 0 until totalLength) { builder.append('#') } - val noDecimalCurrencyFormat = DecimalFormat(builder.toString()) + val noDecimalCurrencyFormat = DecimalFormat(builder.toString(), DecimalFormatSymbols.getInstance(Locale.ROOT)) noDecimalCurrencyFormat.currency = currency noDecimalCurrencyFormat.isGroupingUsed = false return noDecimalCurrencyFormat.format(price) @@ -49,9 +50,8 @@ object PayWithGoogleUtils { val modBreak = 10.0.pow(fractionDigits.toDouble()) val decimalPrice = price / modBreak - // No matter the Locale, Android Pay requires a dot for the decimal separator. - val symbolOverride = DecimalFormatSymbols() - symbolOverride.decimalSeparator = '.' + // No matter the Locale, Android Pay requires a dot for the decimal separator, and Arabic numbers. + val symbolOverride = DecimalFormatSymbols.getInstance(Locale.ROOT) val decimalFormat = DecimalFormat(builder.toString(), symbolOverride) decimalFormat.currency = currency decimalFormat.isGroupingUsed = false From 0cc7c886f64ea0a53f511a41c965efd391b79e50 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 21 Jun 2021 12:42:56 +1000 Subject: [PATCH 2/3] Added unit test PayWithGoogleUtilsTest.getPriceString_whenLocaleWithArabicNumerals_returnsExpectedValue --- .../com/stripe/android/PayWithGoogleUtilsTest.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/payments-core/src/test/java/com/stripe/android/PayWithGoogleUtilsTest.kt b/payments-core/src/test/java/com/stripe/android/PayWithGoogleUtilsTest.kt index d90d0058643..4de1e4c848e 100644 --- a/payments-core/src/test/java/com/stripe/android/PayWithGoogleUtilsTest.kt +++ b/payments-core/src/test/java/com/stripe/android/PayWithGoogleUtilsTest.kt @@ -53,4 +53,17 @@ class PayWithGoogleUtilsTest { val littlePrice = getPriceString(7, Currency.getInstance("CLP")) assertThat(littlePrice).isEqualTo("7") } + + @Test + fun getPriceString_whenLocaleWithArabicNumerals_returnsExpectedValue() { + Locale.setDefault(Locale.Builder().setLanguage("ar").setRegion("AE").build()) + val priceString = getPriceString(100, Currency.getInstance("USD")) + assertThat(priceString).isEqualTo("1.00") + + val littlePrice = getPriceString(8, Currency.getInstance("EUR")) + assertThat(littlePrice).isEqualTo("0.08") + + val bigPrice = getPriceString(20000000, Currency.getInstance("GBP")) + assertThat(bigPrice).isEqualTo("200000.00") + } } From 4afeaff7d9ecd50db02721762d8f1eeefca9a545 Mon Sep 17 00:00:00 2001 From: "Bruno R. Nunes" Date: Tue, 22 Jun 2021 10:05:33 -0500 Subject: [PATCH 3/3] Respect line width --- .../src/main/java/com/stripe/android/PayWithGoogleUtils.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt b/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt index edd5d1ddc3b..e4888468a63 100644 --- a/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt +++ b/payments-core/src/main/java/com/stripe/android/PayWithGoogleUtils.kt @@ -28,7 +28,8 @@ object PayWithGoogleUtils { for (i in 0 until totalLength) { builder.append('#') } - val noDecimalCurrencyFormat = DecimalFormat(builder.toString(), DecimalFormatSymbols.getInstance(Locale.ROOT)) + val noDecimalCurrencyFormat = + DecimalFormat(builder.toString(), DecimalFormatSymbols.getInstance(Locale.ROOT)) noDecimalCurrencyFormat.currency = currency noDecimalCurrencyFormat.isGroupingUsed = false return noDecimalCurrencyFormat.format(price) @@ -50,7 +51,8 @@ object PayWithGoogleUtils { val modBreak = 10.0.pow(fractionDigits.toDouble()) val decimalPrice = price / modBreak - // No matter the Locale, Android Pay requires a dot for the decimal separator, and Arabic numbers. + // No matter the Locale, Android Pay requires a dot for the decimal separator, and Arabic + // numbers. val symbolOverride = DecimalFormatSymbols.getInstance(Locale.ROOT) val decimalFormat = DecimalFormat(builder.toString(), symbolOverride) decimalFormat.currency = currency