2021-06-08 17:40:57 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { getHistory, getNewPath } from '@woocommerce/navigation';
|
|
|
|
import {
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
PAYMENT_GATEWAYS_STORE_NAME,
|
|
|
|
} from '@woocommerce/data';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useMemo, useCallback } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { List, Placeholder as ListPlaceholder } from './components/List';
|
|
|
|
import { Setup, Placeholder as SetupPlaceholder } from './components/Setup';
|
2021-06-28 18:18:42 +00:00
|
|
|
import { WCPaySuggestion } from './components/WCPay';
|
2021-06-08 17:40:57 +00:00
|
|
|
import './plugins/Bacs';
|
|
|
|
|
|
|
|
export const PaymentGatewaySuggestions = ( { query } ) => {
|
|
|
|
const { updatePaymentGateway } = useDispatch( PAYMENT_GATEWAYS_STORE_NAME );
|
2021-06-10 17:10:42 +00:00
|
|
|
const { getPaymentGateway, paymentGateways, isResolving } = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
const installedPaymentGateways = select(
|
|
|
|
PAYMENT_GATEWAYS_STORE_NAME
|
|
|
|
)
|
|
|
|
.getPaymentGateways()
|
|
|
|
.reduce( ( map, gateway ) => {
|
|
|
|
map[ gateway.id ] = gateway;
|
2021-06-08 17:40:57 +00:00
|
|
|
return map;
|
2021-06-10 17:10:42 +00:00
|
|
|
}, {} );
|
|
|
|
|
|
|
|
const mappedSuggestions = select( ONBOARDING_STORE_NAME )
|
|
|
|
.getPaymentGatewaySuggestions()
|
|
|
|
.reduce( ( map, suggestion ) => {
|
|
|
|
const { id } = suggestion;
|
|
|
|
const installedGateway = installedPaymentGateways[
|
|
|
|
suggestion.id
|
|
|
|
]
|
|
|
|
? installedPaymentGateways[ id ]
|
|
|
|
: {};
|
|
|
|
|
|
|
|
const enrichedSuggestion = {
|
|
|
|
installed: !! installedPaymentGateways[ id ],
|
|
|
|
postInstallScripts:
|
|
|
|
installedGateway.post_install_scripts,
|
2021-06-21 18:27:41 +00:00
|
|
|
enabled: installedGateway.enabled || false,
|
2021-06-10 17:10:42 +00:00
|
|
|
needsSetup: installedGateway.needs_setup,
|
|
|
|
settingsUrl: installedGateway.settings_url,
|
|
|
|
connectionUrl: installedGateway.connection_url,
|
|
|
|
setupHelpText: installedGateway.setup_help_text,
|
|
|
|
title: installedGateway.title,
|
|
|
|
requiredSettings: installedGateway.required_settings_keys
|
|
|
|
? installedGateway.required_settings_keys
|
|
|
|
.map(
|
|
|
|
( settingKey ) =>
|
|
|
|
installedGateway.settings[
|
|
|
|
settingKey
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.filter( Boolean )
|
|
|
|
: [],
|
|
|
|
...suggestion,
|
|
|
|
};
|
|
|
|
|
|
|
|
map.set( id, enrichedSuggestion );
|
|
|
|
return map;
|
|
|
|
}, new Map() );
|
|
|
|
|
|
|
|
return {
|
|
|
|
getPaymentGateway: select( PAYMENT_GATEWAYS_STORE_NAME )
|
|
|
|
.getPaymentGateway,
|
|
|
|
getOption: select( OPTIONS_STORE_NAME ).getOption,
|
|
|
|
isResolving: select( ONBOARDING_STORE_NAME ).isResolving(
|
|
|
|
'getPaymentGatewaySuggestions'
|
|
|
|
),
|
|
|
|
paymentGateways: mappedSuggestions,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2021-06-08 17:40:57 +00:00
|
|
|
|
|
|
|
const enablePaymentGateway = ( id ) => {
|
|
|
|
if ( ! id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gateway = getPaymentGateway( id );
|
|
|
|
|
|
|
|
if ( ! gateway || gateway.enabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePaymentGateway( id, {
|
|
|
|
enabled: true,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const markConfigured = useCallback(
|
|
|
|
async ( id, queryParams = {} ) => {
|
2021-06-10 17:10:42 +00:00
|
|
|
if ( ! paymentGateways.get( id ) ) {
|
2021-06-08 17:40:57 +00:00
|
|
|
throw `Payment gateway ${ id } not found in available gateways list`;
|
|
|
|
}
|
|
|
|
|
|
|
|
enablePaymentGateway( id );
|
|
|
|
|
|
|
|
recordEvent( 'tasklist_payment_connect_method', {
|
|
|
|
payment_method: id,
|
|
|
|
} );
|
|
|
|
|
|
|
|
getHistory().push(
|
|
|
|
getNewPath( { ...queryParams, task: 'payments' }, '/', {} )
|
|
|
|
);
|
|
|
|
},
|
2021-06-10 17:10:42 +00:00
|
|
|
[ paymentGateways ]
|
2021-06-08 17:40:57 +00:00
|
|
|
);
|
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
const recordConnectStartEvent = useCallback( ( gatewayId ) => {
|
2021-06-08 17:40:57 +00:00
|
|
|
recordEvent( 'tasklist_payment_connect_start', {
|
2021-06-10 17:10:42 +00:00
|
|
|
payment_method: gatewayId,
|
2021-06-08 17:40:57 +00:00
|
|
|
} );
|
|
|
|
}, [] );
|
|
|
|
|
2021-07-05 23:33:03 +00:00
|
|
|
const recommendation = useMemo(
|
|
|
|
() =>
|
|
|
|
Array.from( paymentGateways.values() )
|
|
|
|
.filter( ( gateway ) => gateway.recommendation_priority )
|
|
|
|
.sort(
|
|
|
|
( a, b ) =>
|
|
|
|
a.recommendation_priority - b.recommendation_priority
|
|
|
|
)
|
|
|
|
.map( ( gateway ) => gateway.id )
|
|
|
|
.shift(),
|
|
|
|
[ paymentGateways ]
|
|
|
|
);
|
2021-06-08 17:40:57 +00:00
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
const currentGateway = useMemo( () => {
|
|
|
|
if ( ! query.id || isResolving || ! paymentGateways.size ) {
|
2021-06-08 17:40:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
const gateway = paymentGateways.get( query.id );
|
2021-06-08 17:40:57 +00:00
|
|
|
|
|
|
|
if ( ! gateway ) {
|
|
|
|
throw `Current gateway ${ query.id } not found in available gateways list`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gateway;
|
2021-06-10 17:10:42 +00:00
|
|
|
}, [ isResolving, query, paymentGateways ] );
|
|
|
|
|
|
|
|
const [ wcPayGateway, enabledGateways, additionalGateways ] = useMemo(
|
|
|
|
() =>
|
|
|
|
Array.from( paymentGateways.values() ).reduce(
|
|
|
|
( all, gateway ) => {
|
|
|
|
const [ wcPay, enabled, additional ] = all;
|
|
|
|
|
|
|
|
// WCPay is handled separately when not installed and configured
|
|
|
|
if (
|
|
|
|
gateway.id === 'woocommerce_payments' &&
|
|
|
|
! ( gateway.installed && ! gateway.needsSetup )
|
|
|
|
) {
|
|
|
|
wcPay.push( gateway );
|
|
|
|
} else if ( gateway.enabled ) {
|
|
|
|
enabled.push( gateway );
|
|
|
|
} else {
|
|
|
|
additional.push( gateway );
|
|
|
|
}
|
|
|
|
|
|
|
|
return all;
|
|
|
|
},
|
|
|
|
[ [], [], [] ]
|
|
|
|
),
|
|
|
|
[ paymentGateways ]
|
|
|
|
);
|
2021-06-08 17:40:57 +00:00
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
if ( query.id && ! currentGateway ) {
|
2021-06-08 17:40:57 +00:00
|
|
|
return <SetupPlaceholder />;
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
if ( currentGateway ) {
|
2021-06-08 17:40:57 +00:00
|
|
|
return (
|
|
|
|
<Setup
|
2021-06-10 17:10:42 +00:00
|
|
|
paymentGateway={ currentGateway }
|
2021-06-08 17:40:57 +00:00
|
|
|
markConfigured={ markConfigured }
|
|
|
|
recordConnectStartEvent={ recordConnectStartEvent }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-task-payments">
|
2021-06-10 17:10:42 +00:00
|
|
|
{ ! paymentGateways.size && <ListPlaceholder /> }
|
2021-06-08 17:40:57 +00:00
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
{ !! wcPayGateway.length && (
|
|
|
|
<WCPaySuggestion paymentGateway={ wcPayGateway[ 0 ] } />
|
2021-06-08 17:40:57 +00:00
|
|
|
) }
|
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
{ !! enabledGateways.length && (
|
2021-06-08 17:40:57 +00:00
|
|
|
<List
|
|
|
|
heading={ __(
|
|
|
|
'Enabled payment gateways',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
recommendation={ recommendation }
|
2021-06-10 17:10:42 +00:00
|
|
|
paymentGateways={ enabledGateways }
|
2021-06-08 17:40:57 +00:00
|
|
|
/>
|
|
|
|
) }
|
|
|
|
|
2021-06-10 17:10:42 +00:00
|
|
|
{ !! additionalGateways.length && (
|
2021-06-08 17:40:57 +00:00
|
|
|
<List
|
|
|
|
heading={ __(
|
|
|
|
'Additional payment gateways',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
recommendation={ recommendation }
|
2021-06-10 17:10:42 +00:00
|
|
|
paymentGateways={ additionalGateways }
|
2021-06-08 17:40:57 +00:00
|
|
|
markConfigured={ markConfigured }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentGatewaySuggestions;
|