Skip to content

Commit 8732391

Browse files
amk-stripecodex
andauthored
Use email from customer session when available (#13161)
* Add email to ElementsSession.Customer Committed-By-Agent: codex Co-authored-by: codex <noreply@openai.com> * Use customer email from elements session when available fix up * Update test * Fix network tests * fix test * Add helper so we don't have to set email everywhere --------- Co-authored-by: codex <noreply@openai.com>
1 parent c2bee91 commit 8732391

27 files changed

Lines changed: 191 additions & 109 deletions

paymentsheet/src/androidTest/java/com/stripe/android/paymentelement/EmbeddedPaymentElementTest.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,6 @@ internal class EmbeddedPaymentElementTest {
266266
response.testBodyFromFile("elements-sessions-requires_pm_with_link_and_cs.json")
267267
}
268268

269-
networkRule.enqueue(
270-
host("api.stripe.com"),
271-
method("GET"),
272-
path("/v1/customers/cus_1"),
273-
) { response ->
274-
response.testBodyFromFile("customer-get-success.json")
275-
}
276-
277269
networkRule.enqueue(
278270
method("POST"),
279271
path("/v1/consumers/sessions/lookup"),

paymentsheet/src/androidTest/java/com/stripe/android/paymentelement/taptoadd/TapToAddTest.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,6 @@ internal class TapToAddTest {
243243
response.elementsSessionTtaWithLink()
244244
}
245245

246-
enqueueRetrieveCustomerRequest()
247-
248246
val info = cardCollectionTestHelper.enqueueSuccessfulTapToCollectFlow()
249247

250248
linkHelper.enqueueLookup()
@@ -277,15 +275,6 @@ internal class TapToAddTest {
277275
}
278276
}
279277

280-
private fun enqueueRetrieveCustomerRequest() {
281-
networkRule.enqueue(
282-
method("GET"),
283-
path("/v1/customers/cus_123"),
284-
) { response ->
285-
response.testBodyFromFile("tta-customer-get-success.json")
286-
}
287-
}
288-
289278
private fun enqueueConfirmRequests() {
290279
networkRule.enqueue(
291280
method("GET"),

paymentsheet/src/androidTest/java/com/stripe/android/paymentsheet/FlowControllerTest.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,14 +1096,6 @@ internal class FlowControllerTest {
10961096
response.testBodyFromFile("elements-sessions-requires_pm_with_link_and_cs.json")
10971097
}
10981098

1099-
networkRule.enqueue(
1100-
host("api.stripe.com"),
1101-
method("GET"),
1102-
path("/v1/customers/cus_1"),
1103-
) { response ->
1104-
response.testBodyFromFile("customer-get-success.json")
1105-
}
1106-
11071099
networkRule.enqueue(
11081100
method("POST"),
11091101
path("/v1/consumers/sessions/lookup"),
@@ -1165,14 +1157,6 @@ internal class FlowControllerTest {
11651157
response.testBodyFromFile("elements-sessions-requires_pm_with_link_and_cs.json")
11661158
}
11671159

1168-
networkRule.enqueue(
1169-
host("api.stripe.com"),
1170-
method("GET"),
1171-
path("/v1/customers/cus_1"),
1172-
) { response ->
1173-
response.testBodyFromFile("customer-get-success.json")
1174-
}
1175-
11761160
networkRule.enqueue(
11771161
method("POST"),
11781162
path("/v1/consumers/sessions/lookup"),

paymentsheet/src/androidTest/java/com/stripe/android/paymentsheet/LinkTest.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,14 +1062,6 @@ internal class LinkTest {
10621062
response.testBodyFromFile("elements-sessions-requires_pm_with_link_and_cs.json")
10631063
}
10641064

1065-
networkRule.enqueue(
1066-
host("api.stripe.com"),
1067-
method("GET"),
1068-
path("/v1/customers/cus_1"),
1069-
) { response ->
1070-
response.testBodyFromFile("customer-get-success.json")
1071-
}
1072-
10731065
networkRule.enqueue(
10741066
method("POST"),
10751067
path("/v1/consumers/sessions/lookup"),

paymentsheet/src/androidTest/resources/elements-sessions-requires_pm_with_link_and_cs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"customer": {
3636
"payment_methods": [],
37+
"email": "example@stripe.com",
3738
"customer_session": {
3839
"id": "cuss_654321",
3940
"livemode": false,

paymentsheet/src/androidTest/resources/tta-customer-get-success.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

paymentsheet/src/main/java/com/stripe/android/common/analytics/experiment/LogLinkHoldbackExperiment.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal class DefaultLogLinkHoldbackExperiment @Inject constructor(
7979
elementsSession.experimentsData
8080
) { "Experiments data required to log exposures" }
8181

82-
val customerEmail = state.getEmail()
82+
val customerEmail = state.getEmail(elementsSession.customer?.email)
8383

8484
val defaultValues = state.getDefaultValues()
8585

@@ -177,11 +177,14 @@ internal class DefaultLogLinkHoldbackExperiment @Inject constructor(
177177
return paymentMethodSaveEnabled && linkDisabledOrEnableLinkSPMFlagEnabled
178178
}
179179

180-
private suspend fun PaymentElementLoader.State.getEmail(): String? {
180+
private suspend fun PaymentElementLoader.State.getEmail(
181+
elementsSessionCustomerEmail: String?
182+
): String? {
181183
paymentMethodMetadata.linkState?.configuration?.customerInfo?.email?.let { return it }
182184
return retrieveCustomerEmail(
183185
configuration = config,
184186
customerMetadata = paymentMethodMetadata.customerMetadata,
187+
customerEmail = elementsSessionCustomerEmail,
185188
)
186189
}
187190
}

paymentsheet/src/main/java/com/stripe/android/model/ElementsSession.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ internal data class ElementsSession(
160160
data class Customer(
161161
val paymentMethods: List<PaymentMethod>,
162162
val defaultPaymentMethod: String?,
163+
val email: String?,
163164
val session: Session,
164165
) : StripeModel {
165166
@Parcelize

paymentsheet/src/main/java/com/stripe/android/model/parsers/ElementsSessionJsonParser.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,13 @@ internal class ElementsSessionJsonParser(
288288
it.isNotBlank()
289289
}
290290

291+
val email = json.optString(FIELD_CUSTOMER_EMAIL).takeIf {
292+
it.isNotBlank()
293+
}
294+
291295
return ElementsSession.Customer(
292296
paymentMethods = mergedPaymentMethods,
297+
email = email,
293298
session = customerSession,
294299
defaultPaymentMethod = defaultPaymentMethod
295300
)
@@ -581,6 +586,7 @@ internal class ElementsSessionJsonParser(
581586
private const val FIELD_CUSTOMER_API_KEY = "api_key"
582587
private const val FIELD_CUSTOMER_API_KEY_EXPIRY = "api_key_expiry"
583588
private const val FIELD_CUSTOMER_NAME = "customer"
589+
private const val FIELD_CUSTOMER_EMAIL = "email"
584590
private const val FIELD_COMPONENTS = "components"
585591
private const val FIELD_MOBILE_PAYMENT_ELEMENT = "mobile_payment_element"
586592
private const val FIELD_CUSTOMER_SHEET = "customer_sheet"

paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/CreateLinkState.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ internal class DefaultCreateLinkState @Inject constructor(
222222
val shippingDetails = configuration.shippingDetails
223223
val customerPhone = getCustomerPhone(shippingDetails, configuration)
224224

225-
val resolvedEmail = retrieveCustomerEmail(configuration, customerMetadata)
225+
val resolvedEmail = retrieveCustomerEmail(
226+
configuration,
227+
customerMetadata,
228+
customerEmail = elementsSession.customer?.email,
229+
)
226230
val customerInfo = LinkConfiguration.CustomerInfo(
227231
name = configuration.defaultBillingDetails?.name,
228232
email = resolvedEmail,

0 commit comments

Comments
 (0)