-
Notifications
You must be signed in to change notification settings - Fork 215
Disabling BNPLs when other official plugins are active #4492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 19 commits
e18b8a9
fc3d51a
e90b2d3
19d0206
de89343
82fcb80
21f2457
d6f85d2
569443d
644c10a
6465faf
c2dfbc9
c33624d
7d76068
ce86f94
3b5160d
4f94543
3760220
4a26e67
b5fc2a5
ed7c159
9bcd50f
7144812
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import { screen, render } from '@testing-library/react'; | ||
import PaymentMethodUnavailableDueConflictPill from '..'; | ||
import { PAYMENT_METHOD_AFFIRM } from 'wcstripe/stripe-utils/constants'; | ||
|
||
describe( 'PaymentMethodUnavailableDueConflictPill', () => { | ||
beforeEach( () => { | ||
global.wc_stripe_settings_params = { has_affirm_gateway_plugin: false }; | ||
} ); | ||
|
||
it( 'should render the "Unavailable due conflict" text', () => { | ||
global.wc_stripe_settings_params = { has_affirm_gateway_plugin: true }; | ||
|
||
render( | ||
<PaymentMethodUnavailableDueConflictPill | ||
id={ PAYMENT_METHOD_AFFIRM } | ||
label="Affirm" | ||
/> | ||
); | ||
|
||
expect( | ||
screen.queryByText( 'Unavailable due conflict' ) | ||
).toBeInTheDocument(); | ||
} ); | ||
|
||
it( 'should not render when other extensions are not active', () => { | ||
const { container } = render( | ||
<PaymentMethodUnavailableDueConflictPill | ||
id={ PAYMENT_METHOD_AFFIRM } | ||
label="Affirm" | ||
/> | ||
); | ||
|
||
expect( container.firstChild ).toBeNull(); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* global wc_stripe_settings_params */ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import React from 'react'; | ||
import styled from '@emotion/styled'; | ||
import interpolateComponents from 'interpolate-components'; | ||
import { Icon, info } from '@wordpress/icons'; | ||
import Popover from 'wcstripe/components/popover'; | ||
import { | ||
PAYMENT_METHOD_AFFIRM, | ||
PAYMENT_METHOD_KLARNA, | ||
} from 'wcstripe/stripe-utils/constants'; | ||
|
||
const StyledPill = styled.span` | ||
display: inline-flex; | ||
align-items: center; | ||
gap: 4px; | ||
padding: 4px 8px; | ||
border: 1px solid #fcf9e8; | ||
border-radius: 2px; | ||
background-color: #fcf9e8; | ||
color: #674600; | ||
font-size: 12px; | ||
font-weight: 400; | ||
line-height: 16px; | ||
width: fit-content; | ||
`; | ||
|
||
const StyledLink = styled.a` | ||
&:focus, | ||
&:visited { | ||
box-shadow: none; | ||
} | ||
`; | ||
|
||
const IconWrapper = styled.span` | ||
height: 16px; | ||
cursor: pointer; | ||
`; | ||
|
||
const AlertIcon = styled( Icon )` | ||
fill: #674600; | ||
`; | ||
|
||
const IconComponent = ( { children, ...props } ) => ( | ||
<IconWrapper { ...props }> | ||
<AlertIcon icon={ info } size="16" /> | ||
{ children } | ||
</IconWrapper> | ||
); | ||
|
||
const PaymentMethodUnavailableDueConflictPill = ( { id, label } ) => { | ||
if ( | ||
( id === PAYMENT_METHOD_AFFIRM && | ||
// eslint-disable-next-line camelcase | ||
wc_stripe_settings_params.has_affirm_gateway_plugin ) || | ||
( id === PAYMENT_METHOD_KLARNA && | ||
// eslint-disable-next-line camelcase | ||
wc_stripe_settings_params.has_klarna_gateway_plugin ) | ||
) { | ||
return ( | ||
<StyledPill> | ||
{ __( | ||
'Unavailable due conflict', | ||
'woocommerce-gateway-stripe' | ||
) } | ||
<Popover | ||
BaseComponent={ IconComponent } | ||
content={ interpolateComponents( { | ||
mixedString: sprintf( | ||
/* translators: $1: a payment method name */ | ||
__( | ||
'%1$s is unavailable due to another official plugin being active.', | ||
'woocommerce-gateway-stripe' | ||
), | ||
label | ||
), | ||
components: { | ||
currencySettingsLink: ( | ||
<StyledLink | ||
href="/wp-admin/admin.php?page=wc-settings&tab=general" | ||
target="_blank" | ||
rel="noreferrer" | ||
onClick={ ( ev ) => { | ||
// Stop propagation is necessary so it doesn't trigger the tooltip click event. | ||
ev.stopPropagation(); | ||
} } | ||
/> | ||
), | ||
}, | ||
} ) } | ||
/> | ||
</StyledPill> | ||
); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default PaymentMethodUnavailableDueConflictPill; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,15 +257,6 @@ public function admin_scripts( $hook_suffix ) { | |
// Show the BNPL promotional banner only if no BNPL payment methods are enabled. | ||
&& ! array_intersect( WC_Stripe_Payment_Methods::BNPL_PAYMENT_METHODS, $enabled_payment_methods ); | ||
|
||
$has_other_bnpl_plugins_active = false; | ||
$available_payment_gateways = WC()->payment_gateways->payment_gateways; | ||
foreach ( $available_payment_gateways as $gateway ) { | ||
if ( ( 'affirm' === $gateway->id || 'klarna_payments' === $gateway->id ) && 'yes' === $gateway->enabled ) { | ||
$has_other_bnpl_plugins_active = true; | ||
break; | ||
} | ||
} | ||
|
||
$params = [ | ||
'time' => time(), | ||
'i18n_out_of_sync' => $message, | ||
|
@@ -287,7 +278,9 @@ public function admin_scripts( $hook_suffix ) { | |
'is_oc_available' => WC_Stripe_Feature_Flags::is_oc_available(), | ||
'oauth_nonce' => wp_create_nonce( 'wc_stripe_get_oauth_urls' ), | ||
'is_sepa_tokens_enabled' => 'yes' === $this->gateway->get_option( 'sepa_tokens_for_other_methods', 'no' ), | ||
'has_other_bnpl_plugins' => $has_other_bnpl_plugins_active, | ||
'has_affirm_gateway_plugin' => WC_Stripe_Helper::has_gateway_plugin_active( WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM ), | ||
'has_klarna_gateway_plugin' => WC_Stripe_Helper::has_gateway_plugin_active( WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_KLARNA ), | ||
'has_other_bnpl_plugins' => WC_Stripe_Helper::has_other_bnpl_plugins_active(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making use of the new helper function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option here would be to reuse the return of both |
||
'is_payments_onboarding_task_completed' => $this->is_payments_onboarding_task_completed(), | ||
]; | ||
wp_localize_script( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,20 @@ class WC_Stripe_Helper { | |
const META_NAME_STRIPE_CURRENCY = '_stripe_currency'; | ||
const PAYMENT_AWAITING_ACTION_META = '_stripe_payment_awaiting_action'; | ||
|
||
/** | ||
* The identifier for the official Affirm gateway plugin. | ||
* | ||
* @var string | ||
*/ | ||
const OFFICIAL_PLUGIN_ID_AFFIRM = 'affirm'; | ||
|
||
/** | ||
* The identifier for the official Klarna gateway plugin. | ||
* | ||
* @var string | ||
*/ | ||
const OFFICIAL_PLUGIN_ID_KLARNA = 'klarna_payments'; | ||
|
||
/** | ||
* List of legacy Stripe gateways. | ||
* | ||
|
@@ -1851,4 +1865,35 @@ public static function get_refund_reason_description( $refund_reason_key ) { | |
return __( 'Unknown reason', 'woocommerce-gateway-stripe' ); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if there are other Buy Now Pay Later plugins active. | ||
* | ||
* @return bool | ||
*/ | ||
public static function has_other_bnpl_plugins_active() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we need this logic in multiple places, I moved it to a new helper function. |
||
$other_bnpl_gateway_ids = [ self::OFFICIAL_PLUGIN_ID_AFFIRM, self::OFFICIAL_PLUGIN_ID_KLARNA ]; | ||
foreach ( $other_bnpl_gateway_ids as $bnpl_gateway_id ) { | ||
if ( self::has_gateway_plugin_active( $bnpl_gateway_id ) ) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Checks if a given payment gateway plugin is active. | ||
* | ||
* @param string $plugin_id | ||
* @return bool | ||
*/ | ||
public static function has_gateway_plugin_active( $plugin_id ) { | ||
$available_payment_gateways = WC()->payment_gateways->payment_gateways ?? []; | ||
foreach ( $available_payment_gateways as $available_payment_gateway ) { | ||
if ( $plugin_id === $available_payment_gateway->id && 'yes' === $available_payment_gateway->enabled ) { | ||
wjrosa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,16 +93,7 @@ public static function init( WC_Stripe_Payment_Gateway $gateway ) { | |
} | ||
} | ||
|
||
$has_other_bnpl_plugins_active = false; | ||
$available_payment_gateways = WC()->payment_gateways->payment_gateways; | ||
$other_bnpl_gateway_ids = [ 'affirm', 'klarna_payments' ]; | ||
foreach ( $available_payment_gateways as $available_payment_gateway ) { | ||
if ( in_array( $available_payment_gateway->id, $other_bnpl_gateway_ids, true ) && 'yes' === $available_payment_gateway->enabled ) { | ||
$has_other_bnpl_plugins_active = true; | ||
break; | ||
} | ||
} | ||
if ( $has_other_bnpl_plugins_active ) { | ||
if ( WC_Stripe_Helper::has_other_bnpl_plugins_active() ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making use of the new helper function. |
||
return; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.