diff --git a/checkout/src/main/java/com/checkout/tokenization/repository/TokenRepositoryImpl.kt b/checkout/src/main/java/com/checkout/tokenization/repository/TokenRepositoryImpl.kt index 3787d18d..2b50646d 100644 --- a/checkout/src/main/java/com/checkout/tokenization/repository/TokenRepositoryImpl.kt +++ b/checkout/src/main/java/com/checkout/tokenization/repository/TokenRepositoryImpl.kt @@ -24,6 +24,7 @@ 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 @@ -31,6 +32,7 @@ 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 @@ -45,7 +47,7 @@ internal class TokenRepositoryImpl( private val logger: TokenizationLogger, private val publicKey: String, private val cvvTokenizationNetworkDataMapper: TokenizationNetworkDataMapper, - private val riskSdkUseCase: UseCase, Unit>, + private val riskSdkUseCase: RiskSdkUseCase, ) : TokenRepository { @VisibleForTesting var networkCoroutineScope = @@ -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 -> { @@ -192,15 +199,18 @@ internal class TokenRepositoryImpl( ) } - private fun handleResponse( + private suspend fun handleResponse( tokenResult: TokenResult, 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 -> { diff --git a/checkout/src/main/java/com/checkout/tokenization/usecase/RiskSdkUseCase.kt b/checkout/src/main/java/com/checkout/tokenization/usecase/RiskSdkUseCase.kt index d8f38d5c..39cefac2 100644 --- a/checkout/src/main/java/com/checkout/tokenization/usecase/RiskSdkUseCase.kt +++ b/checkout/src/main/java/com/checkout/tokenization/usecase/RiskSdkUseCase.kt @@ -2,11 +2,7 @@ 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, @@ -14,16 +10,12 @@ internal class RiskSdkUseCase( private val publicKey: String, private val correlationId: String, private val riskInstanceProvider: RiskInstanceProvider, -) : UseCase, Unit> { - override fun execute(data: TokenResult) { - 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) { + val riskInstance = riskInstanceProvider.provide(context, publicKey, environment, correlationId) + when (data) { + is TokenResult.Success -> riskInstance?.publishData(cardToken = data.result) + is TokenResult.Failure -> {} } } } diff --git a/checkout/src/test/java/com/checkout/tokenization/repository/TokenRepositoryImplTest.kt b/checkout/src/test/java/com/checkout/tokenization/repository/TokenRepositoryImplTest.kt index f8a33670..58a6afe8 100644 --- a/checkout/src/test/java/com/checkout/tokenization/repository/TokenRepositoryImplTest.kt +++ b/checkout/src/test/java/com/checkout/tokenization/repository/TokenRepositoryImplTest.kt @@ -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 @@ -57,7 +58,7 @@ internal class TokenRepositoryImplTest { private lateinit var mockValidateTokenizationDataUseCase: UseCase> @RelaxedMockK - private lateinit var mockRiskSdkUseCase: UseCase, Unit> + private lateinit var mockRiskSdkUseCase: RiskSdkUseCase @RelaxedMockK private lateinit var mockValidateCVVTokenizationDataUseCase: