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

Fix error mapping when returning from 3DS #7499

Merged
merged 4 commits into from
Oct 25, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## XX.XX.XX - 2023-XX-XX

### PaymentSheet
* [FIXED][7499](https://github.com/stripe/stripe-android/pull/7499) Fixed an issue with incorrect error messages when encountering a failure after 3D Secure authentication.

## 20.34.1 - 2023-10-24

### Payments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package com.stripe.android.networking
import android.content.Context
import com.stripe.android.R
import com.stripe.android.core.StripeError
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.SetupIntent
import java.util.Locale
import com.stripe.android.uicore.R as UiCoreR

@Suppress("ComplexMethod")
internal fun StripeError.withLocalizedMessage(context: Context): StripeError {
val newMessage = if (shouldFallBackToLocalizedError) {
context.mapErrorCodeToLocalizedMessage(code)
Expand All @@ -17,6 +18,26 @@ internal fun StripeError.withLocalizedMessage(context: Context): StripeError {
return copy(message = newMessage)
}

internal fun PaymentIntent.Error.withLocalizedMessage(context: Context): PaymentIntent.Error {
val newMessage = if (shouldFallBackToLocalizedError) {
context.mapErrorCodeToLocalizedMessage(code)
} else {
message ?: context.mapErrorCodeToLocalizedMessage(code)
}

return copy(message = newMessage)
}

internal fun SetupIntent.Error.withLocalizedMessage(context: Context): SetupIntent.Error {
val newMessage = if (shouldFallBackToLocalizedError) {
context.mapErrorCodeToLocalizedMessage(code)
} else {
message ?: context.mapErrorCodeToLocalizedMessage(code)
}

return copy(message = newMessage)
}

internal fun Context.mapErrorCodeToLocalizedMessage(code: String?): String? {
val messageResourceId = when (code) {
"incorrect_number" -> R.string.stripe_invalid_card_number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.StripeIntent
import com.stripe.android.networking.mapErrorCodeToLocalizedMessage
import com.stripe.android.networking.withLocalizedMessage

internal class PaymentFlowFailureMessageFactory(
private val context: Context
Expand Down Expand Up @@ -52,8 +52,7 @@ internal class PaymentFlowFailureMessageFactory(
context.resources.getString(R.string.stripe_failure_reason_authentication)
}
paymentIntent.lastPaymentError?.type == PaymentIntent.Error.Type.CardError -> {
val error = paymentIntent.lastPaymentError
context.mapErrorCodeToLocalizedMessage(error.code) ?: error.message
paymentIntent.lastPaymentError.withLocalizedMessage(context).message
}
else -> {
null
Expand All @@ -67,8 +66,7 @@ internal class PaymentFlowFailureMessageFactory(
context.resources.getString(R.string.stripe_failure_reason_authentication)
}
setupIntent.lastSetupError?.type == SetupIntent.Error.Type.CardError -> {
val error = setupIntent.lastSetupError
context.mapErrorCodeToLocalizedMessage(error.code) ?: error.message
setupIntent.lastSetupError.withLocalizedMessage(context).message
}
else -> {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.stripe.android.model.parsers.PaymentIntentJsonParser
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.robolectric.RobolectricTestRunner
import java.util.Locale
import kotlin.test.Test

@RunWith(RobolectricTestRunner::class)
Expand Down Expand Up @@ -81,11 +82,11 @@ class PaymentFlowFailureMessageFactoryTest {
}

@Test
fun `Uses localized error message if available`() {
fun `Uses error message from backend if using suitable locale`() = withLocale(Locale.US) {
val intent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy(
lastPaymentError = PaymentIntent.Error(
code = "card_declined",
message = "this is a message that shouldn't be shown",
message = "this is a message that should be shown",
paymentMethod = mock(),
type = PaymentIntent.Error.Type.CardError,
charge = null,
Expand All @@ -96,15 +97,17 @@ class PaymentFlowFailureMessageFactoryTest {
)

val result = factory.create(intent, StripeIntentResult.Outcome.FAILED)
assertThat(result).isEqualTo("Your card was declined")
assertThat(result).isEqualTo("this is a message that should be shown")
}

@Test
fun `Uses provided error message if no localized message is available`() {
fun `Uses local error message if using locale that we can't properly handle`() = withLocale(
locale = Locale("es", "ar"),
) {
val intent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy(
lastPaymentError = PaymentIntent.Error(
code = "some_code_that_we_dont_know",
message = "this is a message that should be shown",
code = "card_declined",
message = "this is a message that shouldn't be shown",
paymentMethod = mock(),
type = PaymentIntent.Error.Type.CardError,
charge = null,
Expand All @@ -115,6 +118,17 @@ class PaymentFlowFailureMessageFactoryTest {
)

val result = factory.create(intent, StripeIntentResult.Outcome.FAILED)
assertThat(result).isEqualTo("this is a message that should be shown")
assertThat(result).isEqualTo("Your card was declined")
}
}

private fun withLocale(locale: Locale, block: () -> Unit) {
val original = Locale.getDefault()
Locale.setDefault(locale)

try {
block()
} finally {
Locale.setDefault(original)
}
}