-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.ios.ts
173 lines (162 loc) · 6.22 KB
/
index.ios.ts
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
import { Frame } from '@nativescript/core';
import { PlaidLinkBase, PlaidLinkConfig, PlaidLinkEventMetadata, PlaidLinkEventName, PlaidLinkEventViewName, PlaidLinkExitResult, PlaidLinkInstitution, PlaidLinkResultAccount, PlaidLinkResultAccountSubType, PlaidLinkResultAccountType, PlaidLinkResultVerificationStatus, PlaidLinkSuccessResult, PlaidLinkExitResultMetaData, PlaidLinkExitResultError, PlaidLinkResultAccountSubTypeCredit, PlaidLinkResultAccountSubTypeDepository, PlaidLinkResultAccountSubTypeInvestment, PlaidLinkResultAccountSubTypeLoan, PlaidLinkSuccessMetaData } from './common';
export class PlaidLink extends PlaidLinkBase {
private onSuccess: (linkSuccess: PlaidLinkSuccessResult) => void;
private onExit: (linkExit: PlaidLinkExitResult) => void;
private token: string;
private _handler: PLKHandler;
private eventNames = {
0: 'closeOAuth',
1: 'error',
2: 'exit',
3: 'failOAuth',
4: 'handoff',
5: 'open',
6: 'openMyPlaid',
7: 'openOAuth',
8: 'searchInstitution',
9: 'selectInstitution',
10: 'submitCredentials',
11: 'submitMfa',
12: 'transitionView',
13: 'unknown',
};
private viewNames = {
1: 'connected',
2: 'consent',
3: 'credential',
4: 'error',
5: 'exit',
6: 'loading',
7: 'mfa',
8: 'numbers',
9: 'recaptcha',
10: 'selectAccount',
11: 'selectInstitution',
12: 'unknown',
};
constructor() {
super();
}
private static get rootViewController(): UIViewController | undefined {
const keyWindow = UIApplication.sharedApplication.keyWindow;
return keyWindow != null ? keyWindow.rootViewController : undefined;
}
private static get topViewController(): UIViewController | undefined {
const root = PlaidLink.rootViewController;
if (root == null) {
return undefined;
}
return PlaidLink.findTopViewController(root);
}
private static findTopViewController(root: UIViewController): UIViewController | undefined {
const presented = root.presentedViewController;
if (presented != null) {
return PlaidLink.findTopViewController(presented);
}
if (root instanceof UISplitViewController) {
const last = root.viewControllers.lastObject;
if (last == null) {
return root;
}
return PlaidLink.findTopViewController(last);
} else if (root instanceof UINavigationController) {
const top = root.topViewController;
if (top == null) {
return root;
}
return PlaidLink.findTopViewController(top);
} else if (root instanceof UITabBarController) {
const selected = root.selectedViewController;
if (selected == null) {
return root;
}
return PlaidLink.findTopViewController(selected);
} else {
return root;
}
}
private static _listener?: (event: PlaidLinkEventMetadata) => void;
public static setLinkEventListener(listener: (event: PlaidLinkEventMetadata) => void) {
const plaidLink = new PlaidLink();
PlaidLink._listener = listener;
}
public static createHandler(config: PlaidLinkConfig): PlaidLink {
const plaidLink = new PlaidLink();
plaidLink.onExit = config.onExit;
plaidLink.onSuccess = config.onSuccess;
if (config.token) plaidLink.token = config.token;
return plaidLink;
}
public open() {
let config = PLKLinkTokenConfiguration.createWithTokenOnSuccess(this.token, (result) => {
let accounts = [];
const accountsCount = result.metadata.accounts.count;
for (let i = 0; i < accountsCount; i++) {
const account = result.metadata.accounts.objectAtIndex(i);
accounts.push({
id: account.ID,
name: account.name,
mask: account.mask,
subtype: account.subtype.rawStringValue,
verificationStatus: account.verificationStatus?.value,
});
}
this.onSuccess({
publicToken: result.publicToken,
metadata: {
institution: {
id: result.metadata.institution.ID,
name: result.metadata.institution.name,
},
linkSessionId: result.metadata.linkSessionID,
metadataJson: result.metadata.metadataJSON,
accounts: accounts,
},
});
});
config.onExit = (result) => {
this.onExit({
error: {
displayMessage: result.error.localizedFailureReason,
code: result.error ? result.error.code.toString() : null,
},
metadata: {
institution: {
id: result.metadata.institution.ID,
name: result.metadata.institution.name,
},
linkSessionId: result.metadata.linkSessionID,
status: result.metadata.status ? result.metadata.status.value.toString() : null,
requestId: result.metadata.requestID,
},
});
};
config.onEvent = (result) => {
const plaidLink = new PlaidLink();
let args: PlaidLinkEventMetadata = {
eventName: plaidLink.eventNames[result.eventName.value],
metadata: {
errorCode: result.eventMetadata.error ? result.eventMetadata.error.code.toString() : null,
errorMessage: result.eventMetadata.error && result.eventMetadata.error.localizedFailureReason ? result.eventMetadata.error.localizedFailureReason.toString() : null,
institutionId: result.eventMetadata.institutionID,
institutionName: result.eventMetadata.institutionName,
institutionSearchQuery: result.eventMetadata.institutionSearchQuery,
linkSessionId: result.eventMetadata.linkSessionID,
requestId: result.eventMetadata.requestID,
timestamp: result.eventMetadata.timestamp ? result.eventMetadata.timestamp.toString() : null,
viewName: plaidLink.viewNames[result.eventMetadata.viewName.value],
metadataJson: result.eventMetadata.metadataJSON,
},
};
if (PlaidLink._listener) PlaidLink._listener(args);
};
let handler = PLKPlaid.createWithLinkTokenConfigurationError(config);
const topCtrl = PlaidLink.topViewController;
topCtrl.modalPresentationStyle = 0;
handler.openWithPresentationHandler((ctrl) => {
topCtrl.presentViewControllerAnimatedCompletion(ctrl, true, () => {});
});
}
}
export { PlaidLinkConfig, PlaidLinkEventMetadata, PlaidLinkEventName, PlaidLinkEventViewName, PlaidLinkExitResult, PlaidLinkInstitution, PlaidLinkResultAccount, PlaidLinkResultAccountSubType, PlaidLinkResultAccountType, PlaidLinkResultVerificationStatus, PlaidLinkSuccessResult, PlaidLinkExitResultMetaData, PlaidLinkExitResultError, PlaidLinkResultAccountSubTypeCredit, PlaidLinkResultAccountSubTypeDepository, PlaidLinkResultAccountSubTypeInvestment, PlaidLinkResultAccountSubTypeLoan, PlaidLinkSuccessMetaData };