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

Fix fraud prevention token not showing up on checkout pages created via the site editor #8142

Merged
merged 20 commits into from
Feb 13, 2024

Conversation

tpaksu
Copy link
Contributor

@tpaksu tpaksu commented Feb 6, 2024

Fixes #8141

Changes proposed in this Pull Request

This PR changes the way the fraud prevention token is delivered to the browser, and retrieved by the payment methods to be sent to the checkout endpoints. The token will now be present as a global JS variable window.wcpayFraudPreventionToken when the user is viewing the cart page, or the checkout page, when the fraud mitigations are activated for the merchant, instead of being appended as a value in a hidden input.

Testing instructions

All the possible checkout types that use our server needs to be tested to see if the payment is still working as intended, with the fraud mitigations disabled, and enabled.

  • Regular checkout using test card
  • Regular checkout using saved card
  • Regular checkout using one UPE method
  • ApplePay
  • GooglePay
  • WooPay

A way to test those in a single installation:

  • Create a JN site using the "Launch a JN site with this branch" link in the below comment of "botwoo".
  • On the new JN site complete the WC, WooPayments and Stripe onboarding.
  • Install the Storefront theme.
  • Go to Appearance > Storefront page and click the button with both checkboxes selected. This will prepare the demo site with adding example products.
  • On the Customizer which opens after it, click Publish.
  • Return to your store and check if the products are there, and the front page shows the categories and products.
  • Note the blog ID attached to your new blog on the WCPay Dev Tools page.
  • Go to https://mc.a8c.com/woocommerce-payments/index.php and search your new blog ID.
  • On the dashboard for your account, enable the card testing mitigations with setting the card_testing_prevention_enabled account meta to 1.
  • Refresh your account cache using the "clear cache" link on WCPay Dev Tools.
  • The site should be ready to test the above payment methods. If you don't see the payment form, check if WooPayments is enabled on Payments > Settings.

Important

Repeat the tests after completing all tests with switching to the Twenty Twenty Four theme. This will test it with a Site Editor enabled theme, which was the main problem in the issue.


  • 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

@tpaksu tpaksu linked an issue Feb 6, 2024 that may be closed by this pull request
@tpaksu tpaksu changed the title Add changelog Fix fraud prevention token not showing up on checkout pages created via the site editor Feb 6, 2024
@botwoo
Copy link
Collaborator

botwoo commented Feb 6, 2024

Test the build

Option 1. Jetpack Beta

  • Install and activate Jetpack Beta.
  • Use this build by searching for PR number 8142 or branch name fix/8141-fraud-prevention-token-site-editor-problem 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: e706cd7
  • Build time: 2024-02-13 06:13:24 UTC

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

Copy link
Contributor

github-actions bot commented Feb 6, 2024

Size Change: +18 B (0%)

Total Size: 1.26 MB

Filename Size Change
release/woocommerce-payments/dist/blocks-checkout.js 83.7 kB -16 B (0%)
release/woocommerce-payments/dist/checkout.js 36 kB +46 B (0%)
release/woocommerce-payments/dist/payment-request.js 12.2 kB -12 B (0%)
ℹ️ 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.81 kB
release/woocommerce-payments/dist/blocks-checkout.css 1.81 kB
release/woocommerce-payments/dist/checkout-rtl.css 318 B
release/woocommerce-payments/dist/checkout.css 319 B
release/woocommerce-payments/dist/index-rtl.css 37.2 kB
release/woocommerce-payments/dist/index.css 37.2 kB
release/woocommerce-payments/dist/index.js 290 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.05 kB
release/woocommerce-payments/dist/multi-currency-rtl.css 3.29 kB
release/woocommerce-payments/dist/multi-currency-switcher-block.js 59.4 kB
release/woocommerce-payments/dist/multi-currency.css 3.29 kB
release/woocommerce-payments/dist/multi-currency.js 54.8 kB
release/woocommerce-payments/dist/order-rtl.css 693 B
release/woocommerce-payments/dist/order.css 696 B
release/woocommerce-payments/dist/order.js 40.9 kB
release/woocommerce-payments/dist/payment-gateways-rtl.css 1.18 kB
release/woocommerce-payments/dist/payment-gateways.css 1.18 kB
release/woocommerce-payments/dist/payment-gateways.js 38.4 kB
release/woocommerce-payments/dist/payment-request-rtl.css 153 B
release/woocommerce-payments/dist/payment-request.css 153 B
release/woocommerce-payments/dist/product-details.js 919 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 19.4 kB
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 710 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 18.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 20.9 kB
release/woocommerce-payments/dist/woopay-direct-checkout.js 2.06 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 51.3 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 70.9 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.37 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.js 13.5 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.rtl.css 2.37 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.52 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

@tpaksu tpaksu self-assigned this Feb 8, 2024
@tpaksu tpaksu marked this pull request as ready for review February 8, 2024 13:42
@tpaksu tpaksu requested a review from a team February 8, 2024 13:43
Copy link
Contributor

@allie500 allie500 left a comment

Choose a reason for hiding this comment

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

Thanks for working on this fix, Taha! I've reviewed the code and manually tested it a bit and I've found an issue. I left inline comments in the three JS files where const fraudPreventionToken = window.wcpayFraudPreventionToken ?? null; is used. That is where the issue is.

If card_testing_prevention_enabled is enabled for the account and if the fraudPreventionToken token is null then the POST request returns a 400 response and the following error is returned on the checkout:

payment_data[2][value] is not of type string,boolean.

If null is replaced with false then the POST request returns a 402 response and we get the following:

We're not able to process this payment. Please refresh the page and try again.

I think this is the better outcome in a rare instance where the token could not be generated or retrieved. That way the shopper will hopefully at least refresh and retry the order and (also hopefully) the error that prevented the token from being generated or retrieved was just a one-off anomaly that won't occur a second time.

All that said, I ran into a bigger issue related to the null value. After testing null versus false with card_testing_prevention_enabled enabled, I then disabled card_testing_prevention_enabled and refreshed my account cache on the client. Testing with card_testing_prevention_enabled disabled then null is always passed as the value of fraudPreventionToken and the POST request always returns a 400 and the error:

payment_data[2][value] is not of type string,boolean.

The checkout is broken for shoppers when card_testing_prevention_enabled meta is disabled for the account as a result. However if false is passed instead the order is processed successfully.

const fraudPreventionToken = document
.querySelector( '#wcpay-fraud-prevention-token' )
?.getAttribute( 'value' );
const fraudPreventionToken = window.wcpayFraudPreventionToken ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should fallback to false here instead of null. I've included the reasoning in the main review comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this! It is being converted to an empty string when sending to the server. No changes needed on this one.

return document
.querySelector( '#wcpay-fraud-prevention-token' )
?.getAttribute( 'value' );
return window.wcpayFraudPreventionToken ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should fallback to false here instead of null. I've included the reasoning in the main review comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, this was the one that's causing 400 to be returned. Nice catch! Addressed in 008817f.

.querySelector( '#wcpay-fraud-prevention-token' )
?.getAttribute( 'value' );
const fraudPreventionToken =
window.wcpayFraudPreventionToken ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should fallback to false here instead of null. I've included the reasoning in the main review comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this! It is being converted to an empty string when sending to the server. No changes needed on this one too.

@allie500 allie500 self-requested a review February 12, 2024 13:42
Copy link
Contributor

@allie500 allie500 left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the issues that I reported earlier. The latest code changes look good. I've run through the testing and everything is working except for card payments on the twentytwentyfour theme with card_testing_prevention_enabled set to true. I also skipped testing Apple Pay as I don't have an iPhone. I think we can get a fellow team member to test that.

Testing Results:

Storefront theme with card testing prevention disabled:

Payment Method Result
Regular checkout using test card
Regular checkout using saved card
Regular checkout using one UPE method
ApplePay ⏭️
GooglePay
WooPay

Storefront theme with card testing prevention enabled:

Payment Method Result
Regular checkout using test card
Regular checkout using saved card
Regular checkout using one UPE method
ApplePay ⏭️
GooglePay
WooPay

TwentyTwentyFour theme with card testing prevention disabled:

Payment Method Result
Regular checkout using test card
Regular checkout using saved card
Regular checkout using one UPE method
ApplePay ⏭️
GooglePay
WooPay

TwentyTwentyFour theme with card testing prevention enabled:

Payment Method Result
Regular checkout using test card
Regular checkout using saved card
Regular checkout using one UPE method
ApplePay ⏭️
GooglePay
WooPay

Debugging TwentyTwentyFour theme with card testing prevention enabled:

POST https://my-test-site-domain/wp-json/wc/store/v1/checkout?_locale=user

Response: 402 (Payment Required)

Console error:
VM474:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Response body:
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Site has been temporarily disabled</title>
		<meta name="description" content="">
		<meta name="author" content="">

		<style>
		body {
			background-color: #f8f8f8;
			font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
			color: #333;
			padding-top: 20px;
		}
		div {
			display: block;
		}
		.container {
			width: 1100px;
			padding-top: 50px;
			margin-left: auto;
			margin-right: auto;
		}
		.error-msg, .support-msg {
			text-align: center;
		}
		.error-msg {
			margin-bottom: 40px;
		}
		.error-msg h1 {
			font-size: 52px;
			display: block;
			margin: 0px;
		}
		.error-msg p {
			font-size: 20px;
			display: block;
			margin: 10px 0;
		}
		.support-msg p {
			font-size: 14px;
			color: #888;
		}
		</style>
	</head>

	<body>
		<div class="container">
			<div class="error-msg">
				<p>This site has been disabled pending subscription renewal.</p>
			</div>
			<div class="support-msg">
				<p>Website owner? If you think you have reached this message in error, please contact support.</p>
			</div>
		</div>
	</body>
</html>

The console error and response body above seem to be a symptom and not the cause. This highjacked response is a known issue and there's an open PR to deal with it: #8102.

That said, I checked the POST request payload and the data looks good. I don't know why Stripe is returning it as a failed payment. In all tests I was using the 4242 card.

I'll keep digging on this on my end but wanted to share it now to get it on your radar as well.

@allie500 allie500 self-requested a review February 13, 2024 13:47
Copy link
Contributor

@allie500 allie500 left a comment

Choose a reason for hiding this comment

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

I've re-tested the card and saved card payment flows on the JN test site using the twentytwentyfour theme and the payments are now going through. I'm not sure why Stripe was returning a 402 response on those yesterday, but it seems to have been an intermittent issue. With those two tests passing, now all tests are passing. So I think we can ship this.

LGTM

@tpaksu tpaksu added this pull request to the merge queue Feb 13, 2024
Merged via the queue into develop with commit d4c8468 Feb 13, 2024
26 checks passed
@tpaksu tpaksu deleted the fix/8141-fraud-prevention-token-site-editor-problem branch February 13, 2024 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update how the fraud prevention token is added to the checkout page
3 participants