2020-03-16 11:57:23 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-03-16 11:57:23 +00:00
|
|
|
import { Form, H, TextControl } from '@woocommerce/components';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2020-03-16 11:57:23 +00:00
|
|
|
|
2020-08-26 16:14:31 +00:00
|
|
|
class Bacs extends Component {
|
2020-03-16 11:57:23 +00:00
|
|
|
getInitialConfigValues = () => {
|
|
|
|
return {
|
|
|
|
account_name: '',
|
|
|
|
account_number: '',
|
|
|
|
bank_name: '',
|
|
|
|
sort_code: '',
|
|
|
|
iban: '',
|
|
|
|
bic: '',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
validate = ( values ) => {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if ( ! values.account_number && ! values.iban ) {
|
|
|
|
errors.account_number = errors.iban = __(
|
|
|
|
'Please enter an account number or IBAN',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
2020-06-10 23:49:27 +00:00
|
|
|
updateSettings = async ( values ) => {
|
|
|
|
const { updateOptions, createNotice, markConfigured } = this.props;
|
2020-03-16 11:57:23 +00:00
|
|
|
|
2020-06-10 23:49:27 +00:00
|
|
|
const update = await updateOptions( {
|
2020-03-16 11:57:23 +00:00
|
|
|
woocommerce_bacs_settings: {
|
|
|
|
enabled: 'yes',
|
|
|
|
},
|
|
|
|
woocommerce_bacs_accounts: [ values ],
|
|
|
|
} );
|
2020-06-10 23:49:27 +00:00
|
|
|
|
|
|
|
if ( update.success ) {
|
|
|
|
markConfigured( 'bacs' );
|
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__(
|
|
|
|
'Direct bank transfer details added successfully',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
|
|
|
'There was a problem saving your payment setings',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2020-03-16 11:57:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { isOptionsRequesting } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
initialValues={ this.getInitialConfigValues() }
|
|
|
|
onSubmitCallback={ this.updateSettings }
|
|
|
|
validate={ this.validate }
|
|
|
|
>
|
|
|
|
{ ( { getInputProps, handleSubmit } ) => {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<H>
|
|
|
|
{ __(
|
|
|
|
'Add your bank details',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</H>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'These details are required to receive payments via bank transfer',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
<div className="woocommerce-task-payment-method__fields">
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Account name',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'account_name' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Account number',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'account_number' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Bank name',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'bank_name' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'Sort code',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'sort_code' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __( 'IBAN', 'woocommerce-admin' ) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'iban' ) }
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
label={ __(
|
|
|
|
'BIC / Swift',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
required
|
|
|
|
{ ...getInputProps( 'bic' ) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isOptionsRequesting }
|
|
|
|
onClick={ handleSubmit }
|
|
|
|
>
|
|
|
|
{ __( 'Save', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} }
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select ) => {
|
2020-06-10 23:49:27 +00:00
|
|
|
const { isOptionsUpdating } = select( OPTIONS_STORE_NAME );
|
|
|
|
const isOptionsRequesting = isOptionsUpdating();
|
2020-03-16 11:57:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isOptionsRequesting,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2020-06-10 23:49:27 +00:00
|
|
|
const { updateOptions } = dispatch( OPTIONS_STORE_NAME );
|
2020-03-16 11:57:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
updateOptions,
|
|
|
|
};
|
|
|
|
} )
|
2020-08-26 16:14:31 +00:00
|
|
|
)( Bacs );
|