Skip to content
Draft
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
7 changes: 4 additions & 3 deletions paymentsheet/api/paymentsheet.api
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
public abstract interface annotation class com/stripe/android/ExperimentalAllowsRemovalOfLastSavedPaymentMethodApi : java/lang/annotation/Annotation {
}

public final class com/stripe/android/common/nfcscan/ui/ComposableSingletons$NfcInstructionTextLayoutKt {
public static final field INSTANCE Lcom/stripe/android/common/nfcscan/ui/ComposableSingletons$NfcInstructionTextLayoutKt;
public final class com/stripe/android/common/nfcscan/ui/ComposableSingletons$NfcCoilTextLayoutKt {
public static final field INSTANCE Lcom/stripe/android/common/nfcscan/ui/ComposableSingletons$NfcCoilTextLayoutKt;
public fun <init> ()V
public final fun getLambda$1568718839$paymentsheet_release ()Lkotlin/jvm/functions/Function3;
public final fun getLambda$-1325870959$paymentsheet_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$-298415252$paymentsheet_release ()Lkotlin/jvm/functions/Function3;
}

public final class com/stripe/android/common/taptoadd/ui/ComposableSingletons$TapToAddDelayScreenKt {
Expand Down
2 changes: 2 additions & 0 deletions paymentsheet/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ to be saved and used in future checkout sessions. -->
<string name="stripe_tap_to_add_unsupported_device_error_title">Device not supported</string>
<!-- Error message shown to the user to try tapping their different card as their next action. -->
<string name="stripe_tap_to_add_unusable_error_action">Try another card</string>
<!-- Error message with user action shown in NFC scanning flow when the card is expired. -->
<string name="stripe_nfc_expired_error">Card expired. Try another card.</string>
<!-- Button text on a screen asking the user to approve a payment -->
<string name="stripe_upi_polling_cancel">Cancel and pay another way</string>
<!-- Text on a screen asking the user to approve a payment -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class NfcScanningViewModel @Inject constructor(
private val _viewState = MutableStateFlow(
NfcScanningViewState(
tapZone = tapZone,
status = NfcScanningStatus.Idle,
status = NfcScanningStatus.Idle(error = null),
)
)
val viewState: StateFlow<NfcScanningViewState> = _viewState.asStateFlow()
Expand All @@ -62,6 +62,13 @@ internal class NfcScanningViewModel @Inject constructor(
eventReporter.onNfcScanAttemptStarted()
}
is NfcCardScanner.State.Failed -> {
_viewState.emit(
NfcScanningViewState(
tapZone = tapZone,
status = NfcScanningStatus.Idle(error = state.error.userMessage),
)
)

eventReporter.onNfcScanAttemptFailed()
}
is NfcCardScanner.State.Complete -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import androidx.appcompat.app.AppCompatActivity
import com.stripe.android.common.nfcscan.hardware.NfcHardwareDelegate
import com.stripe.android.core.injection.IOContext
import com.stripe.android.core.injection.ViewModelScope
import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.paymentsheet.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -16,9 +19,13 @@ internal interface NfcCardScanner {
sealed interface State {
data object Scanning : State
data class Complete(val cardData: ScannedCardData) : State
data class Failed(val error: Throwable) : State
data class Failed(val error: Error) : State
}

data class Error(
val userMessage: ResolvableString,
)

@samer-stripe samer-stripe Jul 11, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will also output an error code that will be sent alongside our analytics in a follow-up.


val state: Flow<State>

fun start(activity: AppCompatActivity)
Expand Down Expand Up @@ -59,11 +66,17 @@ internal class DefaultNfcCardScanner @Inject constructor(
onSuccess = { cardData ->
_state.emit(NfcCardScanner.State.Complete(cardData))
},
onFailure = { error ->
_state.emit(NfcCardScanner.State.Failed(error))
onFailure = {
_state.emit(NfcCardScanner.State.Failed(GENERIC_ERROR))
}
)
}
}
}

private companion object {
val GENERIC_ERROR = NfcCardScanner.Error(
userMessage = R.string.stripe_tap_to_add_card_default_error_action.resolvableString,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private fun rememberNfcCoilRingState(
if (LocalInspectionMode.current) {
return when (status) {
NfcScanningStatus.Scanned -> NfcCoilRingProgress.Complete
NfcScanningStatus.Idle,
is NfcScanningStatus.Idle,
NfcScanningStatus.Scanning -> NfcCoilRingProgress.Zero
}
}
Expand All @@ -97,7 +97,7 @@ private fun rememberNfcCoilRingState(

LaunchedEffect(status) {
when (status) {
NfcScanningStatus.Idle,
is NfcScanningStatus.Idle,
NfcScanningStatus.Scanning -> {
animatables.snapTo(NfcCoilRingProgress.Zero)
savedState = NfcCoilRingSavedState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal fun NfcCoilContactlessIcon(
status: NfcScanningStatus,
modifier: Modifier = Modifier,
) {
if (LocalInspectionMode.current || status != NfcScanningStatus.Idle) {
if (LocalInspectionMode.current || status !is NfcScanningStatus.Idle) {
NfcCoilStaticIcon(modifier = modifier)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ internal fun NfcCoilLayout(
)
}

NfcCoilInstructionTextLayout(
NfcCoilTextLayout(
containerWidth = maxWidth,
containerHeight = maxHeight,
tapZone = tapZone,
shouldRenderTextAboveCoil = shouldRenderTextAboveCoil,
coilSize = CoilCircleSize,
canShow = canShowInstructionText,
error = (status as? NfcScanningStatus.Idle)?.error,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package com.stripe.android.common.nfcscan.ui

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.stripe.android.common.nfcscan.tapzone.TapZone
import com.stripe.android.common.ui.InlineContentTemplateBuilder
import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.paymentsheet.R
import com.stripe.android.uicore.strings.resolve
import kotlin.math.roundToInt
import com.stripe.payments.model.R as PaymentsModelR

private val TopTextOffset = (-55).dp
private val BottomTextOffset = 30.dp
private val InstructionTextEdgePadding = 20.dp
private val ErrorTextTopSpacing = 8.dp

@Composable
internal fun NfcCoilTextLayout(
containerWidth: Dp,
containerHeight: Dp,
tapZone: TapZone,
coilSize: Dp,
shouldRenderTextAboveCoil: Boolean,
canShow: Boolean,
error: ResolvableString?,
) {
Layout(
content = {
NfcCoilInstructionText(canShow = canShow)
if (error != null) {
NfcCoilErrorText(
message = error,
canShow = canShow,
)
}
},
) { measurables, constraints ->
placeCoilTextElements(
containerWidth = containerWidth,
containerHeight = containerHeight,
tapZone = tapZone,
coilSize = coilSize,
shouldRenderTextAboveCoil = shouldRenderTextAboveCoil,
measurables = measurables,
constraints = constraints,
)
}
}

private fun MeasureScope.placeCoilTextElements(
containerWidth: Dp,
containerHeight: Dp,
tapZone: TapZone,
coilSize: Dp,
measurables: List<Measurable>,
constraints: Constraints,
shouldRenderTextAboveCoil: Boolean,
): MeasureResult {
val horizontalBias = tapZone.xBias * 2 - 1
val verticalBias = tapZone.yBias * 2 - 1

val containerWidthPx = containerWidth.roundToPx()
val containerHeightPx = containerHeight.roundToPx()

if (measurables.isEmpty()) {
return layout(containerWidthPx, containerHeightPx) {}
}

val coilSizePx = coilSize.roundToPx()
val edgePaddingPx = InstructionTextEdgePadding.roundToPx()
val textConstraints = constraints.copy(
maxWidth = (containerWidthPx - edgePaddingPx * 2).coerceAtLeast(0),
)

val instructionPlaceable = measurables[0].measure(textConstraints)
val errorPlaceable = measurables.getOrNull(1)?.measure(textConstraints)

val coilBoxLeft = ((containerWidthPx - coilSizePx) / 2f * (1f + horizontalBias)).roundToInt()
val coilBoxTop = ((containerHeightPx - coilSizePx) / 2f * (1f + verticalBias)).roundToInt()
val coilCenterX = coilBoxLeft + coilSizePx / 2

val instructionY = if (shouldRenderTextAboveCoil) {
coilBoxTop + TopTextOffset.roundToPx()
} else {
coilBoxTop + coilSizePx + BottomTextOffset.roundToPx()
}

return layout(containerWidthPx, containerHeightPx) {
if (instructionPlaceable.width > 0 && instructionPlaceable.height > 0) {
instructionPlaceable.placeRelative(
x = clampedTextX(
placeable = instructionPlaceable,
coilCenterX = coilCenterX,
containerWidthPx = containerWidthPx,
edgePaddingPx = edgePaddingPx,
),
y = instructionY,
)
}

errorPlaceable?.let { placeable ->
if (placeable.width > 0 && placeable.height > 0) {
val errorY = instructionY +
instructionPlaceable.height +
ErrorTextTopSpacing.roundToPx()

placeable.placeRelative(
x = clampedTextX(
placeable = placeable,
coilCenterX = coilCenterX,
containerWidthPx = containerWidthPx,
edgePaddingPx = edgePaddingPx,
),
y = errorY,
)
}
}
}
}

private fun clampedTextX(
placeable: Placeable,
coilCenterX: Int,
containerWidthPx: Int,
edgePaddingPx: Int,
): Int {
val desiredTextStartX = coilCenterX - placeable.width / 2
return desiredTextStartX.coerceIn(
edgePaddingPx,
(containerWidthPx - edgePaddingPx - placeable.width).coerceAtLeast(edgePaddingPx),
)
}

@Composable
private fun NfcCoilInstructionText(
canShow: Boolean,
) {
AnimatedVisibility(
visible = canShow,
enter = fadeIn(),
exit = fadeOut(),
) {
Text(
text = stringResource(R.string.stripe_nfc_scan_hold_card_behind_phone),
color = MaterialTheme.colors.onSurface,
style = MaterialTheme.typography.h4.copy(fontWeight = FontWeight.Bold),
textAlign = TextAlign.Center,
)
}
}

@Composable
private fun NfcCoilErrorText(
message: ResolvableString,
canShow: Boolean,
) {
AnimatedVisibility(
visible = canShow,
enter = fadeIn(),
exit = fadeOut(),
) {
val textStyle = MaterialTheme.typography.body1
val fontSize = textStyle.fontSize

Text(
text = buildAnnotatedString {
appendInlineContent(ERROR_ICON_ID)
appendInlineContent(ERROR_SPACER_ID)
append(message.resolve())
},
inlineContent = InlineContentTemplateBuilder()
.add(
id = ERROR_ICON_ID,
width = fontSize,
height = fontSize,
align = PlaceholderVerticalAlign.TextCenter
) {
Icon(
painter = painterResource(PaymentsModelR.drawable.stripe_ic_error),
contentDescription = null,
tint = MaterialTheme.colors.error,
modifier = Modifier.fillMaxSize()
)
}
.addSpacer(ERROR_SPACER_ID, ERROR_SPACER_WIDTH)
.build(),
color = MaterialTheme.colors.error,
style = textStyle,
textAlign = TextAlign.Center,
)
}
}

private const val ERROR_SPACER_ID = "ERROR_SPACER"
private val ERROR_SPACER_WIDTH = 5.sp

private const val ERROR_ICON_ID = "ERROR_ICON_ID"
Loading
Loading