-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBankAccounts.js
832 lines (746 loc) · 34.2 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
import lodashGet from 'lodash/get';
import lodashHas from 'lodash/has';
import Str from 'expensify-common/lib/str';
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import CONST from '../../CONST';
import ONYXKEYS from '../../ONYXKEYS';
import * as API from '../API';
import BankAccount from '../models/BankAccount';
import Growl from '../Growl';
import {translateLocal} from '../translate';
/**
* List of bank accounts. This data should not be stored in Onyx since it contains unmasked PANs.
*
* @private
*/
let plaidBankAccounts = [];
let bankName = '';
let plaidAccessToken = '';
/** Reimbursement account actively being set up */
let reimbursementAccountInSetup = {};
Onyx.connect({
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
callback: (val) => {
reimbursementAccountInSetup = lodashGet(val, 'achData', {});
},
});
let reimbursementAccountWorkspaceID = null;
Onyx.connect({
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT_WORKSPACE_ID,
callback: (val) => {
reimbursementAccountWorkspaceID = val;
},
});
/**
* Gets the Plaid Link token used to initialize the Plaid SDK
*/
function fetchPlaidLinkToken() {
API.Plaid_GetLinkToken()
.then((response) => {
if (response.jsonCode !== 200) {
return;
}
Onyx.merge(ONYXKEYS.PLAID_LINK_TOKEN, response.linkToken);
});
}
/**
* Navigate to a specific step in the VBA flow
*
* @param {String} stepID
* @param {Object} achData
*/
function goToWithdrawalAccountSetupStep(stepID, achData) {
const newACHData = {...reimbursementAccountInSetup};
// If we go back to Requestor Step, reset any validation and previously answered questions from expectID.
if (!newACHData.useOnfido && stepID === CONST.BANK_ACCOUNT.STEP.REQUESTOR) {
delete newACHData.questions;
delete newACHData.answers;
if (lodashHas(newACHData, CONST.BANK_ACCOUNT.VERIFICATIONS.EXTERNAL_API_RESPONSES)) {
delete newACHData.verifications.externalApiResponses.requestorIdentityID;
delete newACHData.verifications.externalApiResponses.requestorIdentityKBA;
}
}
// When going back to the BankAccountStep from the Company Step, show the manual form instead of Plaid
if (newACHData.currentStep === CONST.BANK_ACCOUNT.STEP.COMPANY && stepID === CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT) {
newACHData.subStep = CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL;
}
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData: {...newACHData, ...achData, currentStep: stepID}});
}
/**
* @param {String} publicToken
* @param {String} bank
*/
function getPlaidBankAccounts(publicToken, bank) {
bankName = bank;
Onyx.merge(ONYXKEYS.PLAID_BANK_ACCOUNTS, {loading: true});
API.BankAccount_Get({
publicToken,
allowDebit: false,
bank,
})
.then((response) => {
if (response.jsonCode === 666 && response.title === CONST.BANK_ACCOUNT.PLAID.ERROR.TOO_MANY_ATTEMPTS) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {isPlaidDisabled: true});
}
plaidAccessToken = response.plaidAccessToken;
// Filter out any accounts that already exist since they cannot be used again.
plaidBankAccounts = _.filter(response.accounts, account => !account.alreadyExists);
if (plaidBankAccounts.length === 0) {
Growl.error(translateLocal('bankAccount.error.noBankAccountAvailable'));
}
Onyx.merge(ONYXKEYS.PLAID_BANK_ACCOUNTS, {
error: {
title: response.title,
message: response.message,
},
loading: false,
accounts: _.map(plaidBankAccounts, account => ({
...account,
accountNumber: Str.maskPAN(account.accountNumber),
})),
bankName,
});
});
}
/**
* We clear these out of storage once we are done with them so the user must re-enter Plaid credentials upon returning.
*/
function clearPlaidBankAccountsAndToken() {
plaidBankAccounts = [];
bankName = '';
Onyx.set(ONYXKEYS.PLAID_BANK_ACCOUNTS, {});
Onyx.set(ONYXKEYS.PLAID_LINK_TOKEN, null);
}
/**
* Adds a bank account via Plaid
*
* @param {Object} account
* @param {String} password
* @param {String} plaidLinkToken
*/
function addPersonalBankAccount(account, password, plaidLinkToken) {
const unmaskedAccount = _.find(plaidBankAccounts, bankAccount => (
bankAccount.plaidAccountID === account.plaidAccountID
));
API.BankAccount_Create({
accountNumber: unmaskedAccount.accountNumber,
addressName: unmaskedAccount.addressName,
allowDebit: false,
confirm: false,
isSavings: unmaskedAccount.isSavings,
password,
routingNumber: unmaskedAccount.routingNumber,
setupType: 'plaid',
additionalData: JSON.stringify({
useOnFido: false,
policyID: '',
plaidLinkToken,
isInSetup: true,
bankAccountInReview: null,
currentStep: 'AccountOwnerInformationStep',
bankName,
plaidAccountID: unmaskedAccount.plaidAccountID,
ownershipType: '',
acceptTerms: true,
country: 'US',
currency: CONST.CURRENCY.USD,
fieldsType: 'local',
plaidAccessToken,
}),
})
.then((response) => {
if (response.jsonCode !== 200) {
alert('There was a problem adding this bank account.');
return;
}
alert('Bank account added successfully.');
});
}
/**
* Fetch and save locally the Onfido SDK token and applicantID
* - The sdkToken is used to initialize the Onfido SDK client
* - The applicantID is combined with the data returned from the Onfido SDK as we need both to create an
* identity check. Note: This happens in Web-Secure when we call Activate_Wallet during the OnfidoStep.
*/
function fetchOnfidoToken() {
// Use Onyx.set() since we are resetting the Onfido flow completely.
Onyx.set(ONYXKEYS.WALLET_ONFIDO, {loading: true});
API.Wallet_GetOnfidoSDKToken()
.then((response) => {
const apiResult = lodashGet(response, ['requestorIdentityOnfido', 'apiResult'], {});
Onyx.merge(ONYXKEYS.WALLET_ONFIDO, {
applicantID: apiResult.applicantID,
sdkToken: apiResult.sdkToken,
loading: false,
hasAcceptedPrivacyPolicy: true,
});
})
.catch(() => Onyx.set(ONYXKEYS.WALLET_ONFIDO, {loading: false, error: CONST.WALLET.ERROR.UNEXPECTED}));
}
/**
* Privately used to update the additionalDetails object in Onyx (which will have various effects on the UI)
*
* @param {Boolean} loading whether we are making the API call to validate the user's provided personal details
* @param {String[]} [errorFields] an array of field names that should display errors in the UI
* @param {String} [additionalErrorMessage] an additional error message to display in the UI
* @private
*/
function setAdditionalDetailsStep(loading, errorFields = null, additionalErrorMessage = '') {
Onyx.merge(ONYXKEYS.WALLET_ADDITIONAL_DETAILS, {loading, errorFields, additionalErrorMessage});
}
/**
* This action can be called repeatedly with different steps until an Expensify Wallet has been activated.
*
* Possible steps:
*
* - OnfidoStep - Creates an identity check by calling Onfido's API (via Web-Secure) with data returned from the SDK
* - AdditionalDetailsStep - Validates a user's provided details against a series of checks
* - TermsStep - Ensures that a user has agreed to all of the terms and conditions
*
* The API will always return the updated userWallet in the response as a convenience so we can avoid calling
* Get&returnValueList=userWallet after we call Wallet_Activate.
*
* @param {String} currentStep
* @param {Object} parameters
* @param {String} [parameters.onfidoData] - JSON string
* @param {Object} [parameters.personalDetails] - JSON string
* @param {Boolean} [parameters.hasAcceptedTerms]
*/
function activateWallet(currentStep, parameters) {
let personalDetails;
let onfidoData;
let hasAcceptedTerms;
if (!_.contains(CONST.WALLET.STEP, currentStep)) {
throw new Error('Invalid currentStep passed to activateWallet()');
}
if (currentStep === CONST.WALLET.STEP.ONFIDO) {
onfidoData = parameters.onfidoData;
Onyx.merge(ONYXKEYS.WALLET_ONFIDO, {error: '', loading: true});
} else if (currentStep === CONST.WALLET.STEP.ADDITIONAL_DETAILS) {
setAdditionalDetailsStep(true);
// Personal details are heavily validated on the API side. We will only do a quick check to ensure the values
// exist in some capacity and then stringify them.
const errorFields = _.reduce(CONST.WALLET.REQUIRED_ADDITIONAL_DETAILS_FIELDS, (missingFields, fieldName) => (
!personalDetails[fieldName] ? [...missingFields, fieldName] : missingFields
), []);
if (!_.isEmpty(errorFields)) {
setAdditionalDetailsStep(false, errorFields);
return;
}
personalDetails = JSON.stringify(parameters.personalDetails);
} else if (currentStep === CONST.WALLET.STEP.TERMS) {
hasAcceptedTerms = parameters.hasAcceptedTerms;
Onyx.merge(ONYXKEYS.WALLET_TERMS, {loading: true});
}
API.Wallet_Activate({
currentStep,
personalDetails,
onfidoData,
hasAcceptedTerms,
})
.then((response) => {
if (response.jsonCode !== 200) {
if (currentStep === CONST.WALLET.STEP.ONFIDO) {
Onyx.merge(ONYXKEYS.WALLET_ONFIDO, {error: response.message, loading: false});
return;
}
if (currentStep === CONST.WALLET.STEP.ADDITIONAL_DETAILS) {
if (response.title === CONST.WALLET.ERROR.MISSING_FIELD) {
setAdditionalDetailsStep(false, response.data.fieldNames);
return;
}
const errorTitles = [
CONST.WALLET.ERROR.IDENTITY_NOT_FOUND,
CONST.WALLET.ERROR.INVALID_SSN,
CONST.WALLET.ERROR.UNEXPECTED,
CONST.WALLET.ERROR.UNABLE_TO_VERIFY,
];
if (errorTitles.includes(response.title)) {
setAdditionalDetailsStep(false, null, response.message);
return;
}
setAdditionalDetailsStep(false);
return;
}
return;
}
Onyx.merge(ONYXKEYS.USER_WALLET, response.userWallet);
if (currentStep === CONST.WALLET.STEP.ONFIDO) {
Onyx.merge(ONYXKEYS.WALLET_ONFIDO, {error: '', loading: true});
} else if (currentStep === CONST.WALLET.STEP.ADDITIONAL_DETAILS) {
setAdditionalDetailsStep(false);
} else if (currentStep === CONST.WALLET.STEP.TERMS) {
Onyx.merge(ONYXKEYS.WALLET_TERMS, {loading: false});
}
});
}
/**
* Fetches information about a user's Expensify Wallet
*
* @typedef {Object} UserWallet
* @property {Number} availableBalance
* @property {Number} currentBalance
* @property {String} currentStep - used to track which step of the "activate wallet" flow a user is in
* @property {('SILVER'|'GOLD')} tierName - will be GOLD when fully activated. SILVER is able to recieve funds only.
*/
function fetchUserWallet() {
API.Get({returnValueList: 'userWallet'})
.then((response) => {
if (response.jsonCode !== 200) {
return;
}
Onyx.merge(ONYXKEYS.USER_WALLET, response.userWallet);
});
}
/**
* Fetch the bank account currently being set up by the user for the free plan if it exists.
*
* @param {String} [stepToOpen]
*/
function fetchFreePlanVerifiedBankAccount(stepToOpen) {
// Remember which account BankAccountStep subStep the user had before so we can set it later
const subStep = lodashGet(reimbursementAccountInSetup, 'subStep', '');
// We are using set here since we will rely on data from the server (not local data) to populate the VBA flow
// and determine which step to navigate to.
Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: true, error: ''});
let bankAccountID;
API.Get({
returnValueList: 'nameValuePairs',
name: CONST.NVP.FREE_PLAN_BANK_ACCOUNT_ID,
})
.then((response) => {
bankAccountID = lodashGet(response, ['nameValuePairs', CONST.NVP.FREE_PLAN_BANK_ACCOUNT_ID,
], '');
const failedValidationAttemptsName = CONST.NVP.FAILED_BANK_ACCOUNT_VALIDATIONS_PREFIX + bankAccountID;
// Now that we have the bank account. Lets grab the rest of the bank info we need
API.Get({
returnValueList: 'nameValuePairs, bankAccountList',
nvpNames: [
failedValidationAttemptsName,
CONST.NVP.KYC_MIGRATION,
CONST.NVP.ACH_DATA_THROTTLED,
CONST.NVP.BANK_ACCOUNT_GET_THROTTLED,
].join(),
})
.then(({bankAccountList, nameValuePairs}) => {
// Users have a limited amount of attempts to get the validations amounts correct.
// Once exceeded, we need to block them from attempting to validate.
const failedValidationAttempts = lodashGet(nameValuePairs, failedValidationAttemptsName, 0);
const maxAttemptsReached = failedValidationAttempts > CONST.BANK_ACCOUNT.VERIFICATION_MAX_ATTEMPTS;
const kycVerificationsMigration = lodashGet(nameValuePairs, CONST.NVP.KYC_MIGRATION, '');
const throttledDate = lodashGet(nameValuePairs, CONST.NVP.ACH_DATA_THROTTLED, '');
const bankAccountJSON = _.find(bankAccountList, account => (
account.bankAccountID === bankAccountID
));
const bankAccount = bankAccountJSON ? new BankAccount(bankAccountJSON) : null;
const throttledHistoryCount = lodashGet(nameValuePairs, CONST.NVP.BANK_ACCOUNT_GET_THROTTLED, 0);
const isPlaidDisabled = throttledHistoryCount > CONST.BANK_ACCOUNT.PLAID.ALLOWED_THROTTLED_COUNT;
// Next we'll build the achData and save it to Onyx
// If the user is already setting up a bank account we will continue the flow for them
let currentStep = reimbursementAccountInSetup.currentStep;
const achData = bankAccount ? bankAccount.toACHData() : {};
if (!stepToOpen && achData.currentStep) {
// eslint-disable-next-line no-use-before-define
currentStep = getNextStepToComplete(achData);
}
achData.useOnfido = true;
achData.policyID = reimbursementAccountWorkspaceID || '';
achData.isInSetup = !bankAccount || bankAccount.isInSetup();
achData.bankAccountInReview = bankAccount && bankAccount.isVerifying();
achData.domainLimit = 0;
// If the bank account has already been created in the db and is not yet open
// let's show the manual form with the previously added values. Otherwise, we will
// make the subStep the previous value.
if (bankAccount && bankAccount.isInSetup()) {
achData.subStep = CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL;
} else {
achData.subStep = subStep;
}
// If we're not in setup, it means we already have a withdrawal account
// and we're upgrading it to a business bank account. So let the user
// review all steps with all info prefilled and editable, unless a specific step was passed.
if (!achData.isInSetup) {
// @TODO Not sure if we need to do this since for
// NewDot none of the accounts are pre-existing ones
currentStep = '';
}
// Temporary fix for Onfido flow. Can be removed by nkuoch after Sept 1 2020.
// @TODO not sure if we still need this or what this is about, but seems like maybe yes...
if (currentStep === CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT && achData.useOnfido) {
const onfidoResponse = lodashGet(
achData,
CONST.BANK_ACCOUNT.VERIFICATIONS.REQUESTOR_IDENTITY_ONFIDO,
);
const sdkToken = lodashGet(onfidoResponse, CONST.BANK_ACCOUNT.ONFIDO_RESPONSE.SDK_TOKEN);
if (sdkToken && !achData.isOnfidoSetupComplete
&& onfidoResponse.status !== CONST.BANK_ACCOUNT.ONFIDO_RESPONSE.PASS
) {
currentStep = CONST.BANK_ACCOUNT.STEP.REQUESTOR;
}
}
// Ensure we route the user to the correct step based on the status of their bank account
if (bankAccount && !currentStep) {
currentStep = bankAccount.isPending() || bankAccount.isVerifying()
? CONST.BANK_ACCOUNT.STEP.VALIDATION
: CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
// @TODO Again, not sure how much of this logic is needed right now
// as we shouldn't be handling any open accounts in New Expensify yet that need to pass any more
// checks or can be upgraded, but leaving in for possible future compatibility.
if (bankAccount.isOpen()) {
if (bankAccount.needsToPassLatestChecks()) {
const hasTriedToUpgrade = bankAccount.getDateSigned()
> (kycVerificationsMigration || '2020-01-13');
currentStep = hasTriedToUpgrade
? CONST.BANK_ACCOUNT.STEP.VALIDATION : CONST.BANK_ACCOUNT.STEP.COMPANY;
achData.bankAccountInReview = hasTriedToUpgrade;
} else {
// We do not show a specific view for the EnableStep since we
// will enable the Expensify card automatically. However, we will still handle
// that step and show the Validate view.
currentStep = CONST.BANK_ACCOUNT.STEP.ENABLE;
}
}
}
// If at this point we still don't have a current step, default to the BankAccountStep
if (!currentStep) {
currentStep = CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
}
// If we are providing a stepToOpen via a deep link then we will always navigate to that step. This
// should be used with caution as it is possible to drop a user into a flow they can't complete e.g.
// if we drop the user into the CompanyStep, but they have no accountNumber or routing Number in
// their achData.
if (stepToOpen) {
currentStep = stepToOpen;
}
// 'error' displays any string set as an error encountered during the add Verified BBA flow.
// If we are fetching a bank account, clear the error to reset.
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {
throttledDate, maxAttemptsReached, error: '', isPlaidDisabled,
});
goToWithdrawalAccountSetupStep(currentStep, achData);
})
.finally(() => {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
});
});
}
const WITHDRAWAL_ACCOUNT_STEPS = [
{
id: CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT,
title: 'Bank Account',
},
{
id: CONST.BANK_ACCOUNT.STEP.COMPANY,
title: 'Company Information',
},
{
id: CONST.BANK_ACCOUNT.STEP.REQUESTOR,
title: 'Requestor Information',
},
{
id: CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT,
title: 'Beneficial Owners',
},
{
id: CONST.BANK_ACCOUNT.STEP.VALIDATION,
title: 'Validate',
},
{
id: CONST.BANK_ACCOUNT.STEP.ENABLE,
title: 'Enable',
},
];
/**
* Get step position in the array
* @private
* @param {String} stepID
* @return {Number}
*/
function getIndexByStepID(stepID) {
return _.findIndex(WITHDRAWAL_ACCOUNT_STEPS, step => step.id === stepID);
}
/**
* Get next step ID
* @param {String} [stepID]
* @return {String}
*/
function getNextStepID(stepID) {
const nextStepIndex = Math.min(
getIndexByStepID(stepID || reimbursementAccountInSetup.currentStep) + 1,
WITHDRAWAL_ACCOUNT_STEPS.length - 1,
);
return lodashGet(WITHDRAWAL_ACCOUNT_STEPS, [nextStepIndex, 'id'], CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT);
}
/**
* @param {Object} achData
* @returns {String}
*/
function getNextStepToComplete(achData) {
if (achData.currentStep === CONST.BANK_ACCOUNT.STEP.REQUESTOR && !achData.isOnfidoSetupComplete) {
return CONST.BANK_ACCOUNT.STEP.REQUESTOR;
}
return getNextStepID(achData.currentStep);
}
/**
* @private
* @param {Number} bankAccountID
*/
function setFreePlanVerifiedBankAccountID(bankAccountID) {
API.SetNameValuePair({name: CONST.NVP.FREE_PLAN_BANK_ACCOUNT_ID, value: bankAccountID});
}
/**
* Show error modal and optionally a specific error message
*
* @param {String} errorModalMessage The error message to be displayed in the modal's body.
*/
function showBankAccountErrorModal(errorModalMessage = null) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {errorModalMessage});
}
/**
* @param {Number} bankAccountID
* @param {String} validateCode
*/
function validateBankAccount(bankAccountID, validateCode) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: true});
API.BankAccount_Validate({bankAccountID, validateCode})
.then((response) => {
if (response.jsonCode === 200) {
Growl.show('Bank Account successfully validated!', CONST.GROWL.SUCCESS, 5000);
Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT, null);
API.User_IsUsingExpensifyCard()
.then(({isUsingExpensifyCard}) => {
const reimbursementAccount = {
loading: false,
error: '',
achData: {state: BankAccount.STATE.OPEN},
};
reimbursementAccount.achData.currentStep = CONST.BANK_ACCOUNT.STEP.ENABLE;
Onyx.merge(ONYXKEYS.USER, {isUsingExpensifyCard});
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, reimbursementAccount);
});
return;
}
// User has input the validate code incorrectly many times so we will return early in this case and not let them enter the amounts again.
if (response.message === CONST.BANK_ACCOUNT.ERROR.MAX_VALIDATION_ATTEMPTS_REACHED) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false, maxAttemptsReached: true});
return;
}
// If the validation amounts entered were incorrect, show specific error
if (response.message === CONST.BANK_ACCOUNT.ERROR.INCORRECT_VALIDATION_AMOUNTS) {
showBankAccountErrorModal(translateLocal('bankAccount.error.validationAmounts'));
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
return;
}
// We are generically showing any other backend errors that might pop up in the validate step
showBankAccountErrorModal(response.message);
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
});
}
/**
* Set the current fields with errors.
*
* @param {String} errors
*/
function setBankAccountFormValidationErrors(errors) {
// We set 'errors' to null first because we don't have a way yet to replace a specific property like 'errors' without merging it
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {errors: null});
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {errors});
}
/**
* Set the current error message.
*
* @param {String} error
*/
function showBankAccountFormValidationError(error) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {error});
}
/**
* Set the current sub step in first step of adding withdrawal bank account
*
* @param {String} subStep - One of {CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL, CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID, null}
*/
function setBankAccountSubStep(subStep) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData: {subStep}});
}
/**
* Create or update the bank account in db with the updated data.
*
* @param {Object} [data]
*/
function setupWithdrawalAccount(data) {
let nextStep;
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: true, errorModalMessage: '', errors: null});
const newACHData = {
...reimbursementAccountInSetup,
...data,
// This param tells Web-Secure that this bank account is from NewDot so we can modify links back to the correct
// app in any communications. It also will be used to provision a customer for the Expensify card automatically
// once their bank account is successfully validated.
enableCardAfterVerified: true,
};
if (data && !_.isUndefined(data.isSavings)) {
newACHData.isSavings = Boolean(data.isSavings);
}
if (!newACHData.setupType) {
newACHData.setupType = newACHData.plaidAccountID
? CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID
: CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL;
}
nextStep = newACHData.currentStep;
// If we are setting up a Plaid account replace the accountNumber with the unmasked number
if (data.plaidAccountID) {
const unmaskedAccount = _.find(plaidBankAccounts, bankAccount => (
bankAccount.plaidAccountID === data.plaidAccountID
));
newACHData.accountNumber = unmaskedAccount.accountNumber;
}
API.BankAccount_SetupWithdrawal(newACHData)
.then((response) => {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData: {...newACHData}});
const currentStep = newACHData.currentStep;
let achData = lodashGet(response, 'achData', {});
let error = lodashGet(achData, CONST.BANK_ACCOUNT.VERIFICATIONS.ERROR_MESSAGE);
const errors = {};
if (response.jsonCode === 200 && !error) {
// Save an NVP with the bankAccountID for this account. This is temporary since we are not showing lists
// of accounts yet and must have some kind of record of which account is the one the user is trying to
// set up for the free plan.
if (achData.bankAccountID) {
setFreePlanVerifiedBankAccountID(achData.bankAccountID);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.REQUESTOR) {
const requestorResponse = lodashGet(
achData,
CONST.BANK_ACCOUNT.VERIFICATIONS.REQUESTOR_IDENTITY_ID,
);
if (newACHData.useOnfido) {
const onfidoResponse = lodashGet(
achData,
CONST.BANK_ACCOUNT.VERIFICATIONS.REQUESTOR_IDENTITY_ONFIDO,
);
const sdkToken = lodashGet(onfidoResponse, CONST.BANK_ACCOUNT.ONFIDO_RESPONSE.SDK_TOKEN);
if (sdkToken && !newACHData.isOnfidoSetupComplete
&& onfidoResponse.status !== CONST.BANK_ACCOUNT.ONFIDO_RESPONSE.PASS
) {
// Requestor Step still needs to run Onfido
achData.sdkToken = sdkToken;
goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.REQUESTOR, achData);
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
return;
}
} else if (requestorResponse) {
// Don't go to next step if Requestor Step needs to ask some questions
let questions = lodashGet(requestorResponse, CONST.BANK_ACCOUNT.QUESTIONS.QUESTION) || [];
if (_.isEmpty(questions)) {
const differentiatorQuestion = lodashGet(
requestorResponse,
CONST.BANK_ACCOUNT.QUESTIONS.DIFFERENTIATOR_QUESTION,
);
if (differentiatorQuestion) {
questions = [differentiatorQuestion];
}
}
if (!_.isEmpty(questions)) {
achData.questions = questions;
goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.REQUESTOR, achData);
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
return;
}
}
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT) {
// Get an up-to-date bank account list so that we can allow the user to validate their newly
// generated bank account
API.Get({returnValueList: 'bankAccountList'})
.then((bankAccountListResponse) => {
const bankAccountJSON = _.findWhere(bankAccountListResponse.bankAccountList, {
bankAccountID: newACHData.bankAccountID,
});
const bankAccount = new BankAccount(bankAccountJSON);
achData = bankAccount.toACHData();
const needsToPassLatestChecks = achData.state === BankAccount.STATE.OPEN
&& achData.needsToPassLatestChecks;
achData.bankAccountInReview = needsToPassLatestChecks
|| achData.state === BankAccount.STATE.VERIFYING;
goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.VALIDATION, achData);
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
});
return;
}
if ((currentStep === CONST.BANK_ACCOUNT.STEP.VALIDATION && newACHData.bankAccountInReview)
|| currentStep === CONST.BANK_ACCOUNT.STEP.ENABLE
) {
// Setup done!
} else {
nextStep = getNextStepID();
}
} else {
if (response.jsonCode === 666 || response.jsonCode === 404) {
error = response.message;
}
if (response.jsonCode === 402) {
if (response.message === CONST.BANK_ACCOUNT.ERROR.MISSING_ROUTING_NUMBER
|| response.message === CONST.BANK_ACCOUNT.ERROR.MAX_ROUTING_NUMBER
) {
errors.routingNumber = true;
achData.subStep = CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL;
} else if (response.message === CONST.BANK_ACCOUNT.ERROR.MISSING_INCORPORATION_STATE) {
error = translateLocal('bankAccount.error.incorporationState');
} else if (response.message === CONST.BANK_ACCOUNT.ERROR.MISSING_INCORPORATION_TYPE) {
error = translateLocal('bankAccount.error.companyType');
} else {
console.error(response.message);
}
}
}
// Go to next step
goToWithdrawalAccountSetupStep(nextStep, achData);
if (_.size(errors)) {
setBankAccountFormValidationErrors(errors);
showBankAccountErrorModal();
}
if (error) {
showBankAccountFormValidationError(error);
showBankAccountErrorModal(error);
}
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
})
.catch((response) => {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false, achData: {...newACHData}});
console.error(response.stack);
showBankAccountErrorModal(translateLocal('common.genericErrorMessage'));
});
}
function hideBankAccountErrors() {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {error: '', errors: null});
}
function setWorkspaceIDForReimbursementAccount(workspaceID) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT_WORKSPACE_ID, workspaceID);
}
/**
* @param {Object} bankAccountData
*/
function updateReimbursementAccountDraft(bankAccountData) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT, bankAccountData);
}
export {
activateWallet,
addPersonalBankAccount,
clearPlaidBankAccountsAndToken,
fetchFreePlanVerifiedBankAccount,
fetchOnfidoToken,
fetchPlaidLinkToken,
fetchUserWallet,
getPlaidBankAccounts,
goToWithdrawalAccountSetupStep,
setupWithdrawalAccount,
validateBankAccount,
hideBankAccountErrors,
showBankAccountErrorModal,
showBankAccountFormValidationError,
setBankAccountFormValidationErrors,
setWorkspaceIDForReimbursementAccount,
setBankAccountSubStep,
updateReimbursementAccountDraft,
};