Skip to content

Commit

Permalink
refactor: inject Risk SDK as UseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
precious-ossai-cko committed Feb 29, 2024
1 parent 3fbb17e commit a007293
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.checkout.tokenization.mapper.response.CVVTokenizationNetworkDataMappe
import com.checkout.tokenization.mapper.response.CardTokenizationNetworkDataMapper
import com.checkout.tokenization.repository.TokenRepository
import com.checkout.tokenization.repository.TokenRepositoryImpl
import com.checkout.tokenization.usecase.RiskSdkUseCase
import com.checkout.tokenization.usecase.ValidateCVVTokenizationDataUseCase
import com.checkout.tokenization.usecase.ValidateTokenizationDataUseCase
import com.checkout.validation.validator.AddressValidator
Expand Down Expand Up @@ -52,8 +53,8 @@ public object CheckoutApiServiceFactory {
publicKey: String,
environment: Environment,
): TokenRepository = TokenRepositoryImpl(
context = context,
environment = environment,
// context = context,
// environment = environment,
networkApiClient = provideNetworkApiClient(publicKey, environment.url),
cardToTokenRequestMapper = CardToTokenRequestMapper(),
cvvToTokenNetworkRequestMapper = CVVToTokenNetworkRequestMapper(),
Expand All @@ -68,6 +69,7 @@ public object CheckoutApiServiceFactory {
logger = TokenizationEventLogger(EventLoggerProvider.provide()),
publicKey = publicKey,
cvvTokenizationNetworkDataMapper = CVVTokenizationNetworkDataMapper(),
riskSdkUseCase = RiskSdkUseCase(environment, context, publicKey),
)

private fun provideNetworkApiClient(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.checkout.tokenization.repository

import android.content.Context
import androidx.annotation.VisibleForTesting
import com.checkout.BuildConfig
import com.checkout.base.mapper.Mapper
import com.checkout.base.model.Environment
import com.checkout.base.usecase.UseCase
import com.checkout.network.response.NetworkApiResponse
import com.checkout.risk.Risk
import com.checkout.risk.RiskConfig
import com.checkout.risk.RiskEnvironment
import com.checkout.tokenization.NetworkApiClient
import com.checkout.tokenization.entity.GooglePayEntity
import com.checkout.tokenization.error.TokenizationError
Expand Down Expand Up @@ -41,8 +36,6 @@ import org.json.JSONObject

@Suppress("LongParameterList")
internal class TokenRepositoryImpl(
private val context: Context,
private val environment: Environment,
private val networkApiClient: NetworkApiClient,
private val cardToTokenRequestMapper: Mapper<Card, TokenRequest>,
private val cvvToTokenNetworkRequestMapper: Mapper<CVVTokenizationRequest, CVVTokenNetworkRequest>,
Expand All @@ -52,6 +45,7 @@ internal class TokenRepositoryImpl(
private val logger: TokenizationLogger,
private val publicKey: String,
private val cvvTokenizationNetworkDataMapper: TokenizationNetworkDataMapper<CVVTokenDetails>,
private val riskSdkUseCase: UseCase<TokenResult<TokenDetails>, Unit>,
) : TokenRepository {
@VisibleForTesting
var networkCoroutineScope =
Expand All @@ -68,25 +62,6 @@ internal class TokenRepositoryImpl(
networkCoroutineScope.launch {
val validationTokenizationDataResult = validateTokenizationDataUseCase.execute(cardTokenRequest.card)

val riskEnvironment =
when (environment) {
Environment.PRODUCTION -> RiskEnvironment.PRODUCTION
Environment.SANDBOX -> RiskEnvironment.SANDBOX
}
val riskInstance =
Risk.getInstance(
context,
RiskConfig(
publicKey = publicKey,
environment = riskEnvironment,
framesMode = true,
),
).let {
it ?: run {
null
}
}

when (validationTokenizationDataResult) {
is ValidationResult.Failure -> {
response = NetworkApiResponse.InternalError(validationTokenizationDataResult.error)
Expand All @@ -106,16 +81,13 @@ internal class TokenRepositoryImpl(
}

val tokenResult = cardTokenizationNetworkDataMapper.toTokenResult(response)
when (tokenResult) {
is TokenResult.Success -> {
riskInstance?.publishData(cardToken = tokenResult.result.token)
}
is TokenResult.Failure -> {}
}

riskSdkUseCase.execute(tokenResult)

launch(Dispatchers.Main) {
handleResponse(tokenResult, cardTokenRequest.onSuccess, cardTokenRequest.onFailure)
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.checkout.tokenization.usecase

import android.content.Context
import com.checkout.base.model.Environment
import com.checkout.base.usecase.UseCase
import com.checkout.risk.PublishDataResult
import com.checkout.risk.Risk
import com.checkout.risk.RiskConfig
import com.checkout.risk.RiskEnvironment
import com.checkout.tokenization.model.TokenDetails
import com.checkout.tokenization.model.TokenResult
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

internal class RiskSdkUseCase(
private val environment: Environment,
private val context: Context,
private val publicKey: String,
) : UseCase<TokenResult<TokenDetails>, Unit> {
override fun execute(data: TokenResult<TokenDetails>) {
val riskEnvironment =
when (environment) {
Environment.PRODUCTION -> RiskEnvironment.PRODUCTION
Environment.SANDBOX -> RiskEnvironment.SANDBOX
}

CoroutineScope(Dispatchers.IO).launch {
val riskInstance =
Risk.getInstance(
context,
RiskConfig(
publicKey = publicKey,
environment = riskEnvironment,
framesMode = true,
),
).let {
it ?: run {
null
}
}

when (data) {
is TokenResult.Success -> {
riskInstance?.publishData(cardToken = data.result.token)
}
is TokenResult.Failure -> {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.checkout.tokenization.repository

import android.content.Context
import com.checkout.base.model.CardScheme
import com.checkout.base.model.Environment
import com.checkout.base.usecase.UseCase
import com.checkout.mock.TokenizationRequestTestData
import com.checkout.mock.TokenizationRequestTestData.cvvTokenizationRequest
Expand All @@ -20,6 +18,8 @@ import com.checkout.tokenization.model.CVVTokenizationResultHandler
import com.checkout.tokenization.model.Card
import com.checkout.tokenization.model.CardTokenRequest
import com.checkout.tokenization.model.GooglePayTokenRequest
import com.checkout.tokenization.model.TokenDetails
import com.checkout.tokenization.model.TokenResult
import com.checkout.tokenization.model.ValidateCVVTokenizationRequest
import com.checkout.tokenization.response.CVVTokenDetailsResponse
import com.checkout.tokenization.response.TokenDetailsResponse
Expand Down Expand Up @@ -57,6 +57,9 @@ internal class TokenRepositoryImplTest {
@RelaxedMockK
private lateinit var mockValidateTokenizationDataUseCase: UseCase<Card, ValidationResult<Unit>>

@RelaxedMockK
private lateinit var mockRiskSdkUseCase: UseCase<TokenResult<TokenDetails>, Unit>

@RelaxedMockK
private lateinit var mockValidateCVVTokenizationDataUseCase:
UseCase<ValidateCVVTokenizationRequest, ValidationResult<Unit>>
Expand All @@ -68,9 +71,6 @@ internal class TokenRepositoryImplTest {

@BeforeEach
fun setUp() {
val mockContext = mockk<Context>()
val mockEnvironment = Environment.SANDBOX

tokenRepositoryImpl =
TokenRepositoryImpl(
networkApiClient = mockTokenNetworkApiClient,
Expand All @@ -82,8 +82,7 @@ internal class TokenRepositoryImplTest {
logger = mockTokenizationLogger,
publicKey = "test_key",
cvvTokenizationNetworkDataMapper = CVVTokenizationNetworkDataMapper(),
context = mockContext,
environment = mockEnvironment,
riskSdkUseCase = mockRiskSdkUseCase,
)
}

Expand Down Expand Up @@ -189,6 +188,8 @@ internal class TokenRepositoryImplTest {

coEvery { mockTokenNetworkApiClient.sendCardTokenRequest(any()) } returns response

coEvery { mockRiskSdkUseCase.execute(any()) } returns Unit

// When
tokenRepositoryImpl.sendCardTokenRequest(
CardTokenRequest(
Expand Down

0 comments on commit a007293

Please sign in to comment.