-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathBankAccounts.ts
577 lines (526 loc) · 19.3 KB
/
BankAccounts.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
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
import Onyx from 'react-native-onyx';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import type {OnfidoDataWithApplicantID} from '@components/Onfido/types';
import * as API from '@libs/API';
import type {
AddPersonalBankAccountParams,
BankAccountHandlePlaidErrorParams,
ConnectBankAccountParams,
DeletePaymentBankAccountParams,
OpenReimbursementAccountPageParams,
ValidateBankAccountWithTransactionsParams,
VerifyIdentityForBankAccountParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as Localize from '@libs/Localize';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Route} from '@src/ROUTES';
import type {PersonalBankAccountForm} from '@src/types/form';
import type {ACHContractStepProps, BeneficialOwnersStepProps, CompanyStepProps, RequestorStepProps} from '@src/types/form/ReimbursementAccountForm';
import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount';
import type {BankAccountStep, BankAccountSubStep} from '@src/types/onyx/ReimbursementAccount';
import type {OnyxData} from '@src/types/onyx/Request';
import * as ReimbursementAccount from './ReimbursementAccount';
export {
goToWithdrawalAccountSetupStep,
setBankAccountFormValidationErrors,
resetReimbursementAccount,
resetFreePlanBankAccount,
hideBankAccountErrors,
setBankAccountSubStep,
updateReimbursementAccountDraft,
requestResetFreePlanBankAccount,
cancelResetFreePlanBankAccount,
} from './ReimbursementAccount';
export {openPlaidBankAccountSelector, openPlaidBankLogin} from './Plaid';
export {openOnfidoFlow, answerQuestionsForWallet, verifyIdentity, acceptWalletTerms} from './Wallet';
type ReimbursementAccountStep = BankAccountStep | '';
type ReimbursementAccountSubStep = BankAccountSubStep | '';
type AccountFormValues = typeof ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM | typeof ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM;
type BusinessAddress = {
addressStreet?: string;
addressCity?: string;
addressState?: string;
addressZipCode?: string;
};
type PersonalAddress = {
requestorAddressStreet?: string;
requestorAddressCity?: string;
requestorAddressState?: string;
requestorAddressZipCode?: string;
};
function clearPlaid(): Promise<void | void[]> {
Onyx.set(ONYXKEYS.PLAID_LINK_TOKEN, '');
Onyx.set(ONYXKEYS.PLAID_CURRENT_EVENT, null);
return Onyx.set(ONYXKEYS.PLAID_DATA, CONST.PLAID.DEFAULT_DATA);
}
function openPlaidView() {
clearPlaid().then(() => ReimbursementAccount.setBankAccountSubStep(CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID));
}
function setPlaidEvent(eventName: string | null) {
Onyx.set(ONYXKEYS.PLAID_CURRENT_EVENT, eventName);
}
/**
* Open the personal bank account setup flow, with an optional exitReportID to redirect to once the flow is finished.
*/
function openPersonalBankAccountSetupView(exitReportID?: string) {
clearPlaid().then(() => {
if (exitReportID) {
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {exitReportID});
}
Navigation.navigate(ROUTES.SETTINGS_ADD_BANK_ACCOUNT);
});
}
/**
* Open the personal bank account setup flow using Plaid, with an optional exitReportID to redirect to once the flow is finished.
*/
function openPersonalBankAccountSetupWithPlaid(exitReportID?: string) {
clearPlaid().then(() => {
if (exitReportID) {
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {exitReportID});
}
Onyx.merge(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, {setupType: CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID});
});
}
function clearPersonalBankAccountSetupType() {
Onyx.merge(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, {setupType: null});
}
/**
* Whether after adding a bank account we should continue with the KYC flow. If so, we must specify the fallback route.
*/
function setPersonalBankAccountContinueKYCOnSuccess(onSuccessFallbackRoute: Route) {
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {onSuccessFallbackRoute});
}
function clearPersonalBankAccount() {
clearPlaid();
Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, null);
Onyx.set(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, null);
clearPersonalBankAccountSetupType();
}
function clearOnfidoToken() {
Onyx.merge(ONYXKEYS.ONFIDO_TOKEN, '');
Onyx.merge(ONYXKEYS.ONFIDO_APPLICANT_ID, '');
}
function updateAddPersonalBankAccountDraft(bankData: Partial<PersonalBankAccountForm>) {
Onyx.merge(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, bankData);
}
/**
* Helper method to build the Onyx data required during setup of a Verified Business Bank Account
*/
function getVBBADataForOnyx(currentStep?: BankAccountStep): OnyxData {
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.getMicroSecondOnyxErrorWithTranslationKey('walletPage.addBankAccountFailure'),
},
},
],
};
}
function addBusinessWebsiteForDraft(websiteUrl: string) {
Onyx.merge(ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM_DRAFT, {website: websiteUrl});
}
/**
* Submit Bank Account step with Plaid data so php can perform some checks.
*/
function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAccount: PlaidBankAccount, policyID: string) {
const parameters: ConnectBankAccountParams = {
bankAccountID,
routingNumber: selectedPlaidBankAccount.routingNumber,
accountNumber: selectedPlaidBankAccount.accountNumber,
bank: selectedPlaidBankAccount.bankName,
plaidAccountID: selectedPlaidBankAccount.plaidAccountID,
plaidAccessToken: selectedPlaidBankAccount.plaidAccessToken,
plaidMask: selectedPlaidBankAccount.mask,
isSavings: selectedPlaidBankAccount.isSavings,
policyID,
};
API.write(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, parameters, getVBBADataForOnyx());
}
/**
* Adds a bank account via Plaid
*
* TODO: offline pattern for this command will have to be added later once the pattern B design doc is complete
*/
function addPersonalBankAccount(account: PlaidBankAccount) {
const parameters: AddPersonalBankAccountParams = {
addressName: account.addressName ?? '',
routingNumber: account.routingNumber,
accountNumber: account.accountNumber,
isSavings: account.isSavings ?? false,
setupType: 'plaid',
bank: account.bankName,
plaidAccountID: account.plaidAccountID,
plaidAccessToken: account.plaidAccessToken,
};
const onyxData: 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,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.USER_WALLET,
value: {
currentStep: CONST.WALLET.STEP.ADDITIONAL_DETAILS,
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_BANK_ACCOUNT,
value: {
isLoading: false,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('walletPage.addBankAccountFailure'),
},
},
],
};
API.write(WRITE_COMMANDS.ADD_PERSONAL_BANK_ACCOUNT, parameters, onyxData);
}
function deletePaymentBankAccount(bankAccountID: number) {
const parameters: DeletePaymentBankAccountParams = {bankAccountID};
const onyxData: OnyxData = {
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},
},
],
};
API.write(WRITE_COMMANDS.DELETE_PAYMENT_BANK_ACCOUNT, parameters, onyxData);
}
/**
* 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 bankAccountID - ID for bank account
* @param params - User personal data
* @param policyID - ID of the policy we're setting the bank account on
* @param isConfirmPage - If we're submitting from the confirmation substep, to trigger all external checks
*/
function updatePersonalInformationForBankAccount(bankAccountID: number, params: RequestorStepProps, policyID: string, isConfirmPage: boolean) {
API.write(
WRITE_COMMANDS.UPDATE_PERSONAL_INFORMATION_FOR_BANK_ACCOUNT,
{
...params,
bankAccountID,
policyID,
confirm: isConfirmPage,
},
getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.REQUESTOR),
);
}
function validateBankAccount(bankAccountID: number, validateCode: string, policyID: string) {
const parameters: ValidateBankAccountWithTransactionsParams = {
bankAccountID,
validateCode,
policyID,
};
const onyxData: OnyxData = {
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,
},
},
],
};
API.write(WRITE_COMMANDS.VALIDATE_BANK_ACCOUNT_WITH_TRANSACTIONS, parameters, onyxData);
}
function clearReimbursementAccount() {
Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT, null);
}
/**
* Function to display and fetch data for Reimbursement Account step
* @param stepToOpen - current step to open
* @param subStep - particular step
* @param localCurrentStep - last step on device
* @param policyID - policy ID
*/
function openReimbursementAccountPage(stepToOpen: ReimbursementAccountStep, subStep: ReimbursementAccountSubStep, localCurrentStep: ReimbursementAccountStep, policyID: string) {
const onyxData: 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 parameters: OpenReimbursementAccountPageParams = {
stepToOpen,
subStep,
localCurrentStep,
policyID,
};
return API.read(READ_COMMANDS.OPEN_REIMBURSEMENT_ACCOUNT_PAGE, parameters, onyxData);
}
/**
* Updates the bank account in the database with the company step data
* @param params - Business step form data
* @param policyID - ID of the policy we're setting the bank account on
* @param isConfirmPage - If we're submitting from the confirmation substep, to trigger all external checks
*/
function updateCompanyInformationForBankAccount(bankAccountID: number, params: Partial<CompanyStepProps>, policyID: string, isConfirmPage: boolean) {
API.write(
WRITE_COMMANDS.UPDATE_COMPANY_INFORMATION_FOR_BANK_ACCOUNT,
{
...params,
bankAccountID,
policyID,
confirm: isConfirmPage,
},
getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.COMPANY),
);
}
/**
* Add beneficial owners for the bank account and verify the accuracy of the information provided
* @param params - Beneficial Owners step form params
*/
function updateBeneficialOwnersForBankAccount(bankAccountID: number, params: Partial<BeneficialOwnersStepProps>, policyID: string) {
API.write(
WRITE_COMMANDS.UPDATE_BENEFICIAL_OWNERS_FOR_BANK_ACCOUNT,
{
...params,
bankAccountID,
policyID,
},
getVBBADataForOnyx(),
);
}
/**
* Accept the ACH terms and conditions and verify the accuracy of the information provided
* @param params - Verification step form params
*/
function acceptACHContractForBankAccount(bankAccountID: number, params: ACHContractStepProps, policyID: string) {
API.write(
WRITE_COMMANDS.ACCEPT_ACH_CONTRACT_FOR_BANK_ACCOUNT,
{
...params,
bankAccountID,
policyID,
},
getVBBADataForOnyx(),
);
}
/**
* Create the bank account with manually entered data.
*/
function connectBankAccountManually(bankAccountID: number, bankAccount: PlaidBankAccount, policyID: string) {
const parameters: ConnectBankAccountParams = {
bankAccountID,
routingNumber: bankAccount.routingNumber,
accountNumber: bankAccount.accountNumber,
bank: bankAccount.bankName,
plaidAccountID: bankAccount.plaidAccountID,
plaidAccessToken: bankAccount.plaidAccessToken,
plaidMask: bankAccount.mask,
isSavings: bankAccount.isSavings,
policyID,
};
API.write(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_MANUALLY, parameters, getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT));
}
/**
* Verify the user's identity via Onfido
*/
function verifyIdentityForBankAccount(bankAccountID: number, onfidoData: OnfidoDataWithApplicantID, policyID?: string) {
const parameters: VerifyIdentityForBankAccountParams = {
bankAccountID,
onfidoData: JSON.stringify(onfidoData),
policyID: policyID ?? '-1',
};
API.write(WRITE_COMMANDS.VERIFY_IDENTITY_FOR_BANK_ACCOUNT, parameters, getVBBADataForOnyx());
}
function openWorkspaceView(policyID: string) {
API.read(
READ_COMMANDS.OPEN_WORKSPACE_VIEW,
{
policyID,
},
{
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,
},
},
],
},
);
}
function handlePlaidError(bankAccountID: number, error: string, errorDescription: string, plaidRequestID: string) {
const parameters: BankAccountHandlePlaidErrorParams = {
bankAccountID,
error,
errorDescription,
plaidRequestID,
};
API.write(WRITE_COMMANDS.BANK_ACCOUNT_HANDLE_PLAID_ERROR, parameters);
}
/**
* Set the reimbursement account loading so that it happens right away, instead of when the API command is processed.
*/
function setReimbursementAccountLoading(isLoading: boolean) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {isLoading});
}
function validatePlaidSelection(values: FormOnyxValues<AccountFormValues>): FormInputErrors<AccountFormValues> {
const errorFields: FormInputErrors<AccountFormValues> = {};
if (!values.selectedPlaidAccountID) {
errorFields.selectedPlaidAccountID = Localize.translateLocal('bankAccount.error.youNeedToSelectAnOption');
}
return errorFields;
}
export {
acceptACHContractForBankAccount,
addBusinessWebsiteForDraft,
addPersonalBankAccount,
clearOnfidoToken,
clearPersonalBankAccount,
clearPlaid,
setPlaidEvent,
openPlaidView,
connectBankAccountManually,
connectBankAccountWithPlaid,
deletePaymentBankAccount,
handlePlaidError,
setPersonalBankAccountContinueKYCOnSuccess,
openPersonalBankAccountSetupView,
clearReimbursementAccount,
openReimbursementAccountPage,
updateBeneficialOwnersForBankAccount,
updateCompanyInformationForBankAccount,
updatePersonalInformationForBankAccount,
openWorkspaceView,
validateBankAccount,
verifyIdentityForBankAccount,
setReimbursementAccountLoading,
openPersonalBankAccountSetupWithPlaid,
updateAddPersonalBankAccountDraft,
clearPersonalBankAccountSetupType,
validatePlaidSelection,
};
export type {BusinessAddress, PersonalAddress};