2021-09-21 19:33:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { WC_ADMIN_NAMESPACE } from '@woocommerce/data';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { createNoticesFromResponse } from '~/lib/notices';
|
|
|
|
|
2022-02-28 19:40:45 +00:00
|
|
|
export function connectWcpay( createNotice, onCatch ) {
|
2021-09-21 19:33:44 +00:00
|
|
|
const errorMessage = __(
|
|
|
|
'There was an error connecting to WooCommerce Payments. Please try again or connect later in store settings.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-09-21 19:33:44 +00:00
|
|
|
);
|
2022-02-28 19:40:45 +00:00
|
|
|
apiFetch( {
|
|
|
|
path: WC_ADMIN_NAMESPACE + '/plugins/connect-wcpay',
|
|
|
|
method: 'POST',
|
|
|
|
} )
|
|
|
|
.then( ( response ) => {
|
|
|
|
window.location = response.connectUrl;
|
2021-09-21 19:33:44 +00:00
|
|
|
} )
|
2022-02-28 19:40:45 +00:00
|
|
|
.catch( () => {
|
|
|
|
createNotice( 'error', errorMessage );
|
|
|
|
if ( typeof onCatch === 'function' ) {
|
|
|
|
onCatch();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2021-09-21 19:33:44 +00:00
|
|
|
|
2022-02-28 19:40:45 +00:00
|
|
|
export function installActivateAndConnectWcpay(
|
|
|
|
reject,
|
|
|
|
createNotice,
|
|
|
|
installAndActivatePlugins
|
|
|
|
) {
|
2021-09-21 19:33:44 +00:00
|
|
|
installAndActivatePlugins( [ 'woocommerce-payments' ] )
|
|
|
|
.then( () => {
|
|
|
|
recordEvent( 'woocommerce_payments_install', {
|
|
|
|
context: 'tasklist',
|
|
|
|
} );
|
|
|
|
|
2022-02-28 19:40:45 +00:00
|
|
|
connectWcpay( createNotice, () => {
|
|
|
|
reject();
|
|
|
|
} );
|
2021-09-21 19:33:44 +00:00
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
createNoticesFromResponse( error );
|
|
|
|
reject();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isWCPaySupported( countryCode ) {
|
|
|
|
const supportedCountries = [
|
|
|
|
'US',
|
|
|
|
'PR',
|
|
|
|
'AU',
|
|
|
|
'CA',
|
|
|
|
'DE',
|
|
|
|
'ES',
|
|
|
|
'FR',
|
|
|
|
'GB',
|
|
|
|
'IE',
|
|
|
|
'IT',
|
|
|
|
'NZ',
|
|
|
|
'AT',
|
|
|
|
'BE',
|
|
|
|
'NL',
|
|
|
|
'PL',
|
|
|
|
'PT',
|
|
|
|
'CH',
|
|
|
|
'HK',
|
|
|
|
'SG',
|
|
|
|
];
|
|
|
|
|
|
|
|
return supportedCountries.includes( countryCode );
|
|
|
|
}
|