2019-10-03 16:03:29 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-12-02 17:39:22 +00:00
|
|
|
|
import { Button } 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';
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WooCommerce dependencies
|
|
|
|
|
*/
|
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-03-15 21:45:19 +00:00
|
|
|
|
import { WC_ADMIN_NAMESPACE } from 'wc-api/constants';
|
2020-06-10 23:49:27 +00:00
|
|
|
|
import { PLUGINS_STORE_NAME, OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
|
|
|
|
class PayPal extends Component {
|
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
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
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderConnectButton() {
|
|
|
|
|
const { connectURL } = this.state;
|
|
|
|
|
return (
|
2020-06-11 19:22:20 +00:00
|
|
|
|
<Button isPrimary href={ connectURL }>
|
2019-10-03 16:03:29 +00:00
|
|
|
|
{ __( 'Connect', 'woocommerce-admin' ) }
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-06-10 23:49:27 +00:00
|
|
|
|
const update = await updateOptions( {
|
2019-10-03 16:03:29 +00:00
|
|
|
|
woocommerce_ppec_paypal_settings: {
|
2020-03-15 21:45:19 +00:00
|
|
|
|
...options.woocommerce_ppec_paypal_settings,
|
2019-10-03 16:03:29 +00:00
|
|
|
|
api_username: values.api_username,
|
|
|
|
|
api_password: values.api_password,
|
|
|
|
|
enabled: 'yes',
|
|
|
|
|
},
|
|
|
|
|
} );
|
|
|
|
|
|
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: '',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate( values ) {
|
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
|
|
if ( ! 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
|
|
|
|
}
|
|
|
|
|
if ( ! 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderManualConfig() {
|
2020-06-10 23:49:27 +00:00
|
|
|
|
const { isOptionsUpdating } = this.props;
|
2019-10-03 16:03:29 +00:00
|
|
|
|
const link = (
|
|
|
|
|
<Link
|
|
|
|
|
href="https://docs.woocommerce.com/document/paypal-express-checkout/#section-8"
|
|
|
|
|
target="_blank"
|
|
|
|
|
type="external"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
const help = interpolateComponents( {
|
|
|
|
|
mixedString: __(
|
|
|
|
|
'Your API details can be obtained from your {{link}}PayPal account{{/link}}',
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
|
|
|
|
components: {
|
|
|
|
|
link,
|
|
|
|
|
},
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Form
|
|
|
|
|
initialValues={ this.getInitialConfigValues() }
|
|
|
|
|
onSubmitCallback={ this.updateSettings }
|
|
|
|
|
validate={ this.validate }
|
|
|
|
|
>
|
|
|
|
|
{ ( { getInputProps, handleSubmit } ) => {
|
|
|
|
|
return (
|
|
|
|
|
<Fragment>
|
|
|
|
|
<TextControl
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label={ __(
|
|
|
|
|
'API Username',
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
) }
|
2019-10-03 16:03:29 +00:00
|
|
|
|
required
|
|
|
|
|
{ ...getInputProps( 'api_username' ) }
|
|
|
|
|
/>
|
|
|
|
|
<TextControl
|
2020-02-14 02:23:21 +00:00
|
|
|
|
label={ __(
|
|
|
|
|
'API Password',
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
) }
|
2019-10-03 16:03:29 +00:00
|
|
|
|
required
|
|
|
|
|
{ ...getInputProps( 'api_password' ) }
|
|
|
|
|
/>
|
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
|
<Button
|
|
|
|
|
onClick={ handleSubmit }
|
|
|
|
|
isPrimary
|
2020-06-10 23:49:27 +00:00
|
|
|
|
isBusy={ isOptionsUpdating }
|
2020-02-14 02:23:21 +00:00
|
|
|
|
>
|
2019-10-03 16:03:29 +00:00
|
|
|
|
{ __( 'Proceed', 'woocommerce-admin' ) }
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
<p>{ help }</p>
|
|
|
|
|
</Fragment>
|
|
|
|
|
);
|
|
|
|
|
} }
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
|
getConnectStep() {
|
|
|
|
|
const { autoConnectFailed, connectURL, isPending } = this.state;
|
|
|
|
|
const connectStep = {
|
|
|
|
|
key: 'connect',
|
|
|
|
|
label: __( 'Connect your PayPal account', 'woocommerce-admin' ),
|
|
|
|
|
};
|
2019-10-03 16:03:29 +00:00
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
|
if ( isPending ) {
|
|
|
|
|
return connectStep;
|
2019-10-03 16:03:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
|
if ( ! autoConnectFailed && connectURL ) {
|
|
|
|
|
return {
|
|
|
|
|
...connectStep,
|
|
|
|
|
description: __(
|
|
|
|
|
'A Paypal account is required to process payments. You will be redirected to the Paypal website to create the connection.',
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
|
|
|
|
content: this.renderConnectButton(),
|
|
|
|
|
};
|
2019-10-03 16:03:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
|
return {
|
|
|
|
|
...connectStep,
|
|
|
|
|
description: __(
|
|
|
|
|
'Connect your store to your PayPal account. Don’t have a PayPal account? Create one.',
|
|
|
|
|
'woocommerce-admin'
|
|
|
|
|
),
|
|
|
|
|
content: this.renderManualConfig(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-04-16 23:58:36 +00:00
|
|
|
|
const { getActivePlugins } = select( PLUGINS_STORE_NAME );
|
2020-06-10 23:49:27 +00:00
|
|
|
|
const options = getOption( 'woocommerce_ppec_paypal_settings' );
|
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,
|
2019-10-03 16:03:29 +00:00
|
|
|
|
options,
|
2020-06-10 23:49:27 +00:00
|
|
|
|
isOptionsUpdating: isOptionsUpdating(),
|
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 );
|