From 9d11cc81a78b095337eed169ab812c5e66e24305 Mon Sep 17 00:00:00 2001 From: Vitalii Vanziak <114916876+vitalii-vanziak-cko@users.noreply.github.com> Date: Tue, 10 Dec 2024 19:53:15 +0200 Subject: [PATCH] feat(POM-440): (DC) Optionally pre-select single regular payment method (#248) --- .../sdk/ui/checkout/DynamicCheckoutInteractor.kt | 15 +++++++++++++-- .../ui/checkout/PODynamicCheckoutConfiguration.kt | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) 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 34131d59..ebc36e02 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 @@ -201,11 +201,22 @@ internal class DynamicCheckoutInteractor( paymentMethods = mappedPaymentMethods ) } - restoreSelectedPaymentMethod() + handleSelectedPaymentMethod() handlePendingSubmit() } - private fun restoreSelectedPaymentMethod() { + private fun handleSelectedPaymentMethod() { + if (configuration.preselectSinglePaymentMethod) { + _state.value.paymentMethods + .partition { it.isExpress() } + .let { pair -> + val expressPaymentMethods = pair.first + val regularPaymentMethods = pair.second + if (expressPaymentMethods.isEmpty() && regularPaymentMethods.size == 1) { + _state.update { it.copy(selectedPaymentMethod = regularPaymentMethods.firstOrNull()) } + } + } + } _state.value.selectedPaymentMethod?.id?.let { id -> paymentMethod(id)?.let { start(it) } .orElse { diff --git a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/PODynamicCheckoutConfiguration.kt b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/PODynamicCheckoutConfiguration.kt index c2f9810d..13560185 100644 --- a/ui/src/main/kotlin/com/processout/sdk/ui/checkout/PODynamicCheckoutConfiguration.kt +++ b/ui/src/main/kotlin/com/processout/sdk/ui/checkout/PODynamicCheckoutConfiguration.kt @@ -20,6 +20,7 @@ data class PODynamicCheckoutConfiguration( val card: CardConfiguration = CardConfiguration(), val googlePay: GooglePayConfiguration = GooglePayConfiguration(), val alternativePayment: AlternativePaymentConfiguration = AlternativePaymentConfiguration(), + val preselectSinglePaymentMethod: Boolean = true, val submitButtonText: String? = null, val cancelButton: CancelButton? = CancelButton(), val paymentSuccess: PaymentSuccess? = PaymentSuccess(),