Skip to content

Commit

Permalink
Expand payment_method when returning Payment Intent & Setup Intent co…
Browse files Browse the repository at this point in the history
…nfirmation result (#2393)

Summary
This gives the user access to the full `PaymentMethod` object after
Payment Intent or Setup Intent confirmation is completed. This object
may have been created when confirming the Intent.

Motivation
Will be used in a follow-up PR

Testing
Added tests and manually verified
  • Loading branch information
mshafrir-stripe authored Apr 16, 2020
1 parent 6fb6fd6 commit d21b7fa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
39 changes: 30 additions & 9 deletions stripe/src/main/java/com/stripe/android/StripeApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
APIConnectionException::class, APIException::class)
override fun retrievePaymentIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String>
): PaymentIntent? {
val paymentIntentId = PaymentIntent.ClientSecret(clientSecret).paymentIntentId

Expand All @@ -159,7 +160,7 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
apiRequestFactory.createGet(
getRetrievePaymentIntentUrl(paymentIntentId),
options,
createClientSecretParam(clientSecret)
createClientSecretParam(clientSecret, expandFields)
),
PaymentIntentJsonParser()
)
Expand Down Expand Up @@ -255,7 +256,8 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
APIConnectionException::class, APIException::class)
override fun retrieveSetupIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String>
): SetupIntent? {
val setupIntentId = SetupIntent.ClientSecret(clientSecret).setupIntentId

Expand All @@ -272,7 +274,7 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
apiRequestFactory.createGet(
getRetrieveSetupIntentUrl(setupIntentId),
options,
createClientSecretParam(clientSecret)
createClientSecretParam(clientSecret, expandFields)
),
SetupIntentJsonParser()
)
Expand Down Expand Up @@ -312,10 +314,16 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
override fun retrieveIntent(
clientSecret: String,
options: ApiRequest.Options,
expandFields: List<String>,
callback: ApiResultCallback<StripeIntent>
) {
RetrieveIntentTask(this, clientSecret, options, callback)
.execute()
RetrieveIntentTask(
this,
clientSecret,
options,
expandFields,
callback
).execute()
}

override fun cancelIntent(
Expand Down Expand Up @@ -1024,8 +1032,12 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
)
}

private fun createClientSecretParam(clientSecret: String): Map<String, String> {
private fun createClientSecretParam(
clientSecret: String,
expandFields: List<String>
): Map<String, Any> {
return mapOf("client_secret" to clientSecret)
.plus(createExpandParam(expandFields))
}

private class Start3ds2AuthTask constructor(
Expand Down Expand Up @@ -1057,16 +1069,25 @@ internal class StripeApiRepository @JvmOverloads internal constructor(
private val stripeRepository: StripeRepository,
private val clientSecret: String,
private val requestOptions: ApiRequest.Options,
private val expandFields: List<String>,
callback: ApiResultCallback<StripeIntent>
) : ApiOperation<StripeIntent>(callback = callback) {

@Throws(StripeException::class)
override suspend fun getResult(): StripeIntent? {
return when {
clientSecret.startsWith("pi_") ->
stripeRepository.retrievePaymentIntent(clientSecret, requestOptions)
stripeRepository.retrievePaymentIntent(
clientSecret,
requestOptions,
expandFields
)
clientSecret.startsWith("seti_") ->
stripeRepository.retrieveSetupIntent(clientSecret, requestOptions)
stripeRepository.retrieveSetupIntent(
clientSecret,
requestOptions,
expandFields
)
else -> null
}
}
Expand Down
26 changes: 18 additions & 8 deletions stripe/src/main/java/com/stripe/android/StripePaymentController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ internal class StripePaymentController internal constructor(
clientSecret: String,
requestOptions: ApiRequest.Options
) {
stripeRepository.retrieveIntent(clientSecret, requestOptions,
object : ApiResultCallback<StripeIntent> {
stripeRepository.retrieveIntent(
clientSecret,
requestOptions,
callback = object : ApiResultCallback<StripeIntent> {
override fun onSuccess(result: StripeIntent) {
handleNextAction(host, result, requestOptions)
}
Expand Down Expand Up @@ -207,8 +209,11 @@ internal class StripePaymentController internal constructor(
val sourceId = result.sourceId.orEmpty()
@StripeIntentResult.Outcome val flowOutcome = result.flowOutcome

stripeRepository.retrieveIntent(getClientSecret(data), requestOptions,
createPaymentIntentCallback(
stripeRepository.retrieveIntent(
getClientSecret(data),
requestOptions,
expandFields = EXPAND_PAYMENT_METHOD,
callback = createPaymentIntentCallback(
requestOptions, flowOutcome, sourceId, shouldCancelSource, callback
)
)
Expand Down Expand Up @@ -239,8 +244,11 @@ internal class StripePaymentController internal constructor(
val sourceId = result.sourceId.orEmpty()
@StripeIntentResult.Outcome val flowOutcome = result.flowOutcome

stripeRepository.retrieveIntent(getClientSecret(data), requestOptions,
createSetupIntentCallback(
stripeRepository.retrieveIntent(
getClientSecret(data),
requestOptions,
expandFields = EXPAND_PAYMENT_METHOD,
callback = createSetupIntentCallback(
requestOptions, flowOutcome, sourceId, shouldCancelSource, callback
)
)
Expand Down Expand Up @@ -529,13 +537,13 @@ internal class StripePaymentController internal constructor(
stripeRepository.confirmPaymentIntent(
params,
requestOptions,
expandFields = listOf("payment_method")
expandFields = EXPAND_PAYMENT_METHOD
)
is ConfirmSetupIntentParams ->
stripeRepository.confirmSetupIntent(
params,
requestOptions,
expandFields = listOf("payment_method")
expandFields = EXPAND_PAYMENT_METHOD
)
else -> null
}
Expand Down Expand Up @@ -963,5 +971,7 @@ internal class StripePaymentController internal constructor(
internal fun getClientSecret(data: Intent): String {
return requireNotNull(PaymentController.Result.fromIntent(data)?.clientSecret)
}

private val EXPAND_PAYMENT_METHOD = listOf("payment_method")
}
}
7 changes: 5 additions & 2 deletions stripe/src/main/java/com/stripe/android/StripeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ internal interface StripeRepository {
APIConnectionException::class, APIException::class)
fun retrievePaymentIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String> = emptyList()
): PaymentIntent?

@Throws(AuthenticationException::class, InvalidRequestException::class,
Expand All @@ -65,7 +66,8 @@ internal interface StripeRepository {
APIConnectionException::class, APIException::class)
fun retrieveSetupIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String> = emptyList()
): SetupIntent?

@Throws(AuthenticationException::class, InvalidRequestException::class,
Expand All @@ -79,6 +81,7 @@ internal interface StripeRepository {
fun retrieveIntent(
clientSecret: String,
options: ApiRequest.Options,
expandFields: List<String> = emptyList(),
callback: ApiResultCallback<StripeIntent>
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ internal abstract class AbsFakeStripeRepository : StripeRepository {

override fun retrievePaymentIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String>
): PaymentIntent? {
return null
}
Expand All @@ -55,7 +56,8 @@ internal abstract class AbsFakeStripeRepository : StripeRepository {

override fun retrieveSetupIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String>
): SetupIntent? {
return null
}
Expand All @@ -71,6 +73,7 @@ internal abstract class AbsFakeStripeRepository : StripeRepository {
override fun retrieveIntent(
clientSecret: String,
options: ApiRequest.Options,
expandFields: List<String>,
callback: ApiResultCallback<StripeIntent>
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ class StripePaymentControllerTest {
verify(stripeRepository).retrieveIntent(
eq(clientSecret),
eq(REQUEST_OPTIONS),
eq(listOf("payment_method")),
apiResultStripeIntentArgumentCaptor.capture()
)
// return a PaymentIntent in `requires_action` state
Expand Down Expand Up @@ -777,7 +778,8 @@ class StripePaymentControllerTest {
private class FakeStripeRepository : AbsFakeStripeRepository() {
override fun retrieveSetupIntent(
clientSecret: String,
options: ApiRequest.Options
options: ApiRequest.Options,
expandFields: List<String>
): SetupIntent {
return SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT
}
Expand All @@ -802,9 +804,10 @@ class StripePaymentControllerTest {
override fun retrieveIntent(
clientSecret: String,
options: ApiRequest.Options,
expandFields: List<String>,
callback: ApiResultCallback<StripeIntent>
) {
super.retrieveIntent(clientSecret, options, callback)
super.retrieveIntent(clientSecret, options, expandFields, callback)
callback.onSuccess(SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT)
}

Expand Down

0 comments on commit d21b7fa

Please sign in to comment.