Skip to content

Commit

Permalink
Pass Accept-Language header in API requests (#1701)
Browse files Browse the repository at this point in the history
This will return localized error messages for
requests to /v1/payment_intents.
  • Loading branch information
mshafrir-stripe authored Oct 15, 2019
1 parent b24d3a3 commit ad7c0bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
7 changes: 1 addition & 6 deletions stripe/src/main/java/com/stripe/android/ApiRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion stripe/src/test/java/com/stripe/android/ApiRequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions stripe/src/test/java/com/stripe/android/StripeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ad7c0bd

Please sign in to comment.