@@ -5,15 +5,21 @@ import androidx.compose.ui.test.hasTestTag
5
5
import androidx.test.core.app.ApplicationProvider
6
6
import com.google.testing.junit.testparameterinjector.TestParameter
7
7
import com.google.testing.junit.testparameterinjector.TestParameterInjector
8
+ import com.stripe.android.core.utils.urlEncode
8
9
import com.stripe.android.model.PaymentMethod
9
10
import com.stripe.android.networktesting.RequestMatchers
11
+ import com.stripe.android.networktesting.RequestMatchers.bodyPart
12
+ import com.stripe.android.networktesting.RequestMatchers.method
13
+ import com.stripe.android.networktesting.RequestMatchers.path
14
+ import com.stripe.android.networktesting.testBodyFromFile
10
15
import com.stripe.android.paymentsheet.ui.SAVED_PAYMENT_OPTION_TAB_LAYOUT_TEST_TAG
11
16
import com.stripe.android.paymentsheet.utils.PaymentSheetLayoutType
12
17
import com.stripe.android.paymentsheet.utils.PaymentSheetLayoutTypeProvider
13
18
import com.stripe.android.paymentsheet.utils.ProductIntegrationTestRunnerContext
14
19
import com.stripe.android.paymentsheet.utils.ProductIntegrationType
15
20
import com.stripe.android.paymentsheet.utils.ProductIntegrationTypeProvider
16
21
import com.stripe.android.paymentsheet.utils.TestRules
22
+ import com.stripe.android.paymentsheet.utils.assertCompleted
17
23
import com.stripe.android.paymentsheet.utils.runProductIntegrationTest
18
24
import com.stripe.android.testing.PaymentMethodFactory
19
25
import org.json.JSONArray
@@ -164,8 +170,106 @@ internal class DefaultPaymentMethodsTest {
164
170
testContext.markTestSucceeded()
165
171
}
166
172
173
+ @Test
174
+ fun setNewCardAsDefault_sendsSetAsDefaultParamInConfirmCall () = runProductIntegrationTest(
175
+ networkRule = networkRule,
176
+ integrationType = integrationType,
177
+ resultCallback = ::assertCompleted,
178
+ ) { testContext ->
179
+ val paymentSheetPage = PaymentSheetPage (composeTestRule)
180
+
181
+ enqueueElementsSessionResponse()
182
+
183
+ launch(
184
+ testContext = testContext,
185
+ paymentMethodLayout = layoutType.paymentMethodLayout,
186
+ hasSavedPaymentMethods = false ,
187
+ )
188
+
189
+ paymentSheetPage.fillOutCardDetails()
190
+ paymentSheetPage.checkSaveForFuture()
191
+ paymentSheetPage.checkSetAsDefaultCheckbox()
192
+
193
+ enqueuePaymentIntentConfirmWithExpectedSetAsDefault(setAsDefaultValue = true )
194
+
195
+ paymentSheetPage.clickPrimaryButton()
196
+ }
197
+
198
+ @Test
199
+ fun payWithNewCard_dontCheckSetAsDefault_sendsSetAsDefaultAsFalseParamInConfirmCall () = runProductIntegrationTest(
200
+ networkRule = networkRule,
201
+ integrationType = integrationType,
202
+ resultCallback = ::assertCompleted,
203
+ ) { testContext ->
204
+ val paymentSheetPage = PaymentSheetPage (composeTestRule)
205
+
206
+ enqueueElementsSessionResponse()
207
+
208
+ launch(
209
+ testContext = testContext,
210
+ paymentMethodLayout = layoutType.paymentMethodLayout,
211
+ hasSavedPaymentMethods = false ,
212
+ )
213
+
214
+ paymentSheetPage.fillOutCardDetails()
215
+ paymentSheetPage.checkSaveForFuture()
216
+
217
+ enqueuePaymentIntentConfirmWithExpectedSetAsDefault(setAsDefaultValue = false )
218
+
219
+ paymentSheetPage.clickPrimaryButton()
220
+ }
221
+
222
+ @Test
223
+ fun payWithNewCard_uncheckSaveForFuture_doesNotSendSetAsDefaultInConfirmCall () = runProductIntegrationTest(
224
+ networkRule = networkRule,
225
+ integrationType = integrationType,
226
+ resultCallback = ::assertCompleted,
227
+ ) { testContext ->
228
+ val paymentSheetPage = PaymentSheetPage (composeTestRule)
229
+
230
+ enqueueElementsSessionResponse()
231
+
232
+ launch(
233
+ testContext = testContext,
234
+ paymentMethodLayout = layoutType.paymentMethodLayout,
235
+ hasSavedPaymentMethods = false ,
236
+ )
237
+
238
+ paymentSheetPage.fillOutCardDetails()
239
+ paymentSheetPage.checkSaveForFuture()
240
+ paymentSheetPage.checkSetAsDefaultCheckbox()
241
+
242
+ // Un-check save for future -- this will hide the set as default checkbox
243
+ paymentSheetPage.checkSaveForFuture()
244
+
245
+ enqueuePaymentIntentConfirmWithoutSetAsDefault()
246
+
247
+ paymentSheetPage.clickPrimaryButton()
248
+ }
249
+
250
+ private fun enqueuePaymentIntentConfirmWithoutSetAsDefault () {
251
+ return networkRule.enqueue(
252
+ method(" POST" ),
253
+ path(" /v1/payment_intents/pi_example/confirm" ),
254
+ bodyPart(urlEncode(" payment_method_data[allow_redisplay]" ), " unspecified" ),
255
+ ) { response ->
256
+ response.testBodyFromFile(" payment-intent-confirm.json" )
257
+ }
258
+ }
259
+
260
+ private fun enqueuePaymentIntentConfirmWithExpectedSetAsDefault (setAsDefaultValue : Boolean ) {
261
+ return networkRule.enqueue(
262
+ method(" POST" ),
263
+ path(" /v1/payment_intents/pi_example/confirm" ),
264
+ bodyPart(urlEncode(" payment_method_data[allow_redisplay]" ), " always" ),
265
+ bodyPart(urlEncode(" set_as_default_payment_method" ), setAsDefaultValue.toString())
266
+ ) { response ->
267
+ response.testBodyFromFile(" payment-intent-confirm.json" )
268
+ }
269
+ }
270
+
167
271
private fun enqueueElementsSessionResponse (
168
- cards : List <PaymentMethod >,
272
+ cards : List <PaymentMethod > = emptyList() ,
169
273
setAsDefaultFeatureEnabled : Boolean = true,
170
274
defaultPaymentMethod : String? = null,
171
275
) {
@@ -187,6 +291,7 @@ internal class DefaultPaymentMethodsTest {
187
291
private fun launch (
188
292
testContext : ProductIntegrationTestRunnerContext ,
189
293
paymentMethodLayout : PaymentSheet .PaymentMethodLayout ,
294
+ hasSavedPaymentMethods : Boolean = true,
190
295
) {
191
296
testContext.launch(
192
297
configuration = PaymentSheet .Configuration (
@@ -199,7 +304,10 @@ internal class DefaultPaymentMethodsTest {
199
304
),
200
305
)
201
306
202
- if (paymentMethodLayout == PaymentSheet .PaymentMethodLayout .Horizontal ) {
307
+ if (
308
+ paymentMethodLayout == PaymentSheet .PaymentMethodLayout .Horizontal &&
309
+ hasSavedPaymentMethods
310
+ ) {
203
311
composeTestRule.waitUntil(timeoutMillis = 5_000 ) {
204
312
composeTestRule
205
313
.onAllNodes(hasTestTag(SAVED_PAYMENT_OPTION_TAB_LAYOUT_TEST_TAG ))
@@ -238,10 +346,6 @@ internal class DefaultPaymentMethodsTest {
238
346
"ordered_payment_method_types_and_wallets": [
239
347
"card"
240
348
],
241
- "card_brand_choice": {
242
- "eligible": true,
243
- "preferred_networks": ["cartes_bancaires"]
244
- },
245
349
"customer": {
246
350
"payment_methods": $cardsStringified ,
247
351
"customer_session": {
0 commit comments