Skip to content

Commit

Permalink
Make Token.Type an enum (#2566)
Browse files Browse the repository at this point in the history
- Fix `PersonTokenParams`'s token type
- Remove `Token.fromString()`
  • Loading branch information
mshafrir-stripe authored Jun 10, 2020
1 parent 11330be commit 1deecfa
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 261 deletions.
17 changes: 17 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@
else -> {}
}
```
- Changes to `Token`
- `Token.TokenType` is renamed to `Token.Type` and is now an enum
```kotlin
// before
when (token.type) {
Token.TokenType.CARD -> {}
Token.TokenType.BANK_ACCOUNT -> {}
else -> {}
}

// after
when (token.type) {
Token.Type.Card -> {}
Token.Type.BankAccount -> {}
else -> {}
}
```
- Changes to `AddPaymentMethodActivity`
- When `CustomerSession` is instantiated with a `stripeAccountId`, it will be used in `AddPaymentMethodActivity`
when creating a payment method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ internal class AnalyticsDataFactory @VisibleForTesting internal constructor(
@JvmSynthetic
internal fun createTokenCreationParams(
productUsageTokens: Set<String>?,
@Token.TokenType tokenType: String
tokenType: Token.Type
): Map<String, Any> {
return createParams(
AnalyticsEvent.TokenCreate,
Expand Down Expand Up @@ -259,7 +259,7 @@ internal class AnalyticsDataFactory @VisibleForTesting internal constructor(
event: AnalyticsEvent,
productUsageTokens: Set<String>? = null,
@Source.SourceType sourceType: String? = null,
@Token.TokenType tokenType: String? = null,
tokenType: Token.Type? = null,
extraParams: Map<String, Any>? = null
): Map<String, Any> {
return createStandardParams(event)
Expand All @@ -276,10 +276,10 @@ internal class AnalyticsDataFactory @VisibleForTesting internal constructor(

private fun createTokenTypeParam(
@Source.SourceType sourceType: String? = null,
@Token.TokenType tokenType: String? = null
tokenType: Token.Type? = null
): Map<String, String> {
val value = when {
tokenType != null -> tokenType
tokenType != null -> tokenType.code
// This is not a source event, so to match iOS we log a token without type
// as type "unknown"
sourceType == null -> "unknown"
Expand Down
31 changes: 11 additions & 20 deletions stripe/src/main/java/com/stripe/android/model/AccountParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,17 @@ data class AccountParams internal constructor(
* See [BusinessTypeParams]
*/
private val businessData: Map<String, @RawValue Any>? = null
) : TokenParams(Token.TokenType.ACCOUNT) {

/**
* Create a string-keyed map representing this object that is ready to be sent over the network.
*
* @return a String-keyed map
*/
override fun toParamMap(): Map<String, Any> {
return mapOf("account" to
mapOf(PARAM_TOS_SHOWN_AND_ACCEPTED to tosShownAndAccepted)
.plus(
businessType?.code?.let { code ->
mapOf(PARAM_BUSINESS_TYPE to code)
.plus(
businessData?.let { mapOf(code to it) }.orEmpty()
)
}.orEmpty()
)
)
}
) : TokenParams(Token.Type.Account) {
override val typeDataParams: Map<String, Any>
get() = mapOf(PARAM_TOS_SHOWN_AND_ACCEPTED to tosShownAndAccepted)
.plus(
businessType?.code?.let { code ->
mapOf(PARAM_BUSINESS_TYPE to code)
.plus(
businessData?.let { mapOf(code to it) }.orEmpty()
)
}.orEmpty()
)

/**
* The business type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ data class BankAccountTokenParams @JvmOverloads constructor(
* [bank_account.routing_number](https://stripe.com/docs/api/tokens/create_bank_account#create_bank_account_token-bank_account-routing_number)
*/
private val routingNumber: String? = null
) : TokenParams(Token.TokenType.BANK_ACCOUNT) {
) : TokenParams(Token.Type.BankAccount) {
enum class Type(internal val code: String) {
Individual("individual"),
Company("company");
Expand All @@ -66,8 +66,8 @@ data class BankAccountTokenParams @JvmOverloads constructor(
}
}

override fun toParamMap(): Map<String, Any> {
val bankAccountParams: Map<String, String> = listOf(
override val typeDataParams: Map<String, Any>
get() = listOf(
PARAM_COUNTRY to country,
PARAM_CURRENCY to currency,
PARAM_ACCOUNT_HOLDER_NAME to accountHolderName,
Expand All @@ -80,9 +80,6 @@ data class BankAccountTokenParams @JvmOverloads constructor(
)
}

return mapOf(Token.TokenType.BANK_ACCOUNT to bankAccountParams)
}

private companion object {
private const val PARAM_COUNTRY = "country"
private const val PARAM_CURRENCY = "currency"
Expand Down
7 changes: 3 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 @@ -191,7 +191,7 @@ data class Card internal constructor(
* See [API Reference](https://stripe.com/docs/api/cards/object#card_object-metadata).
*/
val metadata: Map<String, String>?
) : StripeModel, StripePaymentSource, TokenParams(Token.TokenType.CARD, loggingTokens) {
) : StripeModel, StripePaymentSource, TokenParams(Token.Type.Card, loggingTokens) {

fun toPaymentMethodsParams(): PaymentMethodCreateParams {
return PaymentMethodCreateParams.create(
Expand Down Expand Up @@ -332,8 +332,8 @@ data class Card internal constructor(
}
}

override fun toParamMap(): Map<String, Any> {
return CardParams(
override val typeDataParams: Map<String, Any>
get() = CardParams(
number = number.orEmpty(),
expMonth = expMonth ?: 0,
expYear = expYear ?: 0,
Expand All @@ -349,7 +349,6 @@ data class Card internal constructor(
country = addressCountry
)
).toParamMap()
}

/**
* Builder class for a [Card] model.
Expand Down
4 changes: 1 addition & 3 deletions stripe/src/main/java/com/stripe/android/model/CardParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ internal data class CardParams internal constructor(
)

override fun toParamMap(): Map<String, Any> {
val params: Map<String, Any> = listOf(
return listOf(
PARAM_NUMBER to number,
PARAM_EXP_MONTH to expMonth,
PARAM_EXP_YEAR to expYear,
Expand All @@ -162,8 +162,6 @@ internal data class CardParams internal constructor(
value?.let { mapOf(key to it) }.orEmpty()
)
}

return mapOf(Token.TokenType.CARD to params)
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import kotlinx.android.parcel.Parcelize
@Parcelize
data class CvcTokenParams(
private val cvc: String
) : TokenParams(Token.TokenType.CVC_UPDATE) {
override fun toParamMap(): Map<String, Any> {
return mapOf(
Token.TokenType.CVC_UPDATE to mapOf("cvc" to cvc)
)
}
) : TokenParams(Token.Type.CvcUpdate) {
override val typeDataParams: Map<String, Any>
get() = mapOf("cvc" to cvc)
}
57 changes: 27 additions & 30 deletions stripe/src/main/java/com/stripe/android/model/PersonTokenParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,33 @@ data class PersonTokenParams(
* [person.verification](https://stripe.com/docs/api/tokens/create_person#create_person_token-person-verification)
*/
val verification: Verification? = null
) : TokenParams(Token.TokenType.BANK_ACCOUNT) {
override fun toParamMap(): Map<String, Any> {
return mapOf(PARAM_PERSON to
listOf(
PARAM_ADDRESS to address?.toParamMap(),
PARAM_ADDRESS_KANA to addressKana?.toParamMap(),
PARAM_ADDRESS_KANJI to addressKanji?.toParamMap(),
PARAM_DOB to dateOfBirth?.toParamMap(),
PARAM_EMAIL to email,
PARAM_FIRST_NAME to firstName,
PARAM_FIRST_NAME_KANA to firstNameKana,
PARAM_FIRST_NAME_KANJI to firstNameKanji,
PARAM_GENDER to gender,
PARAM_ID_NUMBER to idNumber,
PARAM_LAST_NAME to lastName,
PARAM_LAST_NAME_KANA to lastNameKana,
PARAM_LAST_NAME_KANJI to lastNameKanji,
PARAM_MAIDEN_NAME to maidenName,
PARAM_METADATA to metadata,
PARAM_PHONE to phone,
PARAM_RELATIONSHIP to relationship?.toParamMap(),
PARAM_SSN_LAST_4 to ssnLast4,
PARAM_VERIFICATION to verification?.toParamMap()
).fold(emptyMap<String, Any>()) { acc, (key, value) ->
acc.plus(
value?.let { mapOf(key to it) }.orEmpty()
)
}
)
}
) : TokenParams(Token.Type.Person) {
override val typeDataParams: Map<String, Any>
get() = listOf(
PARAM_ADDRESS to address?.toParamMap(),
PARAM_ADDRESS_KANA to addressKana?.toParamMap(),
PARAM_ADDRESS_KANJI to addressKanji?.toParamMap(),
PARAM_DOB to dateOfBirth?.toParamMap(),
PARAM_EMAIL to email,
PARAM_FIRST_NAME to firstName,
PARAM_FIRST_NAME_KANA to firstNameKana,
PARAM_FIRST_NAME_KANJI to firstNameKanji,
PARAM_GENDER to gender,
PARAM_ID_NUMBER to idNumber,
PARAM_LAST_NAME to lastName,
PARAM_LAST_NAME_KANA to lastNameKana,
PARAM_LAST_NAME_KANJI to lastNameKanji,
PARAM_MAIDEN_NAME to maidenName,
PARAM_METADATA to metadata,
PARAM_PHONE to phone,
PARAM_RELATIONSHIP to relationship?.toParamMap(),
PARAM_SSN_LAST_4 to ssnLast4,
PARAM_VERIFICATION to verification?.toParamMap()
).fold(emptyMap()) { acc, (key, value) ->
acc.plus(
value?.let { mapOf(key to it) }.orEmpty()
)
}

/**
* The relationship that this person has with the account’s legal entity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import kotlinx.android.parcel.Parcelize
@Parcelize
internal data class PiiTokenParams(
private val personalId: String
) : TokenParams(Token.TokenType.PII) {
override fun toParamMap(): Map<String, Any> {
return mapOf(
Token.TokenType.PII to mapOf("personal_id_number" to personalId)
)
}
) : TokenParams(Token.Type.Pii) {
override val typeDataParams: Map<String, Any>
get() = mapOf("personal_id_number" to personalId)
}
57 changes: 20 additions & 37 deletions stripe/src/main/java/com/stripe/android/model/Token.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.model

import androidx.annotation.StringDef
import com.stripe.android.model.Token.TokenType
import com.stripe.android.model.Token.Type
import com.stripe.android.model.parsers.TokenJsonParser
import java.util.Date
import kotlinx.android.parcel.Parcelize
Expand All @@ -16,70 +15,54 @@ import org.json.JSONObject
data class Token internal constructor(

/**
* @return the Token id
* The Token id
*/
override val id: String,

/**
* @return Get the [TokenType] of this token.
* The [Type] of this token.
*/
@get:TokenType
val type: String,
val type: Type,

/***
* @return the [Date] this token was created
/**
* The [Date] this token was created
*/
val created: Date,

/**
* @return `true` if this token is valid for a real payment, `false` if
* it is only usable for testing
* `true` if this token is valid for a real payment, `false` if it is only usable for testing
*/
val livemode: Boolean,

/**
* @return `true` if this token has been used, `false` otherwise
* `true` if this token has been used, `false` otherwise
*/
val used: Boolean,

/**
* @return the [BankAccount] for this token
* If applicable, the [BankAccount] for this token
*/
val bankAccount: BankAccount? = null,

/**
* @return the [Card] for this token
* If applicable, the [Card] for this token
*/
val card: Card? = null
) : StripeModel, StripePaymentSource {
@Retention(AnnotationRetention.SOURCE)
@StringDef(TokenType.CARD, TokenType.BANK_ACCOUNT, TokenType.PII, TokenType.ACCOUNT,
TokenType.CVC_UPDATE, TokenType.PERSON)
annotation class TokenType {
companion object {
const val CARD: String = "card"
const val BANK_ACCOUNT: String = "bank_account"
const val PII: String = "pii"
const val ACCOUNT: String = "account"
const val CVC_UPDATE: String = "cvc_update"
const val PERSON: String = "person"
enum class Type(internal val code: String) {
Card("card"),
BankAccount("bank_account"),
Pii("pii"),
Account("account"),
CvcUpdate("cvc_update"),
Person("person");

internal companion object {
fun fromCode(code: String?) = values().firstOrNull { it.code == code }
}
}

companion object {
@JvmStatic
fun fromString(jsonString: String?): Token? {
if (jsonString == null) {
return null
}

return runCatching {
JSONObject(jsonString)
}.getOrNull()?.let {
fromJson(it)
}
}

@JvmStatic
fun fromJson(jsonObject: JSONObject?): Token? {
return jsonObject?.let {
Expand Down
8 changes: 6 additions & 2 deletions stripe/src/main/java/com/stripe/android/model/TokenParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package com.stripe.android.model
import android.os.Parcelable

abstract class TokenParams(
@Token.TokenType internal val tokenType: String,
internal val tokenType: Token.Type,
/**
* The SDK components that were involved in the creation of this token
*/
internal val attribution: Set<String> = emptySet()
) : StripeParamsModel, Parcelable
) : StripeParamsModel, Parcelable {
abstract val typeDataParams: Map<String, Any>

override fun toParamMap(): Map<String, Any> = mapOf(tokenType.code to typeDataParams)
}
Loading

0 comments on commit 1deecfa

Please sign in to comment.