2019-08-29 16:41:04 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-15 21:45:19 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2020-03-27 23:24:32 +00:00
|
|
|
import classnames from 'classnames';
|
2020-03-16 11:09:29 +00:00
|
|
|
import { cloneElement, Component } from '@wordpress/element';
|
2019-08-29 16:41:04 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2020-12-21 19:05:29 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Card,
|
|
|
|
CardBody,
|
|
|
|
CardFooter,
|
|
|
|
CardHeader,
|
|
|
|
FormToggle,
|
|
|
|
} from '@wordpress/components';
|
2020-09-03 21:45:40 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-12-21 19:05:29 +00:00
|
|
|
import { H, Plugins } from '@woocommerce/components';
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
2020-03-15 21:45:19 +00:00
|
|
|
getHistory,
|
|
|
|
getNewPath,
|
|
|
|
updateQueryString,
|
|
|
|
} from '@woocommerce/navigation';
|
2020-05-28 08:51:40 +00:00
|
|
|
import {
|
|
|
|
ONBOARDING_STORE_NAME,
|
2020-06-10 23:49:27 +00:00
|
|
|
OPTIONS_STORE_NAME,
|
2020-05-28 08:51:40 +00:00
|
|
|
PLUGINS_STORE_NAME,
|
2020-06-10 23:49:27 +00:00
|
|
|
pluginNames,
|
2020-06-22 22:18:35 +00:00
|
|
|
SETTINGS_STORE_NAME,
|
2020-05-28 08:51:40 +00:00
|
|
|
} from '@woocommerce/data';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2019-08-29 16:41:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-27 23:55:48 +00:00
|
|
|
import { createNoticesFromResponse } from '../../../lib/notices';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { getCountryCode } from '../../../dashboard/utils';
|
2020-03-16 11:09:29 +00:00
|
|
|
import { getPaymentMethods } from './methods';
|
2019-08-29 16:41:04 +00:00
|
|
|
|
|
|
|
class Payments extends Component {
|
2020-03-16 11:09:29 +00:00
|
|
|
constructor( props ) {
|
2019-08-29 16:41:04 +00:00
|
|
|
super( ...arguments );
|
2020-03-16 11:09:29 +00:00
|
|
|
const { methods } = props;
|
|
|
|
|
|
|
|
const enabledMethods = {};
|
|
|
|
methods.forEach(
|
|
|
|
( method ) => ( enabledMethods[ method.key ] = method.isEnabled )
|
|
|
|
);
|
|
|
|
this.state = {
|
2020-06-12 09:38:02 +00:00
|
|
|
busyMethod: null,
|
2020-03-16 11:09:29 +00:00
|
|
|
enabledMethods,
|
2020-06-12 09:38:02 +00:00
|
|
|
recommendedMethod: this.getRecommendedMethod(),
|
2020-03-16 11:09:29 +00:00
|
|
|
};
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
|
|
this.markConfigured = this.markConfigured.bind( this );
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:38:02 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { recommendedMethod } = this.state;
|
2020-06-04 09:36:57 +00:00
|
|
|
|
2020-06-12 09:38:02 +00:00
|
|
|
const method = this.getRecommendedMethod();
|
|
|
|
if ( recommendedMethod !== method ) {
|
2020-06-04 09:36:57 +00:00
|
|
|
this.setState( {
|
2020-06-12 09:38:02 +00:00
|
|
|
recommendedMethod: method,
|
2020-06-04 09:36:57 +00:00
|
|
|
} );
|
|
|
|
}
|
2020-03-16 11:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 09:38:02 +00:00
|
|
|
getRecommendedMethod() {
|
|
|
|
const { methods } = this.props;
|
|
|
|
return methods.find( ( m ) => m.key === 'wcpay' && m.visible )
|
|
|
|
? 'wcpay'
|
|
|
|
: 'stripe';
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:27:37 +00:00
|
|
|
markConfigured( method, queryParams = {} ) {
|
2020-08-24 13:20:57 +00:00
|
|
|
const { clearTaskStatusCache } = this.props;
|
2020-03-16 11:57:23 +00:00
|
|
|
const { enabledMethods } = this.state;
|
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
enabledMethods: {
|
|
|
|
...enabledMethods,
|
|
|
|
[ method ]: true,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2020-08-24 13:20:57 +00:00
|
|
|
clearTaskStatusCache();
|
2019-08-29 16:41:04 +00:00
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
recordEvent( 'tasklist_payment_connect_method', {
|
|
|
|
payment_method: method,
|
2019-10-03 16:03:29 +00:00
|
|
|
} );
|
2020-08-11 12:20:48 +00:00
|
|
|
|
2020-11-26 00:27:37 +00:00
|
|
|
getHistory().push(
|
|
|
|
getNewPath( { ...queryParams, task: 'payments' }, '/', {} )
|
|
|
|
);
|
2019-08-29 16:41:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
getCurrentMethod() {
|
2020-03-16 11:09:29 +00:00
|
|
|
const { methods, query } = this.props;
|
2019-10-03 16:03:29 +00:00
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
if ( ! query.method ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-03 16:03:29 +00:00
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
return methods.find( ( method ) => method.key === query.method );
|
2019-10-07 20:27:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
getInstallStep() {
|
|
|
|
const currentMethod = this.getCurrentMethod();
|
2019-10-03 16:03:29 +00:00
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
if ( ! currentMethod.plugins || ! currentMethod.plugins.length ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-03 16:03:29 +00:00
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
const { activePlugins } = this.props;
|
|
|
|
const pluginsToInstall = currentMethod.plugins.filter(
|
|
|
|
( method ) => ! activePlugins.includes( method )
|
|
|
|
);
|
|
|
|
const pluginNamesString = currentMethod.plugins
|
|
|
|
.map( ( pluginSlug ) => pluginNames[ pluginSlug ] )
|
|
|
|
.join( ' ' + __( 'and', 'woocommerce-admin' ) + ' ' );
|
2019-08-29 16:41:04 +00:00
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
return {
|
|
|
|
key: 'install',
|
|
|
|
label: sprintf(
|
|
|
|
__( 'Install %s', 'woocommerce-admin' ),
|
|
|
|
pluginNamesString
|
|
|
|
),
|
|
|
|
content: (
|
|
|
|
<Plugins
|
2020-08-27 23:55:48 +00:00
|
|
|
onComplete={ ( plugins, response ) => {
|
|
|
|
createNoticesFromResponse( response );
|
2020-03-15 21:45:19 +00:00
|
|
|
recordEvent( 'tasklist_payment_install_method', {
|
|
|
|
plugins: currentMethod.plugins,
|
|
|
|
} );
|
|
|
|
} }
|
2020-08-27 23:55:48 +00:00
|
|
|
onError={ ( errors, response ) =>
|
|
|
|
createNoticesFromResponse( response )
|
|
|
|
}
|
2020-03-15 21:45:19 +00:00
|
|
|
autoInstall
|
|
|
|
pluginSlugs={ currentMethod.plugins }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
isComplete: ! pluginsToInstall.length,
|
|
|
|
};
|
2019-08-29 16:41:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 13:20:57 +00:00
|
|
|
async toggleMethod( key ) {
|
|
|
|
const {
|
|
|
|
clearTaskStatusCache,
|
|
|
|
methods,
|
|
|
|
options,
|
|
|
|
updateOptions,
|
|
|
|
} = this.props;
|
2020-03-16 11:09:29 +00:00
|
|
|
const { enabledMethods } = this.state;
|
|
|
|
const method = methods.find( ( option ) => option.key === key );
|
|
|
|
|
|
|
|
enabledMethods[ key ] = ! enabledMethods[ key ];
|
|
|
|
this.setState( { enabledMethods } );
|
|
|
|
|
|
|
|
recordEvent( 'tasklist_payment_toggle', {
|
|
|
|
enabled: ! method.isEnabled,
|
|
|
|
payment_method: key,
|
|
|
|
} );
|
|
|
|
|
2020-08-24 13:20:57 +00:00
|
|
|
await updateOptions( {
|
2020-03-16 11:09:29 +00:00
|
|
|
[ method.optionName ]: {
|
|
|
|
...options[ method.optionName ],
|
|
|
|
enabled: method.isEnabled ? 'no' : 'yes',
|
|
|
|
},
|
|
|
|
} );
|
2020-08-11 12:20:48 +00:00
|
|
|
|
2020-08-24 13:20:57 +00:00
|
|
|
clearTaskStatusCache();
|
2020-03-16 11:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 09:38:02 +00:00
|
|
|
async handleClick( method ) {
|
|
|
|
const { methods } = this.props;
|
|
|
|
const { key, onClick } = method;
|
|
|
|
|
|
|
|
recordEvent( 'tasklist_payment_setup', {
|
|
|
|
options: methods.map( ( option ) => option.key ),
|
|
|
|
selected: key,
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( onClick ) {
|
|
|
|
this.setState( { busyMethod: key } );
|
|
|
|
await new Promise( onClick )
|
|
|
|
.then( () => {
|
|
|
|
this.setState( { busyMethod: null } );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
this.setState( { busyMethod: null } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQueryString( {
|
|
|
|
method: key,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
render() {
|
2020-03-15 21:45:19 +00:00
|
|
|
const currentMethod = this.getCurrentMethod();
|
2020-06-12 09:38:02 +00:00
|
|
|
const { busyMethod, enabledMethods, recommendedMethod } = this.state;
|
2020-08-11 12:20:48 +00:00
|
|
|
const { methods, query } = this.props;
|
2020-03-15 21:45:19 +00:00
|
|
|
|
|
|
|
if ( currentMethod ) {
|
|
|
|
return (
|
2020-12-21 19:05:29 +00:00
|
|
|
<Card className="woocommerce-task-payment-method woocommerce-task-card">
|
|
|
|
<CardBody>
|
|
|
|
{ cloneElement( currentMethod.container, {
|
|
|
|
query,
|
|
|
|
installStep: this.getInstallStep(),
|
|
|
|
markConfigured: this.markConfigured,
|
|
|
|
hasCbdIndustry: currentMethod.hasCbdIndustry,
|
|
|
|
} ) }
|
|
|
|
</CardBody>
|
2020-03-15 21:45:19 +00:00
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
return (
|
2020-03-15 21:45:19 +00:00
|
|
|
<div className="woocommerce-task-payments">
|
|
|
|
{ methods.map( ( method ) => {
|
|
|
|
const {
|
|
|
|
before,
|
|
|
|
container,
|
|
|
|
content,
|
2020-03-16 11:09:29 +00:00
|
|
|
isConfigured,
|
2020-03-15 21:45:19 +00:00
|
|
|
key,
|
|
|
|
title,
|
|
|
|
visible,
|
|
|
|
} = method;
|
|
|
|
|
|
|
|
if ( ! visible ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:24:32 +00:00
|
|
|
const classes = classnames(
|
|
|
|
'woocommerce-task-payment',
|
2020-12-21 19:05:29 +00:00
|
|
|
'woocommerce-task-card',
|
2020-03-27 23:24:32 +00:00
|
|
|
! isConfigured &&
|
|
|
|
'woocommerce-task-payment-not-configured',
|
|
|
|
'woocommerce-task-payment-' + key
|
|
|
|
);
|
|
|
|
|
2020-03-31 12:58:11 +00:00
|
|
|
const isRecommended =
|
2020-06-04 09:36:57 +00:00
|
|
|
key === recommendedMethod && ! isConfigured;
|
2020-03-31 12:58:11 +00:00
|
|
|
const showRecommendedRibbon =
|
2020-06-04 09:36:57 +00:00
|
|
|
isRecommended && key !== 'wcpay';
|
2020-03-31 12:58:11 +00:00
|
|
|
const showRecommendedPill =
|
2020-06-04 09:36:57 +00:00
|
|
|
isRecommended && key === 'wcpay';
|
2020-03-31 12:58:11 +00:00
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
return (
|
2020-03-27 23:24:32 +00:00
|
|
|
<Card key={ key } className={ classes }>
|
2020-12-21 19:05:29 +00:00
|
|
|
<CardHeader isBorderless>
|
2020-03-31 12:58:11 +00:00
|
|
|
{ showRecommendedRibbon && (
|
|
|
|
<div className="woocommerce-task-payment__recommended-ribbon">
|
|
|
|
<span>
|
|
|
|
{ __(
|
|
|
|
'Recommended',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
) }
|
2020-03-15 21:45:19 +00:00
|
|
|
{ before }
|
2020-12-21 19:05:29 +00:00
|
|
|
</CardHeader>
|
|
|
|
<CardBody>
|
2020-03-15 21:45:19 +00:00
|
|
|
<H className="woocommerce-task-payment__title">
|
|
|
|
{ title }
|
2020-03-31 12:58:11 +00:00
|
|
|
{ showRecommendedPill && (
|
|
|
|
<span className="woocommerce-task-payment__recommended-pill">
|
|
|
|
{ __(
|
|
|
|
'Recommended',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
) }
|
2020-03-15 21:45:19 +00:00
|
|
|
</H>
|
2020-04-08 23:17:32 +00:00
|
|
|
<div className="woocommerce-task-payment__content">
|
2020-03-15 21:45:19 +00:00
|
|
|
{ content }
|
2020-04-08 23:17:32 +00:00
|
|
|
</div>
|
2020-12-21 19:05:29 +00:00
|
|
|
</CardBody>
|
|
|
|
<CardFooter isBorderless>
|
2020-03-16 11:09:29 +00:00
|
|
|
{ container && ! isConfigured ? (
|
2020-03-15 21:45:19 +00:00
|
|
|
<Button
|
2020-06-04 09:36:57 +00:00
|
|
|
isPrimary={ key === recommendedMethod }
|
2020-06-11 19:22:20 +00:00
|
|
|
isSecondary={
|
|
|
|
key !== recommendedMethod
|
|
|
|
}
|
2020-06-12 09:38:02 +00:00
|
|
|
isBusy={ busyMethod === key }
|
|
|
|
disabled={ busyMethod }
|
|
|
|
onClick={ () =>
|
|
|
|
this.handleClick( method )
|
|
|
|
}
|
2020-03-15 21:45:19 +00:00
|
|
|
>
|
|
|
|
{ __( 'Set up', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) : (
|
2020-03-16 11:09:29 +00:00
|
|
|
<FormToggle
|
|
|
|
checked={ enabledMethods[ key ] }
|
|
|
|
onChange={ () =>
|
|
|
|
this.toggleMethod( key )
|
|
|
|
}
|
|
|
|
onClick={ ( e ) => e.stopPropagation() }
|
|
|
|
/>
|
2020-03-15 21:45:19 +00:00
|
|
|
) }
|
2020-12-21 19:05:29 +00:00
|
|
|
</CardFooter>
|
2020-03-15 21:45:19 +00:00
|
|
|
</Card>
|
2019-08-29 16:41:04 +00:00
|
|
|
);
|
2020-03-15 21:45:19 +00:00
|
|
|
} ) }
|
|
|
|
</div>
|
2019-08-29 16:41:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
2020-06-12 09:38:02 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
const { installAndActivatePlugins } = dispatch( PLUGINS_STORE_NAME );
|
|
|
|
const { updateOptions } = dispatch( OPTIONS_STORE_NAME );
|
2020-09-29 15:10:40 +00:00
|
|
|
const {
|
|
|
|
invalidateResolution,
|
|
|
|
invalidateResolutionForStoreSelector,
|
|
|
|
} = dispatch( ONBOARDING_STORE_NAME );
|
|
|
|
invalidateResolution( 'getProfileItems', [] );
|
2020-10-08 21:25:41 +00:00
|
|
|
invalidateResolution( 'getTasksStatus', [] );
|
2020-06-12 09:38:02 +00:00
|
|
|
return {
|
2020-08-24 13:20:57 +00:00
|
|
|
clearTaskStatusCache: () =>
|
|
|
|
invalidateResolutionForStoreSelector( 'getTasksStatus' ),
|
2020-06-12 09:38:02 +00:00
|
|
|
createNotice,
|
|
|
|
installAndActivatePlugins,
|
|
|
|
updateOptions,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
const { createNotice, installAndActivatePlugins } = props;
|
2020-05-28 08:51:40 +00:00
|
|
|
const { getProfileItems } = select( ONBOARDING_STORE_NAME );
|
2020-08-11 12:20:48 +00:00
|
|
|
const { getOption } = select( OPTIONS_STORE_NAME );
|
2020-04-16 23:58:36 +00:00
|
|
|
const { getActivePlugins, isJetpackConnected } = select(
|
|
|
|
PLUGINS_STORE_NAME
|
|
|
|
);
|
2020-06-22 22:18:35 +00:00
|
|
|
const { getSettings } = select( SETTINGS_STORE_NAME );
|
|
|
|
const { general: generalSettings = {} } = getSettings( 'general' );
|
2020-08-24 13:20:57 +00:00
|
|
|
const { getTasksStatus } = select( ONBOARDING_STORE_NAME );
|
2020-06-10 23:49:27 +00:00
|
|
|
|
2020-03-16 11:09:29 +00:00
|
|
|
const activePlugins = getActivePlugins();
|
2020-08-24 13:20:57 +00:00
|
|
|
const onboardingStatus = getTasksStatus();
|
2020-03-16 11:09:29 +00:00
|
|
|
const profileItems = getProfileItems();
|
2020-06-10 23:49:27 +00:00
|
|
|
|
|
|
|
const optionNames = [
|
2020-03-27 23:24:32 +00:00
|
|
|
'woocommerce_woocommerce_payments_settings',
|
2020-03-16 11:09:29 +00:00
|
|
|
'woocommerce_stripe_settings',
|
|
|
|
'woocommerce_ppec_paypal_settings',
|
|
|
|
'woocommerce_payfast_settings',
|
|
|
|
'woocommerce_square_credit_card_settings',
|
|
|
|
'woocommerce_klarna_payments_settings',
|
|
|
|
'woocommerce_kco_settings',
|
|
|
|
'wc_square_refresh_tokens',
|
2020-03-16 11:57:23 +00:00
|
|
|
'woocommerce_cod_settings',
|
|
|
|
'woocommerce_bacs_settings',
|
|
|
|
'woocommerce_bacs_accounts',
|
2020-08-07 00:09:10 +00:00
|
|
|
'woocommerce_eway_settings',
|
2020-12-01 22:10:57 +00:00
|
|
|
'woocommerce_razorpay_settings',
|
2020-06-10 23:49:27 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const options = optionNames.reduce( ( result, name ) => {
|
|
|
|
result[ name ] = getOption( name );
|
|
|
|
return result;
|
|
|
|
}, {} );
|
2020-02-14 02:23:21 +00:00
|
|
|
const countryCode = getCountryCode(
|
2020-06-22 22:18:35 +00:00
|
|
|
generalSettings.woocommerce_default_country
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2019-10-03 16:03:29 +00:00
|
|
|
|
2020-03-16 11:09:29 +00:00
|
|
|
const methods = getPaymentMethods( {
|
|
|
|
activePlugins,
|
|
|
|
countryCode,
|
2020-06-12 09:38:02 +00:00
|
|
|
createNotice,
|
|
|
|
installAndActivatePlugins,
|
2020-04-09 23:10:47 +00:00
|
|
|
isJetpackConnected: isJetpackConnected(),
|
2020-08-24 13:20:57 +00:00
|
|
|
onboardingStatus,
|
2020-02-14 02:23:21 +00:00
|
|
|
options,
|
2020-03-16 11:09:29 +00:00
|
|
|
profileItems,
|
|
|
|
} );
|
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
return {
|
|
|
|
countryCode,
|
2020-03-16 11:09:29 +00:00
|
|
|
profileItems,
|
|
|
|
activePlugins,
|
2019-10-03 16:03:29 +00:00
|
|
|
options,
|
2020-03-16 11:09:29 +00:00
|
|
|
methods,
|
2019-10-03 16:03:29 +00:00
|
|
|
};
|
2019-08-29 16:41:04 +00:00
|
|
|
} )
|
|
|
|
)( Payments );
|