@@ -9,12 +9,28 @@ import Foundation
9
9
@_spi ( STP) import StripeCore
10
10
11
11
protocol ConsentDataSource : AnyObject {
12
+ var email : String ? { get }
12
13
var manifest : FinancialConnectionsSessionManifest { get }
13
14
var consent : FinancialConnectionsConsent { get }
14
15
var merchantLogo : [ String ] ? { get }
15
16
var analyticsClient : FinancialConnectionsAnalyticsClient { get }
16
17
17
- func markConsentAcquired( ) -> Promise < FinancialConnectionsSessionManifest >
18
+ func markConsentAcquired( ) -> Future < ConsentAcquiredResult >
19
+ func completeAssertionIfNeeded(
20
+ possibleError: Error ? ,
21
+ api: FinancialConnectionsAPIClientLogger . API
22
+ ) -> Error ?
23
+ }
24
+
25
+ struct ConsentAcquiredResult {
26
+ var manifest : FinancialConnectionsSessionManifest
27
+ var consumerSession : ConsumerSessionData ? = nil
28
+ var publishableKey : String ? = nil
29
+
30
+ var nextPane : FinancialConnectionsSessionManifest . NextPane {
31
+ // If we have a consumer session, then provide the returning-user experience
32
+ consumerSession != nil ? . networkingLinkLoginWarmup : manifest. nextPane
33
+ }
18
34
}
19
35
20
36
final class ConsentDataSourceImplementation : ConsentDataSource {
@@ -25,24 +41,80 @@ final class ConsentDataSourceImplementation: ConsentDataSource {
25
41
private let apiClient : any FinancialConnectionsAPI
26
42
private let clientSecret : String
27
43
let analyticsClient : FinancialConnectionsAnalyticsClient
44
+ private let elementsSessionContext : ElementsSessionContext ?
45
+
46
+ var email : String ? {
47
+ elementsSessionContext? . prefillDetails? . email
48
+ }
28
49
29
50
init (
30
51
manifest: FinancialConnectionsSessionManifest ,
31
52
consent: FinancialConnectionsConsent ,
32
53
merchantLogo: [ String ] ? ,
33
54
apiClient: any FinancialConnectionsAPI ,
34
55
clientSecret: String ,
35
- analyticsClient: FinancialConnectionsAnalyticsClient
56
+ analyticsClient: FinancialConnectionsAnalyticsClient ,
57
+ elementsSessionContext: ElementsSessionContext ?
36
58
) {
37
59
self . manifest = manifest
38
60
self . consent = consent
39
61
self . merchantLogo = merchantLogo
40
62
self . apiClient = apiClient
41
63
self . clientSecret = clientSecret
42
64
self . analyticsClient = analyticsClient
65
+ self . elementsSessionContext = elementsSessionContext
43
66
}
44
67
45
- func markConsentAcquired( ) -> Promise < FinancialConnectionsSessionManifest > {
46
- return apiClient. markConsentAcquired ( clientSecret: clientSecret)
68
+ func markConsentAcquired( ) -> Future < ConsentAcquiredResult > {
69
+ return apiClient. markConsentAcquired ( clientSecret: clientSecret) . chained { [ weak self] manifest in
70
+ guard let self, manifest. shouldLookupConsumerSession, let email else {
71
+ let result = ConsentAcquiredResult ( manifest: manifest)
72
+ return Promise ( value: result)
73
+ }
74
+
75
+ let promise = Promise < ConsentAcquiredResult > ( )
76
+
77
+ apiClient. consumerSessionLookup (
78
+ emailAddress: email,
79
+ clientSecret: clientSecret,
80
+ sessionId: manifest. id,
81
+ emailSource: . userAction,
82
+ useMobileEndpoints: manifest. verified,
83
+ pane: . consent
84
+ ) . observe { lookupResult in
85
+ switch lookupResult {
86
+ case . success( let response) :
87
+ let result = ConsentAcquiredResult (
88
+ manifest: manifest,
89
+ consumerSession: response. consumerSession,
90
+ publishableKey: response. publishableKey
91
+ )
92
+ promise. resolve ( with: result)
93
+ case . failure:
94
+ let result = ConsentAcquiredResult ( manifest: manifest)
95
+ promise. resolve ( with: result)
96
+ }
97
+ }
98
+
99
+ return promise
100
+ }
101
+ }
102
+
103
+ func completeAssertionIfNeeded(
104
+ possibleError: Error ? ,
105
+ api: FinancialConnectionsAPIClientLogger . API
106
+ ) -> Error ? {
107
+ guard manifest. verified else { return nil }
108
+ return apiClient. completeAssertion (
109
+ possibleError: possibleError,
110
+ api: api,
111
+ pane: . linkLogin
112
+ )
113
+ }
114
+ }
115
+
116
+ private extension FinancialConnectionsSessionManifest {
117
+ var shouldLookupConsumerSession : Bool {
118
+ isLinkWithStripe == true && accountholderCustomerEmailAddress == nil
47
119
}
48
120
}
0 commit comments