-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PaymentMethodList.js
286 lines (252 loc) · 10.5 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import _ from 'underscore';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {FlatList} from 'react-native';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
import {withNetwork} from '../../../components/OnyxProvider';
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 paypalMeDataPropTypes from '../../../components/paypalMeDataPropTypes';
import cardPropTypes from '../../../components/cardPropTypes';
import * as PaymentUtils from '../../../libs/PaymentUtils';
import FormAlertWrapper from '../../../components/FormAlertWrapper';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
import * as PaymentMethods from '../../../libs/actions/PaymentMethods';
import Log from '../../../libs/Log';
const propTypes = {
/** What to do when a menu item is pressed */
onPress: PropTypes.func.isRequired,
/** Account details for PayPal.Me */
payPalMeData: paypalMeDataPropTypes,
/** List of bank accounts */
bankAccountList: PropTypes.objectOf(bankAccountPropTypes),
/** List of cards */
cardList: PropTypes.objectOf(cardPropTypes),
/** Whether the add Payment button be shown on the list */
shouldShowAddPaymentMethodButton: PropTypes.bool,
/** Are we loading payment methods? */
isLoadingPaymentMethods: 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]),
/** Content for the FlatList header component */
listHeaderComponent: PropTypes.func,
...withLocalizePropTypes,
};
const defaultProps = {
payPalMeData: {},
bankAccountList: {},
cardList: {},
userWallet: {
walletLinkedAccountID: 0,
walletLinkedAccountType: '',
},
isLoadingPaymentMethods: true,
shouldShowAddPaymentMethodButton: true,
filterType: '',
actionPaymentMethodType: '',
activePaymentMethodID: '',
selectedMethodID: '',
listHeaderComponent: null,
};
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 any billing cards that are not P2P debit cards for now because you cannot make them your default method, or delete them
const filteredCardList = _.filter(this.props.cardList, card => card.accountData.additionalData.isP2PDebitCard);
let combinedPaymentMethods = PaymentUtils.formatPaymentMethods(this.props.bankAccountList, filteredCardList, this.props.payPalMeData);
if (!_.isEmpty(this.props.filterType)) {
combinedPaymentMethods = _.filter(combinedPaymentMethods, paymentMethod => paymentMethod.accountType === this.props.filterType);
}
if (!this.props.network.isOffline) {
combinedPaymentMethods = _.filter(combinedPaymentMethods, paymentMethod => paymentMethod.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
|| !_.isEmpty(paymentMethod.errors));
}
combinedPaymentMethods = _.map(combinedPaymentMethods, paymentMethod => ({
...paymentMethod,
onPress: e => this.props.onPress(e, paymentMethod.accountType, paymentMethod.accountData, paymentMethod.isDefault, paymentMethod.methodID),
iconFill: this.isPaymentMethodActive(paymentMethod) ? StyleUtils.getIconFillColor(CONST.BUTTON_STATES.PRESSED) : null,
wrapperStyle: this.isPaymentMethodActive(paymentMethod) ? [StyleUtils.getButtonBackgroundColorStyle(CONST.BUTTON_STATES.PRESSED)] : null,
disabled: paymentMethod.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
}));
return combinedPaymentMethods;
}
/**
* Dismisses the error on the payment method
* @param {Object} item
*/
dismissError(item) {
const paymentList = item.accountType === CONST.PAYMENT_METHODS.BANK_ACCOUNT ? ONYXKEYS.BANK_ACCOUNT_LIST : ONYXKEYS.CARD_LIST;
const paymentID = item.accountType === CONST.PAYMENT_METHODS.BANK_ACCOUNT ? lodashGet(item, ['accountData', 'bankAccountID'], '') : lodashGet(item, ['accountData', 'fundID'], '');
if (!paymentID) {
Log.info('Unable to clear payment method error: ', item);
return;
}
if (item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
PaymentMethods.clearDeletePaymentMethodError(paymentList, paymentID);
} else {
PaymentMethods.clearAddPaymentMethodError(paymentList, paymentID);
}
}
/**
* @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}) {
return (
<OfflineWithFeedback
onClose={() => this.dismissError(item)}
pendingAction={item.pendingAction}
errors={item.errors}
errorRowStyles={styles.ph6}
>
<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}
/>
</OfflineWithFeedback>
);
}
/**
* Show add first payment copy when payment methods are
*
* @return {React.Component}
*/
renderListEmptyComponent() {
return (
<Text
style={[styles.popoverMenuItem]}
>
{this.props.translate('paymentMethodList.addFirstPaymentMethod')}
</Text>
);
}
render() {
return (
<>
<FlatList
data={this.getFilteredPaymentMethods()}
renderItem={this.renderItem}
keyExtractor={item => item.key}
ListEmptyComponent={this.renderListEmptyComponent()}
ListHeaderComponent={this.props.listHeaderComponent}
/>
{
this.props.shouldShowAddPaymentMethodButton
&& (
<FormAlertWrapper>
{
isOffline => (
<Button
text={this.props.translate('paymentMethodList.addPaymentMethod')}
icon={Expensicons.CreditCard}
onPress={e => this.props.onPress(e)}
isDisabled={this.props.isLoadingPaymentMethods || isOffline}
style={[styles.mh4, styles.buttonCTA]}
iconStyles={[styles.buttonCTAIcon]}
key="addPaymentMethodButton"
success
shouldShowRightIcon
large
/>
)
}
</FormAlertWrapper>
)
}
</>
);
}
}
PaymentMethodList.propTypes = propTypes;
PaymentMethodList.defaultProps = defaultProps;
export default compose(
withLocalize,
withNetwork(),
withOnyx({
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
cardList: {
key: ONYXKEYS.CARD_LIST,
},
isLoadingPaymentMethods: {
key: ONYXKEYS.IS_LOADING_PAYMENT_METHODS,
},
payPalMeData: {
key: ONYXKEYS.PAYPAL,
},
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
}),
)(PaymentMethodList);