From 32206b0ce7763a2392f7020c0df95adec3d143f6 Mon Sep 17 00:00:00 2001 From: Michael Shafrir Date: Mon, 14 Oct 2019 15:23:03 -0400 Subject: [PATCH] Pass Accept-Language header in API requests This will return localized error messages for requests to /v1/payment_intents. --- .../java/com/stripe/android/ApiRequest.kt | 7 +------ .../java/com/stripe/android/ApiRequestTest.kt | 4 +++- .../java/com/stripe/android/StripeTest.java | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/stripe/src/main/java/com/stripe/android/ApiRequest.kt b/stripe/src/main/java/com/stripe/android/ApiRequest.kt index a0c27353ea9..1d324c4fe64 100644 --- a/stripe/src/main/java/com/stripe/android/ApiRequest.kt +++ b/stripe/src/main/java/com/stripe/android/ApiRequest.kt @@ -40,9 +40,7 @@ internal class ApiRequest internal constructor( mapOf("Stripe-Account" to it) }.orEmpty() ).plus( - (languageTag.takeIf { SHOULD_INCLUDE_ACCEPT_LANGUAGE_HEADER })?.let { - mapOf("Accept-Language" to it) - }.orEmpty() + languageTag?.let { mapOf("Accept-Language" to it) }.orEmpty() ) } @@ -137,9 +135,6 @@ internal class ApiRequest internal constructor( // this is the default user agent set by the system private const val PROP_USER_AGENT = "http.agent" - // TODO(mshafrir-stripe) - enable in next major version - private const val SHOULD_INCLUDE_ACCEPT_LANGUAGE_HEADER = false - @JvmStatic fun createGet( url: String, diff --git a/stripe/src/test/java/com/stripe/android/ApiRequestTest.kt b/stripe/src/test/java/com/stripe/android/ApiRequestTest.kt index 0bee4ec7fb4..d1dc7495556 100644 --- a/stripe/src/test/java/com/stripe/android/ApiRequestTest.kt +++ b/stripe/src/test/java/com/stripe/android/ApiRequestTest.kt @@ -29,6 +29,8 @@ internal class ApiRequestTest { @Test fun getHeaders_withAllRequestOptions_properlyMapsRequestOptions() { + Locale.setDefault(Locale.US) + val stripeAccount = "acct_123abc" val headers = ApiRequest.createGet(StripeApiRepository.sourcesUrl, ApiRequest.Options.create(ApiKeyFixtures.FAKE_PUBLISHABLE_KEY, @@ -41,7 +43,7 @@ internal class ApiRequestTest { ) assertEquals(ApiVersion.get().code, headers["Stripe-Version"]) assertEquals(stripeAccount, headers["Stripe-Account"]) - assertFalse(headers.contains("Accept-Language")) + assertEquals("en-US", headers["Accept-Language"]) } @Test diff --git a/stripe/src/test/java/com/stripe/android/StripeTest.java b/stripe/src/test/java/com/stripe/android/StripeTest.java index 6659111a5be..ab972547837 100644 --- a/stripe/src/test/java/com/stripe/android/StripeTest.java +++ b/stripe/src/test/java/com/stripe/android/StripeTest.java @@ -8,6 +8,7 @@ import com.stripe.android.exception.AuthenticationException; import com.stripe.android.exception.CardException; +import com.stripe.android.exception.InvalidRequestException; import com.stripe.android.exception.StripeException; import com.stripe.android.model.AccountParams; import com.stripe.android.model.Address; @@ -31,6 +32,7 @@ import java.util.concurrent.Executor; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.function.ThrowingRunnable; import org.junit.runner.RunWith; @@ -1039,6 +1041,23 @@ public void run() throws Throwable { assertEquals("Your card number is incorrect.", cardException.getMessage()); } + @Ignore("enable after bumping version to v12.0.0") + public void retrievePaymentIntent_withInvalidClientSecretInGermanyLocale_shouldReturnLocalizedMessage() { + Locale.setDefault(Locale.GERMANY); + + // This card is missing quite a few numbers. + final Stripe stripe = createStripe(); + final InvalidRequestException exception = assertThrows( + InvalidRequestException.class, + new ThrowingRunnable() { + @Override + public void run() throws Throwable { + stripe.retrievePaymentIntentSynchronous("invalid"); + } + }); + assertEquals("Keine solche payment_intent: invalid", exception.getStripeError().getMessage()); + } + @Test public void createTokenSynchronous_withExpiredCard_throwsCardException() { // This card is missing quite a few numbers.