-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathReactNativePlaidLinkSdkModule.kt
More file actions
183 lines (162 loc) · 6.72 KB
/
Copy pathReactNativePlaidLinkSdkModule.kt
File metadata and controls
183 lines (162 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package expo.modules.plaidlinksdk
import android.app.Activity
import android.content.Intent
import com.plaid.link.OnLoadCallback
import com.plaid.link.Plaid
import com.plaid.link.PlaidHeadlessSession
import com.plaid.link.PlaidLayerSession
import com.plaid.link.PlaidLinkSession
import com.plaid.link.PlaidSession
import com.plaid.link.SubmissionData
import com.plaid.link.configuration.LayerTokenConfiguration
import com.plaid.link.configuration.LinkTokenConfiguration
import com.plaid.link.result.LinkExit
import com.plaid.link.result.LinkSuccess
import expo.modules.kotlin.Promise
import expo.modules.kotlin.exception.CodedException
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class ReactNativePlaidLinkSdkModule : Module() {
private var linkSession: PlaidLinkSession? = null
private var layerSession: PlaidLayerSession? = null
private var headlessSession: PlaidHeadlessSession? = null
private var activeSession: PlaidSession? = null
private var sessionCreationError: Throwable? = null
override fun definition() = ModuleDefinition {
Name("ReactNativePlaidLinkSdk")
Constant("sdkVersion") { RNPlaidLinkSdkVersion.SDK_VERSION }
Events("PlaidLink.onSuccess", "PlaidLink.onExit", "PlaidLink.onEvent")
View(ReactNativePlaidLinkSdkView::class) {
Events("onSuccess", "onExit", "onEvent", "onLoad")
Prop("token") { view: ReactNativePlaidLinkSdkView, token: String -> view.setToken(token) }
}
OnActivityResult { _, payload ->
if (payload.requestCode == Plaid.LINK_REQUEST_CODE) {
handleActivityResult(payload.resultCode, payload.data)
}
}
AsyncFunction("createPlaidLinkSession") { token: String, promise: Promise ->
try {
val activity = requireActivity()
Plaid.setLinkEventListener { event -> sendEvent("PlaidLink.onEvent", event.toWritableMap()) }
val config = LinkTokenConfiguration.Builder()
.token(token)
.onLoad(OnLoadCallback { promise.resolve(null) })
.build()
linkSession = Plaid.createPlaidLinkSession(activity, config)
activeSession = linkSession
sessionCreationError = null
} catch (error: Throwable) {
sessionCreationError = error
promise.reject("LINK_SESSION_CREATE_ERROR", error.message ?: "Failed to create Link session", error)
}
}
AsyncFunction("createPlaidLayerSession") { token: String, promise: Promise ->
try {
val activity = requireActivity()
Plaid.setLinkEventListener { event -> sendEvent("PlaidLink.onEvent", event.toWritableMap()) }
val config = LayerTokenConfiguration.Builder().token(token).build()
layerSession = Plaid.createPlaidLayerSession(activity, config)
activeSession = layerSession
sessionCreationError = null
promise.resolve(null)
} catch (error: Throwable) {
sessionCreationError = error
promise.reject("LAYER_SESSION_CREATE_ERROR", error.message ?: "Failed to create Layer session", error)
}
}
AsyncFunction("createPlaidHeadlessSession") { token: String, promise: Promise ->
try {
val activity = requireActivity()
Plaid.setLinkEventListener { event -> sendEvent("PlaidLink.onEvent", event.toWritableMap()) }
val config = LinkTokenConfiguration.Builder()
.token(token)
.onLoad(OnLoadCallback { promise.resolve(null) })
.build()
headlessSession = Plaid.createPlaidHeadlessSession(activity, config)
activeSession = headlessSession
sessionCreationError = null
} catch (error: Throwable) {
sessionCreationError = error
promise.reject("HEADLESS_SESSION_CREATE_ERROR", error.message ?: "Failed to create Headless session", error)
}
}
AsyncFunction("openLinkSession") { _: Boolean, promise: Promise ->
openSession(linkSession, "createPlaidLinkSession was not called.", promise)
}
AsyncFunction("openLayerSession") { promise: Promise ->
openSession(layerSession, "createPlaidLayerSession was not called.", promise)
}
AsyncFunction("startHeadlessSession") { promise: Promise ->
openSession(headlessSession, "createPlaidHeadlessSession was not called.", promise)
}
AsyncFunction("submitLayerData") { phoneNumber: String?, dateOfBirth: String?, params: Map<String, String>?, promise: Promise ->
val session = layerSession
if (session == null) {
promise.reject("PLAID_NO_LAYER_SESSION", "Layer session not found. Call createPlaidLayerSession first.", null)
return@AsyncFunction
}
session.submit(SubmissionData(phoneNumber = phoneNumber, dateOfBirth = dateOfBirth, params = params))
promise.resolve(null)
}
AsyncFunction("syncFinanceKit") { _: String, _: Boolean, _: Int, promise: Promise ->
promise.reject("UNSUPPORTED_ANDROID", "FinanceKit is only available on iOS", null)
}
}
private fun openSession(session: PlaidSession?, missingSessionMessage: String, promise: Promise) {
if (session == null) {
sendCreationExit(missingSessionMessage)
promise.resolve(null)
return
}
try {
val activity = requireActivity()
activeSession = session
session.open(activity)
promise.resolve(null)
} catch (error: Throwable) {
promise.reject("PLAID_OPEN_ERROR", error.message ?: "Failed to open Plaid session", error)
}
}
private fun handleActivityResult(resultCode: Int, data: Intent?) {
when (val result = Plaid.parseResult(Plaid.LINK_REQUEST_CODE, resultCode, data)) {
is LinkSuccess -> {
PlaidEmbeddedResultDispatcher.dispatch(result)
sendEvent("PlaidLink.onSuccess", result.toWritableMap())
clearActiveSession()
}
is LinkExit -> {
PlaidEmbeddedResultDispatcher.dispatch(result)
sendEvent("PlaidLink.onExit", result.toWritableMap())
clearActiveSession()
}
null -> Unit
}
}
private fun clearActiveSession() {
when (activeSession) {
linkSession -> linkSession = null
layerSession -> layerSession = null
headlessSession -> headlessSession = null
}
activeSession = null
}
private fun sendCreationExit(defaultMessage: String) {
val errorMessage = sessionCreationError?.localizedMessage ?: defaultMessage
sendEvent(
"PlaidLink.onExit",
mapOf(
"error" to mapOf(
"errorType" to "creation error",
"errorCode" to "-1",
"errorMessage" to errorMessage,
"displayMessage" to errorMessage,
"errorJson" to "",
),
"metadata" to emptyExitMetadata(),
),
)
}
private fun requireActivity(): Activity = appContext.currentActivity
?: throw CodedException("Could not find current activity.")
}