Skip to content

Commit

Permalink
Add cardParams property to CardInputWidget and CardMultilineWidget (#…
Browse files Browse the repository at this point in the history
…2671)

These properties are currently internal
  • Loading branch information
mshafrir-stripe authored Jul 31, 2020
1 parent 9adaef6 commit 8dbe6fd
Show file tree
Hide file tree
Showing 4 changed files with 1,460 additions and 810 deletions.
94 changes: 75 additions & 19 deletions stripe/src/main/java/com/stripe/android/view/CardInputWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.stripe.android.databinding.CardInputWidgetBinding
import com.stripe.android.model.Address
import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.CardParams
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import kotlin.properties.Delegates
Expand Down Expand Up @@ -208,6 +209,61 @@ class CardInputWidget @JvmOverloads constructor(
return cardBuilder?.build()
}

internal val cardParams: CardParams?
get() {
val cardNumber = cardNumberEditText.cardNumber
val cardDate = expiryDateEditText.validDateFields
val cvcValue = this.cvcValue

cardNumberEditText.shouldShowError = cardNumber == null
expiryDateEditText.shouldShowError = cardDate == null
cvcNumberEditText.shouldShowError = cvcValue == null
postalCodeEditText.shouldShowError =
(postalCodeRequired || usZipCodeRequired) &&
postalCodeEditText.postalCode.isNullOrBlank()

// Announce error messages for accessibility
currentFields
.filter { it.shouldShowError }
.forEach { editText ->
editText.errorMessage?.let { errorMessage ->
editText.announceForAccessibility(errorMessage)
}
}

when {
cardNumber == null -> {
cardNumberEditText.requestFocus()
}
cardDate == null -> {
expiryDateEditText.requestFocus()
}
cvcValue == null -> {
cvcNumberEditText.requestFocus()
}
postalCodeEditText.shouldShowError -> {
postalCodeEditText.requestFocus()
}
else -> {
shouldShowErrorIcon = false
return CardParams(
setOf(LOGGING_TOKEN),
number = cardNumber,
expMonth = cardDate.first,
expYear = cardDate.second,
cvc = cvcValue,
address = Address.Builder()
.setPostalCode(postalCodeValue.takeUnless { it.isNullOrBlank() })
.build()
)
}
}

shouldShowErrorIcon = true

return null
}

/**
* A [Card.Builder] representing the card details and postal code if all fields are valid;
* otherwise `null`
Expand Down Expand Up @@ -1047,26 +1103,26 @@ class CardInputWidget @JvmOverloads constructor(
/**
* A class for tracking the placement and layout of fields
*/
internal class PlacementParameters {
internal var totalLengthInPixels: Int = 0

internal var cardWidth: Int = 0
internal var hiddenCardWidth: Int = 0
internal var peekCardWidth: Int = 0
internal var cardDateSeparation: Int = 0
internal var dateWidth: Int = 0
internal var dateCvcSeparation: Int = 0
internal var cvcWidth: Int = 0
internal var cvcPostalCodeSeparation: Int = 0
internal var postalCodeWidth: Int = 0

internal var cardTouchBufferLimit: Int = 0
internal var dateStartPosition: Int = 0
internal var dateRightTouchBufferLimit: Int = 0
internal var cvcStartPosition: Int = 0
internal var cvcRightTouchBufferLimit: Int = 0
internal data class PlacementParameters(
internal var totalLengthInPixels: Int = 0,

internal var cardWidth: Int = 0,
internal var hiddenCardWidth: Int = 0,
internal var peekCardWidth: Int = 0,
internal var cardDateSeparation: Int = 0,
internal var dateWidth: Int = 0,
internal var dateCvcSeparation: Int = 0,
internal var cvcWidth: Int = 0,
internal var cvcPostalCodeSeparation: Int = 0,
internal var postalCodeWidth: Int = 0,

internal var cardTouchBufferLimit: Int = 0,
internal var dateStartPosition: Int = 0,
internal var dateRightTouchBufferLimit: Int = 0,
internal var cvcStartPosition: Int = 0,
internal var cvcRightTouchBufferLimit: Int = 0,
internal var postalCodeStartPosition: Int = 0

) {
private val cardPeekDateLeftMargin: Int
@JvmSynthetic
get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.stripe.android.databinding.CardMultilineWidgetBinding
import com.stripe.android.model.Address
import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.CardParams
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import java.math.BigDecimal
Expand Down Expand Up @@ -82,6 +83,7 @@ class CardMultilineWidget @JvmOverloads constructor(
private var customCvcLabel: String? = null

private var cardBrand: CardBrand = CardBrand.Unknown

@ColorInt
private val tintColorInt: Int

Expand Down Expand Up @@ -175,6 +177,32 @@ class CardMultilineWidget @JvmOverloads constructor(
return cardBuilder?.build()
}

internal val cardParams: CardParams?
get() {
if (!validateAllFields()) {
shouldShowErrorIcon = true
return null
}

shouldShowErrorIcon = false

val cardDate = requireNotNull(expiryDateEditText.validDateFields)
val cvcValue = cvcEditText.text?.toString()
val postalCode = postalCodeEditText.text?.toString()
.takeIf { shouldShowPostalCode }

return CardParams(
setOf(CARD_MULTILINE_TOKEN),
number = cardNumber.orEmpty(),
expMonth = cardDate.first,
expYear = cardDate.second,
cvc = cvcValue,
address = Address.Builder()
.setPostalCode(postalCode.takeUnless { it.isNullOrBlank() })
.build()
)
}

/**
* A [Card.Builder] representing the card details and postal code if all fields are valid;
* otherwise `null`
Expand Down
Loading

0 comments on commit 8dbe6fd

Please sign in to comment.