2020-03-26 11:11:46 +00:00
/ * *
* External dependencies
* /
2021-12-10 15:26:16 +00:00
import { useMemo , cloneElement } from '@wordpress/element' ;
2020-03-26 11:11:46 +00:00
import { _ _ , sprintf } from '@wordpress/i18n' ;
2021-02-02 04:51:47 +00:00
import { usePaymentMethodDataContext } from '@woocommerce/base-context' ;
2020-03-26 11:11:46 +00:00
import RadioControl from '@woocommerce/base-components/radio-control' ;
2021-03-15 08:50:49 +00:00
import {
usePaymentMethodInterface ,
usePaymentMethods ,
2021-12-10 15:26:16 +00:00
useStoreEvents ,
useEmitResponse ,
2021-04-08 12:31:12 +00:00
} from '@woocommerce/base-context/hooks' ;
2022-04-08 12:11:50 +00:00
import { useDispatch } from '@wordpress/data' ;
2020-03-26 11:11:46 +00:00
2020-05-11 13:29:57 +00:00
/ * *
* @ typedef { import ( '@woocommerce/type-defs/contexts' ) . CustomerPaymentMethod } CustomerPaymentMethod
* @ typedef { import ( '@woocommerce/type-defs/contexts' ) . PaymentStatusDispatch } PaymentStatusDispatch
* /
2020-03-26 11:11:46 +00:00
/ * *
* Returns the option object for a cc or echeck saved payment method token .
*
* @ param { CustomerPaymentMethod } savedPaymentMethod
2021-12-10 15:26:16 +00:00
* @ return { string } label
2020-03-26 11:11:46 +00:00
* /
2021-12-10 15:26:16 +00:00
const getCcOrEcheckLabel = ( { method , expires } ) => {
return sprintf (
/* translators: %1$s is referring to the payment method brand, %2$s is referring to the last 4 digits of the payment card, %3$s is referring to the expiry date. */
_ _ (
'%1$s ending in %2$s (expires %3$s)' ,
'woo-gutenberg-products-block'
2020-03-26 11:11:46 +00:00
) ,
2021-12-10 15:26:16 +00:00
method . brand ,
method . last4 ,
expires
) ;
2020-03-26 11:11:46 +00:00
} ;
/ * *
* Returns the option object for any non specific saved payment method .
*
* @ param { CustomerPaymentMethod } savedPaymentMethod
2021-12-10 15:26:16 +00:00
* @ return { string } label
2020-03-26 11:11:46 +00:00
* /
2021-12-10 15:26:16 +00:00
const getDefaultLabel = ( { method } ) => {
return sprintf (
/* translators: %s is the name of the payment method gateway. */
_ _ ( 'Saved token for %s' , 'woo-gutenberg-products-block' ) ,
method . gateway
) ;
2020-03-26 11:11:46 +00:00
} ;
2021-02-02 04:51:47 +00:00
const SavedPaymentMethodOptions = ( ) => {
2020-03-26 11:11:46 +00:00
const {
customerPaymentMethods ,
2021-02-02 04:51:47 +00:00
activePaymentMethod ,
2020-05-11 13:29:57 +00:00
setActivePaymentMethod ,
2021-02-02 04:51:47 +00:00
activeSavedToken ,
2020-03-26 11:11:46 +00:00
} = usePaymentMethodDataContext ( ) ;
2021-03-15 08:50:49 +00:00
const { paymentMethods } = usePaymentMethods ( ) ;
const paymentMethodInterface = usePaymentMethodInterface ( ) ;
2021-12-10 15:26:16 +00:00
const { noticeContexts } = useEmitResponse ( ) ;
2022-04-08 12:11:50 +00:00
const { removeNotice } = useDispatch ( 'core/notices' ) ;
2021-12-10 15:26:16 +00:00
const { dispatchCheckoutEvent } = useStoreEvents ( ) ;
2020-03-26 11:11:46 +00:00
2021-12-10 15:26:16 +00:00
const options = useMemo ( ( ) => {
2020-09-18 10:27:54 +00:00
const types = Object . keys ( customerPaymentMethods ) ;
2021-12-10 15:26:16 +00:00
return types
2020-09-18 10:27:54 +00:00
. flatMap ( ( type ) => {
const typeMethods = customerPaymentMethods [ type ] ;
return typeMethods . map ( ( paymentMethod ) => {
2021-12-10 15:26:16 +00:00
const isCC = type === 'cc' || type === 'echeck' ;
const paymentMethodSlug = paymentMethod . method . gateway ;
return {
name : ` wc-saved-payment-method-token- ${ paymentMethodSlug } ` ,
label : isCC
? getCcOrEcheckLabel ( paymentMethod )
: getDefaultLabel ( paymentMethod ) ,
value : paymentMethod . tokenId . toString ( ) ,
onChange : ( token ) => {
const savedTokenKey = ` wc- ${ paymentMethodSlug } -payment-token ` ;
setActivePaymentMethod ( paymentMethodSlug , {
token ,
payment _method : paymentMethodSlug ,
[ savedTokenKey ] : token . toString ( ) ,
isSavedToken : true ,
} ) ;
removeNotice (
'wc-payment-error' ,
noticeContexts . PAYMENTS
) ;
dispatchCheckoutEvent (
'set-active-payment-method' ,
{
paymentMethodSlug ,
}
) ;
} ,
} ;
2020-08-11 13:43:03 +00:00
} ) ;
2020-09-18 10:27:54 +00:00
} )
. filter ( Boolean ) ;
2020-05-11 13:29:57 +00:00
} , [
customerPaymentMethods ,
setActivePaymentMethod ,
2021-12-10 15:26:16 +00:00
removeNotice ,
noticeContexts . PAYMENTS ,
dispatchCheckoutEvent ,
2020-05-11 13:29:57 +00:00
] ) ;
2020-09-18 10:27:54 +00:00
2021-03-15 08:50:49 +00:00
const savedPaymentMethodHandler =
! ! activeSavedToken &&
paymentMethods [ activePaymentMethod ] &&
paymentMethods [ activePaymentMethod ] ? . savedTokenComponent
? cloneElement (
paymentMethods [ activePaymentMethod ] ? . savedTokenComponent ,
{ token : activeSavedToken , ... paymentMethodInterface }
)
: null ;
2021-12-10 15:26:16 +00:00
return options . length > 0 ? (
2021-03-15 08:50:49 +00:00
< >
< RadioControl
id = { 'wc-payment-method-saved-tokens' }
selected = { activeSavedToken }
2021-12-10 15:26:16 +00:00
options = { options }
2021-03-15 08:50:49 +00:00
/ >
{ savedPaymentMethodHandler }
< / >
2020-03-26 11:11:46 +00:00
) : null ;
} ;
export default SavedPaymentMethodOptions ;