Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Implement SCA support to transfer owner #49411

Merged
merged 20 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type VerifySetupIntentAndRequestPolicyOwnerChangeParams = {
policyID: string;
};
export default VerifySetupIntentAndRequestPolicyOwnerChangeParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type {default as ActivatePhysicalExpensifyCardParams} from './ActivatePhy
export type {default as AddNewContactMethodParams} from './AddNewContactMethodParams';
export type {default as AddPaymentCardParams} from './AddPaymentCardParams';
export type {default as VerifySetupIntentParams} from './VerifySetupIntentParams';
export type {default as VerifySetupIntentAndRequestPolicyOwnerChangeParams} from './VerifySetupIntentAndRequestPolicyOwnerChangeParams';
export type {default as AddPersonalBankAccountParams} from './AddPersonalBankAccountParams';
export type {default as RestartBankAccountSetupParams} from './RestartBankAccountSetupParams';
export type {default as AddSchoolPrincipalParams} from './AddSchoolPrincipalParams';
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const WRITE_COMMANDS = {
ADD_PAYMENT_CARD: 'AddPaymentCard',
ADD_PAYMENT_CARD_GBP: 'AddPaymentCardGBP',
VERIFY_SETUP_INTENT: 'User_VerifySetupIntent',
VERIFY_SETUP_INTENT_AND_REQUEST_POLICY_OWNER_CHANGE: 'VerifySetupIntentAndRequestPolicyOwnerChange',
TRANSFER_WALLET_BALANCE: 'TransferWalletBalance',
DELETE_PAYMENT_CARD: 'DeletePaymentCard',
UPDATE_PRONOUNS: 'UpdatePronouns',
Expand Down Expand Up @@ -436,6 +437,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.ADD_PAYMENT_CARD]: Parameters.AddPaymentCardParams;
[WRITE_COMMANDS.ADD_PAYMENT_CARD_GBP]: Parameters.AddPaymentCardParams;
[WRITE_COMMANDS.VERIFY_SETUP_INTENT]: Parameters.VerifySetupIntentParams;
[WRITE_COMMANDS.VERIFY_SETUP_INTENT_AND_REQUEST_POLICY_OWNER_CHANGE]: Parameters.VerifySetupIntentAndRequestPolicyOwnerChangeParams;
[WRITE_COMMANDS.DELETE_PAYMENT_CARD]: Parameters.DeletePaymentCardParams;
[WRITE_COMMANDS.UPDATE_PRONOUNS]: Parameters.UpdatePronounsParams;
[WRITE_COMMANDS.UPDATE_DISPLAY_NAME]: Parameters.UpdateDisplayNameParams;
Expand Down
93 changes: 81 additions & 12 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as API from '@libs/API';
import type {
AddBillingCardAndRequestWorkspaceOwnerChangeParams,
AddPaymentCardParams,
CreateWorkspaceFromIOUPaymentParams,
CreateWorkspaceParams,
DeleteWorkspaceAvatarParams,
Expand Down Expand Up @@ -56,7 +57,7 @@
UpdateWorkspaceGeneralSettingsParams,
UpgradeToCorporateParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import DateUtils from '@libs/DateUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
Expand Down Expand Up @@ -837,18 +838,85 @@
},
];

const params: AddBillingCardAndRequestWorkspaceOwnerChangeParams = {
policyID,
cardNumber,
cardYear,
cardMonth,
cardCVV,
addressName,
addressZip,
currency,
};
if (currency === CONST.PAYMENT_CARD_CURRENCY.GBP) {
const params: AddPaymentCardParams = {
cardNumber,
cardYear,
cardMonth,
cardCVV,
addressName,
addressZip,
currency,
isP2PDebitCard: false,
};
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.ADD_PAYMENT_CARD_GBP, params).then((response) => {
waterim marked this conversation as resolved.
Show resolved Hide resolved
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 {
const params: AddBillingCardAndRequestWorkspaceOwnerChangeParams = {
policyID,
cardNumber,
cardYear,
cardMonth,
cardCVV,
addressName,
addressZip,
currency,
};
// eslint-disable-next-line rulesdir/no-multiple-api-calls
API.write(WRITE_COMMANDS.ADD_BILLING_CARD_AND_REQUEST_WORKSPACE_OWNER_CHANGE, params, {optimisticData, successData, failureData});
}
}

/**
* Properly updates the nvp_privateStripeCustomerID onyx data for 3DS payment
*
*/
function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
errorFields: null,
isLoading: true,
isChangeOwnerSuccessful: false,
isChangeOwnerFailed: false,
},
},
];

API.write(WRITE_COMMANDS.ADD_BILLING_CARD_AND_REQUEST_WORKSPACE_OWNER_CHANGE, params, {optimisticData, successData, failureData});
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
isLoading: false,
isChangeOwnerSuccessful: true,
isChangeOwnerFailed: false,
owner: sessionEmail,
ownerAccountID: sessionAccountID,
},
},
];

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
isLoading: false,
isChangeOwnerSuccessful: false,
isChangeOwnerFailed: true,
},
},
];

Check failure on line 918 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, and 'sessionAccountID' does not exist in type 'VerifySetupIntentAndRequestPolicyOwnerChangeParams'.
API.write(WRITE_COMMANDS.VERIFY_SETUP_INTENT_AND_REQUEST_POLICY_OWNER_CHANGE, {sessionAccountID, policyID}, {optimisticData, successData, failureData});
}

/**
Expand Down Expand Up @@ -4884,6 +4952,7 @@
updateCompanyCardName,
setCompanyCardExportAccount,
clearCompanyCardErrorField,
verifySetupIntentAndRequestPolicyOwnerChange,
};

export type {NewCustomUnit};
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import Modal from '@components/Modal';
import ScreenWrapper from '@components/ScreenWrapper';
import useThemeStyles from '@hooks/useThemeStyles';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as PolicyActions from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

type CardAuthenticationModalProps = {
/** Title shown in the header of the modal */
headerTitle?: string;

policyID?: string;
};
function CardAuthenticationModal({headerTitle}: CardAuthenticationModalProps) {
function CardAuthenticationModal({headerTitle, policyID}: CardAuthenticationModalProps) {
const styles = useThemeStyles();
const [authenticationLink] = useOnyx(ONYXKEYS.VERIFY_3DS_SUBSCRIPTION);
const [session] = useOnyx(ONYXKEYS.SESSION);
Expand All @@ -37,11 +40,15 @@ function CardAuthenticationModal({headerTitle}: CardAuthenticationModalProps) {
(event: MessageEvent<string>) => {
const message = event.data;
if (message === CONST.GBP_AUTHENTICATION_COMPLETE) {
PaymentMethods.verifySetupIntent(session?.accountID ?? -1, true);
if (policyID) {
PolicyActions.verifySetupIntentAndRequestPolicyOwnerChange(policyID);
} else {
PaymentMethods.verifySetupIntent(session?.accountID ?? -1, true);
}
onModalClose();
}
},
[onModalClose, session?.accountID],
[onModalClose, policyID, session?.accountID],
);

useEffect(() => {
Expand Down
25 changes: 17 additions & 8 deletions src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import isEmpty from 'lodash/isEmpty';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
Expand All @@ -24,6 +25,7 @@ import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CardUtils from '@libs/CardUtils';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import getPlatform from '@libs/getPlatform';
import * as PolicyUtils from '@libs/PolicyUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
Expand Down Expand Up @@ -80,6 +82,11 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
const policyOwnerDisplayName = ownerDetails.displayName ?? policy?.owner ?? '';
const companyCards = CardUtils.getMemberCards(policy, allCardsList, accountID);

// To render correctly transfer owner button
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {initialValue: {}});
const isNative = getPlatform() === CONST.PLATFORM.IOS || getPlatform() === CONST.PLATFORM.ANDROID;
waterim marked this conversation as resolved.
Show resolved Hide resolved
const shouldShowTransferButton = !isNative || (isNative && !isEmpty(fundList));

// TODO: for now enabled for testing purposes. Change this to check for the actual multiple feeds when API is ready
const hasMultipleFeeds = policy?.areCompanyCardsEnabled;

Expand Down Expand Up @@ -240,14 +247,16 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
</Text>
)}
{isSelectedMemberOwner && isCurrentUserAdmin && !isCurrentUserOwner ? (
<Button
text={translate('workspace.people.transferOwner')}
onPress={startChangeOwnershipFlow}
isDisabled={isOffline}
icon={Expensicons.Transfer}
iconStyles={StyleUtils.getTransformScaleStyle(0.8)}
style={styles.mv5}
/>
shouldShowTransferButton && (
<Button
text={translate('workspace.people.transferOwner')}
onPress={startChangeOwnershipFlow}
isDisabled={isOffline}
icon={Expensicons.Transfer}
iconStyles={StyleUtils.getTransformScaleStyle(0.8)}
style={styles.mv5}
/>
)
) : (
<Button
text={translate('workspace.people.removeWorkspaceMemberButtonTitle')}
Expand Down
29 changes: 18 additions & 11 deletions src/pages/workspace/members/WorkspaceOwnerChangeWrapperPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useEffect} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand All @@ -9,11 +10,13 @@ import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import CardAuthenticationModal from '@pages/settings/Subscription/CardAuthenticationModal';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import withPolicy from '@pages/workspace/withPolicy';
import type {WithPolicyOnyxProps} from '@pages/workspace/withPolicy';
import * as MemberActions from '@userActions/Policy/Member';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import WorkspaceOwnerChangeCheck from './WorkspaceOwnerChangeCheck';
Expand All @@ -24,10 +27,12 @@ type WorkspaceOwnerChangeWrapperPageProps = WithPolicyOnyxProps & StackScreenPro
function WorkspaceOwnerChangeWrapperPage({route, policy}: WorkspaceOwnerChangeWrapperPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();

const [privateStripeCustomerID] = useOnyx(ONYXKEYS.NVP_PRIVATE_STRIPE_CUSTOMER_ID);
const policyID = route.params.policyID;
const accountID = route.params.accountID;
const error = route.params.error;
const isAuthRequired = privateStripeCustomerID?.status === CONST.STRIPE_GBP_AUTH_STATUSES.CARD_AUTHENTICATION_REQUIRED;
const shouldShowPaymentCardForm = error === CONST.POLICY.OWNERSHIP_ERRORS.NO_BILLING_CARD || isAuthRequired;

useEffect(() => {
if (!policy || policy?.isLoading) {
Expand Down Expand Up @@ -71,16 +76,18 @@ function WorkspaceOwnerChangeWrapperPage({route, policy}: WorkspaceOwnerChangeWr
/>
<View style={[styles.containerWithSpaceBetween, error !== CONST.POLICY.OWNERSHIP_ERRORS.NO_BILLING_CARD ? styles.ph5 : styles.ph0, styles.pb0]}>
{policy?.isLoading && <FullScreenLoadingIndicator />}
{!policy?.isLoading &&
(error === CONST.POLICY.OWNERSHIP_ERRORS.NO_BILLING_CARD ? (
<WorkspaceOwnerPaymentCardForm policy={policy} />
) : (
<WorkspaceOwnerChangeCheck
policy={policy}
accountID={accountID}
error={error}
/>
))}
{shouldShowPaymentCardForm && <WorkspaceOwnerPaymentCardForm policy={policy} />}
{!policy?.isLoading && !shouldShowPaymentCardForm && (
<WorkspaceOwnerChangeCheck
policy={policy}
accountID={accountID}
error={error}
/>
)}
<CardAuthenticationModal
headerTitle={translate('subscription.authenticatePaymentCard')}
policyID={policyID}
/>
</View>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function WorkspaceOwnerPaymentCardForm({policy}: WorkspaceOwnerPaymentCardFormPr
cardCVV: values.securityCode,
addressName: values.nameOnCard,
addressZip: values.addressZipCode,
currency: CONST.CURRENCY.USD,
currency: values.currency,
};

PolicyActions.addBillingCardAndRequestPolicyOwnerChange(policyID, cardData);
Expand Down
Loading