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

Convert BankAccount.BankAccountType to BankAccount.Type enum #2547

Merged
merged 1 commit into from
Jun 4, 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
16 changes: 7 additions & 9 deletions stripe/src/main/java/com/stripe/android/model/BankAccount.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.model

import androidx.annotation.Size
import androidx.annotation.StringDef
import kotlinx.android.parcel.Parcelize

/**
Expand All @@ -28,8 +27,7 @@ data class BankAccount internal constructor(
*
* [account_holder_type](https://stripe.com/docs/api/customer_bank_accounts/object#customer_bank_account_object-account_holder_type)
*/
@param:BankAccountType @field:BankAccountType @get:BankAccountType
val accountHolderType: String? = null,
val accountHolderType: Type? = null,

/**
* Name of the bank associated with the routing number (e.g., WELLS FARGO).
Expand Down Expand Up @@ -95,12 +93,12 @@ data class BankAccount internal constructor(
val status: Status? = null
) : StripeModel {

@Retention(AnnotationRetention.SOURCE)
@StringDef(BankAccountType.COMPANY, BankAccountType.INDIVIDUAL)
annotation class BankAccountType {
companion object {
const val COMPANY: String = "company"
const val INDIVIDUAL: String = "individual"
enum class Type(internal val code: String) {
Company("company"),
Individual("individual");

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this pattern but it would be nice if we could put it in an interface. However, I'm not sure how to make the type constraints work

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.model.parsers

import com.stripe.android.model.BankAccount
import com.stripe.android.model.BankAccount.BankAccountType
import com.stripe.android.model.StripeJsonUtils
import org.json.JSONObject

Expand All @@ -10,7 +9,7 @@ internal class BankAccountJsonParser : ModelJsonParser<BankAccount> {
return BankAccount(
id = StripeJsonUtils.optString(json, FIELD_ID),
accountHolderName = StripeJsonUtils.optString(json, FIELD_ACCOUNT_HOLDER_NAME),
accountHolderType = asBankAccountType(
accountHolderType = BankAccount.Type.fromCode(
StripeJsonUtils.optString(json, FIELD_ACCOUNT_HOLDER_TYPE)
),
bankName = StripeJsonUtils.optString(json, FIELD_BANK_NAME),
Expand All @@ -36,21 +35,5 @@ internal class BankAccountJsonParser : ModelJsonParser<BankAccount> {
private const val FIELD_LAST4 = "last4"
private const val FIELD_ROUTING_NUMBER = "routing_number"
private const val FIELD_STATUS = "status"

/**
* Converts a String value into the appropriate [BankAccountType].
*
* @param possibleAccountType a String that might match a [BankAccountType] or be empty.
* @return `null` if the input is blank or of unknown type, else the appropriate
* [BankAccountType].
*/
@BankAccountType
private fun asBankAccountType(possibleAccountType: String?): String? {
return when (possibleAccountType) {
BankAccountType.COMPANY -> BankAccountType.COMPANY
BankAccountType.INDIVIDUAL -> BankAccountType.INDIVIDUAL
else -> null
}
}
}
}
2 changes: 1 addition & 1 deletion stripe/src/test/java/com/stripe/android/StripeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void createBankAccountTokenSynchronous_withValidBankAccount_returnsToken(
assertTrue(bankAccountId.startsWith("ba_"));
assertEquals("Jenny Rosen", returnedBankAccount.getAccountHolderName());
assertEquals(
BankAccount.BankAccountType.INDIVIDUAL,
BankAccount.Type.Individual,
returnedBankAccount.getAccountHolderType()
);
assertEquals("US", returnedBankAccount.getCountryCode());
Expand Down