Skip to content
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

Enable deferred intent creation when initialization process encounters cache unavailability #7686

Merged
merged 16 commits into from
Nov 17, 2023

Conversation

timur27
Copy link
Contributor

@timur27 timur27 commented Nov 9, 2023

Fixes #7482

Changes proposed in this Pull Request

When checking the state of the feature flag for the deferred intent creation UPE, sometimes the cache isn't available which makes the WC_Payments_Features return false. If with this setup, a store has legacy UPE enabled, legacy UPE takes over the initialization (instead of initializing deferred UPE as one would expect), eventually leading to unpredictable state of the registered gateways. This is what the author of #7482 faced.

The main change in this PR is to enable deferred UPE when the cache is unavailable for some reason. Cache being unavailable is a rare/edge-case scenario, and the changes in this PR are not negatively impacting the scenario when we disable the dUPE feature flag for a merchant. The flag state is correctly propagated to the plugin if we do so.

While the unit test suite is initialized, it checks UPE flags. Previously, while checking the flag, all the UPEs were disabled, and thus, the legacy gateway was initialized for tests. Now, when we run dUPE in case of a non-available cache, tests start to run deferred intent creation UPE, which led to multiple failures that this PR fixes. I encourage to review the tests in more detail. Some tests were removed, some were replaced, and some context is left in the comments.

Testing instructions

On develop

  1. Ensure that the server-side flag for dUPE on your local store is enabled
  2. Enable client-side FF for legacy UPE
  3. In WCPay Dev, refresh the cache and confirm you see a fatal error like described Call to a member function is_reusable() on bool in class-wc-payments.php:768 + e is null #7482 (comment)
  4. Checkout fix/more-use-cases-for-enabled-dupe
  5. Repeat steps 1-3 and confirm that you don't see the error from the third step anymore
  6. Perform a sanity check by purchasing a product and confirm that deferred intent creation UPE is being used (e.g. by checking the gateway_type field in the Metadata of payment details on the Stripe dashboard)
  7. Disable dUPE FF in the server's database and confirm that legacy UPE is used if legacy UPE FF is on, and legacy card is used when no UPE FF is enabled)
  8. Run npm run test:php on your local to ensure that the changes in bootstrap.php don't impact the setup negatively.

  • Run npm run changelog to add a changelog file, choose patch to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.
  • Covered with tests (or have a good reason not to test in description ☝️)
  • Tested on mobile (or does not apply)

Post merge

@timur27 timur27 changed the title Fix/more use cases for enabled dupe Enable deferred intent creation when initialization process encounters cache unavailability Nov 9, 2023
@botwoo
Copy link
Collaborator

botwoo commented Nov 9, 2023

Test the build

Option 1. Jetpack Beta

  • Install and activate Jetpack Beta.
  • Use this build by searching for PR number 7686 or branch name fix/more-use-cases-for-enabled-dupe in your-test.site/wp-admin/admin.php?page=jetpack-beta&plugin=woocommerce-payments

Option 2. Jurassic Ninja - available for logged-in A12s

🚀 Launch a JN site with this branch 🚀

ℹ️ Install this Tampermonkey script to get more options.


Build info:

  • Latest commit: a540e9d
  • Build time: 2023-11-17 16:36:22 UTC

Note: the build is updated when a new commit is pushed to this PR.

Comment on lines -62 to -76
public function test_track_disabled_on_upgrade() {
update_option( WC_Payments_Features::UPE_FLAG_NAME, 'disabled' );

Track_Upe_Status::maybe_track();

$this->assertEquals(
[
'wcpay_upe_disabled' => [],
],
Tracker::get_admin_events()
);

$this->assertSame( '1', get_option( Track_Upe_Status::IS_TRACKED_OPTION ) );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check for wcpay_upe_disabled anymore since UPE is always disabled now

@@ -24,78 +24,6 @@ public function tear_down() {
delete_option( '_wcpay_feature_upe' );
}

public function test_get_note() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the non-UPE related tests have been removed from this class, since the plugin now executes UPE by default

Comment on lines -58 to -65
public function test_stripelink_setup_note_null_when_upe_disabled() {
$this->mock_gateway_data( '0', [ 'card', 'link' ], [ 'card' ] );

$note = \WC_Payments_Notes_Set_Up_StripeLink::get_note();

$this->assertNull( $note );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is irrelevant going forward due to UPE being always enabled

/**
* @dataProvider maybe_filter_gateway_title_data_provider
*/
public function test_maybe_filter_gateway_title_with_no_additional_feature_flags_enabled( $data ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering occurs for non-deferred UPE only, so this test is not needed anymore as it will fail because of UPE enabled by default.

public function test_payment_fields_outputs_fields() {
$this->wcpay_gateway->payment_fields();

$this->expectOutputRegex( '/<div id="wcpay-card-element"><\/div>/' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No wcpay-card-element is rendered anymore. Instead, the UPE gateway test tests the correct input element.

Copy link
Contributor

github-actions bot commented Nov 9, 2023

Size Change: 0 B

Total Size: 1.44 MB

ℹ️ View Unchanged
Filename Size
release/woocommerce-payments/assets/css/admin.css 1.06 kB
release/woocommerce-payments/assets/css/success.css 158 B
release/woocommerce-payments/dist/blocks-checkout-rtl.css 1.8 kB
release/woocommerce-payments/dist/blocks-checkout.css 1.8 kB
release/woocommerce-payments/dist/blocks-checkout.js 75.3 kB
release/woocommerce-payments/dist/checkout-rtl.css 441 B
release/woocommerce-payments/dist/checkout.css 442 B
release/woocommerce-payments/dist/checkout.js 28.9 kB
release/woocommerce-payments/dist/index-rtl.css 36.8 kB
release/woocommerce-payments/dist/index.css 36.8 kB
release/woocommerce-payments/dist/index.js 285 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.05 kB
release/woocommerce-payments/dist/multi-currency-rtl.css 3.4 kB
release/woocommerce-payments/dist/multi-currency-switcher-block.js 60.2 kB
release/woocommerce-payments/dist/multi-currency.css 3.4 kB
release/woocommerce-payments/dist/multi-currency.js 55.4 kB
release/woocommerce-payments/dist/order-rtl.css 676 B
release/woocommerce-payments/dist/order.css 679 B
release/woocommerce-payments/dist/order.js 41.4 kB
release/woocommerce-payments/dist/payment-gateways-rtl.css 1.31 kB
release/woocommerce-payments/dist/payment-gateways.css 1.31 kB
release/woocommerce-payments/dist/payment-gateways.js 39 kB
release/woocommerce-payments/dist/payment-request-rtl.css 153 B
release/woocommerce-payments/dist/payment-request.css 153 B
release/woocommerce-payments/dist/payment-request.js 13.1 kB
release/woocommerce-payments/dist/product-details.js 898 B
release/woocommerce-payments/dist/settings-rtl.css 10.3 kB
release/woocommerce-payments/dist/settings.css 10.3 kB
release/woocommerce-payments/dist/settings.js 233 kB
release/woocommerce-payments/dist/subscription-edit-page.js 669 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal-rtl.css 519 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.css 519 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.js 20.4 kB
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 693 B
release/woocommerce-payments/dist/subscriptions-empty-state-rtl.css 117 B
release/woocommerce-payments/dist/subscriptions-empty-state.css 117 B
release/woocommerce-payments/dist/subscriptions-empty-state.js 19.5 kB
release/woocommerce-payments/dist/tos-rtl.css 230 B
release/woocommerce-payments/dist/tos.css 231 B
release/woocommerce-payments/dist/tos.js 21.9 kB
release/woocommerce-payments/dist/upe_checkout-rtl.css 441 B
release/woocommerce-payments/dist/upe_checkout.css 442 B
release/woocommerce-payments/dist/upe_checkout.js 34.1 kB
release/woocommerce-payments/dist/upe_split_checkout-rtl.css 441 B
release/woocommerce-payments/dist/upe_split_checkout.css 442 B
release/woocommerce-payments/dist/upe_split_checkout.js 34.7 kB
release/woocommerce-payments/dist/upe_with_deferred_intent_creation_checkout.js 37.1 kB
release/woocommerce-payments/dist/upe-blocks-checkout-rtl.css 1.8 kB
release/woocommerce-payments/dist/upe-blocks-checkout.css 1.8 kB
release/woocommerce-payments/dist/upe-blocks-checkout.js 41 kB
release/woocommerce-payments/dist/upe-split-blocks-checkout-rtl.css 1.8 kB
release/woocommerce-payments/dist/upe-split-blocks-checkout.css 1.8 kB
release/woocommerce-payments/dist/upe-split-blocks-checkout.js 42.6 kB
release/woocommerce-payments/dist/woopay-express-button-rtl.css 153 B
release/woocommerce-payments/dist/woopay-express-button.css 153 B
release/woocommerce-payments/dist/woopay-express-button.js 52.1 kB
release/woocommerce-payments/dist/woopay-rtl.css 4.18 kB
release/woocommerce-payments/dist/woopay.css 4.19 kB
release/woocommerce-payments/dist/woopay.js 71.7 kB
release/woocommerce-payments/includes/subscriptions/assets/css/plugin-page.css 622 B
release/woocommerce-payments/includes/subscriptions/assets/js/plugin-page.js 812 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/i18n-loader.js 2.43 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/i18n-loader.js 1.01 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-ajax.js 522 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-callables.js 581 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/babel.config.js 160 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.css 2.32 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.js 13.8 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.rtl.css 2.32 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/about.css 1.03 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-empty-state.css 291 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-order-statuses.css 403 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin.css 3.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/checkout.css 299 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/modal.css 742 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/view-subscription.css 572 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/wcs-upgrade.css 411 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin-pointers.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin.js 9.4 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.js 6.8 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.min.js 3.83 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-coupon.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-subscription.js 2.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.js 22.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.min.js 11.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/payment-method-restrictions.js 1.29 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/wcs-meta-boxes-order.js 502 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/payment-methods.js 355 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/single-product.js 429 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js 1.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/wcs-cart.js 781 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/modal.js 1.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/wcs-upgrade.js 1.27 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.css 392 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.js 3.05 kB

compressed-size-action


public function test_update_settings_validation_fails_if_invalid_gateway_id_supplied() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was removed because it's an integration test that works only with the legacy gateway. It's not easy nor elegant to mock gateway in WC_Payments because methods/objects are static.

Since the tested method relies solely on get_upe_available_payment_methods, get_upe_available_payment_methods is now fully tested by test-class-upe-payment-gateway.php::test_get_upe_available_payment_methods()

@timur27 timur27 marked this pull request as ready for review November 9, 2023 21:14
@timur27 timur27 force-pushed the fix/more-use-cases-for-enabled-dupe branch from 60b5c5f to c89c113 Compare November 13, 2023 13:17
@timur27 timur27 force-pushed the fix/more-use-cases-for-enabled-dupe branch 8 times, most recently from 601d9c9 to e0e85b2 Compare November 15, 2023 12:10
@timur27 timur27 force-pushed the fix/more-use-cases-for-enabled-dupe branch 9 times, most recently from 51bdede to e0e85b2 Compare November 16, 2023 11:05
@timur27 timur27 force-pushed the fix/more-use-cases-for-enabled-dupe branch from 6f0bc10 to e95b783 Compare November 16, 2023 12:00
@@ -98,6 +98,9 @@ function() {
require_once $_plugin_dir . 'includes/admin/class-wc-rest-payments-payment-intents-controller.php';
require_once $_plugin_dir . 'includes/class-woopay-tracker.php';
require_once $_plugin_dir . 'includes/admin/class-wc-rest-payments-customer-controller.php';

// Load currency helper class early to ensure its implementation is used over the one resolved during further test initialization.
require_once __DIR__ . '/helpers/class-wc-helper-site-currency.php';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More context on this change in p1700139623363929-slack-CU6SYV31A

@timur27 timur27 requested review from a team and frosso and removed request for a team November 16, 2023 13:05
Copy link
Contributor

@frosso frosso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thanks for your diligence with the UTs! Just a minor, non-blocking question.

@@ -78,6 +78,9 @@ public static function is_upe_split_enabled() {
*/
public static function is_upe_deferred_intent_enabled() {
$account = WC_Payments::get_database_cache()->get( WCPay\Database_Cache::ACCOUNT_KEY, true );
if ( null === $account ) {
return true;
}
return is_array( $account ) && ( $account[ self::DEFERRED_UPE_SERVER_FLAG_NAME ] ?? false );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side question - would it also make sense to return true in case $account[ self::DEFERRED_UPE_SERVER_FLAG_NAME ] is not set?

i.e.:

Suggested change
return is_array( $account ) && ( $account[ self::DEFERRED_UPE_SERVER_FLAG_NAME ] ?? false );
return is_array( $account ) && ( $account[ self::DEFERRED_UPE_SERVER_FLAG_NAME ] ?? true );

Asking because we're basically saying: "if there's no account data - force dUPE". But if $account data is an array without the is_deferred_intent_creation_upe_enabled key, we're saying that dUPE is disabled. Am I getting my neurons too twisted? 😵

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right and abc7347 improves this together with removing one redundant test which came up after the change, thank you!

The scenario with returning disabled dUPE state wouldn't happen because server always returns a value, but this doesn't mean client can rely on this so I agree with you.

@timur27
Copy link
Contributor Author

timur27 commented Nov 17, 2023

Performed a quick successful sanity check after abc7347. Merging.

@timur27 timur27 added this pull request to the merge queue Nov 17, 2023
Merged via the queue into develop with commit 505ebf6 Nov 17, 2023
28 checks passed
@timur27 timur27 deleted the fix/more-use-cases-for-enabled-dupe branch November 17, 2023 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Call to a member function is_reusable() on bool in class-wc-payments.php:768 + e is null
3 participants