Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MainActivity : ComponentActivity() {

val advancedSettingsState by viewModel.advancedSettings.collectAsStateWithLifecycle()
val rememberMeSettings by viewModel.rememberMeSettings.collectAsStateWithLifecycle()
val storedCardSettings by viewModel.storedCardSettings.collectAsStateWithLifecycle()

val context = LocalContext.current

Expand All @@ -48,12 +49,14 @@ class MainActivity : ComponentActivity() {
settingState = settingState,
advancedSettingsState = advancedSettingsState,
rememberMeSettings = rememberMeSettings,
storedCardSettings = storedCardSettings,

showSettings = viewModel::showSettings,
showFlowComponent = { viewModel.showFlowComponent(context) },
updateSettings = viewModel::updateSettings,
updateAdvancedSettings = viewModel::updateAdvancedSettings,
updateRememberMeSettings = viewModel::updateRememberMeSettings,
updateStoredCardSettings = viewModel::updateStoredCardSettings,
onSubmitClicked = viewModel::onSubmit,
onAmountChanged = viewModel::onAmountChanged,
onCheckTermsAndConditions = viewModel::onCheckTermsAndConditions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.checkout.android.components.sample.ui.model.PaymentResultState
import com.checkout.android.components.sample.ui.model.RememberMeSettings
import com.checkout.android.components.sample.ui.model.SettingScreenState
import com.checkout.android.components.sample.ui.model.Settings
import com.checkout.android.components.sample.ui.model.StoredCardSettings
import com.checkout.android.components.sample.ui.model.SubmitPaymentHandler
import com.checkout.components.interfaces.api.PaymentMethodComponent
import com.checkout.components.interfaces.component.CardConfiguration
Expand All @@ -21,6 +22,7 @@ import com.checkout.components.interfaces.component.ComponentOption
import com.checkout.components.interfaces.component.GooglePayConfiguration
import com.checkout.components.interfaces.component.PaymentButtonAction
import com.checkout.components.interfaces.component.RememberMeConfiguration
import com.checkout.components.interfaces.component.StoredCardConfiguration
import com.checkout.components.interfaces.model.ApiCallResult
import com.checkout.components.interfaces.model.CallbackResult
import com.checkout.components.interfaces.model.CardMetadata
Expand All @@ -46,6 +48,7 @@ class MainViewModel @Inject constructor(

private val _advancedSettings = MutableStateFlow(AdvancedSettings())
private val _rememberMeSettings = MutableStateFlow(RememberMeSettings())
private val _storedCardSettings = MutableStateFlow(StoredCardSettings())

val screenState = _screenState.asStateFlow()

Expand All @@ -55,12 +58,16 @@ class MainViewModel @Inject constructor(

val rememberMeSettings = _rememberMeSettings.asStateFlow()

val storedCardSettings = _storedCardSettings.asStateFlow()

fun showFlowComponent(context: Context) {
viewModelScope.launch {
val advancedSettings = advancedSettings.value
val storedCardSettings = _storedCardSettings.value
val config = flowComponent.createConfigurationFromSettings(
context = context,
settings = settingState.value,
storedCardSettings = storedCardSettings,
callbacks = buildComponentCallbacks(),
)

Expand Down Expand Up @@ -97,6 +104,17 @@ class MainViewModel @Inject constructor(
null
}

val storedCardConfiguration = if (storedCardSettings.enabled) {
StoredCardConfiguration(
displayMode = storedCardSettings.displayMode,
captureCardCvv = storedCardSettings.captureCardCvv,
acceptedCardSchemes = storedCardSettings.acceptedCardSchemes,
acceptedCardTypes = storedCardSettings.acceptedCardTypes,
)
} else {
null
}

val specificOptions = ComponentOption(
showPayButton = advancedSettings.showCardPayButton,
googlePayConfiguration = GooglePayConfiguration(
Expand All @@ -110,6 +128,7 @@ class MainViewModel @Inject constructor(
),
addressConfiguration = advancedSettings.addressConfiguration.configuration(),
rememberMeConfiguration = rememberMeConfiguration,
storedCardConfiguration = storedCardConfiguration,
)

val paymentMethodComponent = when (settingState.value.component) {
Expand Down Expand Up @@ -168,6 +187,12 @@ class MainViewModel @Inject constructor(
}
}

fun updateStoredCardSettings(storedCardSettings: StoredCardSettings) {
_storedCardSettings.update {
storedCardSettings
}
}

fun onSubmit() {
viewModelScope.launch {
(screenState.value as? PaymentComponentScreenState)?.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.checkout.components.interfaces.localisation.Locale.ZhHk
import com.checkout.components.interfaces.localisation.Locale.ZhTw
import com.checkout.components.interfaces.model.CardSchemeName
import com.checkout.components.interfaces.model.CardTypeName
import com.checkout.components.interfaces.model.StoredCardDisplayMode
import com.checkout.components.interfaces.uicustomisation.designtoken.ColorTokens
import com.checkout.components.interfaces.uicustomisation.designtoken.DefaultBorderRadius
import com.checkout.components.interfaces.uicustomisation.designtoken.DefaultFonts
Expand Down Expand Up @@ -132,3 +133,13 @@ fun CardTypeName.displayName(): String = this::class.java.simpleName
* @return The simple name of the underlying Java class.
*/
fun CardSchemeName.displayName(): String = this::class.java.simpleName

/**
* Retrieves a display-friendly, title-cased name for the [StoredCardDisplayMode].
*
* @return "Default only" for [StoredCardDisplayMode.DEFAULT_ONLY] and "All" for [StoredCardDisplayMode.ALL].
*/
fun StoredCardDisplayMode.displayName(): String = when (this) {
StoredCardDisplayMode.DEFAULT_ONLY -> "Default only"
StoredCardDisplayMode.ALL -> "All"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import com.checkout.android.components.sample.core.model.Address
import com.checkout.android.components.sample.core.model.AddressAndPhoneNumber
import com.checkout.android.components.sample.core.model.Customer
import com.checkout.android.components.sample.core.model.Phone
import com.checkout.android.components.sample.core.network.model.session.PaymentMethodConfiguration
import com.checkout.android.components.sample.core.network.model.session.PaymentSessions
import com.checkout.android.components.sample.core.network.model.session.StoredCardConfiguration
import com.checkout.android.components.sample.core.network.model.session.SubmitPaymentSession
import com.checkout.android.components.sample.core.network.repository.PaymentSessionRepository
import com.checkout.android.components.sample.extension.toDesignTokens
import com.checkout.android.components.sample.extension.toPaymentSessionLocale
import com.checkout.android.components.sample.ui.model.Components
import com.checkout.android.components.sample.ui.model.PaymentMethods
import com.checkout.android.components.sample.ui.model.Settings
import com.checkout.android.components.sample.ui.model.StoredCardSettings
import com.checkout.components.core.CheckoutComponentsFactory
import com.checkout.components.interfaces.Environment
import com.checkout.components.interfaces.api.CheckoutComponents
Expand Down Expand Up @@ -56,13 +59,16 @@ class FlowComponent @Inject constructor(
*
* @param context The application context.
* @param settings The user-defined settings for the checkout components.
* @param storedCardSettings The stored card settings that drive both the session's `stored_card`
* payment method configuration and the component's stored card configuration.
* @param callbacks The component callbacks to be used for handling component events.
*
* @return A [CheckoutComponentConfiguration] initialized with the provided settings and session data.
*/
suspend fun createConfigurationFromSettings(
context: Context,
settings: Settings = Settings(),
storedCardSettings: StoredCardSettings = StoredCardSettings(),
Comment thread
chintan-soni-cko marked this conversation as resolved.
callbacks: ComponentCallback,
): CheckoutComponentConfiguration {
val publicKey = when (settings.environment) {
Expand All @@ -76,13 +82,14 @@ class FlowComponent @Inject constructor(
}

val paymentSession = PaymentSessions(
enabledPaymentMethods = enablePaymentMethod(settings),
enabledPaymentMethods = enablePaymentMethod(settings, storedCardSettings),
processingChannelId = processingChannelId,
locale = settings.psLocale.toPaymentSessionLocale(),
currency = settings.psCurrency,
billing = buildAddressAndPhone(settings.psCountry),
customer = buildCustomer(settings.psCountry, settings.psEmail),
customer = buildCustomer(settings.psCountry, settings.psEmail, settings.psPhoneNumber),
shipping = buildAddressAndPhone(settings.psCountry),
paymentMethodConfiguration = buildPaymentMethodConfiguration(storedCardSettings),
)

val response = repository.createPaymentSession(
Expand Down Expand Up @@ -289,15 +296,30 @@ class FlowComponent @Inject constructor(
}
}

private fun enablePaymentMethod(settings: Settings): List<String> = if (settings.component == Components.Flow) {
settings.paymentMethods.map { it.name.lowercase() }
} else {
listOf(settings.component.name.lowercase())
private fun enablePaymentMethod(
settings: Settings,
storedCardSettings: StoredCardSettings,
): List<String> {
val methods = if (settings.component == Components.Flow) {
settings.paymentMethods.map { it.name.lowercase() }.toMutableList()
} else {
mutableListOf(settings.component.name.lowercase())
}
if (storedCardSettings.enabled && STORED_CARD_METHOD !in methods) {
methods += STORED_CARD_METHOD
}
return methods
}

private fun buildPhone(country: String): Phone {
val dialingCode = Country.fromIso3166Alpha2(country)?.dialingCode.orEmpty()
return Phone(number = "1234567890", countryCode = "+$dialingCode")
private fun buildPaymentMethodConfiguration(
storedCardSettings: StoredCardSettings,
): PaymentMethodConfiguration? {
if (!storedCardSettings.enabled) {
return null
}
return PaymentMethodConfiguration(
storedCard = StoredCardConfiguration(customerId = storedCardSettings.customerId),
)
}

private fun buildAddress(country: String) = Address(
Expand All @@ -311,16 +333,24 @@ class FlowComponent @Inject constructor(

private fun buildAddressAndPhone(country: String) = AddressAndPhoneNumber(
address = buildAddress(country),
phone = buildPhone(country),
)

private fun buildCustomer(country: String, email: String) = Customer(
email = email,
name = "customerName",
phone = buildPhone(country),
)
private fun buildCustomer(country: String, email: String, phoneNumber: String): Customer {
val dialingCode = Country.fromIso3166Alpha2(country)?.dialingCode.orEmpty()
return Customer(
email = email.takeIf { it.isNotBlank() },
name = "customerName",
phone = phoneNumber
.takeIf { it.isNotBlank() }
?.let { Phone(number = it, countryCode = "+$dialingCode") },
)
}

private fun handleActivityResult(resultCode: Int, data: String) {
checkoutComponent?.handleActivityResult(resultCode, data)
}

private companion object {
const val STORED_CARD_METHOD = "stored_card"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.checkout.android.components.sample.ui.model.PaymentComponentScreenSta
import com.checkout.android.components.sample.ui.model.RememberMeSettings
import com.checkout.android.components.sample.ui.model.SettingScreenState
import com.checkout.android.components.sample.ui.model.Settings
import com.checkout.android.components.sample.ui.model.StoredCardSettings
import com.checkout.android.components.sample.ui.theme.CheckoutComponentSampleTheme
import com.checkout.components.interfaces.api.PaymentMethodComponent

Expand All @@ -53,12 +54,14 @@ fun DemoScreen(
settingState: Settings,
advancedSettingsState: AdvancedSettings,
rememberMeSettings: RememberMeSettings,
storedCardSettings: StoredCardSettings,
modifier: Modifier = Modifier,
showSettings: () -> Unit = {},
showFlowComponent: () -> Unit,
updateSettings: (Settings) -> Unit,
updateAdvancedSettings: (AdvancedSettings) -> Unit,
updateRememberMeSettings: (RememberMeSettings) -> Unit,
updateStoredCardSettings: (StoredCardSettings) -> Unit,
onSubmitClicked: () -> Unit = {},
onAmountChanged: (Int) -> Unit = {},
onCheckTermsAndConditions: (Boolean) -> Unit = {},
Expand Down Expand Up @@ -94,6 +97,8 @@ fun DemoScreen(
onUpdateAdvancedSettings = updateAdvancedSettings,
rememberMeSettings = rememberMeSettings,
onUpdateRememberMeSettings = updateRememberMeSettings,
storedCardSettings = storedCardSettings,
onUpdateStoredCardSettings = updateStoredCardSettings,
)
}

Expand Down Expand Up @@ -199,11 +204,13 @@ private fun DemoScreenPreview() {
settingState = Settings(),
advancedSettingsState = AdvancedSettings(),
rememberMeSettings = RememberMeSettings(),
storedCardSettings = StoredCardSettings(),
showSettings = {},
showFlowComponent = {},
updateSettings = {},
updateAdvancedSettings = {},
updateRememberMeSettings = {},
updateStoredCardSettings = {},
)
}
}
Loading
Loading