Skip to content

Commit

Permalink
Ensure fallbacks and working solution for every gateway type:
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Karimov committed Sep 22, 2023
1 parent d07794a commit 8ab3ac8
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 67 deletions.
70 changes: 53 additions & 17 deletions client/payment-methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ const PaymentMethodsDropdownMenu = ( { setOpenModal } ) => {

const UpeSetupBanner = () => {
const [ , setIsUpeEnabled ] = useIsUpeEnabled();
const { isUpeEnabled, upeType } = useContext( WcPayUpeContext );

const handleEnableUpeClick = () => {
console.log('while enabling, upe type is: ' + upeType);
setIsUpeEnabled( true ).then( () => {
window.location.href = getAdminUrl( {
page: 'wc-admin',
path: '/payments/additional-payment-methods',
} );
if ( upeType === '' ) {
window.location.href = getAdminUrl( {
page: 'wc-admin',
path: '/payments/additional-payment-methods',
} );
}
} );
};

Expand All @@ -89,27 +93,45 @@ const UpeSetupBanner = () => {
} ) }
>
<h3>
{ __(
{ upeType === 'legacy_after_deferred_intent' && __(
'Enable deferred UPE',
'woocommerce-payments'
)}

{ ! isUpeEnabled && __(
'Enable the new WooPayments checkout experience, which will become the default on November 1, 2023',
'woocommerce-payments'
) }
)}

{ upeType === 'split' && __(
'This is deferred UPE (this header will be removed after DEMO)',
'woocommerce-payments'
)}
</h3>
<p>
{ __(
{ upeType === 'legacy_after_deferred_intent' && __(
/* eslint-disable-next-line max-len */
'You previously disabled deferred UPE. Maybe it is time to give it another try?',
'woocommerce-payments'
)}

{ ! isUpeEnabled && __(
/* eslint-disable-next-line max-len */
'This will improve the checkout experience and boost sales with access to additional payment methods, which you’ll be able to manage from here in settings.',
'woocommerce-payments'
) }
)}
</p>

<div className="payment-methods__express-checkouts-actions">
<span className="payment-methods__express-checkouts-get-started">
<Button isSecondary onClick={ handleEnableUpeClick }>
{ __(
'Enable payment methods',
'woocommerce-payments'
) }
</Button>
{ upeType !== 'split' && (
<Button isSecondary onClick={ handleEnableUpeClick }>
{ __(
'Enable payment methods',
'woocommerce-payments'
) }
</Button>
)}
</span>
<ExternalLink href="https://woocommerce.com/document/woopayments/payment-methods/additional-payment-methods/">
{ __( 'Learn more', 'woocommerce-payments' ) }
Expand Down Expand Up @@ -272,12 +294,26 @@ const PaymentMethods = () => {
</>
) }
</h4>
<PaymentMethodsDropdownMenu
setOpenModal={ setOpenModalIdentifier }
/>
{ upeType !== 'legacy_after_deferred_intent' && (
<PaymentMethodsDropdownMenu
setOpenModal={ setOpenModalIdentifier }
/>
)}
</CardHeader>
) }

{ upeType === 'legacy_after_deferred_intent' && (
<CardHeader className="payment-methods__header">
<UpeSetupBanner />
</CardHeader>
)}

{ upeType === 'split' && (
<CardHeader className="payment-methods__header">
<UpeSetupBanner />
</CardHeader>
)}

{ isUpeEnabled && upeType === 'legacy' && (
<CardHeader className="payment-methods__header">
<InlineNotice
Expand Down
14 changes: 11 additions & 3 deletions client/settings/disable-upe-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,22 @@ const DisableUpeModalBody = () => {

const DisableUpeModal = ( { setOpenModal, triggerAfterDisable } ) => {
const [ isUpeEnabled, setIsUpeEnabled ] = useIsUpeEnabled();
const { status } = useContext( WcPayUpeContext );
const { status, upeType } = useContext( WcPayUpeContext );

useEffect( () => {
if ( ! isUpeEnabled ) {
console.log(' upe type is: ' + upeType );
if ( status === 'pending') {
setOpenModal( '' );
triggerAfterDisable();
}
}, [ isUpeEnabled, setOpenModal, triggerAfterDisable ] );
//
// if ( isUpeEnabled && upeType === 'legacy_after_deferred_intent' ) {
// setOpenModal( '' );
// triggerAfterDisable();
// }


}, [ isUpeEnabled, setOpenModal, triggerAfterDisable, status, upeType ] );

useEffect( () => {
if ( status === 'error' ) {
Expand Down
46 changes: 40 additions & 6 deletions client/settings/wcpay-upe-toggle/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const WcPayUpeContextProvider = ( {
const [ , setEnabledPaymentMethods ] = useEnabledPaymentMethodIds();
const { updateAvailablePaymentMethodIds } = useDispatch( STORE_NAME );

useState( () => {
console.log( 'timka: ' + upeType );
}
, [ upeType ] );

const updateFlag = useCallback(
( value ) => {
setStatus( 'pending' );
Expand All @@ -39,16 +44,44 @@ const WcPayUpeContextProvider = ( {
} )
.then( () => {
// new "toggles" will continue being "split" UPE
setUpeType( value ? 'split' : '' );
setIsUpeEnabled( Boolean( value ) );

if ( value ) {
if ( upeType === 'legacy_after_deferred_intent' ) {
setUpeType( 'deferred_upe_from_legacy_upe' );
setIsUpeEnabled( true );
} else if ( upeType === '' ) {
setUpeType( 'deferred_upe_from_legacy_card' );
setIsUpeEnabled( true );
}
}

// down
if ( ! value ) {
if ( upeType === 'deferred_upe_from_legacy_card' ) {
// fallback to card
setUpeType( '' );
setIsUpeEnabled( false );
updateAvailablePaymentMethodIds( [ 'card' ] );
setEnabledPaymentMethods( [ 'card' ] );
}

if ( upeType ==='deferred_upe_from_legacy_upe' ) {
// fallback to upe
setUpeType( 'legacy_after_deferred_intent' );
setIsUpeEnabled( true );
}
}

// setUpeType( value ? 'split' : '' );
// setIsUpeEnabled( Boolean( value ) );

// the backend already takes care of this,
// we're just duplicating the effort
// to ensure that the non-UPE payment methods are removed when the flag is disabled
if ( ! value ) {
updateAvailablePaymentMethodIds( [ 'card' ] );
setEnabledPaymentMethods( [ 'card' ] );
}
// if ( ! value ) {
// updateAvailablePaymentMethodIds( [ 'card' ] );
// setEnabledPaymentMethods( [ 'card' ] );
// }
setStatus( 'resolved' );
} )
.catch( () => {
Expand All @@ -59,6 +92,7 @@ const WcPayUpeContextProvider = ( {
setStatus,
setIsUpeEnabled,
setUpeType,
upeType,
setEnabledPaymentMethods,
updateAvailablePaymentMethodIds,
]
Expand Down
61 changes: 25 additions & 36 deletions includes/admin/class-wc-rest-upe-flag-toggle-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,26 @@ private function update_is_upe_enabled( WP_REST_Request $request ) {
$is_upe_enabled = $request->get_param( 'is_upe_enabled' );

if ( $is_upe_enabled ) {
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '1' );
// update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '1' );.

if ( '1' === get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && 'disabled' === get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// enabling back dupe after falling back to legacy upe.
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '0' );
} elseif ( '0' == get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && '0' == get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// enabling dupe from legacy card for the first time.
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '1' );
} elseif ( '0' == get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && 'disabled' === get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// enabling dupe from legacy card after falling back.
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '1' );
} elseif ( 'disabled' === get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && 'disabled' === get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// enabling legacy upe from fallback from dupe and fallback from legacy upe to card (not needed anymore).
// update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '1' );.
update_option( WC_Payments_Features::UPE_FLAG_NAME, '1' );
} elseif ( 'disabled' === get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && '0' == get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// enabling dupe for legacy card users who previously disabled legacy upe.
update_option( WC_Payments_Features::UPE_FLAG_NAME, '0' );
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, '1' );
}

// WooCommerce core only includes Tracks in admin, not the REST API, so we need to use this wc_admin method
// that includes WC_Tracks in case it's not loaded.
Expand All @@ -135,42 +154,12 @@ private function update_is_upe_enabled( WP_REST_Request $request ) {
return;
}

// If the legacy UPE flag has been enabled, we need to disable the legacy UPE flag.
if ( '1' === get_option( WC_Payments_Features::UPE_FLAG_NAME ) && '0' === get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME ) ) {
update_option( WC_Payments_Features::UPE_FLAG_NAME, 'disabled' );

if ( function_exists( 'wc_admin_record_tracks_event' ) ) {
wc_admin_record_tracks_event(
Track_Events::UPE_DISABLED,
[
'is_bnpl_affirm_afterpay_enabled' => WC_Payments_Features::is_bnpl_affirm_afterpay_enabled(),
]
);
}
} elseif ( '1' === get_option( WC_Payments_Features::UPE_SPLIT_FLAG_NAME ) ) {
// marking the flag as "disabled", so that we can keep track that the merchant explicitly disabled split UPE.
update_option( WC_Payments_Features::UPE_SPLIT_FLAG_NAME, 'disabled' );

if ( function_exists( 'wc_admin_record_tracks_event' ) ) {
wc_admin_record_tracks_event(
Track_Events::SPLIT_UPE_DISABLED,
[
'is_bnpl_affirm_afterpay_enabled' => WC_Payments_Features::is_bnpl_affirm_afterpay_enabled(),
]
);
}
} elseif ( '1' === get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME ) ) {
// marking the flag as "disabled", so that we can keep track that the merchant explicitly disabled deferred intent UPE.
if ( '1' === get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && '0' == get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// legacy UPE users who have dupe -> legacy UPE.
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 'disabled' );
} elseif ( '0' == get_option( WC_Payments_Features::UPE_FLAG_NAME, 0 ) && '1' === get_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) ) {
// dupe -> legacy card.
update_option( WC_Payments_Features::UPE_DEFERRED_INTENT_FLAG_NAME, 'disabled' );

if ( function_exists( 'wc_admin_record_tracks_event' ) ) {
wc_admin_record_tracks_event(
Track_Events::DEFERRED_INTENT_UPE_DISABLED,
[
'is_bnpl_affirm_afterpay_enabled' => WC_Payments_Features::is_bnpl_affirm_afterpay_enabled(),
]
);
}
}

// resetting a few other things:
Expand Down
23 changes: 18 additions & 5 deletions includes/class-wc-payments-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class WC_Payments_Features {
* @return bool
*/
public static function is_upe_enabled() {
$legacy = get_option( self::UPE_FLAG_NAME, '0' );
$split = get_option( self::UPE_SPLIT_FLAG_NAME, '0' );
$deferred = get_option( self::UPE_DEFERRED_INTENT_FLAG_NAME, 0 );
$a = 1;
return self::is_upe_legacy_enabled() || self::is_upe_split_enabled() || self::is_upe_deferred_intent_enabled();
}

Expand All @@ -38,12 +42,21 @@ public static function is_upe_enabled() {
* @return string
*/
public static function get_enabled_upe_type() {
if ( self::is_upe_split_enabled() || self::is_upe_deferred_intent_enabled() ) {
return 'split';

if ( self::is_upe_deferred_intent_enabled() && self::is_upe_legacy_enabled() ) {
return 'deferred_upe_from_legacy_upe';
}

if ( self::is_upe_deferred_intent_enabled() && ! self::is_upe_legacy_enabled() ) {
return 'deferred_upe_from_legacy_card';
}

if ( 'disabled' === get_option( self::UPE_DEFERRED_INTENT_FLAG_NAME, 0 ) && self::is_upe_legacy_enabled() ) {
return 'legacy_after_deferred_intent';
}

if ( self::is_upe_legacy_enabled() ) {
return 'legacy';
if ( ! self::is_upe_enabled() ) {
return 'legacy_card';
}

return '';
Expand Down Expand Up @@ -81,7 +94,7 @@ public static function is_upe_deferred_intent_enabled() {
}

// legacy UPE users who never used dUPE and thus should be forced to dUPE.
if ( self::is_upe_legacy_enabled() && 0 === $deferred_intent_creation_upe_flag_value ) {
if ( self::is_upe_legacy_enabled() && 0 == $deferred_intent_creation_upe_flag_value ) {
return true;
}

Expand Down

0 comments on commit 8ab3ac8

Please sign in to comment.