Skip to content

Commit

Permalink
Create TokenizationMethod enum (#2006)
Browse files Browse the repository at this point in the history
If a card number is tokenized, this is the method that was used.

https://stripe.com/docs/api/cards/object#card_object-tokenization_method
  • Loading branch information
mshafrir-stripe authored Dec 24, 2019
1 parent 87ced49 commit 863bbf7
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 10 deletions.
8 changes: 4 additions & 4 deletions stripe/src/main/java/com/stripe/android/model/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ data class Card internal constructor(
*
* [API Reference](https://stripe.com/docs/api/cards/object#card_object-tokenization_method)
*/
internal val tokenizationMethod: String?,
val tokenizationMethod: TokenizationMethod? = null,

/**
* @return Set of key-value pairs that you can attach to an object. This can be useful fo
Expand Down Expand Up @@ -408,7 +408,7 @@ data class Card internal constructor(
private var customerId: String? = null
private var cvcCheck: String? = null
private var id: String? = null
private var tokenizationMethod: String? = null
private var tokenizationMethod: TokenizationMethod? = null
private var metadata: Map<String, String>? = null
private var loggingTokens: List<String>? = null

Expand Down Expand Up @@ -485,7 +485,7 @@ data class Card internal constructor(
this.id = id
}

fun tokenizationMethod(tokenizationMethod: String?): Builder = apply {
fun tokenizationMethod(tokenizationMethod: TokenizationMethod?): Builder = apply {
this.tokenizationMethod = tokenizationMethod
}

Expand Down Expand Up @@ -528,7 +528,7 @@ data class Card internal constructor(
customerId = customerId.takeUnless { it.isNullOrBlank() },
cvcCheck = cvcCheck.takeUnless { it.isNullOrBlank() },
id = id.takeUnless { it.isNullOrBlank() },
tokenizationMethod = tokenizationMethod.takeUnless { it.isNullOrBlank() },
tokenizationMethod = tokenizationMethod,
metadata = metadata,
loggingTokens = loggingTokens.orEmpty().toMutableList()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ data class CustomerSource internal constructor(
val paymentAsCard = asCard()
return if (paymentAsSource != null && Source.SourceType.CARD == paymentAsSource.type) {
val cardData = paymentAsSource.sourceTypeModel as SourceCardData?
cardData?.tokenizationMethod
} else paymentAsCard?.tokenizationMethod
cardData?.tokenizationMethod?.code
} else {
paymentAsCard?.tokenizationMethod?.code
}
}

val sourceType: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class SourceCardData internal constructor(
@get:ThreeDSecureStatus
val threeDSecureStatus: String?,

val tokenizationMethod: String?
val tokenizationMethod: TokenizationMethod? = null
) : StripeSourceTypeModel() {
@Retention(AnnotationRetention.SOURCE)
@StringDef(ThreeDSecureStatus.REQUIRED, ThreeDSecureStatus.OPTIONAL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.stripe.android.model

/**
* If a card number is tokenized, this is the method that was used. Can be apple_pay or google_pay.
*/
enum class TokenizationMethod(val code: String) {
ApplePay("apple_pay"),
GooglePay("google_pay");

internal companion object {
internal fun fromCode(code: String?): TokenizationMethod? {
return values().firstOrNull {
it.code == code
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.stripe.android.model.Card
import com.stripe.android.model.Card.FundingType
import com.stripe.android.model.CardBrand
import com.stripe.android.model.StripeJsonUtils
import com.stripe.android.model.TokenizationMethod
import org.json.JSONObject

internal class CardJsonParser : ModelJsonParser<Card> {
Expand Down Expand Up @@ -40,7 +41,11 @@ internal class CardJsonParser : ModelJsonParser<Card> {
.id(StripeJsonUtils.optString(json, FIELD_ID))
.last4(StripeJsonUtils.optString(json, FIELD_LAST4))
.name(StripeJsonUtils.optString(json, FIELD_NAME))
.tokenizationMethod(StripeJsonUtils.optString(json, FIELD_TOKENIZATION_METHOD))
.tokenizationMethod(
TokenizationMethod.fromCode(
StripeJsonUtils.optString(json, FIELD_TOKENIZATION_METHOD)
)
)
.metadata(StripeJsonUtils.optHash(json, FIELD_METADATA))
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.SourceCardData
import com.stripe.android.model.StripeJsonUtils
import com.stripe.android.model.TokenizationMethod
import java.util.Locale
import org.json.JSONObject

Expand All @@ -24,7 +25,9 @@ internal class SourceCardDataJsonParser : ModelJsonParser<SourceCardData> {
threeDSecureStatus = asThreeDSecureStatus(
StripeJsonUtils.optString(json, FIELD_THREE_D_SECURE)
),
tokenizationMethod = StripeJsonUtils.optString(json, FIELD_TOKENIZATION_METHOD)
tokenizationMethod = TokenizationMethod.fromCode(
StripeJsonUtils.optString(json, FIELD_TOKENIZATION_METHOD)
)
)
}

Expand Down
33 changes: 33 additions & 0 deletions stripe/src/test/java/com/stripe/android/model/CardFixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,37 @@ object CardFixtures {
}
""".trimIndent()
)))

internal val CARD_GOOGLE_PAY = requireNotNull(CardJsonParser().parse(JSONObject(
"""
{
"id": "card_189fi32eZvKYlo2CHK8NPRME",
"object": "card",
"address_city": "Des Moines",
"address_country": "US",
"address_line1": "123 Any Street",
"address_line1_check": "unavailable",
"address_line2": "456",
"address_state": "IA",
"address_zip": "50305",
"address_zip_check": "unavailable",
"brand": "Visa",
"country": "US",
"currency": "usd",
"customer": "customer77",
"cvc_check": "unavailable",
"exp_month": 8,
"exp_year": 2017,
"funding": "credit",
"fingerprint": "abc123",
"last4": "4242",
"name": "John Cardholder",
"tokenization_method": "google_pay",
"metadata": {
"color": "blue",
"animal": "dog"
}
}
""".trimIndent()
)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.stripe.android.model.parsers

import com.stripe.android.model.CardFixtures
import com.stripe.android.model.TokenizationMethod
import kotlin.test.Test
import kotlin.test.assertEquals

class CardJsonParserTest {
@Test
fun parseGooglePayCard() {
assertEquals(
TokenizationMethod.GooglePay,
CardFixtures.CARD_GOOGLE_PAY.tokenizationMethod
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.SourceCardData
import com.stripe.android.model.SourceFixtures
import com.stripe.android.model.TokenizationMethod
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
Expand Down Expand Up @@ -36,7 +37,7 @@ class SourceCardDataJsonParserTest {
assertEquals(2050, CARD_DATA.expiryYear)
assertEquals("US", CARD_DATA.country)
assertEquals("optional", CARD_DATA.threeDSecureStatus)
assertEquals("apple_pay", CARD_DATA.tokenizationMethod)
assertEquals(TokenizationMethod.ApplePay, CARD_DATA.tokenizationMethod)
}

@Test
Expand Down

0 comments on commit 863bbf7

Please sign in to comment.