Skip to content

Commit

Permalink
Add SOFORT to PaymentMethodCreateParams
Browse files Browse the repository at this point in the history
ANDROID-508
  • Loading branch information
mshafrir-stripe committed Apr 15, 2020
1 parent 6cb4105 commit a3434a0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.android.model
import android.os.Parcelable
import com.stripe.android.ObjectBuilder
import com.stripe.android.Stripe
import java.util.Locale
import kotlinx.android.parcel.Parcelize
import org.json.JSONException
import org.json.JSONObject
Expand All @@ -19,13 +20,17 @@ import org.json.JSONObject
@Parcelize
data class PaymentMethodCreateParams internal constructor(
internal val type: Type,

private val card: Card? = null,
private val ideal: Ideal? = null,
private val fpx: Fpx? = null,
private val sepaDebit: SepaDebit? = null,
private val auBecsDebit: AuBecsDebit? = null,
private val bacsDebit: BacsDebit? = null,
private val sofort: Sofort? = null,

private val billingDetails: PaymentMethod.BillingDetails? = null,

private val metadata: Map<String, String>? = null,
private val productUsage: Set<String> = emptySet()
) : StripeParamsModel, Parcelable {
Expand Down Expand Up @@ -108,6 +113,17 @@ data class PaymentMethodCreateParams internal constructor(
metadata = metadata
)

private constructor(
sofort: Sofort,
billingDetails: PaymentMethod.BillingDetails?,
metadata: Map<String, String>?
) : this(
type = Type.Sofort,
sofort = sofort,
billingDetails = billingDetails,
metadata = metadata
)

override fun toParamMap(): Map<String, Any> {
return mapOf(
PARAM_TYPE to type.code
Expand All @@ -124,6 +140,7 @@ data class PaymentMethodCreateParams internal constructor(
Type.SepaDebit -> sepaDebit?.toParamMap().orEmpty()
Type.AuBecsDebit -> auBecsDebit?.toParamMap().orEmpty()
Type.BacsDebit -> bacsDebit?.toParamMap().orEmpty()
Type.Sofort -> sofort?.toParamMap().orEmpty()
}
)
).plus(
Expand All @@ -139,7 +156,8 @@ data class PaymentMethodCreateParams internal constructor(
Fpx("fpx"),
SepaDebit("sepa_debit", true),
AuBecsDebit("au_becs_debit", true),
BacsDebit("bacs_debit", true)
BacsDebit("bacs_debit", true),
Sofort("sofort")
}

@Parcelize
Expand Down Expand Up @@ -347,6 +365,21 @@ data class PaymentMethodCreateParams internal constructor(
}
}

@Parcelize
data class Sofort(
internal var country: String
) : StripeParamsModel, Parcelable {
override fun toParamMap(): Map<String, Any> {
return mapOf(
PARAM_COUNTRY to country.toUpperCase(Locale.ROOT)
)
}

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

companion object {
private const val PARAM_TYPE = "type"
private const val PARAM_BILLING_DETAILS = "billing_details"
Expand Down Expand Up @@ -418,7 +451,7 @@ data class PaymentMethodCreateParams internal constructor(
}

/**
* @return params for creating a `PaymentMethod.Type.BacsDebit` payment method
* @return params for creating a [PaymentMethod.Type.BacsDebit] payment method
*/
@JvmStatic
@JvmOverloads
Expand All @@ -430,6 +463,19 @@ data class PaymentMethodCreateParams internal constructor(
return PaymentMethodCreateParams(bacsDebit, billingDetails, metadata)
}

/**
* @return params for creating a `SOFORT` payment method
*/
@JvmStatic
@JvmOverloads
internal fun create(
sofort: Sofort,
billingDetails: PaymentMethod.BillingDetails? = null,
metadata: Map<String, String>? = null
): PaymentMethodCreateParams {
return PaymentMethodCreateParams(sofort, billingDetails, metadata)
}

/**
* @param googlePayPaymentData a [JSONObject] derived from Google Pay's
* [PaymentData#toJson()](https://developers.google.com/pay/api/android/reference/client#tojson)
Expand Down
1 change: 1 addition & 0 deletions stripe/src/test/java/com/stripe/android/ApiKeyFixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ internal object ApiKeyFixtures {
const val AU_BECS_DEBIT_PUBLISHABLE_KEY = "pk_test_Bfx5IwoTeK7kUbfS15dcgTrs00beFpWwto"
const val KLARNA_PUBLISHABLE_KEY = "pk_test_8JUGw3jTOFqWQOt5nr2pjzF9"
const val BACS_PUBLISHABLE_KEY = "pk_test_z6Ct4bpx0NUjHii0rsi4XZBf00jmM8qA28"
const val SOFORT_PUBLISHABLE_KEY = "pk_test_vOo1umqsYxSrP5UXfOeL3ecm"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ class PaymentMethodEndToEndTest {
)
)
}

@Test
fun createPaymentMethod_withSofort_shouldCreateObject() {
val params = PaymentMethodCreateParamsFixtures.SOFORT
val paymentMethod =
Stripe(context, ApiKeyFixtures.SOFORT_PUBLISHABLE_KEY)
.createPaymentMethodSynchronous(params)
assertThat(paymentMethod)
.isNotNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ internal object PaymentMethodCreateParamsFixtures {
billingDetails = BILLING_DETAILS
)

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

@JvmStatic
fun createWith(metadata: Map<String, String>): PaymentMethodCreateParams {
return PaymentMethodCreateParams.create(
Expand Down

0 comments on commit a3434a0

Please sign in to comment.