diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationBottomSheet.kt b/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationBottomSheet.kt index 5b13ed741..7859fc60b 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationBottomSheet.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationBottomSheet.kt @@ -51,7 +51,7 @@ internal class CardTokenizationBottomSheet : BaseBottomSheetDialogFragment + legacyEventDispatcher?.preferredSchemeResponse?.collect { response -> handlePreferredScheme(response) } } @@ -696,9 +696,9 @@ internal class CardTokenizationInteractor( card = card, saveCard = _state.value.saveCardField.value.text.toBooleanStrictOrNull() ?: false ) - if (legacyEventDispatcher.subscribedForProcessTokenizedCard()) { + if (legacyEventDispatcher?.subscribedForProcessTokenizedCard() == true) { legacyEventDispatcher.processTokenizedCard(card) - } else if (legacyEventDispatcher.subscribedForProcessTokenizedCardRequest()) { + } else if (legacyEventDispatcher?.subscribedForProcessTokenizedCardRequest() == true) { legacyEventDispatcher.processTokenizedCardRequest(request) } else { eventDispatcher.send(request) @@ -711,7 +711,7 @@ internal class CardTokenizationInteractor( private fun handleCompletion() { interactorScope.launch { - legacyEventDispatcher.completion.collect { result -> + legacyEventDispatcher?.completion?.collect { result -> result.onSuccess { _state.value.tokenizedCard?.let { card -> complete(Success(card)) @@ -752,7 +752,7 @@ internal class CardTokenizationInteractor( interactorScope.launch { val request = POCardTokenizationShouldContinueRequest(failure) latestShouldContinueRequest = request - if (legacyEventDispatcher.subscribedForShouldContinueRequest()) { + if (legacyEventDispatcher?.subscribedForShouldContinueRequest() == true) { legacyEventDispatcher.send(request) } else { eventDispatcher.send(request) @@ -763,7 +763,7 @@ internal class CardTokenizationInteractor( private fun shouldContinueOnFailure() { interactorScope.launch { - legacyEventDispatcher.shouldContinueResponse.collect { response -> + legacyEventDispatcher?.shouldContinueResponse?.collect { response -> handleShouldContinue(response) } } @@ -898,7 +898,7 @@ internal class CardTokenizationInteractor( private fun dispatch(event: POCardTokenizationEvent) { interactorScope.launch { - legacyEventDispatcher.send(event) + legacyEventDispatcher?.send(event) eventDispatcher.send(event) } } diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationViewModel.kt b/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationViewModel.kt index 14fc2f79c..c3192964d 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationViewModel.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/card/tokenization/CardTokenizationViewModel.kt @@ -42,7 +42,7 @@ internal class CardTokenizationViewModel private constructor( class Factory( private val app: Application, private val configuration: POCardTokenizationConfiguration, - private val eventDispatcher: PODefaultCardTokenizationEventDispatcher + private val legacyEventDispatcher: PODefaultCardTokenizationEventDispatcher? ) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T = @@ -55,7 +55,7 @@ internal class CardTokenizationViewModel private constructor( cardsRepository = ProcessOut.instance.cards, cardSchemeProvider = CardSchemeProvider(), addressSpecificationProvider = AddressSpecificationProvider(app), - legacyEventDispatcher = eventDispatcher + legacyEventDispatcher = legacyEventDispatcher ) ) as T } diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutActivity.kt b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutActivity.kt index 0d9dddf81..f828a7a4b 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutActivity.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutActivity.kt @@ -23,7 +23,6 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.google.android.gms.wallet.Wallet.WalletOptions import com.processout.sdk.R import com.processout.sdk.api.ProcessOut -import com.processout.sdk.api.dispatcher.card.tokenization.PODefaultCardTokenizationEventDispatcher import com.processout.sdk.api.dispatcher.napm.PODefaultNativeAlternativePaymentMethodEventDispatcher import com.processout.sdk.api.model.event.POSavedPaymentMethodsEvent import com.processout.sdk.api.model.event.POSavedPaymentMethodsEvent.DidDeleteCustomerToken @@ -67,12 +66,11 @@ internal class DynamicCheckoutActivity : BaseTransparentPortraitActivity() { private lateinit var configuration: PODynamicCheckoutConfiguration private val viewModel: DynamicCheckoutViewModel by viewModels { - val cardTokenizationEventDispatcher = PODefaultCardTokenizationEventDispatcher() val cardTokenization: CardTokenizationViewModel by viewModels { CardTokenizationViewModel.Factory( app = application, configuration = cardTokenizationConfiguration(), - eventDispatcher = cardTokenizationEventDispatcher + legacyEventDispatcher = null ) } val nativeAlternativePaymentEventDispatcher = PODefaultNativeAlternativePaymentMethodEventDispatcher() @@ -87,7 +85,6 @@ internal class DynamicCheckoutActivity : BaseTransparentPortraitActivity() { app = application, configuration = configuration, cardTokenization = cardTokenization, - cardTokenizationEventDispatcher = cardTokenizationEventDispatcher, nativeAlternativePayment = nativeAlternativePayment, nativeAlternativePaymentEventDispatcher = nativeAlternativePaymentEventDispatcher ) diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutInteractor.kt b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutInteractor.kt index 158bd42c2..e8440fb7e 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutInteractor.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutInteractor.kt @@ -11,7 +11,6 @@ import coil.request.ImageRequest import coil.request.ImageResult import com.processout.sdk.R import com.processout.sdk.api.dispatcher.POEventDispatcher -import com.processout.sdk.api.dispatcher.card.tokenization.PODefaultCardTokenizationEventDispatcher import com.processout.sdk.api.dispatcher.napm.PODefaultNativeAlternativePaymentMethodEventDispatcher import com.processout.sdk.api.model.event.PODynamicCheckoutEvent import com.processout.sdk.api.model.event.PODynamicCheckoutEvent.* @@ -75,7 +74,6 @@ internal class DynamicCheckoutInteractor( private val invoicesService: POInvoicesService, private val googlePayService: POGooglePayService, private val cardTokenization: CardTokenizationViewModel, - private val cardTokenizationEventDispatcher: PODefaultCardTokenizationEventDispatcher, private val nativeAlternativePayment: NativeAlternativePaymentViewModel, private val nativeAlternativePaymentEventDispatcher: PODefaultNativeAlternativePaymentMethodEventDispatcher, private val eventDispatcher: POEventDispatcher = POEventDispatcher, @@ -105,6 +103,7 @@ internal class DynamicCheckoutInteractor( private var authorizeInvoiceJob: AuthorizeInvoiceJob? = null private var latestInvoiceRequest: PODynamicCheckoutInvoiceRequest? = null + private var latestCardProcessingRequest: POCardTokenizationProcessingRequest? = null init { interactorScope.launch { @@ -129,7 +128,6 @@ internal class DynamicCheckoutInteractor( collectInvoice() collectInvoiceAuthorizationRequest() collectTokenizedCard() - collectPreferredScheme() collectDefaultValues() collectSavedPaymentMethodsConfiguration() fetchConfiguration() @@ -860,17 +858,18 @@ internal class DynamicCheckoutInteractor( } private fun collectTokenizedCard() { - interactorScope.launch { - cardTokenizationEventDispatcher.processTokenizedCardRequest.collect { request -> - _state.value.selectedPaymentMethod?.let { paymentMethod -> - _state.update { it.copy(processingPaymentMethod = paymentMethod) } - authorizeInvoice( - paymentMethod = paymentMethod, - source = request.card.id, - saveSource = request.saveCard, - clientSecret = configuration.invoiceRequest.clientSecret - ) - } + eventDispatcher.subscribeForRequest( + coroutineScope = interactorScope + ) { request -> + _state.value.selectedPaymentMethod?.let { paymentMethod -> + _state.update { it.copy(processingPaymentMethod = paymentMethod) } + latestCardProcessingRequest = request + authorizeInvoice( + paymentMethod = paymentMethod, + source = request.card.id, + saveSource = request.saveCard, + clientSecret = configuration.invoiceRequest.clientSecret + ) } } } @@ -930,8 +929,11 @@ internal class DynamicCheckoutInteractor( return } when (state.processingPaymentMethod) { - is Card -> interactorScope.launch { - cardTokenizationEventDispatcher.complete(result) + is Card -> latestCardProcessingRequest?.let { request -> + interactorScope.launch { + latestCardProcessingRequest = null + eventDispatcher.send(request.toResponse(result)) + } } is GooglePay, is AlternativePayment, @@ -1001,12 +1003,11 @@ internal class DynamicCheckoutInteractor( } private fun dispatchEvents() { - interactorScope.launch { - cardTokenizationEventDispatcher.events.collect { eventDispatcher.send(it) } - } - interactorScope.launch { - cardTokenizationEventDispatcher.preferredSchemeRequest.collect { request -> - eventDispatcher.send(request) + eventDispatcher.subscribeForRequest( + coroutineScope = interactorScope + ) { request -> + interactorScope.launch { + eventDispatcher.send(request.toResponse(shouldContinue = false)) } } interactorScope.launch { @@ -1080,16 +1081,6 @@ internal class DynamicCheckoutInteractor( POLogger.debug("Deleted local customer token: %s", tokenId) } - private fun collectPreferredScheme() { - eventDispatcher.subscribeForResponse( - coroutineScope = interactorScope - ) { response -> - interactorScope.launch { - cardTokenizationEventDispatcher.preferredScheme(response) - } - } - } - private fun collectDefaultValues() { eventDispatcher.subscribeForResponse( coroutineScope = interactorScope diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutViewModel.kt b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutViewModel.kt index d43376863..d3044b64d 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutViewModel.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/DynamicCheckoutViewModel.kt @@ -7,7 +7,6 @@ import androidx.lifecycle.viewModelScope import com.google.android.gms.wallet.Wallet.WalletOptions import com.processout.sdk.R import com.processout.sdk.api.ProcessOut -import com.processout.sdk.api.dispatcher.card.tokenization.PODefaultCardTokenizationEventDispatcher import com.processout.sdk.api.dispatcher.napm.PODefaultNativeAlternativePaymentMethodEventDispatcher import com.processout.sdk.api.model.response.PODynamicCheckoutPaymentMethod.Display import com.processout.sdk.api.service.googlepay.PODefaultGooglePayService @@ -45,7 +44,6 @@ internal class DynamicCheckoutViewModel private constructor( private val app: Application, private val configuration: PODynamicCheckoutConfiguration, private val cardTokenization: CardTokenizationViewModel, - private val cardTokenizationEventDispatcher: PODefaultCardTokenizationEventDispatcher, private val nativeAlternativePayment: NativeAlternativePaymentViewModel, private val nativeAlternativePaymentEventDispatcher: PODefaultNativeAlternativePaymentMethodEventDispatcher ) : ViewModelProvider.Factory { @@ -65,7 +63,6 @@ internal class DynamicCheckoutViewModel private constructor( .build() ), cardTokenization = cardTokenization, - cardTokenizationEventDispatcher = cardTokenizationEventDispatcher, nativeAlternativePayment = nativeAlternativePayment, nativeAlternativePaymentEventDispatcher = nativeAlternativePaymentEventDispatcher )