2019-10-03 16:03:29 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2020-07-15 12:10:21 +00:00
|
|
|
import { Button, CheckboxControl } from '@wordpress/components';
|
2020-03-15 21:45:19 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-10-03 16:03:29 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import interpolateComponents from 'interpolate-components';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-07-15 12:10:21 +00:00
|
|
|
import { isEmail } from '@wordpress/url';
|
2020-03-15 21:45:19 +00:00
|
|
|
import { Form, Link, Stepper, TextControl } from '@woocommerce/components';
|
2019-12-02 17:39:22 +00:00
|
|
|
import { getQuery } from '@woocommerce/navigation';
|
2020-09-03 21:45:40 +00:00
|
|
|
import {
|
|
|
|
PLUGINS_STORE_NAME,
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
WC_ADMIN_NAMESPACE,
|
|
|
|
} from '@woocommerce/data';
|
2020-08-13 02:05:22 +00:00
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
export class PayPal extends Component {
|
2019-10-03 16:03:29 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
|
|
|
|
this.state = {
|
2020-03-15 21:45:19 +00:00
|
|
|
autoConnectFailed: false,
|
2019-10-03 16:03:29 +00:00
|
|
|
connectURL: '',
|
2020-03-15 21:45:19 +00:00
|
|
|
isPending: false,
|
2019-10-03 16:03:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.updateSettings = this.updateSettings.bind( this );
|
2020-07-15 12:10:21 +00:00
|
|
|
this.validate = this.validate.bind( this );
|
2019-10-03 16:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-03-15 21:45:19 +00:00
|
|
|
const { createNotice, markConfigured } = this.props;
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
|
|
const query = getQuery();
|
|
|
|
// Handle redirect back from PayPal
|
|
|
|
if ( query[ 'paypal-connect' ] ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( query[ 'paypal-connect' ] === '1' ) {
|
2020-03-15 21:45:19 +00:00
|
|
|
createNotice(
|
2019-10-03 16:03:29 +00:00
|
|
|
'success',
|
|
|
|
__( 'PayPal connected successfully.', 'woocommerce-admin' )
|
|
|
|
);
|
2020-03-15 21:45:19 +00:00
|
|
|
markConfigured( 'paypal' );
|
2019-10-03 16:03:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable react/no-did-mount-set-state */
|
|
|
|
this.setState( {
|
2020-03-15 21:45:19 +00:00
|
|
|
autoConnectFailed: true,
|
2019-10-03 16:03:29 +00:00
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-mount-set-state */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-16 11:09:29 +00:00
|
|
|
this.fetchOAuthConnectURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps ) {
|
|
|
|
const { activePlugins } = this.props;
|
|
|
|
|
|
|
|
if (
|
|
|
|
! prevProps.activePlugins.includes(
|
|
|
|
'woocommerce-gateway-paypal-express-checkout'
|
|
|
|
) &&
|
|
|
|
activePlugins.includes(
|
|
|
|
'woocommerce-gateway-paypal-express-checkout'
|
|
|
|
)
|
|
|
|
) {
|
2019-10-03 16:03:29 +00:00
|
|
|
this.fetchOAuthConnectURL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
isWooCommerceServicesConnected() {
|
2020-07-28 02:32:58 +00:00
|
|
|
const {
|
|
|
|
activePlugins,
|
|
|
|
isJetpackConnected,
|
|
|
|
wcsTosAccepted,
|
|
|
|
} = this.props;
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
isJetpackConnected &&
|
|
|
|
wcsTosAccepted &&
|
|
|
|
activePlugins.includes( 'woocommerce-services' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:03:29 +00:00
|
|
|
async fetchOAuthConnectURL() {
|
2020-03-16 11:09:29 +00:00
|
|
|
const { activePlugins } = this.props;
|
|
|
|
|
|
|
|
if (
|
|
|
|
! activePlugins.includes(
|
|
|
|
'woocommerce-gateway-paypal-express-checkout'
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
this.setState( { isPending: true } );
|
2019-10-03 16:03:29 +00:00
|
|
|
try {
|
|
|
|
const result = await apiFetch( {
|
2020-04-20 02:04:13 +00:00
|
|
|
path: WC_ADMIN_NAMESPACE + '/plugins/connect-paypal',
|
2019-10-03 16:03:29 +00:00
|
|
|
method: 'POST',
|
|
|
|
} );
|
|
|
|
if ( ! result || ! result.connectUrl ) {
|
|
|
|
this.setState( {
|
2020-03-15 21:45:19 +00:00
|
|
|
autoConnectFailed: true,
|
2020-07-15 12:10:21 +00:00
|
|
|
isPending: false,
|
2019-10-03 16:03:29 +00:00
|
|
|
} );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState( {
|
|
|
|
connectURL: result.connectUrl,
|
2020-03-15 21:45:19 +00:00
|
|
|
isPending: false,
|
2019-10-03 16:03:29 +00:00
|
|
|
} );
|
|
|
|
} catch ( error ) {
|
|
|
|
this.setState( {
|
2020-03-15 21:45:19 +00:00
|
|
|
autoConnectFailed: true,
|
|
|
|
isPending: false,
|
2019-10-03 16:03:29 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateSettings( values ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
createNotice,
|
2020-03-15 21:45:19 +00:00
|
|
|
options,
|
2020-02-14 02:23:21 +00:00
|
|
|
updateOptions,
|
|
|
|
markConfigured,
|
|
|
|
} = this.props;
|
2019-10-03 16:03:29 +00:00
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
const optionValues = {
|
|
|
|
...options.woocommerce_ppec_paypal_settings,
|
|
|
|
enabled: 'yes',
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( values.create_account ) {
|
|
|
|
// Tell WCS to proxy payment requests.
|
|
|
|
// See: https://github.com/Automattic/woocommerce-services/blob/29dfe0ba6fd3075afe08f917a6ff33c321502d9c/classes/class-wc-connect-paypal-ec.php#L53.
|
|
|
|
optionValues.reroute_requests = 'yes';
|
|
|
|
optionValues.email = values.account_email;
|
|
|
|
} else {
|
|
|
|
optionValues.api_username = values.api_username;
|
|
|
|
optionValues.api_password = values.api_password;
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:49:27 +00:00
|
|
|
const update = await updateOptions( {
|
2020-07-15 12:10:21 +00:00
|
|
|
woocommerce_ppec_paypal_settings: optionValues,
|
2019-10-03 16:03:29 +00:00
|
|
|
} );
|
|
|
|
|
2020-06-10 23:49:27 +00:00
|
|
|
if ( update.success ) {
|
2020-03-15 21:45:19 +00:00
|
|
|
createNotice(
|
2019-10-03 16:03:29 +00:00
|
|
|
'success',
|
|
|
|
__( 'PayPal connected successfully.', 'woocommerce-admin' )
|
|
|
|
);
|
2020-03-15 21:45:19 +00:00
|
|
|
markConfigured( 'paypal' );
|
2019-10-03 16:03:29 +00:00
|
|
|
} else {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'There was a problem saving your payment settings.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2019-10-03 16:03:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getInitialConfigValues() {
|
|
|
|
return {
|
|
|
|
api_username: '',
|
|
|
|
api_password: '',
|
2020-07-15 12:10:21 +00:00
|
|
|
create_account: this.isWooCommerceServicesConnected(),
|
|
|
|
account_email: '',
|
2019-10-03 16:03:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
validate( values ) {
|
|
|
|
const errors = {};
|
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
if ( ! values.create_account && ! values.api_username ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
errors.api_username = __(
|
|
|
|
'Please enter your API username',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
2019-10-03 16:03:29 +00:00
|
|
|
}
|
2020-07-15 12:10:21 +00:00
|
|
|
if ( ! values.create_account && ! values.api_password ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
errors.api_password = __(
|
|
|
|
'Please enter your API password',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
2019-10-03 16:03:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
if (
|
|
|
|
this.isWooCommerceServicesConnected() &&
|
|
|
|
values.create_account &&
|
|
|
|
! isEmail( values.account_email )
|
|
|
|
) {
|
|
|
|
errors.account_email = __(
|
|
|
|
'Please enter a valid email address',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:03:29 +00:00
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
renderAutomaticConfig() {
|
2020-06-10 23:49:27 +00:00
|
|
|
const { isOptionsUpdating } = this.props;
|
2020-07-15 12:10:21 +00:00
|
|
|
const { autoConnectFailed, connectURL, isPending } = this.state;
|
|
|
|
|
|
|
|
const canAutoCreate = this.isWooCommerceServicesConnected();
|
|
|
|
const initialValues = this.getInitialConfigValues();
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
2020-07-15 12:10:21 +00:00
|
|
|
initialValues={ initialValues }
|
2019-10-03 16:03:29 +00:00
|
|
|
onSubmitCallback={ this.updateSettings }
|
|
|
|
validate={ this.validate }
|
|
|
|
>
|
2020-07-15 12:10:21 +00:00
|
|
|
{ ( { getInputProps, handleSubmit, values } ) => {
|
2019-10-03 16:03:29 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2020-07-15 12:10:21 +00:00
|
|
|
{ canAutoCreate && (
|
|
|
|
<div className="woocommerce-task-payments__paypal-auto-create-account">
|
|
|
|
<CheckboxControl
|
|
|
|
label={ __(
|
|
|
|
'Create a PayPal account for me',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
{ ...getInputProps( 'create_account' ) }
|
|
|
|
/>
|
|
|
|
{ values.create_account && (
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Email address',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
type="email"
|
2020-07-28 02:32:58 +00:00
|
|
|
{ ...getInputProps(
|
|
|
|
'account_email'
|
|
|
|
) }
|
2020-07-15 12:10:21 +00:00
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
|
|
|
|
{ ! isPending &&
|
|
|
|
( autoConnectFailed || ! connectURL ) &&
|
2020-07-28 02:32:58 +00:00
|
|
|
( ! canAutoCreate ||
|
|
|
|
! values.create_account ) && (
|
2020-07-15 12:10:21 +00:00
|
|
|
<Fragment>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'API Username',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps(
|
|
|
|
'api_username'
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'API Password',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps(
|
|
|
|
'api_password'
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
onClick={ handleSubmit }
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isOptionsUpdating }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Proceed',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: __(
|
|
|
|
'Your API details can be obtained from your {{link}}PayPal account{{/link}}',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
link: (
|
|
|
|
<Link
|
|
|
|
href="https://docs.woocommerce.com/document/paypal-express-checkout/#section-8"
|
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</p>
|
|
|
|
</Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
) }
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
{ canAutoCreate && values.create_account && (
|
|
|
|
<Button
|
|
|
|
onClick={ handleSubmit }
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isOptionsUpdating }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Create account',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
|
|
|
|
{ ! autoConnectFailed &&
|
|
|
|
connectURL &&
|
2020-07-28 02:32:58 +00:00
|
|
|
( ! canAutoCreate ||
|
|
|
|
! values.create_account ) && (
|
2020-07-15 12:10:21 +00:00
|
|
|
<Fragment>
|
|
|
|
<Button isPrimary href={ connectURL }>
|
|
|
|
{ __(
|
|
|
|
'Connect',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'You will be redirected to the Paypal website to create the connection.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
) }
|
2019-10-03 16:03:29 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} }
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
getConnectStep() {
|
2020-07-15 12:10:21 +00:00
|
|
|
return {
|
2020-03-15 21:45:19 +00:00
|
|
|
key: 'connect',
|
|
|
|
label: __( 'Connect your PayPal account', 'woocommerce-admin' ),
|
|
|
|
description: __(
|
2020-07-15 12:10:21 +00:00
|
|
|
'A Paypal account is required to process payments. Connect your store to your PayPal account.',
|
2020-03-15 21:45:19 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2020-07-15 12:10:21 +00:00
|
|
|
content: this.renderAutomaticConfig(),
|
2020-03-15 21:45:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { installStep } = this.props;
|
|
|
|
const { isPending } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Stepper
|
|
|
|
isVertical
|
|
|
|
isPending={ ! installStep.isComplete || isPending }
|
|
|
|
currentStep={ installStep.isComplete ? 'connect' : 'install' }
|
|
|
|
steps={ [ installStep, this.getConnectStep() ] }
|
|
|
|
/>
|
|
|
|
);
|
2019-10-03 16:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PayPal.defaultProps = {
|
|
|
|
manualConfig: false, // WCS is not required for the PayPal OAuth flow, so we can default to smooth connection.
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-06-10 23:49:27 +00:00
|
|
|
const { getOption, isOptionsUpdating } = select( OPTIONS_STORE_NAME );
|
2020-07-15 12:10:21 +00:00
|
|
|
const { getActivePlugins, isJetpackConnected } = select(
|
|
|
|
PLUGINS_STORE_NAME
|
|
|
|
);
|
|
|
|
const paypalOptions = getOption( 'woocommerce_ppec_paypal_settings' );
|
|
|
|
const wcsOptions = getOption( 'wc_connect_options' );
|
2020-03-16 11:09:29 +00:00
|
|
|
const activePlugins = getActivePlugins();
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
|
|
return {
|
2020-03-16 11:09:29 +00:00
|
|
|
activePlugins,
|
2020-07-15 12:10:21 +00:00
|
|
|
isJetpackConnected: isJetpackConnected(),
|
2020-06-10 23:49:27 +00:00
|
|
|
isOptionsUpdating: isOptionsUpdating(),
|
2020-07-15 12:10:21 +00:00
|
|
|
options: paypalOptions,
|
|
|
|
wcsTosAccepted: wcsOptions && wcsOptions.tos_accepted,
|
2019-10-03 16:03:29 +00:00
|
|
|
};
|
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-10-03 16:03:29 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2020-06-10 23:49:27 +00:00
|
|
|
const { updateOptions } = dispatch( OPTIONS_STORE_NAME );
|
2019-10-03 16:03:29 +00:00
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
updateOptions,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( PayPal );
|