Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cardParams property to CardInputWidget and CardMultilineWidget #2671

Merged
merged 3 commits into from
Jul 31, 2020
Merged
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
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