-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PaymentUtils.js
108 lines (96 loc) · 3.98 KB
/
PaymentUtils.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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import BankAccount from './models/BankAccount';
import * as Expensicons from '../components/Icon/Expensicons';
import getBankIcon from '../components/Icon/BankIcons';
import CONST from '../CONST';
import * as Localize from './Localize';
/**
* Check to see if user has either a debit card or personal bank account added
*
* @param {Array} [fundList]
* @param {Array} [bankAccountList]
* @returns {Boolean}
*/
function hasExpensifyPaymentMethod(fundList = [], bankAccountList = []) {
const validBankAccount = _.some(bankAccountList, (bankAccountJSON) => {
const bankAccount = new BankAccount(bankAccountJSON);
return bankAccount.isDefaultCredit();
});
// Hide any billing cards that are not P2P debit cards for now because you cannot make them your default method, or delete them
const validDebitCard = _.some(fundList, (card) => lodashGet(card, 'accountData.additionalData.isP2PDebitCard', false));
return validBankAccount || validDebitCard;
}
/**
* @param {String} [accountType] - one of {'bankAccount', 'debitCard', 'payPalMe'}
* @param {Object} account
* @returns {String}
*/
function getPaymentMethodDescription(accountType, account) {
if (accountType === CONST.PAYMENT_METHODS.PAYPAL) {
return account.username;
}
if (accountType === CONST.PAYMENT_METHODS.BANK_ACCOUNT) {
return `${Localize.translateLocal('paymentMethodList.accountLastFour')} ${account.accountNumber.slice(-4)}`;
}
if (accountType === CONST.PAYMENT_METHODS.DEBIT_CARD) {
return `${Localize.translateLocal('paymentMethodList.cardLastFour')} ${account.cardNumber.slice(-4)}`;
}
return '';
}
/**
* Get the PaymentMethods list
* @param {Array} bankAccountList
* @param {Array} fundList
* @param {Object} [payPalMeData = null]
* @returns {Array<PaymentMethod>}
*/
function formatPaymentMethods(bankAccountList, fundList, payPalMeData = null) {
const combinedPaymentMethods = [];
_.each(bankAccountList, (bankAccount) => {
// Add all bank accounts besides the wallet
if (bankAccount.type === CONST.BANK_ACCOUNT_TYPES.WALLET) {
return;
}
const {icon, iconSize} = getBankIcon(lodashGet(bankAccount, 'accountData.additionalData.bankName', ''));
combinedPaymentMethods.push({
...bankAccount,
description: getPaymentMethodDescription(bankAccount.accountType, bankAccount.accountData),
icon,
iconSize,
errors: bankAccount.errors,
pendingAction: bankAccount.pendingAction,
});
});
_.each(fundList, (card) => {
const {icon, iconSize} = getBankIcon(lodashGet(card, 'accountData.bank', ''), true);
combinedPaymentMethods.push({
...card,
description: getPaymentMethodDescription(card.accountType, card.accountData),
icon,
iconSize,
errors: card.errors,
pendingAction: card.pendingAction,
});
});
if (!_.isEmpty(payPalMeData)) {
combinedPaymentMethods.push({
...payPalMeData,
description: getPaymentMethodDescription(payPalMeData.accountType, payPalMeData.accountData),
icon: Expensicons.PayPal,
});
}
return combinedPaymentMethods;
}
/**
* @param {Number} currentBalance, in cents
* @param {String} methodType
* @returns {Number} the fee, in cents
*/
function calculateWalletTransferBalanceFee(currentBalance, methodType) {
const transferMethodTypeFeeStructure =
methodType === CONST.WALLET.TRANSFER_METHOD_TYPE.INSTANT ? CONST.WALLET.TRANSFER_METHOD_TYPE_FEE.INSTANT : CONST.WALLET.TRANSFER_METHOD_TYPE_FEE.ACH;
const calculateFee = Math.ceil(currentBalance * (transferMethodTypeFeeStructure.RATE / 100));
return Math.max(calculateFee, transferMethodTypeFeeStructure.MINIMUM_FEE);
}
export {hasExpensifyPaymentMethod, getPaymentMethodDescription, formatPaymentMethods, calculateWalletTransferBalanceFee};