2021-04-20 17:17:19 +00:00
/ * *
* External dependencies
* /
import { __ } from '@wordpress/i18n' ;
import { useSelect , useDispatch } from '@wordpress/data' ;
import { decodeEntities } from '@wordpress/html-entities' ;
2021-07-26 18:34:08 +00:00
import { Card , CardHeader , CardFooter , Button } from '@wordpress/components' ;
2021-04-20 17:17:19 +00:00
import { useEffect , useRef , useState } from '@wordpress/element' ;
import { EllipsisMenu , List , Pill } from '@woocommerce/components' ;
import { Text } from '@woocommerce/experimental' ;
import {
2022-03-07 13:15:06 +00:00
ONBOARDING_STORE_NAME ,
PAYMENT_GATEWAYS_STORE_NAME ,
2021-04-20 17:17:19 +00:00
PLUGINS_STORE_NAME ,
Plugin ,
} from '@woocommerce/data' ;
import { recordEvent } from '@woocommerce/tracks' ;
2021-05-20 11:31:14 +00:00
import ExternalIcon from 'gridicons/dist/external' ;
2021-04-20 17:17:19 +00:00
/ * *
* Internal dependencies
* /
import './payment-recommendations.scss' ;
import { createNoticesFromResponse } from '../lib/notices' ;
2022-03-07 13:15:06 +00:00
import { getPluginSlug } from '~/utils' ;
2022-04-26 03:14:05 +00:00
import { isWcPaySupported } from './utils' ;
2021-04-20 17:17:19 +00:00
const SEE_MORE_LINK =
'https://woocommerce.com/product-category/woocommerce-extensions/payment-gateways/?utm_source=payments_recommendations' ;
2021-09-08 16:41:00 +00:00
const WcPayPromotionGateway = document . querySelector (
'[data-gateway_id="pre_install_woocommerce_payments_promotion"]'
) ;
2021-04-20 17:17:19 +00:00
const PaymentRecommendations : React.FC = ( ) = > {
const [ installingPlugin , setInstallingPlugin ] = useState < string | null > (
null
) ;
2022-03-07 13:15:06 +00:00
const [ isDismissed , setIsDismissed ] = useState < boolean > ( false ) ;
const [ isInstalled , setIsInstalled ] = useState < boolean > ( false ) ;
2022-06-21 08:37:34 +00:00
const { installAndActivatePlugins , dismissRecommendedPlugins } =
useDispatch ( PLUGINS_STORE_NAME ) ;
2021-11-16 13:57:23 +00:00
const { createNotice } = useDispatch ( 'core/notices' ) ;
2022-03-07 13:15:06 +00:00
const {
installedPaymentGateway ,
installedPaymentGateways ,
paymentGatewaySuggestions ,
isResolving ,
} = useSelect (
2022-04-25 05:49:11 +00:00
( select ) = > {
2022-03-07 13:15:06 +00:00
const installingGatewayId =
isInstalled && getPluginSlug ( installingPlugin ) ;
return {
installedPaymentGateway :
installingGatewayId &&
select ( PAYMENT_GATEWAYS_STORE_NAME ) . getPaymentGateway (
installingGatewayId
) ,
installedPaymentGateways : select ( PAYMENT_GATEWAYS_STORE_NAME )
. getPaymentGateways ( )
2022-04-19 08:31:15 +00:00
. reduce (
( gateways : { [ id : string ] : boolean } , gateway ) = > {
if ( installingGatewayId === gateway . id ) {
return gateways ;
}
gateways [ gateway . id ] = true ;
2022-03-07 13:15:06 +00:00
return gateways ;
2022-04-19 08:31:15 +00:00
} ,
{ }
) ,
2022-03-07 13:15:06 +00:00
isResolving : select ( ONBOARDING_STORE_NAME ) . isResolving (
'getPaymentGatewaySuggestions'
) ,
paymentGatewaySuggestions : select (
ONBOARDING_STORE_NAME
) . getPaymentGatewaySuggestions ( ) ,
} ;
} ,
[ isInstalled ]
2021-04-20 17:17:19 +00:00
) ;
2022-03-30 22:46:43 +00:00
2021-04-20 17:17:19 +00:00
const triggeredPageViewRef = useRef ( false ) ;
const shouldShowRecommendations =
2022-03-07 13:15:06 +00:00
paymentGatewaySuggestions &&
paymentGatewaySuggestions . length > 0 &&
2022-04-26 03:14:05 +00:00
! isWcPaySupported ( paymentGatewaySuggestions ) &&
2022-03-07 13:15:06 +00:00
! isDismissed ;
2021-04-20 17:17:19 +00:00
useEffect ( ( ) = > {
2021-09-08 16:41:00 +00:00
if (
( shouldShowRecommendations ||
2022-03-07 13:15:06 +00:00
( WcPayPromotionGateway && ! isResolving ) ) &&
2021-09-08 16:41:00 +00:00
! triggeredPageViewRef . current
) {
2021-04-20 17:17:19 +00:00
triggeredPageViewRef . current = true ;
2022-03-07 13:15:06 +00:00
const eventProps = ( paymentGatewaySuggestions || [ ] ) . reduce (
2022-04-19 08:31:15 +00:00
( props : { [ key : string ] : boolean } , plugin : Plugin ) = > {
2021-11-16 13:57:23 +00:00
if ( plugin . plugins && plugin . plugins . length > 0 ) {
2021-09-08 16:41:00 +00:00
return {
. . . props ,
2021-11-16 13:57:23 +00:00
[ plugin . plugins [ 0 ] . replace ( /\-/g , '_' ) +
2021-09-08 16:41:00 +00:00
'_displayed' ] : true ,
} ;
}
return props ;
} ,
{
woocommerce_payments_displayed : ! ! WcPayPromotionGateway ,
}
) ;
recordEvent (
'settings_payments_recommendations_pageview' ,
eventProps
) ;
2021-04-20 17:17:19 +00:00
}
2022-03-07 13:15:06 +00:00
} , [ shouldShowRecommendations , WcPayPromotionGateway , isResolving ] ) ;
useEffect ( ( ) = > {
if ( ! installedPaymentGateway ) {
return ;
}
window . location . href = installedPaymentGateway . settings_url ;
} , [ installedPaymentGateway ] ) ;
2021-04-20 17:17:19 +00:00
if ( ! shouldShowRecommendations ) {
return null ;
}
2021-11-16 13:57:23 +00:00
const dismissPaymentRecommendations = async ( ) = > {
2022-03-07 13:15:06 +00:00
setIsDismissed ( true ) ;
2021-04-20 17:17:19 +00:00
recordEvent ( 'settings_payments_recommendations_dismiss' , { } ) ;
2021-11-16 13:57:23 +00:00
const success = await dismissRecommendedPlugins ( 'payments' ) ;
2022-03-07 13:15:06 +00:00
if ( ! success ) {
setIsDismissed ( false ) ;
2021-11-16 13:57:23 +00:00
createNotice (
'error' ,
__ (
2022-03-07 13:15:06 +00:00
'There was a problem hiding the "Additional ways to get paid" card.' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-11-16 13:57:23 +00:00
)
) ;
}
2021-04-20 17:17:19 +00:00
} ;
const setupPlugin = ( plugin : Plugin ) = > {
if ( installingPlugin ) {
return ;
}
2021-11-16 13:57:23 +00:00
setInstallingPlugin ( plugin . id ) ;
2021-04-20 17:17:19 +00:00
recordEvent ( 'settings_payments_recommendations_setup' , {
2021-11-16 13:57:23 +00:00
extension_selected : plugin.plugins [ 0 ] ,
2021-04-20 17:17:19 +00:00
} ) ;
2021-11-16 13:57:23 +00:00
installAndActivatePlugins ( [ plugin . plugins [ 0 ] ] )
2021-04-20 17:17:19 +00:00
. then ( ( ) = > {
2022-03-07 13:15:06 +00:00
setIsInstalled ( true ) ;
2021-04-20 17:17:19 +00:00
} )
. catch ( ( response : { errors : Record < string , string > } ) = > {
createNoticesFromResponse ( response ) ;
setInstallingPlugin ( null ) ;
} ) ;
} ;
2022-03-07 13:15:06 +00:00
const pluginsList = ( paymentGatewaySuggestions || [ ] )
. filter ( ( plugin : Plugin ) = > {
return (
! installedPaymentGateways [ plugin . id ] &&
plugin . plugins ? . length &&
( ! window . wcAdminFeatures [ 'wc-pay-promotion' ] ||
! plugin . id . startsWith ( 'woocommerce_payments' ) )
) ;
} )
. map ( ( plugin : Plugin ) = > {
2021-04-20 17:17:19 +00:00
return {
2021-11-16 13:57:23 +00:00
key : plugin.id ,
2021-04-20 17:17:19 +00:00
title : (
< >
{ plugin . title }
{ plugin . recommended && (
2022-03-30 09:00:04 +00:00
< Pill > { __ ( 'Recommended' , 'woocommerce' ) } < / Pill >
2021-04-20 17:17:19 +00:00
) }
< / >
) ,
2021-11-16 13:57:23 +00:00
content : decodeEntities ( plugin . content ) ,
2021-04-20 17:17:19 +00:00
after : (
< Button
isSecondary
onClick = { ( ) = > setupPlugin ( plugin ) }
2021-11-16 13:57:23 +00:00
isBusy = { installingPlugin === plugin . id }
2021-04-20 17:17:19 +00:00
disabled = { ! ! installingPlugin }
>
2022-03-07 13:15:06 +00:00
{ plugin . actionText ||
2022-03-30 09:00:04 +00:00
__ ( 'Get started' , 'woocommerce' ) }
2021-04-20 17:17:19 +00:00
< / Button >
) ,
2022-03-07 13:15:06 +00:00
before : (
< img src = { plugin . square_image || plugin . image } alt = "" / >
) ,
2021-04-20 17:17:19 +00:00
} ;
2022-03-07 13:15:06 +00:00
} ) ;
2021-04-20 17:17:19 +00:00
return (
2021-07-26 18:34:08 +00:00
< Card size = "medium" className = "woocommerce-recommended-payments-card" >
< CardHeader >
2021-04-20 17:17:19 +00:00
< div className = "woocommerce-recommended-payments-card__header" >
2021-06-28 01:14:59 +00:00
< Text
variant = "title.small"
as = "p"
size = "20"
lineHeight = "28px"
>
2022-07-04 08:58:10 +00:00
{ __ ( 'Recommended payment providers' , 'woocommerce' ) }
2021-04-20 17:17:19 +00:00
< / Text >
< Text
className = {
'woocommerce-recommended-payments__header-heading'
}
variant = "caption"
2021-06-28 01:14:59 +00:00
as = "p"
size = "12"
lineHeight = "16px"
2021-04-20 17:17:19 +00:00
>
{ __ (
'We recommend adding one of the following payment extensions to your store. The extension will be installed and activated for you when you click "Get started".' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-04-20 17:17:19 +00:00
) }
< / Text >
< / div >
< div className = "woocommerce-card__menu woocommerce-card__header-item" >
< EllipsisMenu
2022-03-30 09:00:04 +00:00
label = { __ ( 'Task List Options' , 'woocommerce' ) }
2021-04-20 17:17:19 +00:00
renderContent = { ( ) = > (
< div className = "woocommerce-review-activity-card__section-controls" >
< Button
onClick = { dismissPaymentRecommendations }
>
2022-03-30 09:00:04 +00:00
{ __ ( 'Hide this' , 'woocommerce' ) }
2021-04-20 17:17:19 +00:00
< / Button >
< / div >
) }
/ >
< / div >
< / CardHeader >
2021-07-26 18:34:08 +00:00
< List items = { pluginsList } / >
2021-04-20 17:17:19 +00:00
< CardFooter >
2021-06-08 02:06:02 +00:00
< Button href = { SEE_MORE_LINK } target = "_blank" isTertiary >
2022-07-04 08:58:10 +00:00
{ __ ( 'Discover other payment providers' , 'woocommerce' ) }
2021-05-20 11:31:14 +00:00
< ExternalIcon size = { 18 } / >
2021-04-20 17:17:19 +00:00
< / Button >
< / CardFooter >
< / Card >
) ;
} ;
export default PaymentRecommendations ;