diff --git a/stripe/src/main/java/com/stripe/android/model/PaymentMethod.kt b/stripe/src/main/java/com/stripe/android/model/PaymentMethod.kt index 5f5bbcc95a2..75cf01c3b19 100644 --- a/stripe/src/main/java/com/stripe/android/model/PaymentMethod.kt +++ b/stripe/src/main/java/com/stripe/android/model/PaymentMethod.kt @@ -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 @@ -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 @@ -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 @@ -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, @@ -215,7 +223,8 @@ data class PaymentMethod internal constructor( fpx = fpx, ideal = ideal, sepaDebit = sepaDebit, - auBecsDebit = auBecsDebit + auBecsDebit = auBecsDebit, + bacsDebit = bacsDebit ) } } @@ -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? { diff --git a/stripe/src/main/java/com/stripe/android/model/parsers/PaymentMethodJsonParser.kt b/stripe/src/main/java/com/stripe/android/model/parsers/PaymentMethodJsonParser.kt index fd57ab76850..9cd0bc9c66d 100644 --- a/stripe/src/main/java/com/stripe/android/model/parsers/PaymentMethodJsonParser.kt +++ b/stripe/src/main/java/com/stripe/android/model/parsers/PaymentMethodJsonParser.kt @@ -54,6 +54,12 @@ internal class PaymentMethodJsonParser : ModelJsonParser { AuBecsDebitJsonParser().parse(it) } ) + PaymentMethod.Type.BacsDebit -> + builder.setBacsDebit( + json.optJSONObject(type.code)?.let { + BacsDebitJsonParser().parse(it) + } + ) } return builder.build() @@ -234,6 +240,22 @@ internal class PaymentMethodJsonParser : ModelJsonParser { } } + internal class BacsDebitJsonParser : ModelJsonParser { + 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" diff --git a/stripe/src/test/java/com/stripe/android/PaymentMethodEndToEndTest.kt b/stripe/src/test/java/com/stripe/android/PaymentMethodEndToEndTest.kt index 76817ca29ef..5e4a0fd451b 100644 --- a/stripe/src/test/java/com/stripe/android/PaymentMethodEndToEndTest.kt +++ b/stripe/src/test/java/com/stripe/android/PaymentMethodEndToEndTest.kt @@ -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 @@ -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" + ) + ) } } diff --git a/stripe/src/test/java/com/stripe/android/model/PaymentMethodFixtures.kt b/stripe/src/test/java/com/stripe/android/model/PaymentMethodFixtures.kt index 290b9966d1b..65a04709da6 100644 --- a/stripe/src/test/java/com/stripe/android/model/PaymentMethodFixtures.kt +++ b/stripe/src/test/java/com/stripe/android/model/PaymentMethodFixtures.kt @@ -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( """ { @@ -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, diff --git a/stripe/src/test/java/com/stripe/android/model/parsers/PaymentMethodJsonParserTest.kt b/stripe/src/test/java/com/stripe/android/model/parsers/PaymentMethodJsonParserTest.kt index 49e2b8cf684..4c2e8fe621d 100644 --- a/stripe/src/test/java/com/stripe/android/model/parsers/PaymentMethodJsonParserTest.kt +++ b/stripe/src/test/java/com/stripe/android/model/parsers/PaymentMethodJsonParserTest.kt @@ -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)