-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PaymentMethods.ts
558 lines (502 loc) · 16.9 KB
/
PaymentMethods.ts
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import {createRef} from 'react';
import type {MutableRefObject} from 'react';
import type {GestureResponderEvent} from 'react-native';
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import * as API from '@libs/API';
import type {
AddPaymentCardParams,
DeletePaymentCardParams,
MakeDefaultPaymentMethodParams,
PaymentCardParams,
TransferWalletBalanceParams,
UpdateBillingCurrencyParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as CardUtils from '@libs/CardUtils';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import INPUT_IDS from '@src/types/form/AddPaymentCardForm';
import type {BankAccountList, FundList} from '@src/types/onyx';
import type {AccountData} from '@src/types/onyx/Fund';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type PaymentMethod from '@src/types/onyx/PaymentMethod';
import type {FilterMethodPaymentType} from '@src/types/onyx/WalletTransfer';
type KYCWallRef = {
continueAction?: (event?: GestureResponderEvent | KeyboardEvent, iouPaymentType?: PaymentMethodType) => void;
};
/**
* Sets up a ref to an instance of the KYC Wall component.
*/
const kycWallRef: MutableRefObject<KYCWallRef | null> = createRef<KYCWallRef>();
/**
* When we successfully add a payment method or pass the KYC checks we will continue with our setup action if we have one set.
*/
function continueSetup(fallbackRoute?: Route) {
if (!kycWallRef.current?.continueAction) {
Navigation.goBack(fallbackRoute);
return;
}
// Close the screen (Add Debit Card, Add Bank Account, or Enable Payments) on success and continue with setup
Navigation.goBack(fallbackRoute);
kycWallRef.current.continueAction();
}
function openWalletPage() {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_PAYMENT_METHODS,
value: true,
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_PAYMENT_METHODS,
value: false,
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_PAYMENT_METHODS,
value: false,
},
];
return API.read(READ_COMMANDS.OPEN_PAYMENTS_PAGE, null, {
optimisticData,
successData,
failureData,
});
}
function getMakeDefaultPaymentOnyxData(
bankAccountID: number,
fundID: number,
previousPaymentMethod?: PaymentMethod,
currentPaymentMethod?: PaymentMethod,
isOptimisticData = true,
): OnyxUpdate[] {
const onyxData: OnyxUpdate[] = [
isOptimisticData
? {
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.USER_WALLET,
value: {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
walletLinkedAccountID: bankAccountID || fundID,
walletLinkedAccountType: bankAccountID ? CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT : CONST.PAYMENT_METHODS.DEBIT_CARD,
// Only clear the error if this is optimistic data. If this is failure data, we do not want to clear the error that came from the server.
errors: null,
},
}
: {
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.USER_WALLET,
value: {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
walletLinkedAccountID: bankAccountID || fundID,
walletLinkedAccountType: bankAccountID ? CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT : CONST.PAYMENT_METHODS.DEBIT_CARD,
},
},
];
if (previousPaymentMethod?.methodID) {
onyxData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: previousPaymentMethod.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT ? ONYXKEYS.BANK_ACCOUNT_LIST : ONYXKEYS.FUND_LIST,
value: {
[previousPaymentMethod.methodID]: {
isDefault: !isOptimisticData,
},
},
});
}
if (currentPaymentMethod?.methodID) {
onyxData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: currentPaymentMethod.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT ? ONYXKEYS.BANK_ACCOUNT_LIST : ONYXKEYS.FUND_LIST,
value: {
[currentPaymentMethod.methodID]: {
isDefault: isOptimisticData,
},
},
});
}
return onyxData;
}
/**
* Sets the default bank account or debit card for an Expensify Wallet
*
*/
function makeDefaultPaymentMethod(bankAccountID: number, fundID: number, previousPaymentMethod?: PaymentMethod, currentPaymentMethod?: PaymentMethod) {
const parameters: MakeDefaultPaymentMethodParams = {
bankAccountID,
fundID,
};
API.write(WRITE_COMMANDS.MAKE_DEFAULT_PAYMENT_METHOD, parameters, {
optimisticData: getMakeDefaultPaymentOnyxData(bankAccountID, fundID, previousPaymentMethod, currentPaymentMethod, true),
failureData: getMakeDefaultPaymentOnyxData(bankAccountID, fundID, previousPaymentMethod, currentPaymentMethod, false),
});
}
/**
* Calls the API to add a new card.
*
*/
function addPaymentCard(params: PaymentCardParams) {
const cardMonth = CardUtils.getMonthFromExpirationDateString(params.expirationDate);
const cardYear = CardUtils.getYearFromExpirationDateString(params.expirationDate);
const parameters: AddPaymentCardParams = {
cardNumber: CardUtils.getMCardNumberString(params.cardNumber),
cardYear,
cardMonth,
cardCVV: params.securityCode,
addressName: params.nameOnCard,
addressZip: params.addressZipCode,
currency: CONST.PAYMENT_CARD_CURRENCY.USD,
isP2PDebitCard: true,
};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM,
value: {isLoading: true},
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM,
value: {isLoading: false},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM,
value: {isLoading: false},
},
];
API.write(WRITE_COMMANDS.ADD_PAYMENT_CARD, parameters, {
optimisticData,
successData,
failureData,
});
}
/**
* Calls the API to add a new card.
*
*/
function addSubscriptionPaymentCard(cardData: {
cardNumber: string;
cardYear: string;
cardMonth: string;
cardCVV: string;
addressName: string;
addressZip: string;
currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
}) {
const {cardNumber, cardYear, cardMonth, cardCVV, addressName, addressZip, currency} = cardData;
const parameters: AddPaymentCardParams = {
cardNumber,
cardYear,
cardMonth,
cardCVV,
addressName,
addressZip,
currency,
isP2PDebitCard: false,
};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM,
value: {isLoading: true},
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM,
value: {isLoading: false},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM,
value: {isLoading: false},
},
];
if (currency === CONST.PAYMENT_CARD_CURRENCY.GBP) {
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.ADD_PAYMENT_CARD_GBP, parameters, {optimisticData, successData, failureData}).then((response) => {
if (response?.jsonCode !== CONST.JSON_CODE.SUCCESS) {
return;
}
// We are using this onyx key to open Modal and preview iframe. Potentially we can save the whole object which come from side effect
Onyx.set(ONYXKEYS.VERIFY_3DS_SUBSCRIPTION, (response as {authenticationLink: string}).authenticationLink);
});
} else {
// eslint-disable-next-line rulesdir/no-multiple-api-calls
API.write(WRITE_COMMANDS.ADD_PAYMENT_CARD, parameters, {
optimisticData,
successData,
failureData,
});
}
}
/**
* Resets the values for the add payment card form back to their initial states
*/
function clearPaymentCardFormErrorAndSubmit() {
Onyx.set(ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM, {
isLoading: false,
errors: undefined,
[INPUT_IDS.SETUP_COMPLETE]: false,
[INPUT_IDS.NAME_ON_CARD]: '',
[INPUT_IDS.CARD_NUMBER]: '',
[INPUT_IDS.EXPIRATION_DATE]: '',
[INPUT_IDS.SECURITY_CODE]: '',
[INPUT_IDS.ADDRESS_STREET]: '',
[INPUT_IDS.ADDRESS_ZIP_CODE]: '',
[INPUT_IDS.ADDRESS_STATE]: '',
[INPUT_IDS.ACCEPT_TERMS]: '',
[INPUT_IDS.CURRENCY]: CONST.PAYMENT_CARD_CURRENCY.USD,
});
}
/**
* Clear 3ds flow - when verification will be finished
*
*/
function clearPaymentCard3dsVerification() {
Onyx.set(ONYXKEYS.VERIFY_3DS_SUBSCRIPTION, '');
}
/**
* Properly updates the nvp_privateStripeCustomerID onyx data for 3DS payment
*
*/
function verifySetupIntent(accountID: number, isVerifying = true) {
API.write(WRITE_COMMANDS.VERIFY_SETUP_INTENT, {accountID, isVerifying});
}
/**
* Set currency for payments
*
*/
function setPaymentMethodCurrency(currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>) {
Onyx.merge(ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM, {
[INPUT_IDS.CURRENCY]: currency,
});
}
/**
* Call the API to transfer wallet balance.
*
*/
function transferWalletBalance(paymentMethod: PaymentMethod) {
const paymentMethodIDKey =
paymentMethod.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT ? CONST.PAYMENT_METHOD_ID_KEYS.BANK_ACCOUNT : CONST.PAYMENT_METHOD_ID_KEYS.DEBIT_CARD;
const parameters: TransferWalletBalanceParams = {
[paymentMethodIDKey]: paymentMethod.methodID,
};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: 'merge',
key: ONYXKEYS.WALLET_TRANSFER,
value: {
loading: true,
errors: null,
},
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: 'merge',
key: ONYXKEYS.WALLET_TRANSFER,
value: {
loading: false,
shouldShowSuccess: true,
paymentMethodType: paymentMethod.accountType,
},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: 'merge',
key: ONYXKEYS.WALLET_TRANSFER,
value: {
loading: false,
shouldShowSuccess: false,
},
},
];
API.write(WRITE_COMMANDS.TRANSFER_WALLET_BALANCE, parameters, {
optimisticData,
successData,
failureData,
});
}
function resetWalletTransferData() {
Onyx.merge(ONYXKEYS.WALLET_TRANSFER, {
selectedAccountType: '',
selectedAccountID: null,
filterPaymentMethodType: null,
loading: false,
shouldShowSuccess: false,
});
}
function saveWalletTransferAccountTypeAndID(selectedAccountType: string, selectedAccountID: string) {
Onyx.merge(ONYXKEYS.WALLET_TRANSFER, {selectedAccountType, selectedAccountID});
}
/**
* Toggles the user's selected type of payment method (bank account or debit card) on the wallet transfer balance screen.
*
*/
function saveWalletTransferMethodType(filterPaymentMethodType?: FilterMethodPaymentType) {
Onyx.merge(ONYXKEYS.WALLET_TRANSFER, {filterPaymentMethodType});
}
function dismissSuccessfulTransferBalancePage() {
Onyx.merge(ONYXKEYS.WALLET_TRANSFER, {shouldShowSuccess: false});
Navigation.goBack();
}
/**
* Looks through each payment method to see if there is an existing error
*
*/
function hasPaymentMethodError(bankList: OnyxEntry<BankAccountList>, fundList: OnyxEntry<FundList>): boolean {
const combinedPaymentMethods = {...bankList, ...fundList};
return Object.values(combinedPaymentMethods).some((item) => Object.keys(item.errors ?? {}).length);
}
type PaymentListKey = typeof ONYXKEYS.BANK_ACCOUNT_LIST | typeof ONYXKEYS.FUND_LIST;
/**
* Clears the error for the specified payment item
* @param paymentListKey The onyx key for the provided payment method
* @param paymentMethodID
*/
function clearDeletePaymentMethodError(paymentListKey: PaymentListKey, paymentMethodID: number) {
Onyx.merge(paymentListKey, {
[paymentMethodID]: {
pendingAction: null,
errors: null,
},
});
}
/**
* If there was a failure adding a payment method, clearing it removes the payment method from the list entirely
* @param paymentListKey The onyx key for the provided payment method
* @param paymentMethodID
*/
function clearAddPaymentMethodError(paymentListKey: PaymentListKey, paymentMethodID: number) {
Onyx.merge(paymentListKey, {
[paymentMethodID]: null,
});
}
/**
* Clear any error(s) related to the user's wallet
*/
function clearWalletError() {
Onyx.merge(ONYXKEYS.USER_WALLET, {errors: null});
}
/**
* Clear any error(s) related to the user's wallet terms
*/
function clearWalletTermsError() {
Onyx.merge(ONYXKEYS.WALLET_TERMS, {errors: null});
}
function deletePaymentCard(fundID: number) {
const parameters: DeletePaymentCardParams = {
fundID,
};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.FUND_LIST}`,
value: {[fundID]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE}},
},
];
API.write(WRITE_COMMANDS.DELETE_PAYMENT_CARD, parameters, {
optimisticData,
});
}
/**
* Call the API to change billing currency.
*
*/
function updateBillingCurrency(currency: ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>, cardCVV: string) {
const parameters: UpdateBillingCurrencyParams = {
cardCVV,
currency,
};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM,
value: {
isLoading: true,
errors: null,
},
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM,
value: {
isLoading: false,
},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.FORMS.CHANGE_BILLING_CURRENCY_FORM,
value: {
isLoading: false,
},
},
];
API.write(WRITE_COMMANDS.UPDATE_BILLING_CARD_CURRENCY, parameters, {
optimisticData,
successData,
failureData,
});
}
/**
* Set payment card form with API data
*
*/
function setPaymentCardForm(values: AccountData) {
Onyx.merge(ONYXKEYS.FORMS.ADD_PAYMENT_CARD_FORM, {
[INPUT_IDS.CARD_NUMBER]: values.cardNumber,
[INPUT_IDS.EXPIRATION_DATE]: `${values.cardMonth}${values.cardYear?.toString()?.substring(2)}`,
[INPUT_IDS.ADDRESS_STREET]: values.addressStreet,
[INPUT_IDS.ADDRESS_ZIP_CODE]: values.addressZip?.toString(),
[INPUT_IDS.ADDRESS_STATE]: values.addressState,
[INPUT_IDS.CURRENCY]: values.currency,
});
}
export {
deletePaymentCard,
addPaymentCard,
openWalletPage,
makeDefaultPaymentMethod,
kycWallRef,
continueSetup,
addSubscriptionPaymentCard,
clearPaymentCardFormErrorAndSubmit,
dismissSuccessfulTransferBalancePage,
transferWalletBalance,
resetWalletTransferData,
saveWalletTransferAccountTypeAndID,
saveWalletTransferMethodType,
hasPaymentMethodError,
updateBillingCurrency,
clearDeletePaymentMethodError,
clearAddPaymentMethodError,
clearWalletError,
setPaymentMethodCurrency,
clearPaymentCard3dsVerification,
clearWalletTermsError,
setPaymentCardForm,
verifySetupIntent,
};