-
Notifications
You must be signed in to change notification settings - Fork 3k
/
BankAccounts.js
123 lines (118 loc) · 3.93 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
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import CONST from '../../CONST';
import * as API from '../API';
import * as Plaid from './Plaid';
import * as ReimbursementAccount from './ReimbursementAccount';
import ONYXKEYS from '../../ONYXKEYS';
import * as PaymentMethods from './PaymentMethods';
import Growl from '../Growl';
import * as Localize from '../Localize';
export {
setupWithdrawalAccount,
fetchFreePlanVerifiedBankAccount,
goToWithdrawalAccountSetupStep,
showBankAccountErrorModal,
showBankAccountFormValidationError,
setBankAccountFormValidationErrors,
resetFreePlanBankAccount,
validateBankAccount,
hideBankAccountErrors,
setWorkspaceIDForReimbursementAccount,
setBankAccountSubStep,
updateReimbursementAccountDraft,
requestResetFreePlanBankAccount,
cancelResetFreePlanBankAccount,
} from './ReimbursementAccount';
export {
fetchPlaidBankAccounts,
clearPlaidBankAccountsAndToken,
fetchPlaidLinkToken,
getPlaidBankAccounts,
getBankName,
getPlaidAccessToken,
} from './Plaid';
export {
fetchOnfidoToken,
activateWallet,
fetchUserWallet,
} from './Wallet';
/**
* Adds a bank account via Plaid
*
* @param {Object} account
* @param {String} password
* @param {String} plaidLinkToken
*/
function addPersonalBankAccount(account, password, plaidLinkToken) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: true});
const unmaskedAccount = _.find(Plaid.getPlaidBankAccounts(), 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: Plaid.getBankName(),
plaidAccountID: unmaskedAccount.plaidAccountID,
ownershipType: '',
acceptTerms: true,
country: 'US',
currency: CONST.CURRENCY.USD,
fieldsType: 'local',
plaidAccessToken: Plaid.getPlaidAccessToken(),
}),
})
.then((response) => {
if (response.jsonCode === 200) {
PaymentMethods.getPaymentMethods()
.then(() => {
PaymentMethods.continueSetup();
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
});
return;
}
if (response.message === 'Incorrect Expensify password entered') {
ReimbursementAccount.setBankAccountFormValidationErrors({password: true});
}
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
})
.catch(() => {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {loading: false});
});
}
/**
* Deletes a bank account
*
* @param {Number} bankAccountID
*/
function deleteBankAccount(bankAccountID) {
API.DeleteBankAccount({
bankAccountID,
}).then((response) => {
if (response.jsonCode === 200) {
Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, {[bankAccountID]: null});
Growl.show(Localize.translateLocal('paymentsPage.deleteBankAccountSuccess'), CONST.GROWL.SUCCESS, 3000);
} else {
Growl.show(Localize.translateLocal('common.genericErrorMessage'), CONST.GROWL.ERROR, 3000);
}
}).catch(() => {
Growl.show(Localize.translateLocal('common.genericErrorMessage'), CONST.GROWL.ERROR, 3000);
});
}
export {
addPersonalBankAccount,
deleteBankAccount,
};