Skip to content

Commit

Permalink
Fix tests and make code tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Dec 5, 2023
1 parent fbd4a0b commit 67cfa84
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
if (pendingResult is InternalPaymentResult.Completed) {
// If we just received a transaction result after process death, we don't error. Instead, we dismiss
// PaymentSheet and return a `Completed` result to the caller.
val usedPaymentMethod = error.asPaymentSheetLoadingException.paymentMethod
val usedPaymentMethod = error.asPaymentSheetLoadingException.usedPaymentMethod
handlePaymentCompleted(usedPaymentMethod, finishImmediately = true)
} else {
setStripeIntent(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ internal object StripeIntentValidator {

val exception = when {
stripeIntent is PaymentIntent && stripeIntent.confirmationMethod != Automatic -> {
PaymentSheetLoadingException.InvalidConfirmationMethod(paymentMethod, stripeIntent.confirmationMethod)
PaymentSheetLoadingException.InvalidConfirmationMethod(stripeIntent.confirmationMethod)
}
stripeIntent is PaymentIntent && stripeIntent.isInTerminalState -> {
PaymentSheetLoadingException.PaymentIntentInTerminalState(paymentMethod, stripeIntent.status)
}
stripeIntent is PaymentIntent && (stripeIntent.amount == null || stripeIntent.currency == null) -> {
PaymentSheetLoadingException.MissingAmountOrCurrency(paymentMethod)
PaymentSheetLoadingException.MissingAmountOrCurrency
}
stripeIntent is SetupIntent && stripeIntent.isInTerminalState -> {
PaymentSheetLoadingException.SetupIntentInTerminalState(paymentMethod, stripeIntent.status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ internal class DefaultPaymentSheetLoader @Inject constructor(
val requested = stripeIntent.paymentMethodTypes.joinToString(separator = ", ")
val supported = lpmRepository.values().joinToString(separator = ", ") { it.code }

throw PaymentSheetLoadingException.NoPaymentMethodTypesAvailable(
paymentMethod = stripeIntent.paymentMethod,
requested = requested,
supported = supported,
)
throw PaymentSheetLoadingException.NoPaymentMethodTypesAvailable(requested, supported)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import com.stripe.android.paymentsheet.state.PaymentSheetLoadingException.Unknow
internal sealed class PaymentSheetLoadingException : Throwable() {

abstract val type: String
abstract val paymentMethod: PaymentMethod?
abstract val usedPaymentMethod: PaymentMethod?

data class InvalidConfirmationMethod(
override val paymentMethod: PaymentMethod?,
private val confirmationMethod: PaymentIntent.ConfirmationMethod,
) : PaymentSheetLoadingException() {

override val usedPaymentMethod: PaymentMethod? = null

override val type: String = "invalidConfirmationMethod"

override val message: String = """
Expand All @@ -27,11 +28,12 @@ internal sealed class PaymentSheetLoadingException : Throwable() {
}

data class NoPaymentMethodTypesAvailable(
override val paymentMethod: PaymentMethod?,
private val requested: String,
private val supported: String,
) : PaymentSheetLoadingException() {

override val usedPaymentMethod: PaymentMethod? = null

override val type: String = "noPaymentMethodTypesAvailable"

override val message: String
Expand All @@ -40,7 +42,7 @@ internal sealed class PaymentSheetLoadingException : Throwable() {
}

data class PaymentIntentInTerminalState(
override val paymentMethod: PaymentMethod?,
override val usedPaymentMethod: PaymentMethod?,
private val status: StripeIntent.Status?,
) : PaymentSheetLoadingException() {

Expand All @@ -54,7 +56,7 @@ internal sealed class PaymentSheetLoadingException : Throwable() {
}

data class SetupIntentInTerminalState(
override val paymentMethod: PaymentMethod?,
override val usedPaymentMethod: PaymentMethod?,
private val status: StripeIntent.Status?,
) : PaymentSheetLoadingException() {

Expand All @@ -67,9 +69,8 @@ internal sealed class PaymentSheetLoadingException : Throwable() {
""".trimIndent()
}

data class MissingAmountOrCurrency(
override val paymentMethod: PaymentMethod?,
) : PaymentSheetLoadingException() {
object MissingAmountOrCurrency : PaymentSheetLoadingException() {
override val usedPaymentMethod: PaymentMethod? = null
override val type: String = "missingAmountOrCurrency"
override val message: String = "PaymentIntent must contain amount and currency."
}
Expand All @@ -83,7 +84,7 @@ internal sealed class PaymentSheetLoadingException : Throwable() {

override val message: String? = cause.message

override val paymentMethod: PaymentMethod? = null
override val usedPaymentMethod: PaymentMethod? = null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.repositories.CustomerRepository
import com.stripe.android.testing.FeatureFlagTestRule
import com.stripe.android.testing.PaymentIntentFactory
import com.stripe.android.testing.PaymentMethodFactory
import com.stripe.android.ui.core.forms.resources.LpmRepository
import com.stripe.android.utils.FakeCustomerRepository
import com.stripe.android.utils.FakeElementsSessionRepository
Expand Down Expand Up @@ -392,18 +393,21 @@ internal class DefaultPaymentSheetLoaderTest {

@Test
fun `load() when PaymentIntent has invalid status should return null`() = runTest {
val paymentIntent = PaymentIntentFixtures.PI_SUCCEEDED.copy(
paymentMethod = PaymentMethodFactory.card(),
)
val paymentMethod = paymentIntent.paymentMethod!!

val result = createPaymentSheetLoader(
stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy(
status = Succeeded,
),
stripeIntent = paymentIntent,
).load(
initializationMode = PaymentSheet.InitializationMode.PaymentIntent(
clientSecret = PaymentSheetFixtures.PAYMENT_INTENT_CLIENT_SECRET.value,
),
PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY
).exceptionOrNull()

assertThat(result).isEqualTo(PaymentSheetLoadingException.PaymentIntentInTerminalState(Succeeded))
assertThat(result).isEqualTo(PaymentSheetLoadingException.PaymentIntentInTerminalState(paymentMethod, Succeeded))
}

@Test
Expand Down Expand Up @@ -703,7 +707,7 @@ internal class DefaultPaymentSheetLoaderTest {

@Test
fun `Emits correct events when loading fails for deferred intent`() = runTest {
val error = PaymentSheetLoadingException.PaymentIntentInTerminalState(status = Canceled)
val error = PaymentSheetLoadingException.PaymentIntentInTerminalState(usedPaymentMethod = null, status = Canceled)
val loader = createPaymentSheetLoader(error = error)

loader.load(
Expand Down

0 comments on commit 67cfa84

Please sign in to comment.