-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseKYCWall.tsx
287 lines (243 loc) · 11.2 KB
/
BaseKYCWall.tsx
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
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {Dimensions} from 'react-native';
import type {EmitterSubscription, GestureResponderEvent, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import AddPaymentMethodMenu from '@components/AddPaymentMethodMenu';
import * as BankAccounts from '@libs/actions/BankAccounts';
import getClickedTargetLocation from '@libs/getClickedTargetLocation';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import * as PaymentUtils from '@libs/PaymentUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as Policy from '@userActions/Policy/Policy';
import * as Wallet from '@userActions/Wallet';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {BankAccountList, FundList, ReimbursementAccount, UserWallet, WalletTerms} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import viewRef from '@src/types/utils/viewRef';
import type {AnchorPosition, DomRect, KYCWallProps, PaymentMethod} from './types';
// This sets the Horizontal anchor position offset for POPOVER MENU.
const POPOVER_MENU_ANCHOR_POSITION_HORIZONTAL_OFFSET = 20;
type BaseKYCWallOnyxProps = {
/** The user's wallet */
userWallet: OnyxEntry<UserWallet>;
/** Information related to the last step of the wallet activation flow */
walletTerms: OnyxEntry<WalletTerms>;
/** List of user's cards */
fundList: OnyxEntry<FundList>;
/** List of bank accounts */
bankAccountList: OnyxEntry<BankAccountList>;
/** The reimbursement account linked to the Workspace */
reimbursementAccount: OnyxEntry<ReimbursementAccount>;
};
type BaseKYCWallProps = KYCWallProps & BaseKYCWallOnyxProps;
// This component allows us to block various actions by forcing the user to first add a default payment method and successfully make it through our Know Your Customer flow
// before continuing to take whatever action they originally intended to take. It requires a button as a child and a native event so we can get the coordinates and use it
// to render the AddPaymentMethodMenu in the correct location.
function KYCWall({
addBankAccountRoute,
addDebitCardRoute,
anchorAlignment = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
},
bankAccountList = {},
chatReportID = '',
children,
enablePaymentsRoute,
fundList,
iouReport,
onSelectPaymentMethod = () => {},
onSuccessfulKYC,
reimbursementAccount,
shouldIncludeDebitCard = true,
shouldListenForResize = false,
source,
userWallet,
walletTerms,
shouldShowPersonalBankAccountOption = false,
}: BaseKYCWallProps) {
const anchorRef = useRef<HTMLDivElement | View>(null);
const transferBalanceButtonRef = useRef<HTMLDivElement | View | null>(null);
const [shouldShowAddPaymentMenu, setShouldShowAddPaymentMenu] = useState(false);
const [anchorPosition, setAnchorPosition] = useState({
anchorPositionVertical: 0,
anchorPositionHorizontal: 0,
});
const getAnchorPosition = useCallback(
(domRect: DomRect): AnchorPosition => {
if (anchorAlignment.vertical === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP) {
return {
anchorPositionVertical: domRect.top + domRect.height + CONST.MODAL.POPOVER_MENU_PADDING,
anchorPositionHorizontal: domRect.left + POPOVER_MENU_ANCHOR_POSITION_HORIZONTAL_OFFSET,
};
}
return {
anchorPositionVertical: domRect.top - CONST.MODAL.POPOVER_MENU_PADDING,
anchorPositionHorizontal: domRect.left,
};
},
[anchorAlignment.vertical],
);
/**
* Set position of the transfer payment menu
*/
const setPositionAddPaymentMenu = ({anchorPositionVertical, anchorPositionHorizontal}: AnchorPosition) => {
setAnchorPosition({
anchorPositionVertical,
anchorPositionHorizontal,
});
};
const setMenuPosition = useCallback(() => {
if (!transferBalanceButtonRef.current) {
return;
}
const buttonPosition = getClickedTargetLocation(transferBalanceButtonRef.current as HTMLDivElement);
const position = getAnchorPosition(buttonPosition);
setPositionAddPaymentMenu(position);
}, [getAnchorPosition]);
const selectPaymentMethod = useCallback(
(paymentMethod: PaymentMethod) => {
onSelectPaymentMethod(paymentMethod);
if (paymentMethod === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT) {
BankAccounts.openPersonalBankAccountSetupView();
} else if (paymentMethod === CONST.PAYMENT_METHODS.DEBIT_CARD) {
Navigation.navigate(addDebitCardRoute);
} else if (paymentMethod === CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT) {
if (iouReport && ReportUtils.isIOUReport(iouReport)) {
const policyID = Policy.createWorkspaceFromIOUPayment(iouReport);
// Navigate to the bank account set up flow for this specific policy
Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute('', policyID));
return;
}
Navigation.navigate(addBankAccountRoute);
}
},
[addBankAccountRoute, addDebitCardRoute, iouReport, onSelectPaymentMethod],
);
/**
* Take the position of the button that calls this method and show the Add Payment method menu when the user has no valid payment method.
* If they do have a valid payment method they are navigated to the "enable payments" route to complete KYC checks.
* If they are already KYC'd we will continue whatever action is gated behind the KYC wall.
*
*/
const continueAction = useCallback(
(event?: GestureResponderEvent | KeyboardEvent, iouPaymentType?: PaymentMethodType) => {
const currentSource = walletTerms?.source ?? source;
/**
* Set the source, so we can tailor the process according to how we got here.
* We do not want to set this on mount, as the source can change upon completing the flow, e.g. when upgrading the wallet to Gold.
*/
Wallet.setKYCWallSource(source, chatReportID);
if (shouldShowAddPaymentMenu) {
setShouldShowAddPaymentMenu(false);
return;
}
// Use event target as fallback if anchorRef is null for safety
const targetElement = anchorRef.current ?? (event?.currentTarget as HTMLDivElement);
transferBalanceButtonRef.current = targetElement;
const isExpenseReport = ReportUtils.isExpenseReport(iouReport);
const paymentCardList = fundList ?? {};
// Check to see if user has a valid payment method on file and display the add payment popover if they don't
if (
(isExpenseReport && reimbursementAccount?.achData?.state !== CONST.BANK_ACCOUNT.STATE.OPEN) ||
(!isExpenseReport && bankAccountList !== null && !PaymentUtils.hasExpensifyPaymentMethod(paymentCardList, bankAccountList, shouldIncludeDebitCard))
) {
Log.info('[KYC Wallet] User does not have valid payment method');
if (!shouldIncludeDebitCard) {
selectPaymentMethod(CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT);
return;
}
const clickedElementLocation = getClickedTargetLocation(targetElement as HTMLDivElement);
const position = getAnchorPosition(clickedElementLocation);
setPositionAddPaymentMenu(position);
setShouldShowAddPaymentMenu(true);
return;
}
if (!isExpenseReport) {
// Ask the user to upgrade to a gold wallet as this means they have not yet gone through our Know Your Customer (KYC) checks
const hasActivatedWallet = userWallet?.tierName && [CONST.WALLET.TIER_NAME.GOLD, CONST.WALLET.TIER_NAME.PLATINUM].some((name) => name === userWallet.tierName);
if (!hasActivatedWallet) {
Log.info('[KYC Wallet] User does not have active wallet');
Navigation.navigate(enablePaymentsRoute);
return;
}
}
Log.info('[KYC Wallet] User has valid payment method and passed KYC checks or did not need them');
onSuccessfulKYC(iouPaymentType, currentSource);
},
[
bankAccountList,
chatReportID,
enablePaymentsRoute,
fundList,
getAnchorPosition,
iouReport,
onSuccessfulKYC,
reimbursementAccount?.achData?.state,
selectPaymentMethod,
shouldIncludeDebitCard,
shouldShowAddPaymentMenu,
source,
userWallet?.tierName,
walletTerms?.source,
],
);
useEffect(() => {
let dimensionsSubscription: EmitterSubscription | null = null;
PaymentMethods.kycWallRef.current = {continueAction};
if (shouldListenForResize) {
dimensionsSubscription = Dimensions.addEventListener('change', setMenuPosition);
}
return () => {
if (shouldListenForResize && dimensionsSubscription) {
dimensionsSubscription.remove();
}
PaymentMethods.kycWallRef.current = null;
};
}, [chatReportID, setMenuPosition, shouldListenForResize, continueAction]);
return (
<>
<AddPaymentMethodMenu
isVisible={shouldShowAddPaymentMenu}
iouReport={iouReport}
onClose={() => setShouldShowAddPaymentMenu(false)}
anchorRef={anchorRef}
anchorPosition={{
vertical: anchorPosition.anchorPositionVertical,
horizontal: anchorPosition.anchorPositionHorizontal,
}}
anchorAlignment={anchorAlignment}
onItemSelected={(item: PaymentMethod) => {
setShouldShowAddPaymentMenu(false);
selectPaymentMethod(item);
}}
shouldShowPersonalBankAccountOption={shouldShowPersonalBankAccountOption}
/>
{children(continueAction, viewRef(anchorRef))}
</>
);
}
KYCWall.displayName = 'BaseKYCWall';
export default withOnyx<BaseKYCWallProps, BaseKYCWallOnyxProps>({
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
walletTerms: {
key: ONYXKEYS.WALLET_TERMS,
},
fundList: {
key: ONYXKEYS.FUND_LIST,
},
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
// @ts-expect-error: ONYXKEYS.REIMBURSEMENT_ACCOUNT is conflicting with ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
})(KYCWall);