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

Create ConfirmSetupIntentParams.createWithoutPaymentMethod() #1731

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import com.stripe.android.model.ConfirmStripeIntentParams.Companion.API_PARAM_PA
import com.stripe.android.model.ConfirmStripeIntentParams.Companion.API_PARAM_RETURN_URL
import com.stripe.android.model.ConfirmStripeIntentParams.Companion.API_PARAM_USE_STRIPE_SDK

data class ConfirmSetupIntentParams private constructor(
data class ConfirmSetupIntentParams internal constructor(
override val clientSecret: String,
private val paymentMethodId: String?,
val paymentMethodCreateParams: PaymentMethodCreateParams?,
private val returnUrl: String?,
internal val paymentMethodId: String? = null,
mshafrir-stripe marked this conversation as resolved.
Show resolved Hide resolved
val paymentMethodCreateParams: PaymentMethodCreateParams? = null,
private val returnUrl: String? = null,
private val useStripeSdk: Boolean
) : ConfirmStripeIntentParams {

Expand All @@ -33,10 +33,12 @@ data class ConfirmSetupIntentParams private constructor(
* @return a String-keyed map
*/
override fun toParamMap(): Map<String, Any> {
val params = mutableMapOf(
val params = mapOf(
API_PARAM_CLIENT_SECRET to clientSecret,
API_PARAM_USE_STRIPE_SDK to useStripeSdk
)
).plus(
returnUrl?.let { mapOf(API_PARAM_RETURN_URL to it) }.orEmpty()
).toMutableMap()

if (paymentMethodCreateParams != null) {
params[API_PARAM_PAYMENT_METHOD_DATA] = paymentMethodCreateParams.toParamMap()
Expand All @@ -47,10 +49,6 @@ data class ConfirmSetupIntentParams private constructor(
params[API_PARAM_PAYMENT_METHOD_ID] = paymentMethodId
}

if (returnUrl != null) {
params[API_PARAM_RETURN_URL] = returnUrl
}

return params.toMap()
}

Expand All @@ -66,7 +64,6 @@ data class ConfirmSetupIntentParams private constructor(
return builder
}

@VisibleForTesting
internal class Builder internal constructor(
private val clientSecret: String
) : ObjectBuilder<ConfirmSetupIntentParams> {
Expand Down Expand Up @@ -109,14 +106,40 @@ data class ConfirmSetupIntentParams private constructor(
}

companion object {
/**
* Create the parameters necessary for confirming a SetupIntent, without specifying a payment method
* to attach to the SetupIntent. Only use this if a payment method has already been attached
* to the SetupIntent.
*
* @param clientSecret The client secret of this SetupIntent. Used for client-side retrieval using a publishable key.
* @param returnUrl The URL to redirect your customer back to after they authenticate on the payment method’s app or site.
* If you’d prefer to redirect to a mobile application, you can alternatively supply an application URI scheme.
* This parameter is only used for cards and other redirect-based payment methods.
*
* @return params that can be use to confirm a SetupIntent
*/
@JvmStatic
@JvmOverloads
fun createWithoutPaymentMethod(
clientSecret: String,
returnUrl: String? = null
): ConfirmSetupIntentParams {
return Builder(clientSecret)
.setReturnUrl(returnUrl)
.build()
}

/**
* Create the parameters necessary for confirming a SetupIntent while attaching a
* PaymentMethod that already exits.
*
* @param paymentMethodId the ID of the PaymentMethod that is being attached to the
* SetupIntent being confirmed
* @param clientSecret client secret from the SetupIntent being confirmed
* @param returnUrl the URL the customer should be redirected to after the authorization process
* @param paymentMethodId ID of the payment method (a PaymentMethod, Card, BankAccount, or
* saved Source object) to attach to this SetupIntent.
* @param clientSecret The client secret of this SetupIntent. Used for client-side retrieval using a publishable key.
* @param returnUrl The URL to redirect your customer back to after they authenticate on the payment method’s app or site.
* If you’d prefer to redirect to a mobile application, you can alternatively supply an application URI scheme.
* This parameter is only used for cards and other redirect-based payment methods.
*
* @return params that can be use to confirm a SetupIntent
*/
@JvmStatic
Expand All @@ -137,9 +160,11 @@ data class ConfirmSetupIntentParams private constructor(
*
* @param paymentMethodCreateParams the params to create a new PaymentMethod that will be
* attached to the SetupIntent being confirmed
* @param clientSecret client secret from the SetupIntent being confirmed
* @param returnUrl the URL the customer should be redirected to after the authorization
* process
* @param clientSecret The client secret of this SetupIntent. Used for client-side retrieval using a publishable key.
* @param returnUrl The URL to redirect your customer back to after they authenticate on the payment method’s app or site.
* If you’d prefer to redirect to a mobile application, you can alternatively supply an application URI scheme.
* This parameter is only used for cards and other redirect-based payment methods.
*
* @return params that can be use to confirm a SetupIntent
*/
@JvmOverloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ interface ConfirmStripeIntentParams : StripeParamsModel {
fun withShouldUseStripeSdk(shouldUseStripeSdk: Boolean): ConfirmStripeIntentParams

companion object {
const val API_PARAM_CLIENT_SECRET = "client_secret"
const val API_PARAM_RETURN_URL = "return_url"
const val API_PARAM_PAYMENT_METHOD_ID = "payment_method"
const val API_PARAM_PAYMENT_METHOD_DATA = "payment_method_data"
const val API_PARAM_USE_STRIPE_SDK = "use_stripe_sdk"
internal const val API_PARAM_CLIENT_SECRET: String = "client_secret"
internal const val API_PARAM_RETURN_URL: String = "return_url"
internal const val API_PARAM_PAYMENT_METHOD_ID: String = "payment_method"
internal const val API_PARAM_PAYMENT_METHOD_DATA: String = "payment_method_data"
internal const val API_PARAM_USE_STRIPE_SDK: String = "use_stripe_sdk"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ConfirmSetupIntentParamsTest {
@Test
fun shouldUseStripeSdk_withPaymentMethodCreateParams() {
val confirmSetupIntentParams = ConfirmSetupIntentParams.create(
PM_CREATE_PARAMS,
PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
"client_secret",
"return_url"
)
Expand All @@ -46,7 +46,8 @@ class ConfirmSetupIntentParamsTest {
@Test
fun toBuilder_withPaymentMethodCreateParams_shouldCreateEqualObject() {
val confirmSetupIntentParams = ConfirmSetupIntentParams.create(
PM_CREATE_PARAMS, "client_secret", "return_url")
PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
"client_secret", "return_url")
assertEquals(confirmSetupIntentParams,
confirmSetupIntentParams.toBuilder().build())
}
Expand All @@ -67,7 +68,7 @@ class ConfirmSetupIntentParamsTest {
@Test
fun create_withPaymentMethodCreateParams_shouldPopulateParamMapCorrectly() {
val confirmSetupIntentParams = ConfirmSetupIntentParams.create(
PM_CREATE_PARAMS,
PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
"client_secret", null
)
val params = confirmSetupIntentParams.toParamMap()
Expand All @@ -78,15 +79,10 @@ class ConfirmSetupIntentParamsTest {
assertNotNull(paymentMethodData["card"])
}

companion object {

private val PM_CREATE_PARAMS = PaymentMethodCreateParams.create(
PaymentMethodCreateParams.Card.Builder()
.setNumber("4242424242424242")
.setExpiryMonth(1)
.setExpiryYear(2024)
.setCvc("111")
.build(), null
)
@Test
fun create_withoutPaymentMethod() {
val params = ConfirmSetupIntentParams.createWithoutPaymentMethod("client_secret")
assertNull(params.paymentMethodCreateParams)
assertNull(params.paymentMethodId)
}
}