Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
NEXT_VERSION_BUMP: MINOR
## XX.XX.XX - 20XX-XX-XX

### CryptoOnramp
* [ADDED] Added `OnrampCoordinator.deleteWalletAddress(walletId:)` to delete a registered wallet address.

### PaymentSheet
* [FIXED] LinkController (private preview) now returns an error when no funding sources are available for a Link session, rather than silently falling back to card.
* [ADDED] Added support for the Agrobank, MBSB Bank, and Bank of China FPX banks, and the FPX bank list is now displayed in alphabetical order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.hasSetTextAction
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isEnabled
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onRoot
Expand All @@ -24,6 +25,7 @@ import com.stripe.android.testing.FeatureFlagTestRule
import com.stripe.android.testing.RetryRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExternalResource
import org.junit.rules.RuleChain
import org.junit.runner.RunWith
import kotlin.time.Duration
Expand All @@ -40,11 +42,18 @@ class OnrampFlowTest {
isEnabled = false
)

private val clearPreferencesRule = object : ExternalResource() {
override fun before() {
clearPrefs()
}
}

@get:Rule
val rule = RuleChain.emptyRuleChain()
.around(composeRule)
.around(attestationFeatureFlagTestRule)
.around(RetryRule(3))
.around(clearPreferencesRule)
.around(activityRule)

private val defaultTimeout: Duration = 30.seconds
Expand All @@ -61,8 +70,6 @@ class OnrampFlowTest {
@OptIn(ExperimentalTestApi::class)
@Test
fun testCheckoutFlow() {
clearPrefs()

waitForTag(LOGIN_EMAIL_TAG)

// Enter test login credentials previously registered with the demo backend.
Expand Down Expand Up @@ -151,7 +158,7 @@ class OnrampFlowTest {
}

private fun performClickOnNode(tag: String, timeoutMs: Long = defaultTimeout.inWholeMilliseconds) {
waitForTag(tag = tag, timeoutMs = timeoutMs)
waitForEnabledTag(tag = tag, timeoutMs = timeoutMs)

val node = composeRule.onNodeWithTag(tag)
runCatching { node.performScrollTo() }
Expand All @@ -162,6 +169,14 @@ class OnrampFlowTest {
node.performClick()
}

@OptIn(ExperimentalTestApi::class)
private fun waitForEnabledTag(tag: String, timeoutMs: Long) {
composeRule.waitUntilExactlyOneExists(
matcher = hasTestTag(tag).and(isEnabled()),
timeoutMillis = timeoutMs
)
}

private fun scrollContentUp() {
runCatching {
composeRule.onRoot().performTouchInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ internal const val LOGIN_LOGIN_BUTTON_TAG = "LoginLoginButtonTag"
internal const val LOGIN_REGISTER_BUTTON_TAG = "LoginRegisterButtonTag"
internal const val AUTHENTICATE_BUTTON_TAG = "AuthenticateButtonTag"
internal const val REGISTER_WALLET_BUTTON_TAG = "RegisterWalletButtonTag"
internal const val DELETE_WALLET_BUTTON_TAG_PREFIX = "DeleteWalletButtonTag-"
internal const val GET_WALLET_OWNERSHIP_CHALLENGE_BUTTON_TAG = "GetWalletOwnershipChallengeButtonTag"
internal const val SUBMIT_WALLET_OWNERSHIP_SIGNATURE_BUTTON_TAG = "SubmitWalletOwnershipSignatureButtonTag"
internal const val COLLECT_CARD_BUTTON_TAG = "CollectCardButtonTag"
Expand All @@ -17,3 +18,7 @@ internal const val INITIALIZATION_FAILURE_ALERT_TITLE_TAG = "InitializationFailu
internal const val INITIALIZATION_FAILURE_ALERT_MESSAGE_TAG = "InitializationFailureAlertMessageTag"
internal const val SNACKBAR_TAG = "SnackbarTag"
internal const val SNACKBAR_TEXT_TAG = "SnackbarTextTag"

internal fun deleteWalletButtonTag(walletId: String): String {
return "$DELETE_WALLET_BUTTON_TAG_PREFIX$walletId"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.stripe.android.crypto.onramp.example.model.OnrampUiState
import com.stripe.android.crypto.onramp.example.model.OnrampUserData
import com.stripe.android.crypto.onramp.example.model.Screen
import com.stripe.android.crypto.onramp.example.model.SourceCurrency
import com.stripe.android.crypto.onramp.example.network.CustomerWallet
import com.stripe.android.crypto.onramp.example.network.LoginSignUpResponse
import com.stripe.android.crypto.onramp.example.network.OnrampSessionResponse
import com.stripe.android.crypto.onramp.example.network.SettlementSpeed
Expand All @@ -35,6 +36,7 @@ import com.stripe.android.crypto.onramp.model.OnrampCheckoutResult
import com.stripe.android.crypto.onramp.model.OnrampCollectPaymentMethodResult
import com.stripe.android.crypto.onramp.model.OnrampConfigurationResult
import com.stripe.android.crypto.onramp.model.OnrampCreateCryptoPaymentTokenResult
import com.stripe.android.crypto.onramp.model.OnrampDeleteWalletAddressResult
import com.stripe.android.crypto.onramp.model.OnrampGetWalletOwnershipChallengeResult
import com.stripe.android.crypto.onramp.model.OnrampHasLinkAccountResult
import com.stripe.android.crypto.onramp.model.OnrampLogOutResult
Expand Down Expand Up @@ -580,6 +582,36 @@ internal class OnrampViewModel(
}
}

fun refreshWallets() {
val currentState = _uiState.value
if (currentState.isWalletsLoading) return

val authToken = currentState.authToken
if (authToken.isNullOrBlank()) {
_message.value = "No auth token found. Please log in again."
return
}

_uiState.update { it.copy(isWalletsLoading = true) }

viewModelScope.launch {
when (val result = testBackendRepository.fetchCustomerWallets(authToken)) {
is Result.Success -> {
_uiState.update {
it.copy(
wallets = result.value.data,
isWalletsLoading = false
)
}
}
is Result.Failure -> {
_message.value = "Failed to fetch wallets: ${result.error.message}"
_uiState.update { it.copy(isWalletsLoading = false) }
}
}
}
}

fun getWalletOwnershipChallenge(walletAddress: String, network: CryptoNetwork) {
viewModelScope.launch {
val trimmedWalletAddress = walletAddress.trim()
Expand Down Expand Up @@ -630,6 +662,36 @@ internal class OnrampViewModel(
}
}

fun deleteWallet(wallet: CustomerWallet) {
if (_uiState.value.isWalletsLoading) return

_uiState.update { it.copy(isWalletsLoading = true) }

viewModelScope.launch {
when (val result = onrampCoordinator.deleteWalletAddress(wallet.id)) {
is OnrampDeleteWalletAddressResult.Completed -> {
_message.value = "Wallet deleted successfully!"
_uiState.update { currentState ->
val deletedCurrentWallet = currentState.walletAddress == wallet.walletAddress &&
currentState.network?.value == wallet.network
currentState.copy(
wallets = currentState.wallets.filterNot { it.id == wallet.id },
walletAddress = currentState.walletAddress.takeUnless { deletedCurrentWallet },
network = currentState.network.takeUnless { deletedCurrentWallet },
isWalletsLoading = false
)
}
}
is OnrampDeleteWalletAddressResult.Failed -> {
_uiState.update { it.copy(isWalletsLoading = false) }
handleError(result.error) {
_message.value = "Failed to delete wallet: ${result.error.message}"
}
}
}
}
}

fun submitWalletOwnershipSignature(signature: String) {
viewModelScope.launch {
val challengeId = _uiState.value.walletOwnershipChallengeId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.crypto.onramp.example.model

import android.os.Parcel
import android.os.Parcelable
import com.stripe.android.crypto.onramp.example.network.CustomerWallet
import com.stripe.android.crypto.onramp.example.network.OnrampSessionResponse
import com.stripe.android.crypto.onramp.example.network.SettlementSpeed
import com.stripe.android.crypto.onramp.model.CryptoNetwork
Expand All @@ -24,6 +25,8 @@ internal data class OnrampUiState(
val cryptoPaymentToken: String? = null,
val walletAddress: String? = null,
val network: CryptoNetwork? = null,
val wallets: List<CustomerWallet> = emptyList(),
val isWalletsLoading: Boolean = false,
val walletOwnershipChallengeId: String? = null,
val walletOwnershipChallengeMessage: String? = null,
val walletOwnershipChallengeExpiresAt: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.stripe.android.crypto.onramp.example.network

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class CustomerWalletsResponse(
val count: Int,
val data: List<CustomerWallet>
)

@Parcelize
@Serializable
data class CustomerWallet(
val id: String,
val network: String,
@SerialName("wallet_address")
val walletAddress: String,
@SerialName("verified_ownership")
val verifiedOwnership: Boolean
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ class TestBackendRepository {
ignoreUnknownKeys = true
}

suspend fun fetchCustomerWallets(
authToken: String
): ApiResult<CustomerWalletsResponse, FuelError> {
return withContext(Dispatchers.IO) {
manager.get("$baseUrl/customer_wallets?limit=$CUSTOMER_WALLETS_LIMIT")
.timeout(SESSION_CREATION_TIMEOUT)
.timeoutRead(SESSION_CREATION_TIMEOUT)
.header("Authorization", "Bearer $authToken")
.suspendable()
.awaitModel(CustomerWalletsResponse.serializer(), json)
}
}

suspend fun createOnrampSession(
paymentToken: String,
walletAddress: String,
Expand Down Expand Up @@ -204,3 +217,4 @@ suspend fun <T : Any> Request.awaitModel(
}

private const val SESSION_CREATION_TIMEOUT = 60000 // 60 seconds
private const val CUSTOMER_WALLETS_LIMIT = 50
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ internal fun OnrampScreen(
uiState = uiState,
onAuthenticate = onAuthenticateUser,
onRegisterWalletAddress = onRegisterWalletAddress,
onDeleteWallet = viewModel::deleteWallet,
onRefreshWallets = viewModel::refreshWallets,
onGetWalletOwnershipChallenge = viewModel::getWalletOwnershipChallenge,
onSubmitWalletOwnershipSignature = viewModel::submitWalletOwnershipSignature,
onWalletOwnershipSignatureChange = viewModel::updateWalletOwnershipSignatureInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.stripe.android.crypto.onramp.example.model.OnrampUiState
import com.stripe.android.crypto.onramp.example.model.SourceCurrency
import com.stripe.android.crypto.onramp.example.network.CustomerWallet
import com.stripe.android.crypto.onramp.example.network.SettlementSpeed
import com.stripe.android.crypto.onramp.model.CryptoNetwork
import com.stripe.android.crypto.onramp.model.KycInfo
Expand All @@ -34,6 +35,8 @@ internal fun AuthenticatedOperationsScreen(
uiState: OnrampUiState,
onAuthenticate: (String) -> Unit,
onRegisterWalletAddress: (String, CryptoNetwork) -> Unit,
onDeleteWallet: (CustomerWallet) -> Unit,
onRefreshWallets: () -> Unit,
onGetWalletOwnershipChallenge: (String, CryptoNetwork) -> Unit,
onSubmitWalletOwnershipSignature: (String) -> Unit,
onWalletOwnershipSignatureChange: (String) -> Unit,
Expand Down Expand Up @@ -73,10 +76,12 @@ internal fun AuthenticatedOperationsScreen(
var isKycExpanded by remember { mutableStateOf(false) }
var isIdentifierExpanded by remember { mutableStateOf(false) }

LaunchedEffect(Unit) {
onRefreshWallets()
}

LaunchedEffect(uiState.walletAddress) {
if (!uiState.walletAddress.isNullOrBlank()) {
walletAddressInput = uiState.walletAddress
}
walletAddressInput = uiState.walletAddress ?: DEFAULT_WALLET_ADDRESS
}

LaunchedEffect(uiState.network) {
Expand Down Expand Up @@ -109,6 +114,8 @@ internal fun AuthenticatedOperationsScreen(
AuthenticateSection(onAuthenticate = onAuthenticate)

WalletAddressSection(
wallets = uiState.wallets,
isLoading = uiState.isWalletsLoading,
walletAddress = walletAddressInput,
onWalletAddressChange = { walletAddressInput = it },
selectedNetwork = selectedNetwork,
Expand All @@ -117,7 +124,9 @@ internal fun AuthenticatedOperationsScreen(
onSelectNetwork = { selectedNetwork = it },
onRegisterWalletAddress = {
onRegisterWalletAddress(walletAddressInput, selectedNetwork)
}
},
onDeleteWallet = onDeleteWallet,
onRefreshWallets = onRefreshWallets
)

WalletOwnershipSection(
Expand Down
Loading