diff --git a/modules/expensify-react-native-wallet.tgz b/modules/expensify-react-native-wallet.tgz index c2de445997b0..4341827d4d9f 100644 Binary files a/modules/expensify-react-native-wallet.tgz and b/modules/expensify-react-native-wallet.tgz differ diff --git a/package-lock.json b/package-lock.json index 87f4a50bda92..c2ba1fd4adb6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3841,7 +3841,7 @@ "node_modules/@expensify/react-native-wallet": { "version": "0.1.0", "resolved": "file:modules/expensify-react-native-wallet.tgz", - "integrity": "sha512-9v+fsSOwda6Evm/Q9pXniX8HM/RZ7932EChVHawTxfOFIDWiGu0WAbQci/uUGYIvv5CVUuZ5N+CqXDts9bfrYw==", + "integrity": "sha512-1nMDZVHQRBRcHX0BRbOxYfeAUXuGYS8oBV1sOUGbwaOSGh39IkBrNBozBF8vDv5zYrPSxlhYs8oZYV5J5u+gJA==", "license": "MIT", "workspaces": [ "./example" diff --git a/src/components/AddToWalletButton/index.native.tsx b/src/components/AddToWalletButton/index.native.tsx index 4248c224deac..6e18ec8d579a 100644 --- a/src/components/AddToWalletButton/index.native.tsx +++ b/src/components/AddToWalletButton/index.native.tsx @@ -1,11 +1,14 @@ import {AddToWalletButton as RNAddToWalletButton} from '@expensify/react-native-wallet'; +import type {TokenizationStatus} from '@expensify/react-native-wallet'; import React, {useCallback, useEffect, useState} from 'react'; -import {ActivityIndicator, View} from 'react-native'; +import {ActivityIndicator, Alert, View} from 'react-native'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; +import {openWalletPage} from '@libs/actions/PaymentMethods'; import getPlatform from '@libs/getPlatform'; +import Log from '@libs/Log'; import {checkIfWalletIsAvailable, handleAddCardToWallet, isCardInWallet} from '@libs/Wallet/index'; import CONST from '@src/CONST'; import type AddToWalletButtonProps from './types'; @@ -35,7 +38,20 @@ function AddToWalletButton({card, cardHolderName, cardDescription, buttonStyle}: const handleOnPress = useCallback(() => { setIsLoading(true); - handleAddCardToWallet(card, cardHolderName, cardDescription, () => setIsLoading(false)); + handleAddCardToWallet(card, cardHolderName, cardDescription, () => setIsLoading(false)) + .then((status: TokenizationStatus) => { + if (status === 'success') { + Log.info('Card added to wallet'); + openWalletPage(); + } else { + setIsLoading(false); + } + }) + .catch((error) => { + setIsLoading(false); + Log.warn(`Error while adding card to wallet: ${error}`); + Alert.alert('Failed to add card to wallet', 'Please try again later.'); + }); }, [card, cardDescription, cardHolderName]); useEffect(() => { diff --git a/src/libs/Wallet/index.android.ts b/src/libs/Wallet/index.android.ts index 958cbedbc8cb..bc0388deb36b 100644 --- a/src/libs/Wallet/index.android.ts +++ b/src/libs/Wallet/index.android.ts @@ -1,7 +1,5 @@ import {addCardToGoogleWallet, checkWalletAvailability, getCardStatusBySuffix, getSecureWalletInfo} from '@expensify/react-native-wallet'; import type {AndroidCardData, AndroidWalletData, CardStatus, TokenizationStatus} from '@expensify/react-native-wallet'; -import {Alert} from 'react-native'; -import {openWalletPage} from '@libs/actions/PaymentMethods'; import {createDigitalGoogleWallet} from '@libs/actions/Wallet'; import Log from '@libs/Log'; import type {Card} from '@src/types/onyx'; @@ -10,29 +8,10 @@ function checkIfWalletIsAvailable(): Promise { return checkWalletAvailability(); } -function handleAddCardToWallet(card: Card, cardHolderName: string, cardDescription: string, onFinished?: () => void) { - getSecureWalletInfo() - .then((walletData: AndroidWalletData) => { - createDigitalGoogleWallet({cardHolderName, ...walletData}) - .then((cardData: AndroidCardData) => { - addCardToGoogleWallet(cardData) - .then((status: TokenizationStatus) => { - if (status === 'success') { - Log.info('Card added to wallet'); - openWalletPage(); - } else { - onFinished?.(); - } - }) - .catch((error) => { - Log.warn(`addCardToGoogleWallet error: ${error}`); - Alert.alert('Failed to add card to wallet.', 'Please try again later.'); - }); - }) - - .catch((error) => Log.warn(`createDigitalWallet error: ${error}`)); - }) - .catch((error) => Log.warn(`getSecureWalletInfo error: ${error}`)); +function handleAddCardToWallet(card: Card, cardHolderName: string): Promise { + return getSecureWalletInfo().then((walletData: AndroidWalletData) => + createDigitalGoogleWallet({cardHolderName, ...walletData}).then((cardData: AndroidCardData) => addCardToGoogleWallet(cardData)), + ); } function isCardInWallet(card: Card): Promise { diff --git a/src/libs/Wallet/index.ios.ts b/src/libs/Wallet/index.ios.ts index 2cfdfea99596..8f3dd391de96 100644 --- a/src/libs/Wallet/index.ios.ts +++ b/src/libs/Wallet/index.ios.ts @@ -1,6 +1,5 @@ import {addCardToAppleWallet, checkWalletAvailability, getCardStatusByIdentifier, getCardStatusBySuffix} from '@expensify/react-native-wallet'; -import type {IOSCardData} from '@expensify/react-native-wallet'; -import {Alert} from 'react-native'; +import type {IOSCardData, TokenizationStatus} from '@expensify/react-native-wallet'; import {issuerEncryptPayloadCallback} from '@libs/actions/Wallet'; import Log from '@libs/Log'; import CONST from '@src/CONST'; @@ -10,7 +9,7 @@ function checkIfWalletIsAvailable(): Promise { return checkWalletAvailability(); } -function handleAddCardToWallet(card: Card, cardHolderName: string, cardDescription: string, onFinished?: () => void) { +function handleAddCardToWallet(card: Card, cardHolderName: string, cardDescription: string): Promise { const data = { network: CONST.COMPANY_CARDS.CARD_TYPE.VISA, lastDigits: card.lastFourPAN, @@ -18,25 +17,18 @@ function handleAddCardToWallet(card: Card, cardHolderName: string, cardDescripti cardHolderName, } as IOSCardData; - addCardToAppleWallet(data, issuerEncryptPayloadCallback) - .then(() => { - Log.info('Card added to wallet'); - onFinished?.(); - }) - .catch((e) => { - Log.warn(`handleAddCardToWallet error: ${e}`); - Alert.alert('Failed to add card to wallet', 'Please try again later.'); - }); + return addCardToAppleWallet(data, issuerEncryptPayloadCallback); } function isCardInWallet(card: Card): Promise { - if (card.state !== CONST.EXPENSIFY_CARD.STATE.OPEN) { + const panReferenceID = card.nameValuePairs?.expensifyCard_panReferenceID; + if (!panReferenceID) { return Promise.resolve(false); } let callback = null; if (card.token) { - callback = getCardStatusByIdentifier(card.token, CONST.COMPANY_CARDS.CARD_TYPE.VISA); + callback = getCardStatusByIdentifier(panReferenceID, CONST.COMPANY_CARDS.CARD_TYPE.VISA); } else if (card.lastFourPAN) { callback = getCardStatusBySuffix(card.lastFourPAN); } diff --git a/src/libs/Wallet/index.ts b/src/libs/Wallet/index.ts index 7707db63b35c..21f8bf240284 100644 --- a/src/libs/Wallet/index.ts +++ b/src/libs/Wallet/index.ts @@ -1,3 +1,4 @@ +import type {TokenizationStatus} from '@expensify/react-native-wallet'; import type {Card} from '@src/types/onyx'; function checkIfWalletIsAvailable(): Promise { @@ -5,8 +6,8 @@ function checkIfWalletIsAvailable(): Promise { } // eslint-disable-next-line @typescript-eslint/no-unused-vars -function handleAddCardToWallet(_card: Card, _cardHolderName: string, _cardDescription: string, _onFinished?: () => void) { - Promise.reject(new Error('Add to wallet is not supported on this platform')); +function handleAddCardToWallet(_card: Card, _cardHolderName: string, _cardDescription: string, _onFinished?: () => void): Promise { + return Promise.reject(new Error('Add to wallet is not supported on this platform')); } // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/src/types/onyx/Card.ts b/src/types/onyx/Card.ts index f8b8177659e9..d4ac3ecf1bc4 100644 --- a/src/types/onyx/Card.ts +++ b/src/types/onyx/Card.ts @@ -111,6 +111,10 @@ type Card = OnyxCommon.OnyxValueWithOfflineFeedback<{ /** Card expiration date */ expirationDate?: string; + /** Card's primary account identifier */ + // eslint-disable-next-line @typescript-eslint/naming-convention + expensifyCard_panReferenceID?: string; + /** List of token reference ids */ // eslint-disable-next-line @typescript-eslint/naming-convention expensifyCard_tokenReferenceIdList?: string[];