2020-02-24 03:54:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import interpolateComponents from 'interpolate-components';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-03-15 21:45:19 +00:00
|
|
|
import { Form, Link, Stepper, TextControl } from '@woocommerce/components';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2020-02-24 03:54:46 +00:00
|
|
|
|
|
|
|
class PayFast extends Component {
|
|
|
|
getInitialConfigValues = () => {
|
|
|
|
return {
|
|
|
|
merchant_id: '',
|
|
|
|
merchant_key: '',
|
|
|
|
pass_phrase: '',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
validate = ( values ) => {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if ( ! values.merchant_id ) {
|
|
|
|
errors.merchant_id = __(
|
|
|
|
'Please enter your merchant ID',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! values.merchant_key ) {
|
|
|
|
errors.merchant_key = __(
|
|
|
|
'Please enter your merchant key',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! values.pass_phrase ) {
|
|
|
|
errors.pass_phrase = __(
|
|
|
|
'Please enter your passphrase',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
2020-06-10 23:49:27 +00:00
|
|
|
updateSettings = async ( values ) => {
|
|
|
|
const { updateOptions, createNotice, markConfigured } = this.props;
|
2020-02-24 03:54:46 +00:00
|
|
|
|
|
|
|
// Because the PayFast extension only works with the South African Rand
|
|
|
|
// currency, force the store to use it while setting the PayFast settings
|
2020-06-10 23:49:27 +00:00
|
|
|
const update = await updateOptions( {
|
2020-02-24 03:54:46 +00:00
|
|
|
woocommerce_currency: 'ZAR',
|
|
|
|
woocommerce_payfast_settings: {
|
|
|
|
merchant_id: values.merchant_id,
|
|
|
|
merchant_key: values.merchant_key,
|
|
|
|
pass_phrase: values.pass_phrase,
|
|
|
|
enabled: 'yes',
|
|
|
|
},
|
|
|
|
} );
|
2020-06-10 23:49:27 +00:00
|
|
|
|
|
|
|
if ( update.success ) {
|
|
|
|
markConfigured( 'payfast' );
|
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__( 'PayFast connected successfully', 'woocommerce-admin' )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
2020-11-11 20:09:44 +00:00
|
|
|
'There was a problem saving your payment settings',
|
2020-06-10 23:49:27 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2020-02-24 03:54:46 +00:00
|
|
|
};
|
|
|
|
|
2020-03-15 21:45:19 +00:00
|
|
|
renderConnectStep() {
|
|
|
|
const { isOptionsRequesting } = this.props;
|
2020-02-24 03:54:46 +00:00
|
|
|
const helpText = interpolateComponents( {
|
|
|
|
mixedString: __(
|
|
|
|
'Your API details can be obtained from your {{link}}PayFast account{{/link}}',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
link: (
|
|
|
|
<Link
|
|
|
|
href="https://www.payfast.co.za/"
|
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
initialValues={ this.getInitialConfigValues() }
|
|
|
|
onSubmitCallback={ this.updateSettings }
|
|
|
|
validate={ this.validate }
|
|
|
|
>
|
|
|
|
{ ( { getInputProps, handleSubmit } ) => {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Merchant ID',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'merchant_id' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Merchant Key',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'merchant_key' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Passphrase',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'pass_phrase' ) }
|
|
|
|
/>
|
|
|
|
<Button
|
2020-03-15 21:45:19 +00:00
|
|
|
isPrimary
|
|
|
|
isBusy={ isOptionsRequesting }
|
|
|
|
onClick={ handleSubmit }
|
2020-02-24 03:54:46 +00:00
|
|
|
>
|
2020-03-15 21:45:19 +00:00
|
|
|
{ __( 'Proceed', 'woocommerce-admin' ) }
|
2020-02-24 03:54:46 +00:00
|
|
|
</Button>
|
2020-03-15 21:45:19 +00:00
|
|
|
|
2020-02-24 03:54:46 +00:00
|
|
|
<p>{ helpText }</p>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} }
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
2020-03-15 21:45:19 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { installStep, isOptionsRequesting } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Stepper
|
|
|
|
isVertical
|
|
|
|
isPending={ ! installStep.isComplete || isOptionsRequesting }
|
|
|
|
currentStep={ installStep.isComplete ? 'connect' : 'install' }
|
|
|
|
steps={ [
|
|
|
|
installStep,
|
|
|
|
{
|
|
|
|
key: 'connect',
|
|
|
|
label: __(
|
|
|
|
'Connect your PayFast account',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
content: this.renderConnectStep(),
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2020-02-24 03:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
2020-03-15 21:45:19 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-06-10 23:49:27 +00:00
|
|
|
const { isOptionsUpdating } = select( OPTIONS_STORE_NAME );
|
|
|
|
const isOptionsRequesting = isOptionsUpdating();
|
2020-03-15 21:45:19 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isOptionsRequesting,
|
|
|
|
};
|
|
|
|
} ),
|
2020-02-24 03:54:46 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2020-06-10 23:49:27 +00:00
|
|
|
const { updateOptions } = dispatch( OPTIONS_STORE_NAME );
|
2020-02-24 03:54:46 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
updateOptions,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( PayFast );
|