forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankAccounts.js
452 lines (421 loc) · 14.1 KB
/
BankAccounts.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import Onyx from 'react-native-onyx';
import CONST from '../../CONST';
import * as API from '../API';
import ONYXKEYS from '../../ONYXKEYS';
import * as ErrorUtils from '../ErrorUtils';
import * as PlaidDataProps from '../../pages/ReimbursementAccount/plaidDataPropTypes';
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
import * as ReimbursementAccount from './ReimbursementAccount';
export {
goToWithdrawalAccountSetupStep,
setBankAccountFormValidationErrors,
resetReimbursementAccount,
resetFreePlanBankAccount,
hideBankAccountErrors,
setWorkspaceIDForReimbursementAccount,
setBankAccountSubStep,
updateReimbursementAccountDraft,
requestResetFreePlanBankAccount,
cancelResetFreePlanBankAccount,
} from './ReimbursementAccount';
export {openPlaidBankAccountSelector, openPlaidBankLogin} from './Plaid';
export {openOnfidoFlow, answerQuestionsForWallet, verifyIdentity, acceptWalletTerms} from './Wallet';
function clearPlaid() {
Onyx.set(ONYXKEYS.PLAID_LINK_TOKEN, '');
return Onyx.set(ONYXKEYS.PLAID_DATA, PlaidDataProps.plaidDataDefaultProps);
}
function openPlaidView() {
clearPlaid().then(() => ReimbursementAccount.setBankAccountSubStep(CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID));
}
/**
* Open the personal bank account setup flow, with an optional exitReportID to redirect to once the flow is finished.
* @param {String} exitReportID
*/
function openPersonalBankAccountSetupView(exitReportID) {
clearPlaid().then(() => {
if (exitReportID) {
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {exitReportID});
}
Navigation.navigate(ROUTES.SETTINGS_ADD_BANK_ACCOUNT);
});
}
function clearPersonalBankAccount() {
clearPlaid();
Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {});
}
function clearOnfidoToken() {
Onyx.merge(ONYXKEYS.ONFIDO_TOKEN, '');
}
/**
* Helper method to build the Onyx data required during setup of a Verified Business Bank Account
* @param {String | undefined} currentStep The name of the bank account setup step for which we will update the draft value when we receive the response from the API.
* @returns {Object}
*/
function getVBBADataForOnyx(currentStep = undefined) {
return {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: true,
errors: null,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
errors: null,
// When setting up a bank account, we save the draft form values in Onyx.
// When we update the information for a step, the value of some fields that are returned from the API
// can be different from the value that we stored as the draft in Onyx (i.e. the phone number is formatted).
// This is why we store the current step used to call the API in order to update the corresponding draft data in Onyx.
// If currentStep is undefined that means this step don't need to update the data of the draft in Onyx.
draftStep: currentStep,
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
errors: ErrorUtils.getMicroSecondOnyxError('walletPage.addBankAccountFailure'),
},
},
],
};
}
/**
* Submit Bank Account step with Plaid data so php can perform some checks.
*
* @param {Number} bankAccountID
* @param {Object} selectedPlaidBankAccount
*/
function connectBankAccountWithPlaid(bankAccountID, selectedPlaidBankAccount) {
const commandName = 'ConnectBankAccountWithPlaid';
const parameters = {
bankAccountID,
routingNumber: selectedPlaidBankAccount.routingNumber,
accountNumber: selectedPlaidBankAccount.accountNumber,
bank: selectedPlaidBankAccount.bankName,
plaidAccountID: selectedPlaidBankAccount.plaidAccountID,
plaidAccessToken: selectedPlaidBankAccount.plaidAccessToken,
};
API.write(commandName, parameters, getVBBADataForOnyx());
}
/**
* Adds a bank account via Plaid
*
* @param {Object} account
* @TODO offline pattern for this command will have to be added later once the pattern B design doc is complete
*/
function addPersonalBankAccount(account) {
const commandName = 'AddPersonalBankAccount';
const parameters = {
addressName: account.addressName,
routingNumber: account.routingNumber,
accountNumber: account.accountNumber,
isSavings: account.isSavings,
setupType: 'plaid',
bank: account.bankName,
plaidAccountID: account.plaidAccountID,
plaidAccessToken: account.plaidAccessToken,
};
const onyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_BANK_ACCOUNT,
value: {
isLoading: true,
errors: null,
plaidAccountID: account.plaidAccountID,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_BANK_ACCOUNT,
value: {
isLoading: false,
errors: null,
shouldShowSuccess: true,
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_BANK_ACCOUNT,
value: {
isLoading: false,
errors: ErrorUtils.getMicroSecondOnyxError('walletPage.addBankAccountFailure'),
},
},
],
};
API.write(commandName, parameters, onyxData);
}
function deletePaymentBankAccount(bankAccountID) {
API.write(
'DeletePaymentBankAccount',
{
bankAccountID,
},
{
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.BANK_ACCOUNT_LIST}`,
value: {[bankAccountID]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE}},
},
],
// Sometimes pusher updates aren't received when we close the App while still offline,
// so we are setting the bankAccount to null here to ensure that it gets cleared out once we come back online.
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.BANK_ACCOUNT_LIST}`,
value: {[bankAccountID]: null},
},
],
},
);
}
/**
* Update the user's personal information on the bank account in database.
*
* This action is called by the requestor step in the Verified Bank Account flow
*
* @param {Object} params
*
* @param {String} [params.dob]
* @param {String} [params.firstName]
* @param {String} [params.lastName]
* @param {String} [params.requestorAddressStreet]
* @param {String} [params.requestorAddressCity]
* @param {String} [params.requestorAddressState]
* @param {String} [params.requestorAddressZipCode]
* @param {String} [params.ssnLast4]
* @param {String} [params.isControllingOfficer]
* @param {Object} [params.onfidoData]
* @param {Boolean} [params.isOnfidoSetupComplete]
*/
function updatePersonalInformationForBankAccount(params) {
API.write('UpdatePersonalInformationForBankAccount', params, getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.REQUESTOR));
}
/**
* @param {Number} bankAccountID
* @param {String} validateCode
*/
function validateBankAccount(bankAccountID, validateCode) {
API.write(
'ValidateBankAccountWithTransactions',
{
bankAccountID,
validateCode,
},
{
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: true,
errors: null,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
},
},
],
},
);
}
function clearReimbursementAccount() {
Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT, null);
}
function openReimbursementAccountPage(stepToOpen, subStep, localCurrentStep) {
const onyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: true,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
},
},
],
};
const param = {
stepToOpen,
subStep,
localCurrentStep,
};
return API.read('OpenReimbursementAccountPage', param, onyxData);
}
/**
* Updates the bank account in the database with the company step data
*
* @param {Object} bankAccount
* @param {Number} [bankAccount.bankAccountID]
*
* Fields from BankAccount step
* @param {String} [bankAccount.routingNumber]
* @param {String} [bankAccount.accountNumber]
* @param {String} [bankAccount.bankName]
* @param {String} [bankAccount.plaidAccountID]
* @param {String} [bankAccount.plaidAccessToken]
* @param {Boolean} [bankAccount.isSavings]
*
* Fields from Company step
* @param {String} [bankAccount.companyName]
* @param {String} [bankAccount.addressStreet]
* @param {String} [bankAccount.addressCity]
* @param {String} [bankAccount.addressState]
* @param {String} [bankAccount.addressZipCode]
* @param {String} [bankAccount.companyPhone]
* @param {String} [bankAccount.website]
* @param {String} [bankAccount.companyTaxID]
* @param {String} [bankAccount.incorporationType]
* @param {String} [bankAccount.incorporationState]
* @param {String} [bankAccount.incorporationDate]
* @param {Boolean} [bankAccount.hasNoConnectionToCannabis]
* @param {String} policyID
*/
function updateCompanyInformationForBankAccount(bankAccount, policyID) {
API.write('UpdateCompanyInformationForBankAccount', {...bankAccount, policyID}, getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.COMPANY));
}
/**
* Add beneficial owners for the bank account, accept the ACH terms and conditions and verify the accuracy of the information provided
*
* @param {Object} params
*
* // ACH Contract Step
* @param {Boolean} [params.ownsMoreThan25Percent]
* @param {Boolean} [params.hasOtherBeneficialOwners]
* @param {Boolean} [params.acceptTermsAndConditions]
* @param {Boolean} [params.certifyTrueInformation]
* @param {String} [params.beneficialOwners]
*/
function updateBeneficialOwnersForBankAccount(params) {
API.write('UpdateBeneficialOwnersForBankAccount', {...params}, getVBBADataForOnyx());
}
/**
* Create the bank account with manually entered data.
*
* @param {number} [bankAccountID]
* @param {String} [accountNumber]
* @param {String} [routingNumber]
* @param {String} [plaidMask]
*
*/
function connectBankAccountManually(bankAccountID, accountNumber, routingNumber, plaidMask) {
API.write(
'ConnectBankAccountManually',
{
bankAccountID,
accountNumber,
routingNumber,
plaidMask,
},
getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT),
);
}
/**
* Verify the user's identity via Onfido
*
* @param {Number} bankAccountID
* @param {Object} onfidoData
*/
function verifyIdentityForBankAccount(bankAccountID, onfidoData) {
API.write(
'VerifyIdentityForBankAccount',
{
bankAccountID,
onfidoData: JSON.stringify(onfidoData),
},
getVBBADataForOnyx(),
);
}
function openWorkspaceView() {
API.read('OpenWorkspaceView');
}
function handlePlaidError(bankAccountID, error, error_description, plaidRequestID) {
API.write('BankAccount_HandlePlaidError', {
bankAccountID,
error,
error_description,
plaidRequestID,
});
}
/**
* Set the reimbursement account loading so that it happens right away, instead of when the API command is processed.
*
* @param {Boolean} isLoading
*/
function setReimbursementAccountLoading(isLoading) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {isLoading});
}
export {
addPersonalBankAccount,
clearOnfidoToken,
clearPersonalBankAccount,
clearPlaid,
openPlaidView,
connectBankAccountManually,
connectBankAccountWithPlaid,
deletePaymentBankAccount,
handlePlaidError,
openPersonalBankAccountSetupView,
clearReimbursementAccount,
openReimbursementAccountPage,
updateBeneficialOwnersForBankAccount,
updateCompanyInformationForBankAccount,
updatePersonalInformationForBankAccount,
openWorkspaceView,
validateBankAccount,
verifyIdentityForBankAccount,
setReimbursementAccountLoading,
};