Skip to content

Commit

Permalink
Add support for BACS Debit Payment Method (#2335)
Browse files Browse the repository at this point in the history
See `PaymentMethod.bacsDebit`
  • Loading branch information
mshafrir-stripe authored Mar 31, 2020
1 parent ca934fd commit 7780f2d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
22 changes: 19 additions & 3 deletions stripe/src/main/java/com/stripe/android/model/PaymentMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ data class PaymentMethod internal constructor(
*/
@JvmField val sepaDebit: SepaDebit? = null,

@JvmField val auBecsDebit: AuBecsDebit? = null
@JvmField val auBecsDebit: AuBecsDebit? = null,

@JvmField val bacsDebit: BacsDebit? = null
) : StripeModel {

@Parcelize
Expand All @@ -120,7 +122,8 @@ data class PaymentMethod internal constructor(
Fpx("fpx", false),
Ideal("ideal"),
SepaDebit("sepa_debit"),
AuBecsDebit("au_becs_debit");
AuBecsDebit("au_becs_debit"),
BacsDebit("bacs_debit");

override fun toString(): String {
return code
Expand Down Expand Up @@ -148,6 +151,7 @@ data class PaymentMethod internal constructor(
private var fpx: Fpx? = null
private var sepaDebit: SepaDebit? = null
private var auBecsDebit: AuBecsDebit? = null
private var bacsDebit: BacsDebit? = null

fun setId(id: String?): Builder = apply {
this.id = id
Expand Down Expand Up @@ -201,6 +205,10 @@ data class PaymentMethod internal constructor(
this.auBecsDebit = auBecsDebit
}

fun setBacsDebit(bacsDebit: BacsDebit?): Builder = apply {
this.bacsDebit = bacsDebit
}

override fun build(): PaymentMethod {
return PaymentMethod(
id = id,
Expand All @@ -215,7 +223,8 @@ data class PaymentMethod internal constructor(
fpx = fpx,
ideal = ideal,
sepaDebit = sepaDebit,
auBecsDebit = auBecsDebit
auBecsDebit = auBecsDebit,
bacsDebit = bacsDebit
)
}
}
Expand Down Expand Up @@ -628,6 +637,13 @@ data class PaymentMethod internal constructor(
@JvmField val last4: String?
) : StripeModel

@Parcelize
data class BacsDebit internal constructor(
@JvmField val fingerprint: String?,
@JvmField val last4: String?,
@JvmField val sortCode: String?
) : StripeModel

companion object {
@JvmStatic
fun fromJson(paymentMethod: JSONObject?): PaymentMethod? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ internal class PaymentMethodJsonParser : ModelJsonParser<PaymentMethod> {
AuBecsDebitJsonParser().parse(it)
}
)
PaymentMethod.Type.BacsDebit ->
builder.setBacsDebit(
json.optJSONObject(type.code)?.let {
BacsDebitJsonParser().parse(it)
}
)
}

return builder.build()
Expand Down Expand Up @@ -234,6 +240,22 @@ internal class PaymentMethodJsonParser : ModelJsonParser<PaymentMethod> {
}
}

internal class BacsDebitJsonParser : ModelJsonParser<PaymentMethod.BacsDebit> {
override fun parse(json: JSONObject): PaymentMethod.BacsDebit? {
return PaymentMethod.BacsDebit(
fingerprint = StripeJsonUtils.optString(json, FIELD_FINGERPRINT),
last4 = StripeJsonUtils.optString(json, FIELD_LAST4),
sortCode = StripeJsonUtils.optString(json, FIELD_SORT_CODE)
)
}

private companion object {
private const val FIELD_FINGERPRINT = "fingerprint"
private const val FIELD_LAST4 = "last4"
private const val FIELD_SORT_CODE = "sort_code"
}
}

private companion object {
private const val FIELD_ID = "id"
private const val FIELD_BILLING_DETAILS = "billing_details"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.android
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParamsFixtures
import kotlin.test.Test
import org.junit.runner.RunWith
Expand All @@ -19,7 +20,15 @@ class PaymentMethodEndToEndTest {
val paymentMethod =
Stripe(context, ApiKeyFixtures.BACS_PUBLISHABLE_KEY)
.createPaymentMethodSynchronous(params)
assertThat(paymentMethod)
.isNotNull()
assertThat(paymentMethod?.type)
.isEqualTo(PaymentMethod.Type.BacsDebit)
assertThat(paymentMethod?.bacsDebit)
.isEqualTo(
PaymentMethod.BacsDebit(
fingerprint = "UkSG0HfCGxxrja1H",
last4 = "2345",
sortCode = "108800"
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ internal object PaymentMethodFixtures {
metadata = emptyMap()
)

val BACS_DEBIT_PAYMENT_METHOD = PaymentMethod(
id = "pm_1GSREqL6pqDH",
created = 1585588648L,
liveMode = false,
type = PaymentMethod.Type.BacsDebit,
billingDetails = BILLING_DETAILS,
bacsDebit = PaymentMethod.BacsDebit(
fingerprint = "UkSG0Hf",
last4 = "2345",
sortCode = "108800"
),
metadata = emptyMap()
)

val SEPA_DEBIT_JSON = JSONObject(
"""
{
Expand Down Expand Up @@ -300,6 +314,38 @@ internal object PaymentMethodFixtures {
""".trimIndent()
)

val BACS_DEBIT_JSON = JSONObject(
"""
{
"id": "pm_1GSREqL6pqDH",
"object": "payment_method",
"bacs_debit": {
"fingerprint": "UkSG0Hf",
"last4": "2345",
"sort_code": "108800"
},
"billing_details": {
"address": {
"city": "San Francisco",
"country": "US",
"line1": "510 Townsend St",
"line2": null,
"postal_code": "94103",
"state": "CA"
},
"email": "patrick@example.com",
"name": "Patrick",
"phone": "123-456-7890"
},
"created": 1585588648,
"customer": null,
"livemode": false,
"metadata": {},
"type": "bacs_debit"
}
""".trimIndent()
)

val CARD_PAYMENT_METHODS = listOf(
PaymentMethod(
type = PaymentMethod.Type.Card,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class PaymentMethodJsonParserTest {
.isEqualTo(PaymentMethodFixtures.AU_BECS_DEBIT_PAYMENT_METHOD)
}

@Test
fun parse_withBacsDebit_shouldCreateExpectedObject() {
assertThat(PaymentMethodJsonParser().parse(PaymentMethodFixtures.BACS_DEBIT_JSON))
.isEqualTo(PaymentMethodFixtures.BACS_DEBIT_PAYMENT_METHOD)
}

@Test
fun parse_withSepaDebit_shouldCreateExpectedObject() {
assertThat(PaymentMethodFixtures.SEPA_DEBIT_PAYMENT_METHOD)
Expand Down

0 comments on commit 7780f2d

Please sign in to comment.