-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPaymentMethodList.js
266 lines (229 loc) · 8.81 KB
/
PaymentMethodList.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
import _ from 'underscore';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {FlatList} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import styles from '../../../styles/styles';
import * as StyleUtils from '../../../styles/StyleUtils';
import MenuItem from '../../../components/MenuItem';
import Button from '../../../components/Button';
import Text from '../../../components/Text';
import compose from '../../../libs/compose';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import ONYXKEYS from '../../../ONYXKEYS';
import CONST from '../../../CONST';
import * as Expensicons from '../../../components/Icon/Expensicons';
import bankAccountPropTypes from '../../../components/bankAccountPropTypes';
import * as PaymentUtils from '../../../libs/PaymentUtils';
const MENU_ITEM = 'menuItem';
const BUTTON = 'button';
const propTypes = {
/** What to do when a menu item is pressed */
onPress: PropTypes.func.isRequired,
/** User's paypal.me username if they have one */
payPalMeUsername: PropTypes.string,
/** List of bank accounts */
bankAccountList: PropTypes.objectOf(bankAccountPropTypes),
/** List of cards */
cardList: PropTypes.objectOf(PropTypes.shape({
/** The name of the institution (bank of america, etc */
cardName: PropTypes.string,
/** The masked credit card number */
cardNumber: PropTypes.string,
/** The ID of the card in the cards DB */
cardID: PropTypes.number,
})),
/** Whether the add Payment button be shown on the list */
shouldShowAddPaymentMethodButton: PropTypes.bool,
/** Type to filter the payment Method list */
filterType: PropTypes.oneOf([CONST.PAYMENT_METHODS.DEBIT_CARD, CONST.PAYMENT_METHODS.BANK_ACCOUNT, '']),
/** User wallet props */
userWallet: PropTypes.shape({
/** The ID of the linked account */
walletLinkedAccountID: PropTypes.number,
/** The type of the linked account (debitCard or bankAccount) */
walletLinkedAccountType: PropTypes.string,
}),
/** Type of active/highlighted payment method */
actionPaymentMethodType: PropTypes.oneOf([..._.values(CONST.PAYMENT_METHODS), '']),
/** ID of active/highlighted payment method */
activePaymentMethodID: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** ID of selected payment method */
selectedMethodID: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
...withLocalizePropTypes,
};
const defaultProps = {
payPalMeUsername: '',
bankAccountList: {},
cardList: {},
userWallet: {
walletLinkedAccountID: 0,
walletLinkedAccountType: '',
},
shouldShowAddPaymentMethodButton: true,
filterType: '',
actionPaymentMethodType: '',
activePaymentMethodID: '',
selectedMethodID: '',
};
class PaymentMethodList extends Component {
constructor(props) {
super(props);
this.renderItem = this.renderItem.bind(this);
}
/**
* @param {Boolean} isDefault
* @returns {*}
*/
getDefaultBadgeText(isDefault = false) {
if (!isDefault) {
return null;
}
const defaultablePaymentMethodCount = _.reduce(this.getFilteredPaymentMethods(), (count, method) => (
(method.accountType === CONST.PAYMENT_METHODS.BANK_ACCOUNT || method.accountType === CONST.PAYMENT_METHODS.DEBIT_CARD)
? count + 1
: count
), 0);
if (defaultablePaymentMethodCount <= 1) {
return null;
}
return this.props.translate('paymentMethodList.defaultPaymentMethod');
}
/**
* @returns {Array}
*/
getFilteredPaymentMethods() {
// Hide the billing card from the payments menu for now because you can't make it your default method, or delete it
const filteredCardList = _.filter(this.props.cardList, card => !card.additionalData.isBillingCard);
let combinedPaymentMethods = PaymentUtils.formatPaymentMethods(this.props.bankAccountList, filteredCardList, this.props.payPalMeUsername, this.props.userWallet);
if (!_.isEmpty(this.props.filterType)) {
combinedPaymentMethods = _.filter(combinedPaymentMethods, paymentMethod => paymentMethod.accountType === this.props.filterType);
}
combinedPaymentMethods = _.map(combinedPaymentMethods, paymentMethod => ({
...paymentMethod,
type: MENU_ITEM,
onPress: e => this.props.onPress(e, paymentMethod.accountType, paymentMethod.accountData, paymentMethod.isDefault),
iconFill: this.isPaymentMethodActive(paymentMethod) ? StyleUtils.getIconFillColor(CONST.BUTTON_STATES.PRESSED) : null,
wrapperStyle: this.isPaymentMethodActive(paymentMethod) ? [StyleUtils.getButtonBackgroundColorStyle(CONST.BUTTON_STATES.PRESSED)] : null,
}));
return combinedPaymentMethods;
}
/**
* Take all of the different payment methods and create a list that can be easily digested by renderItem
*
* @returns {Array}
*/
createPaymentMethodList() {
const combinedPaymentMethods = this.getFilteredPaymentMethods();
// If we have not added any payment methods, show a default empty state
if (_.isEmpty(combinedPaymentMethods)) {
combinedPaymentMethods.push({
key: 'addFirstPaymentMethodHelpText',
text: this.props.translate('paymentMethodList.addFirstPaymentMethod'),
});
}
if (!this.props.shouldShowAddPaymentMethodButton) {
return combinedPaymentMethods;
}
combinedPaymentMethods.push({
type: BUTTON,
text: this.props.translate('paymentMethodList.addPaymentMethod'),
icon: Expensicons.CreditCard,
style: [styles.mh4],
iconStyles: [styles.mr4],
onPress: e => this.props.onPress(e),
isDisabled: this.props.isLoadingPayments,
shouldShowRightIcon: true,
success: true,
key: 'addPaymentMethodButton',
});
return combinedPaymentMethods;
}
/**
* @param {Object} paymentMethod
* @param {String|Number} paymentMethod.methodID
* @param {String} paymentMethod.accountType
* @return {Boolean}
*/
isPaymentMethodActive(paymentMethod) {
return paymentMethod.accountType === this.props.actionPaymentMethodType && paymentMethod.methodID === this.props.activePaymentMethodID;
}
/**
* Create a menuItem for each passed paymentMethod
*
* @param {Object} params
* @param {Object} params.item
*
* @return {React.Component}
*/
renderItem({item}) {
if (item.type === MENU_ITEM) {
return (
<MenuItem
onPress={item.onPress}
title={item.title}
description={item.description}
icon={item.icon}
disabled={item.disabled}
iconFill={item.iconFill}
iconHeight={item.iconSize}
iconWidth={item.iconSize}
badgeText={this.getDefaultBadgeText(item.isDefault)}
wrapperStyle={item.wrapperStyle}
shouldShowSelectedState={this.props.shouldShowSelectedState}
isSelected={this.props.selectedMethodID === item.methodID}
/>
);
}
if (item.type === BUTTON) {
return (
<Button
text={item.text}
icon={item.icon}
onPress={item.onPress}
isDisabled={item.isDisabled}
style={item.style}
iconStyles={item.iconStyles}
success={item.success}
shouldShowRightIcon={item.shouldShowRightIcon}
extraLarge
/>
);
}
return (
<Text
style={[styles.popoverMenuItem]}
>
{item.text}
</Text>
);
}
render() {
return (
<FlatList
data={this.createPaymentMethodList()}
renderItem={this.renderItem}
keyExtractor={item => item.key}
/>
);
}
}
PaymentMethodList.propTypes = propTypes;
PaymentMethodList.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
cardList: {
key: ONYXKEYS.CARD_LIST,
},
payPalMeUsername: {
key: ONYXKEYS.NVP_PAYPAL_ME_ADDRESS,
},
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
}),
)(PaymentMethodList);