|
| 1 | +package com.stripe.android.paymentsheet.ui |
| 2 | + |
| 3 | +import android.content.res.Resources |
| 4 | +import androidx.compose.foundation.Image |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 8 | +import androidx.compose.foundation.layout.height |
| 9 | +import androidx.compose.foundation.layout.width |
| 10 | +import androidx.compose.foundation.shape.ZeroCornerSize |
| 11 | +import androidx.compose.material.Card |
| 12 | +import androidx.compose.material.Divider |
| 13 | +import androidx.compose.material.MaterialTheme |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.getValue |
| 16 | +import androidx.compose.runtime.mutableStateOf |
| 17 | +import androidx.compose.runtime.remember |
| 18 | +import androidx.compose.ui.Alignment |
| 19 | +import androidx.compose.ui.Modifier |
| 20 | +import androidx.compose.ui.layout.onSizeChanged |
| 21 | +import androidx.compose.ui.platform.testTag |
| 22 | +import androidx.compose.ui.res.painterResource |
| 23 | +import androidx.compose.ui.res.stringResource |
| 24 | +import androidx.compose.ui.unit.dp |
| 25 | +import com.stripe.android.CardBrandFilter |
| 26 | +import com.stripe.android.R |
| 27 | +import com.stripe.android.model.CardBrand |
| 28 | +import com.stripe.android.model.PaymentMethod |
| 29 | +import com.stripe.android.uicore.getBorderStroke |
| 30 | +import com.stripe.android.uicore.stripeColors |
| 31 | +import com.stripe.android.uicore.stripeShapes |
| 32 | +import com.stripe.android.uicore.utils.collectAsState |
| 33 | + |
| 34 | +@Composable |
| 35 | +internal fun CardDetailsEditUI( |
| 36 | + cardEditUIHandler: CardEditUIHandler, |
| 37 | + isExpiredCard: Boolean, |
| 38 | + modifier: Modifier = Modifier |
| 39 | +) { |
| 40 | + val state by cardEditUIHandler.state.collectAsState() |
| 41 | + val dividerHeight = remember { mutableStateOf(0.dp) } |
| 42 | + |
| 43 | + Card( |
| 44 | + border = MaterialTheme.getBorderStroke(false), |
| 45 | + elevation = 0.dp, |
| 46 | + modifier = modifier.testTag(UPDATE_PM_CARD_TEST_TAG), |
| 47 | + ) { |
| 48 | + Column { |
| 49 | + CardNumberField( |
| 50 | + card = state.card, |
| 51 | + selectedBrand = state.selectedCardBrand, |
| 52 | + shouldShowCardBrandDropdown = cardEditUIHandler.showCardBrandDropdown, |
| 53 | + cardBrandFilter = cardEditUIHandler.cardBrandFilter, |
| 54 | + savedPaymentMethodIcon = cardEditUIHandler.paymentMethodIcon, |
| 55 | + onBrandChoiceChanged = { |
| 56 | + cardEditUIHandler.onBrandChoiceChanged(it) |
| 57 | + }, |
| 58 | + ) |
| 59 | + Divider( |
| 60 | + color = MaterialTheme.stripeColors.componentDivider, |
| 61 | + thickness = MaterialTheme.stripeShapes.borderStrokeWidth.dp, |
| 62 | + ) |
| 63 | + Row(modifier = Modifier.fillMaxWidth()) { |
| 64 | + ExpiryField( |
| 65 | + expiryMonth = state.card.expiryMonth, |
| 66 | + expiryYear = state.card.expiryYear, |
| 67 | + isExpired = isExpiredCard, |
| 68 | + modifier = Modifier |
| 69 | + .weight(1F) |
| 70 | + .onSizeChanged { |
| 71 | + dividerHeight.value = |
| 72 | + (it.height / Resources.getSystem().displayMetrics.density).dp |
| 73 | + }, |
| 74 | + ) |
| 75 | + Divider( |
| 76 | + modifier = Modifier |
| 77 | + .height(dividerHeight.value) |
| 78 | + .width(MaterialTheme.stripeShapes.borderStrokeWidth.dp), |
| 79 | + color = MaterialTheme.stripeColors.componentDivider, |
| 80 | + ) |
| 81 | + CvcField(cardBrand = state.card.brand, modifier = Modifier.weight(1F)) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +@Composable |
| 88 | +private fun CardNumberField( |
| 89 | + card: PaymentMethod.Card, |
| 90 | + selectedBrand: CardBrandChoice, |
| 91 | + cardBrandFilter: CardBrandFilter, |
| 92 | + shouldShowCardBrandDropdown: Boolean, |
| 93 | + savedPaymentMethodIcon: Int, |
| 94 | + onBrandChoiceChanged: (CardBrandChoice) -> Unit, |
| 95 | +) { |
| 96 | + CommonTextField( |
| 97 | + value = "•••• •••• •••• ${card.last4}", |
| 98 | + label = stringResource(id = R.string.stripe_acc_label_card_number), |
| 99 | + trailingIcon = { |
| 100 | + if (shouldShowCardBrandDropdown) { |
| 101 | + CardBrandDropdown( |
| 102 | + selectedBrand = selectedBrand, |
| 103 | + availableBrands = card.getAvailableNetworks(cardBrandFilter), |
| 104 | + onBrandChoiceChanged = onBrandChoiceChanged, |
| 105 | + ) |
| 106 | + } else { |
| 107 | + PaymentMethodIconFromResource( |
| 108 | + iconRes = savedPaymentMethodIcon, |
| 109 | + colorFilter = null, |
| 110 | + alignment = Alignment.Center, |
| 111 | + modifier = Modifier, |
| 112 | + ) |
| 113 | + } |
| 114 | + }, |
| 115 | + ) |
| 116 | +} |
| 117 | + |
| 118 | +@Composable |
| 119 | +private fun ExpiryField( |
| 120 | + expiryMonth: Int?, |
| 121 | + expiryYear: Int?, |
| 122 | + isExpired: Boolean, |
| 123 | + modifier: Modifier |
| 124 | +) { |
| 125 | + CommonTextField( |
| 126 | + modifier = modifier.testTag(UPDATE_PM_EXPIRY_FIELD_TEST_TAG), |
| 127 | + value = formattedExpiryDate(expiryMonth = expiryMonth, expiryYear = expiryYear), |
| 128 | + label = stringResource(id = com.stripe.android.uicore.R.string.stripe_expiration_date_hint), |
| 129 | + shape = MaterialTheme.shapes.small.copy( |
| 130 | + topStart = ZeroCornerSize, |
| 131 | + topEnd = ZeroCornerSize, |
| 132 | + bottomEnd = ZeroCornerSize, |
| 133 | + ), |
| 134 | + shouldShowError = isExpired, |
| 135 | + ) |
| 136 | +} |
| 137 | + |
| 138 | +private fun formattedExpiryDate(expiryMonth: Int?, expiryYear: Int?): String { |
| 139 | + @Suppress("ComplexCondition") |
| 140 | + if ( |
| 141 | + expiryMonth == null || |
| 142 | + monthIsInvalid(expiryMonth) || |
| 143 | + expiryYear == null || |
| 144 | + yearIsInvalid(expiryYear) |
| 145 | + ) { |
| 146 | + return "••/••" |
| 147 | + } |
| 148 | + |
| 149 | + val formattedExpiryMonth = if (expiryMonth < OCTOBER) { |
| 150 | + "0$expiryMonth" |
| 151 | + } else { |
| 152 | + expiryMonth.toString() |
| 153 | + } |
| 154 | + |
| 155 | + @Suppress("MagicNumber") |
| 156 | + val formattedExpiryYear = expiryYear.toString().substring(2, 4) |
| 157 | + |
| 158 | + return "$formattedExpiryMonth/$formattedExpiryYear" |
| 159 | +} |
| 160 | + |
| 161 | +private fun monthIsInvalid(expiryMonth: Int): Boolean { |
| 162 | + return expiryMonth < JANUARY || expiryMonth > DECEMBER |
| 163 | +} |
| 164 | + |
| 165 | +private fun yearIsInvalid(expiryYear: Int): Boolean { |
| 166 | + // Since we use 2-digit years to represent the expiration year, we should keep dates to |
| 167 | + // this century. |
| 168 | + return expiryYear < YEAR_2000 || expiryYear > YEAR_2100 |
| 169 | +} |
| 170 | + |
| 171 | +@Composable |
| 172 | +private fun CvcField(cardBrand: CardBrand, modifier: Modifier) { |
| 173 | + val cvc = buildString { |
| 174 | + repeat(cardBrand.maxCvcLength) { |
| 175 | + append("•") |
| 176 | + } |
| 177 | + } |
| 178 | + CommonTextField( |
| 179 | + modifier = modifier.testTag(UPDATE_PM_CVC_FIELD_TEST_TAG), |
| 180 | + value = cvc, |
| 181 | + label = stringResource(id = R.string.stripe_cvc_number_hint), |
| 182 | + shape = MaterialTheme.shapes.small.copy( |
| 183 | + topStart = ZeroCornerSize, |
| 184 | + topEnd = ZeroCornerSize, |
| 185 | + bottomStart = ZeroCornerSize |
| 186 | + ), |
| 187 | + trailingIcon = { |
| 188 | + Image( |
| 189 | + painter = painterResource(cardBrand.cvcIcon), |
| 190 | + contentDescription = null, |
| 191 | + ) |
| 192 | + }, |
| 193 | + ) |
| 194 | +} |
| 195 | + |
| 196 | +private const val JANUARY = 1 |
| 197 | +private const val OCTOBER = 10 |
| 198 | +private const val DECEMBER = 12 |
| 199 | +private const val YEAR_2000 = 2000 |
| 200 | +private const val YEAR_2100 = 2100 |
0 commit comments