Skip to content

Commit

Permalink
Implement embeddedViewDisplaysMandateText. (#9906)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Jan 14, 2025
1 parent a8b5ca7 commit 83ebb13
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
Expand All @@ -27,6 +29,7 @@ import com.stripe.android.paymentsheet.CreateIntentResult
import com.stripe.android.paymentsheet.ExternalPaymentMethodConfirmHandler
import com.stripe.android.paymentsheet.example.playground.PlaygroundState
import com.stripe.android.paymentsheet.example.playground.activity.FawryActivity
import com.stripe.android.paymentsheet.example.playground.settings.EmbeddedViewDisplaysMandateSettingDefinition
import com.stripe.android.paymentsheet.example.playground.settings.PlaygroundConfigurationData
import com.stripe.android.paymentsheet.example.samples.ui.shared.BuyButton
import com.stripe.android.paymentsheet.example.samples.ui.shared.PaymentMethodSelector
Expand Down Expand Up @@ -60,6 +63,7 @@ internal class EmbeddedPlaygroundActivity : AppCompatActivity(), ExternalPayment
},
resultCallback = ::handleEmbeddedResult,
).externalPaymentMethodConfirmHandler(this)
val embeddedViewDisplaysMandateText = playgroundState.snapshot[EmbeddedViewDisplaysMandateSettingDefinition]
setContent {
val embeddedPaymentElement = rememberEmbeddedPaymentElement(embeddedBuilder)

Expand All @@ -82,29 +86,48 @@ internal class EmbeddedPlaygroundActivity : AppCompatActivity(), ExternalPayment
val selectedPaymentOption by embeddedPaymentElement.paymentOption.collectAsState()

selectedPaymentOption?.let { selectedPaymentOption ->
PaymentMethodSelector(
isEnabled = true,
paymentMethodLabel = selectedPaymentOption.label,
paymentMethodPainter = selectedPaymentOption.iconPainter,
clickable = false,
onClick = { },
EmbeddedContentWithSelectedPaymentOption(
embeddedPaymentElement = embeddedPaymentElement,
selectedPaymentOption = selectedPaymentOption,
embeddedViewDisplaysMandateText = embeddedViewDisplaysMandateText,
)
}
}
}
}

BuyButton(buyButtonEnabled = true) {
embeddedPaymentElement.confirm()
}
@Composable
private fun ColumnScope.EmbeddedContentWithSelectedPaymentOption(
embeddedPaymentElement: EmbeddedPaymentElement,
selectedPaymentOption: EmbeddedPaymentElement.PaymentOptionDisplayData,
embeddedViewDisplaysMandateText: Boolean,
) {
PaymentMethodSelector(
isEnabled = true,
paymentMethodLabel = selectedPaymentOption.label,
paymentMethodPainter = selectedPaymentOption.iconPainter,
clickable = false,
onClick = { },
)

Button(
onClick = {
embeddedPaymentElement.clearPaymentOption()
},
Modifier.fillMaxWidth(),
) {
Text("Clear selected")
}
}
if (!embeddedViewDisplaysMandateText) {
selectedPaymentOption.mandateText?.let { mandateText ->
Text(mandateText)
}
}

BuyButton(buyButtonEnabled = true) {
embeddedPaymentElement.confirm()
}

Button(
onClick = {
embeddedPaymentElement.clearPaymentOption()
},
Modifier.fillMaxWidth(),
) {
Text("Clear selected")
}
}

override fun confirmExternalPaymentMethod(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.stripe.android.paymentsheet.example.playground.settings

import com.stripe.android.paymentelement.EmbeddedPaymentElement
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi
import com.stripe.android.paymentsheet.example.playground.PlaygroundState

@OptIn(ExperimentalEmbeddedPaymentElementApi::class)
internal object EmbeddedViewDisplaysMandateSettingDefinition : BooleanSettingsDefinition(
key = "embeddedViewDisplaysMandate",
displayName = "Embedded View Displays Mandate",
defaultValue = true,
) {
override fun applicable(configurationData: PlaygroundConfigurationData): Boolean {
return configurationData.integrationType == PlaygroundConfigurationData.IntegrationType.Embedded
}

override fun configure(
value: Boolean,
configurationBuilder: EmbeddedPaymentElement.Configuration.Builder,
playgroundState: PlaygroundState.Payment,
configurationData: PlaygroundSettingDefinition.EmbeddedConfigurationData
) {
configurationBuilder.embeddedViewDisplaysMandateText(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ internal class PlaygroundSettings private constructor(
CardBrandAcceptanceSettingsDefinition,
FeatureFlagSettingsDefinition(FeatureFlags.instantDebitsIncentives),
FeatureFlagSettingsDefinition(FeatureFlags.enableDefaultPaymentMethods),
EmbeddedViewDisplaysMandateSettingDefinition,
)

private val nonUiSettingDefinitions: List<PlaygroundSettingDefinition<*>> = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ import kotlin.coroutines.CoroutineContext
internal interface EmbeddedContentHelper {
val embeddedContent: StateFlow<EmbeddedContent?>

fun dataLoaded(paymentMethodMetadata: PaymentMethodMetadata, rowStyle: Embedded.RowStyle)
fun dataLoaded(
paymentMethodMetadata: PaymentMethodMetadata,
rowStyle: Embedded.RowStyle,
embeddedViewDisplaysMandateText: Boolean,
)
}

internal fun interface EmbeddedContentHelperFactory {
Expand Down Expand Up @@ -94,17 +98,24 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
}
coroutineScope.launch {
mandate.collect { mandate ->
_embeddedContent.update { originalEmbeddedContent ->
originalEmbeddedContent?.copy(mandate = mandate)
if (state.value?.embeddedViewDisplaysMandateText == true) {
_embeddedContent.update { originalEmbeddedContent ->
originalEmbeddedContent?.copy(mandate = mandate)
}
}
}
}
}

override fun dataLoaded(paymentMethodMetadata: PaymentMethodMetadata, rowStyle: Embedded.RowStyle) {
override fun dataLoaded(
paymentMethodMetadata: PaymentMethodMetadata,
rowStyle: Embedded.RowStyle,
embeddedViewDisplaysMandateText: Boolean,
) {
savedStateHandle[STATE_KEY_EMBEDDED_CONTENT] = State(
paymentMethodMetadata = paymentMethodMetadata,
rowStyle = rowStyle,
embeddedViewDisplaysMandateText = embeddedViewDisplaysMandateText,
)
}

Expand Down Expand Up @@ -262,6 +273,7 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
class State(
val paymentMethodMetadata: PaymentMethodMetadata,
val rowStyle: Embedded.RowStyle,
val embeddedViewDisplaysMandateText: Boolean,
) : Parcelable

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ internal class SharedPaymentElementViewModel @Inject constructor(
selectionHolder.set(state.paymentSelection)
embeddedContentHelper.dataLoaded(
paymentMethodMetadata = state.paymentMethodMetadata,
rowStyle = configuration.appearance.embeddedAppearance.style
rowStyle = configuration.appearance.embeddedAppearance.style,
embeddedViewDisplaysMandateText = configuration.embeddedViewDisplaysMandateText,
)
ConfigureResult.Succeeded()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class DefaultEmbeddedContentHelperTest {
.isNull()
val paymentMethodMetadata = PaymentMethodMetadataFactory.create()
val rowStyle = Embedded.RowStyle.FlatWithRadio.defaultLight
embeddedContentHelper.dataLoaded(paymentMethodMetadata, rowStyle)
embeddedContentHelper.dataLoaded(paymentMethodMetadata, rowStyle, embeddedViewDisplaysMandateText = true)
val state = savedStateHandle.get<DefaultEmbeddedContentHelper.State?>(STATE_KEY_EMBEDDED_CONTENT)
assertThat(state?.paymentMethodMetadata).isEqualTo(paymentMethodMetadata)
assertThat(state?.rowStyle).isEqualTo(rowStyle)
Expand All @@ -39,7 +39,8 @@ internal class DefaultEmbeddedContentHelperTest {
assertThat(awaitItem()).isNull()
embeddedContentHelper.dataLoaded(
PaymentMethodMetadataFactory.create(),
Embedded.RowStyle.FlatWithRadio.defaultLight
Embedded.RowStyle.FlatWithRadio.defaultLight,
embeddedViewDisplaysMandateText = true,
)
assertThat(awaitItem()).isNotNull()
}
Expand All @@ -51,7 +52,8 @@ internal class DefaultEmbeddedContentHelperTest {
assertThat(awaitItem()).isNull()
embeddedContentHelper.dataLoaded(
PaymentMethodMetadataFactory.create(),
Embedded.RowStyle.FlatWithRadio.defaultLight
Embedded.RowStyle.FlatWithRadio.defaultLight,
embeddedViewDisplaysMandateText = true,
)
awaitItem().run {
assertThat(this).isNotNull()
Expand All @@ -65,14 +67,33 @@ internal class DefaultEmbeddedContentHelperTest {
}
}

@Test
fun `setting mandate when embeddedViewDisplaysMandateText does not emit event with mandate`() = testScenario {
embeddedContentHelper.embeddedContent.test {
assertThat(awaitItem()).isNull()
embeddedContentHelper.dataLoaded(
PaymentMethodMetadataFactory.create(),
Embedded.RowStyle.FlatWithRadio.defaultLight,
embeddedViewDisplaysMandateText = false,
)
awaitItem().run {
assertThat(this).isNotNull()
assertThat(this?.mandate).isNull()
}
savedStateHandle[MANDATE_KEY_EMBEDDED_CONTENT] = "Hi".resolvableString
ensureAllEventsConsumed() // Updating the mandate shouldn't emit any more events.
}
}

@Test
fun `initializing embeddedContentHelper with paymentMethodMetadata emits correct initial event`() = testScenario(
setup = {
set(
STATE_KEY_EMBEDDED_CONTENT,
DefaultEmbeddedContentHelper.State(
PaymentMethodMetadataFactory.create(),
Embedded.RowStyle.FloatingButton.default
Embedded.RowStyle.FloatingButton.default,
embeddedViewDisplaysMandateText = true,
)
)
}
Expand All @@ -89,7 +110,8 @@ internal class DefaultEmbeddedContentHelperTest {
STATE_KEY_EMBEDDED_CONTENT,
DefaultEmbeddedContentHelper.State(
PaymentMethodMetadataFactory.create(),
Embedded.RowStyle.FloatingButton.default
Embedded.RowStyle.FloatingButton.default,
embeddedViewDisplaysMandateText = true
)
)
set(MANDATE_KEY_EMBEDDED_CONTENT, "Hi".resolvableString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ internal class FakeEmbeddedContentHelper(
private val _dataLoadedTurbine = Turbine<DefaultEmbeddedContentHelper.State>()
val dataLoadedTurbine: ReceiveTurbine<DefaultEmbeddedContentHelper.State> = _dataLoadedTurbine

override fun dataLoaded(paymentMethodMetadata: PaymentMethodMetadata, rowStyle: Embedded.RowStyle) {
_dataLoadedTurbine.add(DefaultEmbeddedContentHelper.State(paymentMethodMetadata, rowStyle))
override fun dataLoaded(
paymentMethodMetadata: PaymentMethodMetadata,
rowStyle: Embedded.RowStyle,
embeddedViewDisplaysMandateText: Boolean,
) {
_dataLoadedTurbine.add(
DefaultEmbeddedContentHelper.State(
paymentMethodMetadata = paymentMethodMetadata,
rowStyle = rowStyle,
embeddedViewDisplaysMandateText = embeddedViewDisplaysMandateText,
)
)
}

fun validate() {
Expand Down

0 comments on commit 83ebb13

Please sign in to comment.