@@ -3,21 +3,23 @@ import React, {useCallback, useEffect, useRef, useState} from 'react';
33import { Dimensions } from 'react-native' ;
44import type { EmitterSubscription , GestureResponderEvent , View } from 'react-native' ;
55import AddPaymentMethodMenu from '@components/AddPaymentMethodMenu' ;
6+ import useLocalize from '@hooks/useLocalize' ;
67import useOnyx from '@hooks/useOnyx' ;
78import { openPersonalBankAccountSetupView } from '@libs/actions/BankAccounts' ;
8- import { completePaymentOnboarding } from '@libs/actions/IOU' ;
9+ import { completePaymentOnboarding , savePreferredPaymentMethod } from '@libs/actions/IOU' ;
10+ import { moveIOUReportToPolicy , moveIOUReportToPolicyAndInviteSubmitter } from '@libs/actions/Report' ;
911import getClickedTargetLocation from '@libs/getClickedTargetLocation' ;
1012import Log from '@libs/Log' ;
1113import Navigation from '@libs/Navigation/Navigation' ;
1214import { hasExpensifyPaymentMethod } from '@libs/PaymentUtils' ;
13- import { getBankAccountRoute , isExpenseReport as isExpenseReportReportUtils , isIOUReport } from '@libs/ReportUtils' ;
15+ import { getBankAccountRoute , getPolicyExpenseChat , isExpenseReport as isExpenseReportReportUtils , isIOUReport } from '@libs/ReportUtils' ;
1416import { kycWallRef } from '@userActions/PaymentMethods' ;
1517import { createWorkspaceFromIOUPayment } from '@userActions/Policy/Policy' ;
1618import { setKYCWallSource } from '@userActions/Wallet' ;
1719import CONST from '@src/CONST' ;
1820import ONYXKEYS from '@src/ONYXKEYS' ;
1921import ROUTES from '@src/ROUTES' ;
20- import type { BankAccountList } from '@src/types/onyx' ;
22+ import type { BankAccountList , Policy } from '@src/types/onyx' ;
2123import type { PaymentMethodType } from '@src/types/onyx/OriginalMessage' ;
2224import { getEmptyObject } from '@src/types/utils/EmptyObject' ;
2325import viewRef from '@src/types/utils/viewRef' ;
@@ -54,6 +56,8 @@ function KYCWall({
5456 const [ reimbursementAccount ] = useOnyx ( ONYXKEYS . REIMBURSEMENT_ACCOUNT , { canBeMissing : true } ) ;
5557 const [ chatReport ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT } ${ chatReportID } ` , { canBeMissing : true } ) ;
5658
59+ const { formatPhoneNumber} = useLocalize ( ) ;
60+
5761 const anchorRef = useRef < HTMLDivElement | View > ( null ) ;
5862 const transferBalanceButtonRef = useRef < HTMLDivElement | View | null > ( null ) ;
5963
@@ -64,6 +68,8 @@ function KYCWall({
6468 anchorPositionHorizontal : 0 ,
6569 } ) ;
6670
71+ const [ lastPaymentMethod ] = useOnyx ( ONYXKEYS . NVP_LAST_PAYMENT_METHOD , { canBeMissing : true } ) ;
72+
6773 const getAnchorPosition = useCallback (
6874 ( domRect : DomRect ) : AnchorPosition => {
6975 if ( anchorAlignment . vertical === CONST . MODAL . ANCHOR_ORIGIN_VERTICAL . TOP ) {
@@ -103,31 +109,55 @@ function KYCWall({
103109 } , [ getAnchorPosition ] ) ;
104110
105111 const selectPaymentMethod = useCallback (
106- ( paymentMethod : PaymentMethod ) => {
107- onSelectPaymentMethod ( paymentMethod ) ;
112+ ( paymentMethod ?: PaymentMethod , policy ?: Policy ) => {
113+ if ( paymentMethod ) {
114+ onSelectPaymentMethod ( paymentMethod ) ;
115+ }
108116
109117 if ( paymentMethod === CONST . PAYMENT_METHODS . PERSONAL_BANK_ACCOUNT ) {
110118 openPersonalBankAccountSetupView ( { shouldSetUpUSBankAccount : isIOUReport ( iouReport ) } ) ;
111119 } else if ( paymentMethod === CONST . PAYMENT_METHODS . DEBIT_CARD ) {
112120 Navigation . navigate ( addDebitCardRoute ?? ROUTES . HOME ) ;
113- } else if ( paymentMethod === CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT ) {
121+ } else if ( paymentMethod === CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT || policy ) {
114122 if ( iouReport && isIOUReport ( iouReport ) ) {
123+ if ( policy ) {
124+ const policyExpenseChatReportID = getPolicyExpenseChat ( iouReport . ownerAccountID , policy . id ) ?. reportID ;
125+ if ( ! policyExpenseChatReportID ) {
126+ const { policyExpenseChatReportID : newPolicyExpenseChatReportID } = moveIOUReportToPolicyAndInviteSubmitter ( iouReport . reportID , policy . id , formatPhoneNumber ) ?? { } ;
127+ savePreferredPaymentMethod ( iouReport . policyID , policy . id , CONST . LAST_PAYMENT_METHOD . IOU , lastPaymentMethod ?. [ policy . id ] ) ;
128+ Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( newPolicyExpenseChatReportID ) ) ;
129+ } else {
130+ moveIOUReportToPolicy ( iouReport . reportID , policy . id , true ) ;
131+ savePreferredPaymentMethod ( iouReport . policyID , policy . id , CONST . LAST_PAYMENT_METHOD . IOU , lastPaymentMethod ?. [ policy . id ] ) ;
132+ Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( policyExpenseChatReportID ) ) ;
133+ }
134+
135+ if ( policy ?. achAccount ) {
136+ return ;
137+ }
138+ // Navigate to the bank account set up flow for this specific policy
139+ Navigation . navigate ( ROUTES . BANK_ACCOUNT_WITH_STEP_TO_OPEN . getRoute ( policy . id ) ) ;
140+ return ;
141+ }
142+
115143 const { policyID, workspaceChatReportID, reportPreviewReportActionID, adminsChatReportID} = createWorkspaceFromIOUPayment ( iouReport ) ?? { } ;
144+ if ( policyID && iouReport ?. policyID ) {
145+ savePreferredPaymentMethod ( iouReport . policyID , policyID , CONST . LAST_PAYMENT_METHOD . IOU , lastPaymentMethod ?. [ iouReport ?. policyID ] ) ;
146+ }
116147 completePaymentOnboarding ( CONST . PAYMENT_SELECTED . BBA , adminsChatReportID , policyID ) ;
117148 if ( workspaceChatReportID ) {
118149 Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( workspaceChatReportID , reportPreviewReportActionID ) ) ;
119150 }
120151
121152 // Navigate to the bank account set up flow for this specific policy
122153 Navigation . navigate ( ROUTES . BANK_ACCOUNT_WITH_STEP_TO_OPEN . getRoute ( policyID ) ) ;
123-
124154 return ;
125155 }
126156 const bankAccountRoute = addBankAccountRoute ?? getBankAccountRoute ( chatReport ) ;
127157 Navigation . navigate ( bankAccountRoute ) ;
128158 }
129159 } ,
130- [ addBankAccountRoute , addDebitCardRoute , chatReport , iouReport , onSelectPaymentMethod ] ,
160+ [ addBankAccountRoute , addDebitCardRoute , chatReport , iouReport , onSelectPaymentMethod , formatPhoneNumber , lastPaymentMethod ] ,
131161 ) ;
132162
133163 /**
@@ -137,7 +167,7 @@ function KYCWall({
137167 *
138168 */
139169 const continueAction = useCallback (
140- ( event ?: GestureResponderEvent | KeyboardEvent , iouPaymentType ?: PaymentMethodType ) => {
170+ ( event ?: GestureResponderEvent | KeyboardEvent , iouPaymentType ?: PaymentMethodType , paymentMethod ?: PaymentMethod , policy ?: Policy ) => {
141171 const currentSource = walletTerms ?. source ?? source ;
142172
143173 /**
@@ -171,6 +201,19 @@ function KYCWall({
171201 return ;
172202 }
173203
204+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
205+ if ( paymentMethod || policy ) {
206+ setShouldShowAddPaymentMenu ( false ) ;
207+ selectPaymentMethod ( paymentMethod , policy ) ;
208+ return ;
209+ }
210+
211+ if ( iouPaymentType && isExpenseReport ) {
212+ setShouldShowAddPaymentMenu ( false ) ;
213+ selectPaymentMethod ( CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT ) ;
214+ return ;
215+ }
216+
174217 const clickedElementLocation = getClickedTargetLocation ( targetElement as HTMLDivElement ) ;
175218 const position = getAnchorPosition ( clickedElementLocation ) ;
176219
@@ -183,13 +226,20 @@ function KYCWall({
183226 // Ask the user to upgrade to a gold wallet as this means they have not yet gone through our Know Your Customer (KYC) checks
184227 const hasActivatedWallet = userWallet ?. tierName && [ CONST . WALLET . TIER_NAME . GOLD , CONST . WALLET . TIER_NAME . PLATINUM ] . some ( ( name ) => name === userWallet . tierName ) ;
185228
186- if ( ! hasActivatedWallet ) {
229+ if ( ! hasActivatedWallet && ! policy ) {
187230 Log . info ( '[KYC Wallet] User does not have active wallet' ) ;
188231
189232 Navigation . navigate ( enablePaymentsRoute ) ;
190233
191234 return ;
192235 }
236+
237+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
238+ if ( policy || ( paymentMethod && ( ! hasActivatedWallet || paymentMethod !== CONST . PAYMENT_METHODS . PERSONAL_BANK_ACCOUNT ) ) ) {
239+ setShouldShowAddPaymentMenu ( false ) ;
240+ selectPaymentMethod ( paymentMethod , policy ) ;
241+ return ;
242+ }
193243 }
194244
195245 Log . info ( '[KYC Wallet] User has valid payment method and passed KYC checks or did not need them' ) ;
0 commit comments