From c31da5e8d03bd83e4c9b47b12c702f09a9030600 Mon Sep 17 00:00:00 2001 From: sshropshire <58225613+sshropshire@users.noreply.github.com> Date: Thu, 28 Sep 2023 09:06:43 -0500 Subject: [PATCH] Card Vault iOS Alignment (#195) * Rename card vaulting types to have Card prefix. * Clean up detekt lint errors. * Update CHANGELOG. --- CHANGELOG.md | 3 ++ .../paypal/android/cardpayments/CardClient.kt | 19 +++++++------ ...{VaultListener.kt => CardVaultListener.kt} | 4 +-- .../{VaultRequest.kt => CardVaultRequest.kt} | 2 +- .../{VaultResult.kt => CardVaultResult.kt} | 2 +- .../DataVaultPaymentMethodTokensAPI.kt | 4 +-- .../cardpayments/CardClientUnitTest.kt | 22 +++++++-------- .../paypal/android/ui/vault/VaultFragment.kt | 28 +++++++++---------- .../paypal/android/ui/vault/VaultUiState.kt | 4 +-- .../paypal/android/ui/vault/VaultViewModel.kt | 8 +++--- 10 files changed, 50 insertions(+), 46 deletions(-) rename CardPayments/src/main/java/com/paypal/android/cardpayments/{VaultListener.kt => CardVaultListener.kt} (84%) rename CardPayments/src/main/java/com/paypal/android/cardpayments/{VaultRequest.kt => CardVaultRequest.kt} (93%) rename CardPayments/src/main/java/com/paypal/android/cardpayments/{VaultResult.kt => CardVaultResult.kt} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee1cb7894..ea344ae1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ * Make `ThreeDSecureResult` class internal * Make `Environment` enum associated values internal * Remove `OrderRequest` class + * Rename `VaultRequest` to `CardVaultRequest` + * Rename `VaultResult` to `CardVaultResult` + * Rename `VaultListener` to `CardVaultListener` * CorePayments * Remove `open` modifier on `PayPalSDKError` diff --git a/CardPayments/src/main/java/com/paypal/android/cardpayments/CardClient.kt b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardClient.kt index d113dbc97..5caa07d5a 100644 --- a/CardPayments/src/main/java/com/paypal/android/cardpayments/CardClient.kt +++ b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardClient.kt @@ -21,7 +21,7 @@ import kotlinx.coroutines.launch * Use this client to approve an order with a [Card]. * * @property approveOrderListener listener to receive callbacks from [CardClient.approveOrder]. - * @property vaultListener listener to receive callbacks form [CardClient.vault]. + * @property cardVaultListener listener to receive callbacks form [CardClient.vault]. */ class CardClient internal constructor( activity: FragmentActivity, @@ -37,7 +37,7 @@ class CardClient internal constructor( /** * @suppress */ - var vaultListener: VaultListener? = null + var cardVaultListener: CardVaultListener? = null private val lifeCycleObserver = CardLifeCycleObserver(this) @@ -46,7 +46,7 @@ class CardClient internal constructor( } private val vaultExceptionHandler = CoreCoroutineExceptionHandler { error -> - vaultListener?.onVaultFailure(error) + cardVaultListener?.onVaultFailure(error) } private var orderId: String? = null @@ -130,20 +130,21 @@ class CardClient internal constructor( * Call this method to attach a payment source to a setup token. * * @param context [Context] Android context - * @param vaultRequest [VaultRequest] request containing details about the setup token and card to use for vaulting. + * @param cardVaultRequest [CardVaultRequest] request containing details about the setup token + * and card to use for vaulting. */ - fun vault(context: Context, vaultRequest: VaultRequest) { + fun vault(context: Context, cardVaultRequest: CardVaultRequest) { val applicationContext = context.applicationContext CoroutineScope(dispatcher).launch(vaultExceptionHandler) { - updateSetupToken(applicationContext, vaultRequest) + updateSetupToken(applicationContext, cardVaultRequest) } } - private suspend fun updateSetupToken(context: Context, vaultRequest: VaultRequest) { - val result = vaultRequest.run { + private suspend fun updateSetupToken(context: Context, cardVaultRequest: CardVaultRequest) { + val result = cardVaultRequest.run { paymentMethodTokensAPI.updateSetupToken(context, setupTokenId, card) } - vaultListener?.onVaultSuccess(result) + cardVaultListener?.onVaultSuccess(result) } internal fun handleBrowserSwitchResult(activity: FragmentActivity) { diff --git a/CardPayments/src/main/java/com/paypal/android/cardpayments/VaultListener.kt b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultListener.kt similarity index 84% rename from CardPayments/src/main/java/com/paypal/android/cardpayments/VaultListener.kt rename to CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultListener.kt index 7e2d5e7d0..268c370be 100644 --- a/CardPayments/src/main/java/com/paypal/android/cardpayments/VaultListener.kt +++ b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultListener.kt @@ -8,13 +8,13 @@ import com.paypal.android.corepayments.PayPalSDKError * * Listener to receive callbacks form [CardClient.vault]. */ -interface VaultListener { +interface CardVaultListener { /** * Called when a successful vault has occurred. */ @MainThread - fun onVaultSuccess(result: VaultResult) + fun onVaultSuccess(result: CardVaultResult) /** * Called when a vault failure has occurred. diff --git a/CardPayments/src/main/java/com/paypal/android/cardpayments/VaultRequest.kt b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultRequest.kt similarity index 93% rename from CardPayments/src/main/java/com/paypal/android/cardpayments/VaultRequest.kt rename to CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultRequest.kt index 9c555d9b8..24eefa823 100644 --- a/CardPayments/src/main/java/com/paypal/android/cardpayments/VaultRequest.kt +++ b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultRequest.kt @@ -12,7 +12,7 @@ import kotlinx.parcelize.Parcelize * @property card card payment source to attach to the setup token. */ @Parcelize -data class VaultRequest( +data class CardVaultRequest( val setupTokenId: String, val card: Card, ) : Parcelable diff --git a/CardPayments/src/main/java/com/paypal/android/cardpayments/VaultResult.kt b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultResult.kt similarity index 93% rename from CardPayments/src/main/java/com/paypal/android/cardpayments/VaultResult.kt rename to CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultResult.kt index b0b68340d..da55b5033 100644 --- a/CardPayments/src/main/java/com/paypal/android/cardpayments/VaultResult.kt +++ b/CardPayments/src/main/java/com/paypal/android/cardpayments/CardVaultResult.kt @@ -12,7 +12,7 @@ import kotlinx.parcelize.Parcelize * @param status the status of the updated setup token */ @Parcelize -data class VaultResult( +data class CardVaultResult( val setupTokenId: String, val status: String ) : Parcelable diff --git a/CardPayments/src/main/java/com/paypal/android/cardpayments/DataVaultPaymentMethodTokensAPI.kt b/CardPayments/src/main/java/com/paypal/android/cardpayments/DataVaultPaymentMethodTokensAPI.kt index 7433af129..ac2fbf515 100644 --- a/CardPayments/src/main/java/com/paypal/android/cardpayments/DataVaultPaymentMethodTokensAPI.kt +++ b/CardPayments/src/main/java/com/paypal/android/cardpayments/DataVaultPaymentMethodTokensAPI.kt @@ -19,7 +19,7 @@ internal class DataVaultPaymentMethodTokensAPI internal constructor( ResourceLoader() ) - suspend fun updateSetupToken(context: Context, setupTokenId: String, card: Card): VaultResult { + suspend fun updateSetupToken(context: Context, setupTokenId: String, card: Card): CardVaultResult { val query = resourceLoader.loadRawResource(context, R.raw.graphql_query_update_setup_token) val cardNumber = card.number.replace("\\s".toRegex(), "") @@ -58,7 +58,7 @@ internal class DataVaultPaymentMethodTokensAPI internal constructor( graphQLClient.send(graphQLRequest, queryName = "UpdateVaultSetupToken") graphQLResponse.data?.let { responseJSON -> val setupToken = responseJSON.getJSONObject("updateVaultSetupToken") - return VaultResult( + return CardVaultResult( setupTokenId = setupToken.getString("id"), status = setupToken.getString("status") ) diff --git a/CardPayments/src/test/java/com/paypal/android/cardpayments/CardClientUnitTest.kt b/CardPayments/src/test/java/com/paypal/android/cardpayments/CardClientUnitTest.kt index adcc1af97..6a98be1ef 100644 --- a/CardPayments/src/test/java/com/paypal/android/cardpayments/CardClientUnitTest.kt +++ b/CardPayments/src/test/java/com/paypal/android/cardpayments/CardClientUnitTest.kt @@ -41,7 +41,7 @@ class CardClientUnitTest { private val orderId = "sample-order-id" private val cardRequest = CardRequest(orderId, card, "return_url") - private val vaultRequest = VaultRequest(setupTokenId = "fake-setup-token-id", card = card) + private val cardVaultRequest = CardVaultRequest(setupTokenId = "fake-setup-token-id", card = card) private val checkoutOrdersAPI = mockk(relaxed = true) private val paymentMethodTokensAPI = mockk(relaxed = true) @@ -59,7 +59,7 @@ class CardClientUnitTest { private val browserSwitchClient = mockk(relaxed = true) private val approveOrderListener = mockk(relaxed = true) - private val vaultListener = mockk(relaxed = true) + private val cardVaultListener = mockk(relaxed = true) private val applicationContext = ApplicationProvider.getApplicationContext() @@ -180,19 +180,19 @@ class CardClientUnitTest { fun `vault notifies listener of update setup token success`() = runTest { val sut = createCardClient(testScheduler) - val vaultResult = VaultResult("fake-setup-token-id-from-result", "fake-status") + val cardVaultResult = CardVaultResult("fake-setup-token-id-from-result", "fake-status") coEvery { paymentMethodTokensAPI.updateSetupToken(applicationContext, "fake-setup-token-id", card) - } returns vaultResult + } returns cardVaultResult - sut.vault(activity, vaultRequest) + sut.vault(activity, cardVaultRequest) advanceUntilIdle() - val resultSlot = slot() - verify(exactly = 1) { vaultListener.onVaultSuccess(capture(resultSlot)) } + val resultSlot = slot() + verify(exactly = 1) { cardVaultListener.onVaultSuccess(capture(resultSlot)) } val actual = resultSlot.captured - assertEquals(vaultResult, actual) + assertEquals(cardVaultResult, actual) } @Test @@ -204,11 +204,11 @@ class CardClientUnitTest { paymentMethodTokensAPI.updateSetupToken(applicationContext, "fake-setup-token-id", card) } throws error - sut.vault(activity, vaultRequest) + sut.vault(activity, cardVaultRequest) advanceUntilIdle() val errorSlot = slot() - verify(exactly = 1) { vaultListener.onVaultFailure(capture(errorSlot)) } + verify(exactly = 1) { cardVaultListener.onVaultFailure(capture(errorSlot)) } val capturedError = errorSlot.captured assertEquals("mock_error_message", capturedError.errorDescription) @@ -225,7 +225,7 @@ class CardClientUnitTest { dispatcher ) sut.approveOrderListener = approveOrderListener - sut.vaultListener = vaultListener + sut.cardVaultListener = cardVaultListener return sut } diff --git a/Demo/src/main/java/com/paypal/android/ui/vault/VaultFragment.kt b/Demo/src/main/java/com/paypal/android/ui/vault/VaultFragment.kt index 67a890cce..177a59c13 100644 --- a/Demo/src/main/java/com/paypal/android/ui/vault/VaultFragment.kt +++ b/Demo/src/main/java/com/paypal/android/ui/vault/VaultFragment.kt @@ -36,9 +36,9 @@ import com.paypal.android.api.model.SetupToken import com.paypal.android.api.services.SDKSampleServerAPI import com.paypal.android.cardpayments.Card import com.paypal.android.cardpayments.CardClient -import com.paypal.android.cardpayments.VaultListener -import com.paypal.android.cardpayments.VaultRequest -import com.paypal.android.cardpayments.VaultResult +import com.paypal.android.cardpayments.CardVaultListener +import com.paypal.android.cardpayments.CardVaultRequest +import com.paypal.android.cardpayments.CardVaultResult import com.paypal.android.corepayments.CoreConfig import com.paypal.android.corepayments.PayPalSDKError import com.paypal.android.models.TestCard @@ -119,10 +119,10 @@ class VaultFragment : Fragment() { val configuration = CoreConfig(clientId = clientId) cardClient = CardClient(requireActivity(), configuration) - cardClient.vaultListener = object : VaultListener { - override fun onVaultSuccess(result: VaultResult) { + cardClient.cardVaultListener = object : CardVaultListener { + override fun onVaultSuccess(result: CardVaultResult) { viewModel.isUpdateSetupTokenLoading = false - viewModel.vaultResult = result + viewModel.cardVaultResult = result } override fun onVaultFailure(error: PayPalSDKError) { @@ -132,8 +132,8 @@ class VaultFragment : Fragment() { } val card = parseCard(viewModel.uiState.value) - val vaultRequest = VaultRequest(viewModel.setupToken!!.id, card) - cardClient.vault(requireContext(), vaultRequest) + val cardVaultRequest = CardVaultRequest(viewModel.setupToken!!.id, card) + cardClient.vault(requireContext(), cardVaultRequest) } } @@ -217,9 +217,9 @@ class VaultFragment : Fragment() { onSubmit = { onAttachCardToSetupTokenSubmit() } ) } - uiState.vaultResult?.let { vaultResult -> + uiState.cardVaultResult?.let { vaultResult -> Spacer(modifier = Modifier.size(8.dp)) - VaultSuccessView(vaultResult = vaultResult) + VaultSuccessView(cardVaultResult = vaultResult) Spacer(modifier = Modifier.size(8.dp)) CreatePaymentTokenForm( uiState = uiState, @@ -234,7 +234,7 @@ class VaultFragment : Fragment() { } @Composable - fun VaultSuccessView(vaultResult: VaultResult) { + fun VaultSuccessView(cardVaultResult: CardVaultResult) { OutlinedCard( modifier = Modifier.fillMaxWidth() ) { @@ -245,8 +245,8 @@ class VaultFragment : Fragment() { text = "Vault Success", style = MaterialTheme.typography.titleLarge, ) - PropertyView(name = "Setup Token Id", value = vaultResult.setupTokenId) - PropertyView(name = "Status", value = vaultResult.status) + PropertyView(name = "Setup Token Id", value = cardVaultResult.setupTokenId) + PropertyView(name = "Status", value = cardVaultResult.status) } } } @@ -270,7 +270,7 @@ class VaultFragment : Fragment() { "1234", "fake-card-brand" ), - vaultResult = VaultResult("456", "fake-status") + cardVaultResult = CardVaultResult("456", "fake-status") ), onCreateSetupTokenSubmit = {}, onAttachCardToSetupTokenSubmit = {}, diff --git a/Demo/src/main/java/com/paypal/android/ui/vault/VaultUiState.kt b/Demo/src/main/java/com/paypal/android/ui/vault/VaultUiState.kt index a3a548c7a..0185582db 100644 --- a/Demo/src/main/java/com/paypal/android/ui/vault/VaultUiState.kt +++ b/Demo/src/main/java/com/paypal/android/ui/vault/VaultUiState.kt @@ -2,7 +2,7 @@ package com.paypal.android.ui.vault import com.paypal.android.api.model.PaymentToken import com.paypal.android.api.model.SetupToken -import com.paypal.android.cardpayments.VaultResult +import com.paypal.android.cardpayments.CardVaultResult data class VaultUiState( val setupToken: SetupToken? = null, @@ -14,5 +14,5 @@ data class VaultUiState( val cardNumber: String = "", val cardExpirationDate: String = "", val cardSecurityCode: String = "", - val vaultResult: VaultResult? = null + val cardVaultResult: CardVaultResult? = null ) diff --git a/Demo/src/main/java/com/paypal/android/ui/vault/VaultViewModel.kt b/Demo/src/main/java/com/paypal/android/ui/vault/VaultViewModel.kt index 8d13a0b1e..5232606bb 100644 --- a/Demo/src/main/java/com/paypal/android/ui/vault/VaultViewModel.kt +++ b/Demo/src/main/java/com/paypal/android/ui/vault/VaultViewModel.kt @@ -3,7 +3,7 @@ package com.paypal.android.ui.vault import androidx.lifecycle.ViewModel import com.paypal.android.api.model.PaymentToken import com.paypal.android.api.model.SetupToken -import com.paypal.android.cardpayments.VaultResult +import com.paypal.android.cardpayments.CardVaultResult import com.paypal.android.models.TestCard import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow @@ -67,10 +67,10 @@ class VaultViewModel : ViewModel() { _uiState.update { it.copy(cardSecurityCode = value) } } - var vaultResult: VaultResult? - get() = _uiState.value.vaultResult + var cardVaultResult: CardVaultResult? + get() = _uiState.value.cardVaultResult set(value) { - _uiState.update { it.copy(vaultResult = value) } + _uiState.update { it.copy(cardVaultResult = value) } } fun prefillCard(testCard: TestCard) {