Skip to content

Commit

Permalink
Add support for Bank Account as source on Customer object (#2641)
Browse files Browse the repository at this point in the history
Fixes #2601
  • Loading branch information
mshafrir-stripe authored Jul 10, 2020
1 parent 045a4c4 commit a0c91b8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 18 deletions.
4 changes: 2 additions & 2 deletions stripe/src/main/java/com/stripe/android/model/BankAccount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class BankAccount internal constructor(
*
* [id](https://stripe.com/docs/api/customer_bank_accounts/object#customer_bank_account_object-id)
*/
val id: String? = null,
override val id: String? = null,

/**
* The name of the person or business that owns the bank account.
Expand Down Expand Up @@ -91,7 +91,7 @@ data class BankAccount internal constructor(
* [status](https://stripe.com/docs/api/customer_bank_accounts/object#customer_bank_account_object-status)
*/
val status: Status? = null
) : StripeModel {
) : StripeModel, StripePaymentSource {

enum class Type(internal val code: String) {
Company("company"),
Expand Down
2 changes: 0 additions & 2 deletions stripe/src/main/java/com/stripe/android/model/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,6 @@ data class Card internal constructor(
}

companion object {
internal const val OBJECT_TYPE = "card"

/**
* Create a Card object from a raw JSON string.
*
Expand Down
2 changes: 0 additions & 2 deletions stripe/src/main/java/com/stripe/android/model/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,6 @@ data class Source internal constructor(
) : StripeModel

companion object {
internal const val OBJECT_TYPE = "source"

internal const val EURO: String = "eur"
internal const val USD: String = "usd"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class CustomerJsonParser : ModelJsonParser<Customer> {
val hasMore: Boolean
val totalCount: Int?
val url: String?
val sources: List<CustomerSource>?
val sources: List<CustomerSource>
if (sourcesJson != null && VALUE_LIST == StripeJsonUtils.optString(sourcesJson, FIELD_OBJECT)) {
hasMore = StripeJsonUtils.optBoolean(sourcesJson, FIELD_HAS_MORE)
totalCount = StripeJsonUtils.optInteger(sourcesJson, FIELD_TOTAL_COUNT)
Expand All @@ -44,7 +44,15 @@ internal class CustomerJsonParser : ModelJsonParser<Customer> {
sources = emptyList()
}

return Customer(id, defaultSource, shippingInformation, sources, hasMore, totalCount, url)
return Customer(
id = id,
defaultSource = defaultSource,
shippingInformation = shippingInformation,
sources = sources,
hasMore = hasMore,
totalCount = totalCount,
url = url
)
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package com.stripe.android.model.parsers

import com.stripe.android.model.Card
import com.stripe.android.model.CustomerSource
import com.stripe.android.model.Source
import com.stripe.android.model.StripeJsonUtils
import com.stripe.android.model.StripeJsonUtils.optString
import com.stripe.android.model.StripePaymentSource
import org.json.JSONObject

internal class CustomerSourceJsonParser : ModelJsonParser<CustomerSource> {
override fun parse(json: JSONObject): CustomerSource? {
val sourceObject: StripePaymentSource? =
when (StripeJsonUtils.optString(json, "object")) {
Card.OBJECT_TYPE -> CardJsonParser().parse(json)
Source.OBJECT_TYPE -> SourceJsonParser().parse(json)
when (optString(json, "object")) {
"card" -> CardJsonParser().parse(json)
"source" -> SourceJsonParser().parse(json)
"bank_account" -> BankAccountJsonParser().parse(json)
else -> null
}

return if (sourceObject == null) {
null
} else {
CustomerSource(sourceObject)
return sourceObject?.let {
CustomerSource(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.stripe.android.model.parsers

import com.google.common.truth.Truth.assertThat
import com.stripe.android.model.BankAccount
import com.stripe.android.model.Customer
import com.stripe.android.model.CustomerSource
import kotlin.test.Test
import org.json.JSONObject

class CustomerJsonParserTest {

@Test
fun `should correctly parse Customer with BankAccount source`() {
assertThat(
CustomerJsonParser()
.parse(CUSTOMER_JSON)
).isEqualTo(
Customer(
id = "cus_HcLIwF3BCi",
defaultSource = "ba_1H3NOMCRMbs6FrXfahj",
shippingInformation = null,
sources = listOf(
CustomerSource(
BankAccount(
id = "ba_1H3NOMCRMbs6FrXfahj",
accountHolderName = "Test Bank Account",
accountHolderType = BankAccount.Type.Individual,
bankName = "STRIPE TEST BANK",
countryCode = "US",
currency = "usd",
fingerprint = "wxXSAD5idPUzgBEz",
last4 = "6789",
routingNumber = "110000000",
status = BankAccount.Status.New
)
)
),
hasMore = false,
totalCount = 1,
url = "/v1/customers/cus_HcLIwF3BCi/sources"
)
)
}

private companion object {
private val CUSTOMER_JSON = JSONObject(
"""
{
"id": "cus_HcLIwF3BCi",
"object": "customer",
"created": 1594327465,
"default_source": "ba_1H3NOMCRMbs6FrXfahj",
"description": "mobile SDK example customer",
"email": null,
"livemode": false,
"shipping": null,
"sources": {
"object": "list",
"data": [{
"id": "ba_1H3NOMCRMbs6FrXfahj",
"object": "bank_account",
"account_holder_name": "Test Bank Account",
"account_holder_type": "individual",
"bank_name": "STRIPE TEST BANK",
"country": "US",
"currency": "usd",
"customer": "cus_HcLIwF3BCiRp0t",
"fingerprint": "wxXSAD5idPUzgBEz",
"last4": "6789",
"metadata": {},
"routing_number": "110000000",
"status": "new"
}],
"has_more": false,
"total_count": 1,
"url": "\/v1\/customers\/cus_HcLIwF3BCi\/sources"
}
}
""".trimIndent()
)
}
}

0 comments on commit a0c91b8

Please sign in to comment.