@@ -5,19 +5,20 @@ import type {EmitterSubscription, GestureResponderEvent, View} from 'react-nativ
55import AddPaymentMethodMenu from '@components/AddPaymentMethodMenu' ;
66import useOnyx from '@hooks/useOnyx' ;
77import { openPersonalBankAccountSetupView } from '@libs/actions/BankAccounts' ;
8- import { completePaymentOnboarding } from '@libs/actions/IOU' ;
8+ import { completePaymentOnboarding , savePreferredPaymentMethod } from '@libs/actions/IOU' ;
9+ import { moveIOUReportToPolicy , moveIOUReportToPolicyAndInviteSubmitter } from '@libs/actions/Report' ;
910import getClickedTargetLocation from '@libs/getClickedTargetLocation' ;
1011import Log from '@libs/Log' ;
1112import Navigation from '@libs/Navigation/Navigation' ;
1213import { hasExpensifyPaymentMethod } from '@libs/PaymentUtils' ;
13- import { isExpenseReport as isExpenseReportReportUtils , isIOUReport } from '@libs/ReportUtils' ;
14+ import { getPolicyExpenseChat , isExpenseReport as isExpenseReportReportUtils , isIOUReport } from '@libs/ReportUtils' ;
1415import { kycWallRef } from '@userActions/PaymentMethods' ;
1516import { createWorkspaceFromIOUPayment } from '@userActions/Policy/Policy' ;
1617import { setKYCWallSource } from '@userActions/Wallet' ;
1718import CONST from '@src/CONST' ;
1819import ONYXKEYS from '@src/ONYXKEYS' ;
1920import ROUTES from '@src/ROUTES' ;
20- import type { BankAccountList } from '@src/types/onyx' ;
21+ import type { BankAccountList , Policy } from '@src/types/onyx' ;
2122import type { PaymentMethodType } from '@src/types/onyx/OriginalMessage' ;
2223import { getEmptyObject } from '@src/types/utils/EmptyObject' ;
2324import viewRef from '@src/types/utils/viewRef' ;
@@ -102,24 +103,48 @@ function KYCWall({
102103 } , [ getAnchorPosition ] ) ;
103104
104105 const selectPaymentMethod = useCallback (
105- ( paymentMethod : PaymentMethod ) => {
106- onSelectPaymentMethod ( paymentMethod ) ;
106+ ( paymentMethod ?: PaymentMethod , policy ?: Policy ) => {
107+ if ( paymentMethod ) {
108+ onSelectPaymentMethod ( paymentMethod ) ;
109+ }
107110
108111 if ( paymentMethod === CONST . PAYMENT_METHODS . PERSONAL_BANK_ACCOUNT ) {
109112 openPersonalBankAccountSetupView ( ) ;
110113 } else if ( paymentMethod === CONST . PAYMENT_METHODS . DEBIT_CARD ) {
111114 Navigation . navigate ( addDebitCardRoute ?? ROUTES . HOME ) ;
112- } else if ( paymentMethod === CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT ) {
115+ } else if ( paymentMethod === CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT || policy ) {
113116 if ( iouReport && isIOUReport ( iouReport ) ) {
117+ if ( policy ) {
118+ const policyExpenseChatReportID = getPolicyExpenseChat ( iouReport . ownerAccountID , policy . id ) ?. reportID ;
119+ if ( ! policyExpenseChatReportID ) {
120+ const { policyExpenseChatReportID : newPolicyExpenseChatReportID } = moveIOUReportToPolicyAndInviteSubmitter ( iouReport . reportID , policy . id ) ?? { } ;
121+ savePreferredPaymentMethod ( iouReport . policyID , policy . id , CONST . LAST_PAYMENT_METHOD . IOU ) ;
122+ Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( newPolicyExpenseChatReportID ) ) ;
123+ } else {
124+ moveIOUReportToPolicy ( iouReport . reportID , policy . id , true ) ;
125+ savePreferredPaymentMethod ( iouReport . policyID , policy . id , CONST . LAST_PAYMENT_METHOD . IOU ) ;
126+ Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( policyExpenseChatReportID ) ) ;
127+ }
128+
129+ if ( policy ?. achAccount ) {
130+ return ;
131+ }
132+ // Navigate to the bank account set up flow for this specific policy
133+ Navigation . navigate ( ROUTES . BANK_ACCOUNT_WITH_STEP_TO_OPEN . getRoute ( policy . id ) ) ;
134+ return ;
135+ }
136+
114137 const { policyID, workspaceChatReportID, reportPreviewReportActionID, adminsChatReportID} = createWorkspaceFromIOUPayment ( iouReport ) ?? { } ;
138+ if ( policyID ) {
139+ savePreferredPaymentMethod ( iouReport . policyID , policyID , CONST . LAST_PAYMENT_METHOD . IOU ) ;
140+ }
115141 completePaymentOnboarding ( CONST . PAYMENT_SELECTED . BBA , adminsChatReportID , policyID ) ;
116142 if ( workspaceChatReportID ) {
117143 Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( workspaceChatReportID , reportPreviewReportActionID ) ) ;
118144 }
119145
120146 // Navigate to the bank account set up flow for this specific policy
121147 Navigation . navigate ( ROUTES . BANK_ACCOUNT_WITH_STEP_TO_OPEN . getRoute ( policyID ) ) ;
122-
123148 return ;
124149 }
125150 Navigation . navigate ( addBankAccountRoute ) ;
@@ -135,7 +160,7 @@ function KYCWall({
135160 *
136161 */
137162 const continueAction = useCallback (
138- ( event ?: GestureResponderEvent | KeyboardEvent , iouPaymentType ?: PaymentMethodType ) => {
163+ ( event ?: GestureResponderEvent | KeyboardEvent , iouPaymentType ?: PaymentMethodType , paymentMethod ?: PaymentMethod , policy ?: Policy ) => {
139164 const currentSource = walletTerms ?. source ?? source ;
140165
141166 /**
@@ -169,6 +194,19 @@ function KYCWall({
169194 return ;
170195 }
171196
197+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
198+ if ( paymentMethod || policy ) {
199+ setShouldShowAddPaymentMenu ( false ) ;
200+ selectPaymentMethod ( paymentMethod , policy ) ;
201+ return ;
202+ }
203+
204+ if ( iouPaymentType && isExpenseReport ) {
205+ setShouldShowAddPaymentMenu ( false ) ;
206+ selectPaymentMethod ( CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT ) ;
207+ return ;
208+ }
209+
172210 const clickedElementLocation = getClickedTargetLocation ( targetElement as HTMLDivElement ) ;
173211 const position = getAnchorPosition ( clickedElementLocation ) ;
174212
@@ -181,13 +219,20 @@ function KYCWall({
181219 // Ask the user to upgrade to a gold wallet as this means they have not yet gone through our Know Your Customer (KYC) checks
182220 const hasActivatedWallet = userWallet ?. tierName && [ CONST . WALLET . TIER_NAME . GOLD , CONST . WALLET . TIER_NAME . PLATINUM ] . some ( ( name ) => name === userWallet . tierName ) ;
183221
184- if ( ! hasActivatedWallet ) {
222+ if ( ! hasActivatedWallet && ! policy ) {
185223 Log . info ( '[KYC Wallet] User does not have active wallet' ) ;
186224
187225 Navigation . navigate ( enablePaymentsRoute ) ;
188226
189227 return ;
190228 }
229+
230+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
231+ if ( paymentMethod || policy ) {
232+ setShouldShowAddPaymentMenu ( false ) ;
233+ selectPaymentMethod ( paymentMethod , policy ) ;
234+ return ;
235+ }
191236 }
192237
193238 Log . info ( '[KYC Wallet] User has valid payment method and passed KYC checks or did not need them' ) ;
0 commit comments