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

Throw on get saved payment methods failure in test mode PaymentSheet #7999

Merged
merged 7 commits into from
Feb 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ internal class DefaultPaymentSheetLoader @Inject constructor(
val paymentMethods = customerRepository.getPaymentMethods(
customerConfig = customerConfig,
types = paymentMethodTypes,
silentlyFail = true,
).getOrDefault(emptyList())
silentlyFail = stripeIntent.isLiveMode,
jaynewstrom-stripe marked this conversation as resolved.
Show resolved Hide resolved
).getOrThrow()
jaynewstrom-stripe marked this conversation as resolved.
Show resolved Hide resolved

return paymentMethods.filter { paymentMethod ->
paymentMethod.hasExpectedDetails()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,30 @@ internal class DefaultPaymentSheetLoaderTest {
.containsExactly(PaymentMethod.Type.Card)
}

@Test
fun `when getPaymentMethods fails in test mode, load() fails`() =
runTest {
val expectedException = IllegalArgumentException("invalid API key provided")
val customerRepository =
FakeCustomerRepository(PAYMENT_METHODS, onGetPaymentMethods = { Result.failure(expectedException) })
val loader = createPaymentSheetLoader(
stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy(
isLiveMode = false,
),
customerRepo = customerRepository
)

val loadResult = loader.load(
initializationMode = PaymentSheet.InitializationMode.PaymentIntent(
clientSecret = PaymentSheetFixtures.PAYMENT_INTENT_CLIENT_SECRET.value,
),
PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY
)

val actualException = loadResult.exceptionOrNull()
assertThat(actualException?.cause).isEqualTo(expectedException)
}

@Test
fun `load() with customer should filter out invalid payment method types`() =
runTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import com.stripe.android.paymentsheet.repositories.CustomerRepository
internal open class FakeCustomerRepository(
private val paymentMethods: List<PaymentMethod> = emptyList(),
private val customer: Customer? = null,
private val onGetPaymentMethods: () -> Result<List<PaymentMethod>> = {
Result.success(paymentMethods)
},
private val onDetachPaymentMethod: () -> Result<PaymentMethod> = {
Result.failure(NotImplementedError())
},
Expand All @@ -19,7 +22,6 @@ internal open class FakeCustomerRepository(
Result.failure(NotImplementedError())
}
) : CustomerRepository {
lateinit var savedPaymentMethod: PaymentMethod
var error: Throwable? = null

override suspend fun retrieveCustomer(
Expand All @@ -31,7 +33,7 @@ internal open class FakeCustomerRepository(
customerConfig: PaymentSheet.CustomerConfiguration,
types: List<PaymentMethod.Type>,
silentlyFail: Boolean,
): Result<List<PaymentMethod>> = Result.success(paymentMethods)
): Result<List<PaymentMethod>> = onGetPaymentMethods()

override suspend fun detachPaymentMethod(
customerConfig: PaymentSheet.CustomerConfiguration,
Expand Down
Loading