2021-04-27 15:14:42 +00:00
/ * *
* External dependencies
* /
import { _ _ } from '@wordpress/i18n' ;
import { useDispatch , useSelect } from '@wordpress/data' ;
import { getHistory , getNewPath } from '@woocommerce/navigation' ;
import {
ONBOARDING _STORE _NAME ,
OPTIONS _STORE _NAME ,
PLUGINS _STORE _NAME ,
SETTINGS _STORE _NAME ,
} from '@woocommerce/data' ;
import { recordEvent } from '@woocommerce/tracks' ;
import { useMemo , useState } from '@wordpress/element' ;
/ * *
* Internal dependencies
* /
import { PaymentMethodList } from './components/PaymentMethodList' ;
2021-06-10 17:10:42 +00:00
import { WCPaySuggestion } from './components/WCPaySuggestion' ;
2021-04-27 15:14:42 +00:00
import { getCountryCode } from '../../../dashboard/utils' ;
import { getPaymentMethods } from './methods' ;
import { PaymentSetup } from './components/PaymentSetup' ;
2021-05-28 23:26:38 +00:00
import { sift } from '~/utils' ;
2021-04-27 15:14:42 +00:00
export const setMethodEnabledOption = async (
optionName ,
value ,
{ clearTaskStatusCache , updateOptions , options }
) => {
const methodOptions = options [ optionName ] ;
// Don't update the option if it already has the same value.
if ( methodOptions . enabled !== value ) {
await updateOptions ( {
[ optionName ] : {
... methodOptions ,
enabled : value ,
} ,
} ) ;
clearTaskStatusCache ( ) ;
}
} ;
export const LocalPayments = ( { query } ) => {
const { createNotice } = useDispatch ( 'core/notices' ) ;
const {
installAndActivatePlugins ,
invalidateResolutionForStoreSelector : invalidatePluginStoreSelector ,
} = useDispatch ( PLUGINS _STORE _NAME ) ;
const { updateOptions } = useDispatch ( OPTIONS _STORE _NAME ) ;
const {
invalidateResolution ,
invalidateResolutionForStoreSelector ,
} = useDispatch ( ONBOARDING _STORE _NAME ) ;
const { methods , options } = useSelect ( ( select ) => {
const { getProfileItems } = select ( ONBOARDING _STORE _NAME ) ;
const { getOption } = select ( OPTIONS _STORE _NAME ) ;
const {
getActivePlugins ,
isJetpackConnected ,
getPaypalOnboardingStatus ,
hasFinishedResolution ,
} = select ( PLUGINS _STORE _NAME ) ;
const { getSettings } = select ( SETTINGS _STORE _NAME ) ;
const { general : generalSettings = { } } = getSettings ( 'general' ) ;
const { getTasksStatus } = select ( ONBOARDING _STORE _NAME ) ;
const activePlugins = getActivePlugins ( ) ;
const onboardingStatus = getTasksStatus ( ) ;
const profileItems = getProfileItems ( ) ;
const optionNames = [
'woocommerce_woocommerce_payments_settings' ,
'woocommerce_stripe_settings' ,
'woocommerce-ppcp-settings' ,
'woocommerce_ppcp-gateway_settings' ,
'woocommerce_payfast_settings' ,
'woocommerce_square_credit_card_settings' ,
'woocommerce_klarna_payments_settings' ,
'woocommerce_kco_settings' ,
'wc_square_refresh_tokens' ,
'woocommerce_cod_settings' ,
'woocommerce_bacs_settings' ,
'woocommerce_bacs_accounts' ,
'woocommerce_eway_settings' ,
'woocommerce_razorpay_settings' ,
'woocommerce_mollie_payments_settings' ,
'woocommerce_payubiz_settings' ,
'woocommerce_paystack_settings' ,
'woocommerce_woo-mercado-pago-basic_settings' ,
] ;
const retrievedOptions = optionNames . reduce ( ( result , name ) => {
result [ name ] = getOption ( name ) ;
return result ;
} , { } ) ;
const countryCode = getCountryCode (
generalSettings . woocommerce _default _country
) ;
const paypalOnboardingStatus = activePlugins . includes (
'woocommerce-paypal-payments'
)
? getPaypalOnboardingStatus ( )
: null ;
return {
methods : getPaymentMethods ( {
activePlugins ,
countryCode ,
createNotice ,
installAndActivatePlugins ,
isJetpackConnected : isJetpackConnected ( ) ,
onboardingStatus ,
options : retrievedOptions ,
profileItems ,
paypalOnboardingStatus ,
loadingPaypalStatus :
! hasFinishedResolution ( 'getPaypalOnboardingStatus' ) &&
! paypalOnboardingStatus ,
} ) ,
options : retrievedOptions ,
} ;
} ) ;
const recommendedMethod = useMemo ( ( ) => {
const method = methods . find (
( m ) =>
( m . key === 'wcpay' && m . visible ) ||
( m . key === 'mercadopago' && m . visible )
) ;
if ( ! method ) {
return 'stripe' ;
}
return method . key ;
} , [ methods ] ) ;
const markConfigured = async ( methodKey , queryParams = { } ) => {
const method = methods . find ( ( option ) => option . key === methodKey ) ;
if ( ! method ) {
throw ` Method ${ methodKey } not found in available methods list ` ;
}
setEnabledMethods ( {
... enabledMethods ,
[ methodKey ] : true ,
} ) ;
const clearTaskStatusCache = ( ) => {
invalidateResolution ( 'getProfileItems' , [ ] ) ;
invalidateResolution ( 'getTasksStatus' , [ ] ) ;
invalidateResolutionForStoreSelector ( 'getTasksStatus' ) ;
invalidatePluginStoreSelector ( 'getPaypalOnboardingStatus' ) ;
} ;
await setMethodEnabledOption ( method . optionName , 'yes' , {
clearTaskStatusCache ,
updateOptions ,
options ,
} ) ;
recordEvent ( 'tasklist_payment_connect_method' , {
payment _method : methodKey ,
} ) ;
2021-04-28 21:24:21 +00:00
getHistory ( ) . push ( getNewPath ( { ... queryParams } , '/' , { } ) ) ;
2021-04-27 15:14:42 +00:00
} ;
const recordConnectStartEvent = ( methodName ) => {
recordEvent ( 'tasklist_payment_connect_start' , {
payment _method : methodName ,
} ) ;
} ;
const currentMethod = useMemo ( ( ) => {
2021-06-08 17:40:57 +00:00
if ( ! query . id ) {
2021-04-27 15:14:42 +00:00
return null ;
}
2021-06-08 17:40:57 +00:00
const method = methods . find ( ( m ) => m . key === query . id ) ;
2021-04-27 15:14:42 +00:00
if ( ! method ) {
2021-06-08 17:40:57 +00:00
throw ` Current method ${ query . id } not found in available methods list ` ;
2021-04-27 15:14:42 +00:00
}
return method ;
} , [ query ] ) ;
const [ enabledMethods , setEnabledMethods ] = useState (
methods . reduce ( ( acc , method ) => {
acc [ method . key ] = method . isEnabled ;
return acc ;
} , { } )
) ;
if ( currentMethod ) {
return (
< PaymentSetup
method = { currentMethod }
markConfigured = { markConfigured }
recordConnectStartEvent = { recordConnectStartEvent }
/ >
) ;
}
const [ enabledCardMethods , additionalCardMethods ] = sift (
methods ,
( method ) => method . isEnabled && method . isConfigured
) ;
const wcPayIndex = additionalCardMethods . findIndex (
( m ) => m . key === 'wcpay'
) ;
const wcPayMethod =
wcPayIndex === - 1
? null
2021-06-10 17:10:42 +00:00
: additionalCardMethods . splice ( wcPayIndex , 1 ) [ 0 ] ;
2021-04-27 15:14:42 +00:00
return (
< div className = "woocommerce-task-payments" >
2021-05-28 23:26:38 +00:00
{ ! ! wcPayMethod && (
2021-06-10 17:10:42 +00:00
< WCPaySuggestion
onSetupCallback = { wcPayMethod . onClick }
paymentGateway = { {
... wcPayMethod ,
description : _ _ (
'Try the new way to get paid. Securely accept credit and debit cards on your site. Manage transactions without leaving your WordPress dashboard. Only with WooCommerce Payments.' ,
'wc-admin'
) ,
} }
/ >
2021-05-28 23:26:38 +00:00
) }
2021-04-27 15:14:42 +00:00
{ ! ! enabledCardMethods . length && (
< PaymentMethodList
recommendedMethod = { recommendedMethod }
heading = { _ _ ( 'Enabled payment methods' , 'wc-admin' ) }
methods = { enabledCardMethods }
/ >
) }
{ ! ! additionalCardMethods . length && (
< PaymentMethodList
recommendedMethod = { recommendedMethod }
heading = { _ _ ( 'Additional payment methods' , 'wc-admin' ) }
methods = { additionalCardMethods }
markConfigured = { markConfigured }
/ >
) }
< / d i v >
) ;
} ;
export default LocalPayments ;