diff --git a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmFragment.kt b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmFragment.kt index 3f13a63e0..81d5af18f 100644 --- a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmFragment.kt +++ b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmFragment.kt @@ -16,7 +16,7 @@ import com.processout.example.ui.screen.nativeapm.NativeApmUiState.* import com.processout.sdk.core.onFailure import com.processout.sdk.core.onSuccess import com.processout.sdk.ui.napm.PONativeAlternativePaymentConfiguration -import com.processout.sdk.ui.napm.PONativeAlternativePaymentConfiguration.Button +import com.processout.sdk.ui.napm.PONativeAlternativePaymentConfiguration.* import com.processout.sdk.ui.napm.PONativeAlternativePaymentLauncher import com.processout.sdk.ui.napm.delegate.PONativeAlternativePaymentDelegate import com.processout.sdk.ui.nativeapm.PONativeAlternativePaymentMethodConfiguration @@ -73,18 +73,26 @@ class NativeApmFragment : BaseFragment( } private fun setOnClickListeners() { - binding.launchNativeApmButton.setOnClickListener { - onSubmitClick(launchCompose = false) + binding.buttonAuthorizeLegacy.setOnClickListener { + onSubmitClick(launchCompose = false, tokenize = false) } - binding.launchNativeApmComposeButton.setOnClickListener { - onSubmitClick(launchCompose = true) + binding.buttonAuthorizeCompose.setOnClickListener { + onSubmitClick(launchCompose = true, tokenize = false) + } + binding.buttonTokenizeCompose.setOnClickListener { + onSubmitClick(launchCompose = true, tokenize = true) } } - private fun onSubmitClick(launchCompose: Boolean) { + private fun onSubmitClick(launchCompose: Boolean, tokenize: Boolean) { val amount = binding.amountInput.text.toString() val currency = binding.currencyInput.text.toString() - viewModel.createInvoice(amount, currency, launchCompose) + viewModel.createInvoice( + amount = amount, + currency = currency, + launchCompose = launchCompose, + tokenize = tokenize + ) } private fun handle(uiState: NativeApmUiState) { @@ -98,13 +106,36 @@ class NativeApmFragment : BaseFragment( private fun launch(uiModel: NativeApmUiModel) { if (uiModel.launchCompose) { - launcherCompose.launch( - PONativeAlternativePaymentConfiguration( - invoiceId = uiModel.invoiceId, - gatewayConfigurationId = uiModel.gatewayConfigurationId, - submitButton = Button() + if (uiModel.tokenize) { + launcherCompose.launch( + PONativeAlternativePaymentConfiguration( + flow = Flow.Tokenization( + customerId = uiModel.customerId, + customerTokenId = uiModel.customerTokenId, + gatewayConfigurationId = uiModel.gatewayConfigurationId + ), + cancelButton = CancelButton(), + paymentConfirmation = PaymentConfirmationConfiguration( + confirmButton = Button(), + cancelButton = CancelButton(disabledForSeconds = 3) + ) + ) ) - ) + } else { + launcherCompose.launch( + PONativeAlternativePaymentConfiguration( + flow = Flow.Authorization( + invoiceId = uiModel.invoiceId, + gatewayConfigurationId = uiModel.gatewayConfigurationId + ), + cancelButton = CancelButton(), + paymentConfirmation = PaymentConfirmationConfiguration( + confirmButton = Button(), + cancelButton = CancelButton(disabledForSeconds = 3) + ) + ) + ) + } } else { launcher.launch( PONativeAlternativePaymentMethodConfiguration( @@ -128,8 +159,9 @@ class NativeApmFragment : BaseFragment( with(binding) { amountInput.isEnabled = isEnabled currencyInput.isEnabled = isEnabled - launchNativeApmButton.isClickable = isEnabled - launchNativeApmComposeButton.isClickable = isEnabled + buttonAuthorizeLegacy.isClickable = isEnabled + buttonAuthorizeCompose.isClickable = isEnabled + buttonTokenizeCompose.isClickable = isEnabled amountInput.clearFocus() currencyInput.clearFocus() } diff --git a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmUiState.kt b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmUiState.kt index 6b899a3cc..b7c8f04f2 100644 --- a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmUiState.kt +++ b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmUiState.kt @@ -13,5 +13,8 @@ sealed class NativeApmUiState { data class NativeApmUiModel( val invoiceId: String, val gatewayConfigurationId: String, - val launchCompose: Boolean + val customerId: String, + val customerTokenId: String, + val launchCompose: Boolean, + val tokenize: Boolean ) diff --git a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt index 585e5a3a4..2e94b71d2 100644 --- a/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt +++ b/example/src/main/kotlin/com/processout/example/ui/screen/nativeapm/NativeApmViewModel.kt @@ -6,8 +6,11 @@ import androidx.lifecycle.viewModelScope import com.processout.example.ui.screen.nativeapm.NativeApmUiState.* import com.processout.sdk.api.ProcessOut import com.processout.sdk.api.model.request.POCreateCustomerRequest +import com.processout.sdk.api.model.request.POCreateCustomerTokenRequest +import com.processout.sdk.api.model.request.POCreateCustomerTokenRequestBody import com.processout.sdk.api.model.request.POCreateInvoiceRequest import com.processout.sdk.api.model.response.POCustomer +import com.processout.sdk.api.model.response.POCustomerToken import com.processout.sdk.api.service.POCustomerTokensService import com.processout.sdk.api.service.POInvoicesService import com.processout.sdk.core.getOrNull @@ -44,15 +47,18 @@ class NativeApmViewModel( fun createInvoice( amount: String, currency: String, - launchCompose: Boolean + launchCompose: Boolean, + tokenize: Boolean ) { _uiState.value = Submitting viewModelScope.launch { + val customerId = createCustomer()?.id ?: String() + val customerTokenId = createCustomerToken(customerId)?.id ?: String() val request = POCreateInvoiceRequest( name = UUID.randomUUID().toString(), amount = amount, currency = currency, - customerId = createCustomer()?.id + customerId = customerId ) invoices.createInvoice(request) .onSuccess { invoice -> @@ -60,7 +66,10 @@ class NativeApmViewModel( NativeApmUiModel( invoiceId = invoice.id, gatewayConfigurationId = gatewayConfigurationId, - launchCompose = launchCompose + customerId = customerId, + customerTokenId = customerTokenId, + launchCompose = launchCompose, + tokenize = tokenize ) ) }.onFailure { _uiState.value = Failure(it) } @@ -76,6 +85,14 @@ class NativeApmViewModel( ) ).getOrNull() + private suspend fun createCustomerToken(customerId: String): POCustomerToken? = + customerTokens.createCustomerToken( + POCreateCustomerTokenRequest( + customerId = customerId, + body = POCreateCustomerTokenRequestBody() + ) + ).getOrNull() + fun onLaunched() { _uiState.value = Launched } diff --git a/example/src/main/res/layout/fragment_native_apm.xml b/example/src/main/res/layout/fragment_native_apm.xml index 4c6caa92c..1674702fa 100644 --- a/example/src/main/res/layout/fragment_native_apm.xml +++ b/example/src/main/res/layout/fragment_native_apm.xml @@ -14,7 +14,7 @@ android:defaultFocusHighlightEnabled="false" android:fillViewport="true" android:isScrollContainer="true" - app:layout_constraintBottom_toTopOf="@id/footer_divider" + app:layout_constraintBottom_toTopOf="@id/buttons" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" @@ -76,38 +76,48 @@ - - -