-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27923 from software-mansion-labs/@Skalakid/ts/Pay…
…mentUtils [TS migration] Migrate 'PaymentUtils.js' lib to TypeScript
- Loading branch information
Showing
5 changed files
with
102 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {SvgProps} from 'react-native-svg'; | ||
import BankAccountModel from './models/BankAccount'; | ||
import getBankIcon from '../components/Icon/BankIcons'; | ||
import CONST from '../CONST'; | ||
import * as Localize from './Localize'; | ||
import Fund from '../types/onyx/Fund'; | ||
import BankAccount from '../types/onyx/BankAccount'; | ||
|
||
type AccountType = BankAccount['accountType'] | Fund['accountType']; | ||
|
||
type PaymentMethod = (BankAccount | Fund) & { | ||
description: string; | ||
icon: React.FC<SvgProps>; | ||
iconSize?: number; | ||
}; | ||
|
||
/** | ||
* Check to see if user has either a debit card or personal bank account added | ||
*/ | ||
function hasExpensifyPaymentMethod(fundList: Record<string, Fund>, bankAccountList: Record<string, BankAccount>): boolean { | ||
const validBankAccount = Object.values(bankAccountList).some((bankAccountJSON) => { | ||
const bankAccount = new BankAccountModel(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 = Object.values(fundList).some((card) => card?.accountData?.additionalData?.isP2PDebitCard ?? false); | ||
|
||
return validBankAccount || validDebitCard; | ||
} | ||
|
||
function getPaymentMethodDescription(accountType: AccountType, account: BankAccount['accountData'] | Fund['accountData']): string { | ||
if (account) { | ||
if (accountType === CONST.PAYMENT_METHODS.BANK_ACCOUNT && 'accountNumber' in account) { | ||
return `${Localize.translateLocal('paymentMethodList.accountLastFour')} ${account.accountNumber?.slice(-4)}`; | ||
} | ||
if (accountType === CONST.PAYMENT_METHODS.DEBIT_CARD && 'cardNumber' in account) { | ||
return `${Localize.translateLocal('paymentMethodList.cardLastFour')} ${account.cardNumber?.slice(-4)}`; | ||
} | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* Get the PaymentMethods list | ||
*/ | ||
function formatPaymentMethods(bankAccountList: Record<string, BankAccount>, fundList: Record<string, Fund>): PaymentMethod[] { | ||
const combinedPaymentMethods: PaymentMethod[] = []; | ||
|
||
Object.values(bankAccountList).forEach((bankAccount) => { | ||
// Add all bank accounts besides the wallet | ||
if (bankAccount?.accountData?.type === CONST.BANK_ACCOUNT_TYPES.WALLET) { | ||
return; | ||
} | ||
|
||
const {icon, iconSize} = getBankIcon(bankAccount?.accountData?.additionalData?.bankName ?? '', false); | ||
combinedPaymentMethods.push({ | ||
...bankAccount, | ||
description: getPaymentMethodDescription(bankAccount?.accountType, bankAccount.accountData), | ||
icon, | ||
iconSize, | ||
}); | ||
}); | ||
|
||
Object.values(fundList).forEach((card) => { | ||
const {icon, iconSize} = getBankIcon(card?.accountData?.bank ?? '', true); | ||
combinedPaymentMethods.push({ | ||
...card, | ||
description: getPaymentMethodDescription(card?.accountType, card.accountData), | ||
icon, | ||
iconSize, | ||
}); | ||
}); | ||
|
||
return combinedPaymentMethods; | ||
} | ||
|
||
function calculateWalletTransferBalanceFee(currentBalance: number, methodType: string): number { | ||
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters