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

Add support for SOFORT PaymentMethod #2381

Merged
merged 1 commit into from
Apr 15, 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
22 changes: 18 additions & 4 deletions stripe/src/main/java/com/stripe/android/model/PaymentMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ data class PaymentMethod internal constructor(

@JvmField val auBecsDebit: AuBecsDebit? = null,

@JvmField val bacsDebit: BacsDebit? = null
@JvmField val bacsDebit: BacsDebit? = null,

@JvmField val sofort: Sofort? = null
) : StripeModel {

@Parcelize
Expand All @@ -119,11 +121,12 @@ data class PaymentMethod internal constructor(
) : Parcelable {
Card("card"),
CardPresent("card_present"),
Fpx("fpx", false),
Fpx("fpx", isReusable = false),
Ideal("ideal"),
SepaDebit("sepa_debit"),
AuBecsDebit("au_becs_debit"),
BacsDebit("bacs_debit");
BacsDebit("bacs_debit"),
Sofort("sofort", isReusable = false);

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

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

fun setSofort(sofort: Sofort?): Builder = apply {
this.sofort = sofort
}

override fun build(): PaymentMethod {
return PaymentMethod(
id = id,
Expand All @@ -224,7 +232,8 @@ data class PaymentMethod internal constructor(
ideal = ideal,
sepaDebit = sepaDebit,
auBecsDebit = auBecsDebit,
bacsDebit = bacsDebit
bacsDebit = bacsDebit,
sofort = sofort
)
}
}
Expand Down Expand Up @@ -644,6 +653,11 @@ data class PaymentMethod internal constructor(
@JvmField val sortCode: String?
) : StripeModel

@Parcelize
data class Sofort internal constructor(
@JvmField val country: String?
) : StripeModel

companion object {
@JvmStatic
fun fromJson(paymentMethod: JSONObject?): PaymentMethod? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ sealed class PaymentMethodOptionsParams(
}
}

// TODO(mshafrir-stripe): extend `PaymentMethodOptionsParams` once PaymentMethod.Type is created
@Parcelize
data class Sofort(
var preferredLanguage: String? = null
) : StripeParamsModel, Parcelable {
) : PaymentMethodOptionsParams(PaymentMethod.Type.Sofort) {
override fun toParamMap(): Map<String, Any> {
return preferredLanguage?.let {
mapOf(PARAM_PREFERRED_LANGUAGE to it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ internal class PaymentMethodJsonParser : ModelJsonParser<PaymentMethod> {
BacsDebitJsonParser().parse(it)
}
)
PaymentMethod.Type.Sofort ->
builder.setSofort(
json.optJSONObject(type.code)?.let {
SofortJsonParser().parse(it)
}
)
}

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

internal class SofortJsonParser : ModelJsonParser<PaymentMethod.Sofort> {
override fun parse(json: JSONObject): PaymentMethod.Sofort {
return PaymentMethod.Sofort(
country = StripeJsonUtils.optString(json, FIELD_COUNTRY)
)
}

private companion object {
private const val FIELD_COUNTRY = "country"
}
}

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 @@ -38,7 +38,13 @@ class PaymentMethodEndToEndTest {
val paymentMethod =
Stripe(context, ApiKeyFixtures.SOFORT_PUBLISHABLE_KEY)
.createPaymentMethodSynchronous(params)
assertThat(paymentMethod)
.isNotNull()
assertThat(paymentMethod?.type)
.isEqualTo(PaymentMethod.Type.Sofort)
assertThat(paymentMethod?.sofort)
.isEqualTo(
PaymentMethod.Sofort(
country = "DE"
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal object PaymentMethodCreateParamsFixtures {

internal val SOFORT = PaymentMethodCreateParams.create(
sofort = PaymentMethodCreateParams.Sofort(
country = "de"
country = "DE"
),
billingDetails = BILLING_DETAILS
)
Expand Down