Skip to content

Commit 2786425

Browse files
wjrosadiegocurbelo
andauthored
Add pill to inform about card requirement when OC is enabled (#4541)
* Blocking disabling of cards when OC is enabled * Changelog and readme entries * Add new pill to inform about the card requirement when OC is enabled * Block the checkbox as well * Refactor logic to force enablement during init * Unit tests * Fix tests * Fix payment method pills swapping * Testing custom checked+disabled style * Remove custom checkbox style * Removing unnecessary import * Removing the need for a page refresh * Changelog and readme entries * Update client/settings/general-settings-section/payment-method-checkbox.js Co-authored-by: Diego Curbelo <[email protected]> * Fix lint issue --------- Co-authored-by: Diego Curbelo <[email protected]>
1 parent a3abb57 commit 2786425

File tree

7 files changed

+146
-6
lines changed

7 files changed

+146
-6
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 9.8.0 - xxxx-xx-xx =
4+
* Add - A new pill to the payment methods page to indicate the credit card requirement when the Optimized Checkout feature is enabled
45
* Add - Tracks the toggle of the Optimized Checkout feature in the promotional banner
56
* Fix - Force the card payment method to be enabled when the Optimized Checkout is enabled in the merchant's Payment Method Configuration
67
* Update - Deactivates Affirm or Klarna when other official plugins are active in merchant's Payment Method Configuration
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import { screen, render } from '@testing-library/react';
3+
import PaymentMethodRequiredForOCPill from '..';
4+
import { PAYMENT_METHOD_CARD } from 'wcstripe/stripe-utils/constants';
5+
import { useIsOCEnabled } from 'wcstripe/data';
6+
7+
jest.mock( 'wcstripe/data', () => ( {
8+
useIsOCEnabled: jest.fn(),
9+
} ) );
10+
11+
describe( 'PaymentMethodRequiredForOCPill', () => {
12+
beforeEach( () => {
13+
useIsOCEnabled.mockReturnValue( [ false, jest.fn() ] );
14+
} );
15+
16+
it( 'should render the "Required for the Optimized Checkout Suite" text', () => {
17+
useIsOCEnabled.mockReturnValue( [ true, jest.fn() ] );
18+
global.wc_stripe_settings_params = { is_oc_enabled: true };
19+
20+
render(
21+
<PaymentMethodRequiredForOCPill
22+
id={ PAYMENT_METHOD_CARD }
23+
label="Card"
24+
/>
25+
);
26+
27+
expect( screen.queryByText( 'Required' ) ).toBeInTheDocument();
28+
} );
29+
30+
it( 'should not render when OC is not active', () => {
31+
const { container } = render(
32+
<PaymentMethodRequiredForOCPill
33+
id={ PAYMENT_METHOD_CARD }
34+
label="Card"
35+
/>
36+
);
37+
38+
expect( container.firstChild ).toBeNull();
39+
} );
40+
} );
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { __, sprintf } from '@wordpress/i18n';
2+
import React from 'react';
3+
import styled from '@emotion/styled';
4+
import interpolateComponents from 'interpolate-components';
5+
import { Icon, info } from '@wordpress/icons';
6+
import Popover from 'wcstripe/components/popover';
7+
import { PAYMENT_METHOD_CARD } from 'wcstripe/stripe-utils/constants';
8+
import { useIsOCEnabled } from 'wcstripe/data';
9+
10+
const StyledPill = styled.span`
11+
display: inline-flex;
12+
align-items: center;
13+
gap: 4px;
14+
padding: 4px 8px;
15+
border: 1px solid #fcf9e8;
16+
border-radius: 2px;
17+
background-color: #fcf9e8;
18+
color: #674600;
19+
font-size: 12px;
20+
font-weight: 400;
21+
line-height: 16px;
22+
width: fit-content;
23+
`;
24+
25+
const StyledLink = styled.a`
26+
&:focus,
27+
&:visited {
28+
box-shadow: none;
29+
}
30+
`;
31+
32+
const IconWrapper = styled.span`
33+
height: 16px;
34+
cursor: pointer;
35+
`;
36+
37+
const AlertIcon = styled( Icon )`
38+
fill: #674600;
39+
`;
40+
41+
const IconComponent = ( { children, ...props } ) => (
42+
<IconWrapper { ...props }>
43+
<AlertIcon icon={ info } size="16" />
44+
{ children }
45+
</IconWrapper>
46+
);
47+
48+
const PaymentMethodRequiredForOCPill = ( { id, label } ) => {
49+
const [ isOCEnabled ] = useIsOCEnabled();
50+
if ( id === PAYMENT_METHOD_CARD && isOCEnabled ) {
51+
return (
52+
<StyledPill>
53+
{ __( 'Required', 'woocommerce-gateway-stripe' ) }
54+
<Popover
55+
BaseComponent={ IconComponent }
56+
content={ interpolateComponents( {
57+
mixedString: sprintf(
58+
/* translators: $1: a payment method name */
59+
__(
60+
'%1$s is required for the Optimized Checkout Suite.',
61+
'woocommerce-gateway-stripe'
62+
),
63+
label
64+
),
65+
components: {
66+
currencySettingsLink: (
67+
<StyledLink
68+
href="/wp-admin/admin.php?page=wc-settings&tab=general"
69+
target="_blank"
70+
rel="noreferrer"
71+
onClick={ ( ev ) => {
72+
// Stop propagation is necessary so it doesn't trigger the tooltip click event.
73+
ev.stopPropagation();
74+
} }
75+
/>
76+
),
77+
},
78+
} ) }
79+
/>
80+
</StyledPill>
81+
);
82+
}
83+
84+
return null;
85+
};
86+
87+
export default PaymentMethodRequiredForOCPill;

client/settings/general-settings-section/payment-method-checkbox.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const PaymentMethodCheckbox = ( {
3333
label,
3434
isAllowingManualCapture,
3535
disabled,
36+
disabledButChecked,
3637
} ) => {
3738
const [ isManualCaptureEnabled ] = useManualCapture();
3839
const [ isConfirmationModalOpen, setIsConfirmationModalOpen ] = useState(
@@ -44,9 +45,12 @@ const PaymentMethodCheckbox = ( {
4445
] = useEnabledPaymentMethodIds();
4546
const [ , setIsStripeEnabled ] = useIsStripeEnabled();
4647
const { isUpeEnabled } = useContext( UpeToggleContext );
48+
const checked =
49+
! disabled &&
50+
( disabledButChecked || enabledPaymentMethods.includes( id ) );
4751

4852
const handleCheckboxChange = ( hasBeenChecked ) => {
49-
if ( disabled ) {
53+
if ( disabled || disabledButChecked ) {
5054
return;
5155
}
5256
if ( ! hasBeenChecked ) {
@@ -107,10 +111,8 @@ const PaymentMethodCheckbox = ( {
107111
<StyledCheckbox
108112
label={ <VisuallyHidden>{ label }</VisuallyHidden> }
109113
onChange={ handleCheckboxChange }
110-
checked={
111-
disabled ? false : enabledPaymentMethods.includes( id )
112-
}
113-
disabled={ disabled }
114+
checked={ checked }
115+
disabled={ disabled || disabledButChecked }
114116
/>
115117
) }
116118
{ isConfirmationModalOpen && (

client/settings/general-settings-section/payment-method-description.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import RecurringPaymentIcon from '../../components/recurring-payment-icon';
55
import PaymentMethodCapabilityStatusPill from 'wcstripe/components/payment-method-capability-status-pill';
66
import PaymentMethodDeprecationPill from 'wcstripe/components/payment-method-deprecation-pill';
77
import PaymentMethodUnavailableDueConflictPill from 'wcstripe/components/payment-method-unavailable-due-conflict-pill';
8+
import PaymentMethodRequiredForOCPill from 'wcstripe/components/payment-method-required-for-oc-pill';
89

910
const Wrapper = styled.div`
1011
display: flex;
@@ -74,6 +75,10 @@ const PaymentMethodDescription = ( {
7475
id={ id }
7576
label={ label }
7677
/>
78+
<PaymentMethodRequiredForOCPill
79+
id={ id }
80+
label={ label }
81+
/>
7782
</>
7883
) }
7984
</LabelWrapper>

client/settings/general-settings-section/payment-method.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import interpolateComponents from 'interpolate-components';
77
import PaymentMethodsMap from '../../payment-methods-map';
88
import PaymentMethodDescription from './payment-method-description';
99
import PaymentMethodCheckbox from './payment-method-checkbox';
10-
import { useManualCapture } from 'wcstripe/data';
10+
import { useIsOCEnabled, useManualCapture } from 'wcstripe/data';
1111
import {
1212
PAYMENT_METHOD_AFFIRM,
1313
PAYMENT_METHOD_AFTERPAY_CLEARPAY,
@@ -105,6 +105,7 @@ const StyledFees = styled( PaymentMethodFeesPill )`
105105
`;
106106

107107
const PaymentMethod = ( { method, data } ) => {
108+
const [ isOCEnabled ] = useIsOCEnabled();
108109
const [ isManualCaptureEnabled ] = useManualCapture();
109110
const paymentMethodCurrencies = usePaymentMethodCurrencies( method );
110111

@@ -137,6 +138,8 @@ const PaymentMethod = ( { method, data } ) => {
137138
// eslint-disable-next-line camelcase
138139
wc_stripe_settings_params.has_klarna_gateway_plugin );
139140

141+
const isDisabledButChecked = PAYMENT_METHOD_CARD === method && isOCEnabled;
142+
140143
return (
141144
<div key={ method }>
142145
<ListElement
@@ -151,6 +154,7 @@ const PaymentMethod = ( { method, data } ) => {
151154
label={ label }
152155
isAllowingManualCapture={ isAllowingManualCapture }
153156
disabled={ deprecated || isDisabled }
157+
disabledButChecked={ isDisabledButChecked }
154158
/>
155159
<PaymentMethodWrapper>
156160
<PaymentMethodDescription

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 9.8.0 - xxxx-xx-xx =
114+
* Add - A new pill to the payment methods page to indicate the credit card requirement when the Optimized Checkout feature is enabled
114115
* Add - Tracks the toggle of the Optimized Checkout feature in the promotional banner
115116
* Fix - Force the card payment method to be enabled when the Optimized Checkout is enabled in the merchant's Payment Method Configuration
116117
* Update - Deactivates Affirm or Klarna when other official plugins are active in merchant's Payment Method Configuration

0 commit comments

Comments
 (0)