Skip to content

Commit

Permalink
PRISM-11246: await risk sdk actions
Browse files Browse the repository at this point in the history
chore: update type
  • Loading branch information
precious-ossai-cko committed May 16, 2024
1 parent a829a5a commit 3c95601
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import com.checkout.tokenization.request.GooglePayTokenNetworkRequest
import com.checkout.tokenization.request.TokenRequest
import com.checkout.tokenization.response.CVVTokenDetailsResponse
import com.checkout.tokenization.response.TokenDetailsResponse
import com.checkout.tokenization.usecase.RiskSdkUseCase
import com.checkout.tokenization.utils.TokenizationConstants
import com.checkout.validation.model.ValidationResult
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONException
import org.json.JSONObject

Expand All @@ -45,7 +47,7 @@ internal class TokenRepositoryImpl(
private val logger: TokenizationLogger,
private val publicKey: String,
private val cvvTokenizationNetworkDataMapper: TokenizationNetworkDataMapper<CVVTokenDetails>,
private val riskSdkUseCase: UseCase<TokenResult<String>, Unit>,
private val riskSdkUseCase: RiskSdkUseCase,
) : TokenRepository {
@VisibleForTesting
var networkCoroutineScope =
Expand Down Expand Up @@ -126,8 +128,13 @@ internal class TokenRepositoryImpl(
launch(Dispatchers.Main) {
when (tokenResult) {
is TokenResult.Success -> {
resultHandler(CVVTokenizationResultHandler.Success(tokenResult.result))
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
try {
withContext(Dispatchers.IO) {
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
}
} finally {
resultHandler(CVVTokenizationResultHandler.Success(tokenResult.result))
}
}

is TokenResult.Failure -> {
Expand Down Expand Up @@ -192,15 +199,18 @@ internal class TokenRepositoryImpl(
)
}

private fun handleResponse(
private suspend fun handleResponse(
tokenResult: TokenResult<TokenDetails>,
success: (tokenDetails: TokenDetails) -> Unit,
failure: (errorMessage: String) -> Unit,
) {
when (tokenResult) {
is TokenResult.Success -> {
is TokenResult.Success -> try {
withContext(Dispatchers.IO) {
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
}
} finally {
success(tokenResult.result)
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
}

is TokenResult.Failure -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ package com.checkout.tokenization.usecase

import android.content.Context
import com.checkout.base.model.Environment
import com.checkout.base.usecase.UseCase
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,
private val correlationId: String,
private val riskInstanceProvider: RiskInstanceProvider,
) : UseCase<TokenResult<String>, Unit> {
override fun execute(data: TokenResult<String>) {
CoroutineScope(Dispatchers.IO).launch {
val riskInstance = riskInstanceProvider.provide(context, publicKey, environment, correlationId)
when (data) {
is TokenResult.Success -> {
riskInstance?.publishData(cardToken = data.result)
}
is TokenResult.Failure -> {}
}
) {
suspend fun execute(data: TokenResult<String>) {
val riskInstance = riskInstanceProvider.provide(context, publicKey, environment, correlationId)
when (data) {
is TokenResult.Success -> riskInstance?.publishData(cardToken = data.result)
is TokenResult.Failure -> {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.checkout.tokenization.model.TokenResult
import com.checkout.tokenization.model.ValidateCVVTokenizationRequest
import com.checkout.tokenization.response.CVVTokenDetailsResponse
import com.checkout.tokenization.response.TokenDetailsResponse
import com.checkout.tokenization.usecase.RiskSdkUseCase
import com.checkout.tokenization.utils.TokenizationConstants
import com.checkout.validation.model.ValidationResult
import io.mockk.coEvery
Expand Down Expand Up @@ -57,7 +58,7 @@ internal class TokenRepositoryImplTest {
private lateinit var mockValidateTokenizationDataUseCase: UseCase<Card, ValidationResult<Unit>>

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

@RelaxedMockK
private lateinit var mockValidateCVVTokenizationDataUseCase:
Expand Down

0 comments on commit 3c95601

Please sign in to comment.