Skip to content

Commit

Permalink
Show promo badge in bank form
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Dec 3, 2024
1 parent 1667ce6 commit 9511890
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ internal data class BankFormScreenState(
val isProcessing: Boolean
get() = _isProcessing && linkedBankAccount == null

val promoBadgeState: PromoBadgeState?
get() = if (promoText != null && linkedBankAccount != null) {
PromoBadgeState(promoText, eligible = linkedBankAccount.eligibleForIncentive)
} else {
null
}

val promoDisclaimerText: ResolvableString?
get() = promoText?.let {
if (isPaymentFlow) {
if (linkedBankAccount?.eligibleForIncentive == false) {
return null
} else if (isPaymentFlow) {
resolvableString(R.string.stripe_paymentsheet_bank_payment_promo_for_payment, it)
} else {
resolvableString(R.string.stripe_paymentsheet_bank_payment_promo_for_setup, it)
Expand All @@ -41,8 +50,14 @@ internal data class BankFormScreenState(
val financialConnectionsSessionId: String?,
val mandateText: ResolvableString,
val isVerifyingWithMicrodeposits: Boolean,
val eligibleForIncentive: Boolean = false,
) : Parcelable

data class PromoBadgeState(
val promoText: String,
val eligible: Boolean,
)

sealed interface ResultIdentifier : Parcelable {
@Parcelize
data class Session(val id: String) : ResultIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import com.stripe.android.paymentsheet.PaymentSheet.BillingDetailsCollectionConf
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.model.PaymentSelection.New
import com.stripe.android.paymentsheet.paymentdatacollection.FormArguments
import com.stripe.android.paymentsheet.paymentdatacollection.ach.BankFormScreenState.PromoBadgeState
import com.stripe.android.paymentsheet.ui.Mandate
import com.stripe.android.paymentsheet.ui.PromoBadge
import com.stripe.android.ui.core.elements.SaveForFutureUseElement
import com.stripe.android.ui.core.elements.SaveForFutureUseElementUI
import com.stripe.android.ui.core.elements.SimpleDialogElementUI
Expand Down Expand Up @@ -150,6 +152,7 @@ internal fun BankAccountForm(
isProcessing = state.isProcessing,
bankName = linkedBankAccount.bankName,
last4 = linkedBankAccount.last4,
promoBadgeState = state.promoBadgeState,
saveForFutureUseElement = saveForFutureUseElement,
onRemoveAccount = onRemoveAccount,
)
Expand Down Expand Up @@ -362,6 +365,7 @@ private fun AccountDetailsForm(
isProcessing: Boolean,
bankName: String?,
last4: String?,
promoBadgeState: PromoBadgeState?,
saveForFutureUseElement: SaveForFutureUseElement,
onRemoveAccount: () -> Unit,
) {
Expand Down Expand Up @@ -404,6 +408,13 @@ private fun AccountDetailsForm(
modifier = Modifier.alpha(if (isProcessing) 0.5f else 1f),
color = MaterialTheme.stripeColors.onComponent,
)

promoBadgeState?.let { badgeState ->
PromoBadge(
text = badgeState.promoText,
eligible = badgeState.eligible,
)
}
}

IconButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@ import androidx.compose.ui.unit.dp
import com.stripe.android.uicore.StripeThemeDefaults
import com.stripe.android.uicore.getOnSuccessBackgroundColor
import com.stripe.android.uicore.getSuccessBackgroundColor
import com.stripe.android.uicore.stripeColors
import java.util.Locale

@Composable
internal fun PromoBadge(
text: String,
modifier: Modifier = Modifier,
eligible: Boolean = true,
tinyMode: Boolean = false,
) {
// TODO(tillh-stripe): Revisit how we want the badge text to scale in tiny mode
FixedTextSize(fixed = tinyMode) {
val backgroundColor = Color(
color = StripeThemeDefaults.primaryButtonStyle.getSuccessBackgroundColor(LocalContext.current),
)
val backgroundColor = if (eligible) {
Color(
color = StripeThemeDefaults.primaryButtonStyle.getSuccessBackgroundColor(LocalContext.current),
)
} else {
MaterialTheme.stripeColors.componentBorder
}

val foregroundColor = Color(
color = StripeThemeDefaults.primaryButtonStyle.getOnSuccessBackgroundColor(LocalContext.current),
)
val foregroundColor = if (eligible) {
Color(
color = StripeThemeDefaults.primaryButtonStyle.getOnSuccessBackgroundColor(LocalContext.current),
)
} else {
MaterialTheme.stripeColors.onComponent
}

val shape = MaterialTheme.shapes.medium

Expand All @@ -47,7 +57,7 @@ internal fun PromoBadge(
)
) {
Text(
text = formatPromoText(text),
text = formatPromoText(text, eligible),
color = foregroundColor,
style = MaterialTheme.typography.caption.copy(
fontSize = StripeThemeDefaults.typography.xSmallFontSize,
Expand All @@ -58,7 +68,10 @@ internal fun PromoBadge(
}

@Composable
private fun formatPromoText(text: String): String {
private fun formatPromoText(
text: String,
eligible: Boolean,
): String {
val context = LocalContext.current
val currentLocale: Locale = if (SDK_INT >= N) {
context.resources.configuration.locales[0]
Expand All @@ -68,7 +81,15 @@ private fun formatPromoText(text: String): String {
}

val isEnglish = currentLocale.language == Locale.ENGLISH.language
return if (isEnglish) "Get $text" else text
return if (isEnglish) {
if (eligible) {
"Get $text"
} else {
"No $text promo"
}
} else {
text
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ internal class AccountPreviewScreenshotTest {
}
}

@Test
fun testWithPromoBadge() {
paparazzi.snapshot {
BankAccountForm(
state = BankFormScreenStateFactory.createWithSession(
sessionId = "session_1234",
promoText = "$5",
),
instantDebits = true,
isPaymentFlow = true,
formArgs = formArguments,
nameController = createNameController(),
emailController = createEmailController(),
phoneController = createPhoneNumberController(),
addressController = createAddressController(fillAddress = false),
sameAsShippingElement = sameAsShippingElement,
saveForFutureUseElement = saveForFutureUseElement,
showCheckbox = false,
lastTextFieldIdentifier = null,
onRemoveAccount = {},
)
}
}

private fun createNameController(): TextFieldController {
return NameConfig.createController("John Doe")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ internal object BankFormScreenStateFactory {
sessionId: String,
isVerifyingWithMicrodeposits: Boolean = false,
mandateText: ResolvableString = "Some legal text".resolvableString,
promoText: String? = null,
): BankFormScreenState {
return create(
resultIdentifier = BankFormScreenState.ResultIdentifier.Session(sessionId),
isVerifyingWithMicrodeposits = isVerifyingWithMicrodeposits,
mandateText = mandateText,
promoText = promoText,
)
}

fun createWithPaymentMethod(
paymentMethod: PaymentMethod,
isVerifyingWithMicrodeposits: Boolean = false,
mandateText: ResolvableString = "Some legal text".resolvableString,
promoText: String? = null,
): BankFormScreenState {
return create(
resultIdentifier = BankFormScreenState.ResultIdentifier.PaymentMethod(paymentMethod),
isVerifyingWithMicrodeposits = isVerifyingWithMicrodeposits,
mandateText = mandateText,
promoText = promoText,
)
}

private fun create(
resultIdentifier: BankFormScreenState.ResultIdentifier,
isVerifyingWithMicrodeposits: Boolean,
mandateText: ResolvableString,
promoText: String?,
): BankFormScreenState {
return BankFormScreenState(
isPaymentFlow = true,
Expand All @@ -46,7 +51,8 @@ internal object BankFormScreenStateFactory {
last4 = "6789",
mandateText = mandateText,
isVerifyingWithMicrodeposits = isVerifyingWithMicrodeposits,
)
),
promoText = promoText,
)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9511890

Please sign in to comment.