Skip to content

SQUARE-355: Duplicate credit card field rendered on checkout in Chrome (classic checkout) - #571

Merged
iamdharmesh merged 8 commits into
trunkfrom
SQUARE-355
Nov 4, 2026
Merged

iamdharmesh merged 8 commits into
trunkfrom
SQUARE-355

Conversation

@faisal-alvi

@faisal-alvi faisal-alvi commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

All Submissions:

  • Does your code follow the WooCommerce Sniffs variant of WordPress coding standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Will this change require new documentation or changes to existing documentation?

Changes proposed in this Pull Request:

On the classic (shortcode) checkout, the Square credit card field can render twice. Shoppers then don't know which field to use, and the merchant who reported it has lost orders because of it. Despite the ticket title, this isn't specific to Chrome. It's a timing issue: any plugin that triggers a single update_checkout while the page loads can cause it, delivery-date pickers for example.

The card form is only recorded as "built" after Square's async card setup finishes. If another checkout update arrives during that window, the existing guard doesn't see a form yet, so it starts a second build, and both cards mount into the same container. A checkout update that lands while an existing form is being torn down can double-build the same way.

This PR makes the card form lifecycle safe against overlapping updates:

  • The card form is marked as building before any async work starts. A checkout update that arrives mid-build is queued and applied once when the build finishes, instead of starting a second build.
  • The card is only exposed once it has finished mounting.
  • When the form has to be rebuilt, the old form is released before it is destroyed, so a further update can't destroy it twice. The rebuild goes ahead even if destroying the old form fails, so the shopper still gets a card field.
  • Failures while building the form are caught and logged instead of ending up as unhandled promise rejections. A card that failed to mount is cleaned up before any queued update is applied.
  • New e2e tests (a94.credit-card-form-lifecycle.spec.js) cover a checkout update while the card is loading, and clicking Place order before the card is ready, then retrying.

Block checkout is not affected (it uses a separate code path).

Closes https://linear.app/a8c/issue/SQUARE-355/duplicate-credit-card-field-rendered-on-checkout-in-chrome-classic

Steps to test the changes in this Pull Request:

  1. On a store with Square credit card enabled, set up a classic checkout page).
  2. Add this snippet (Code Snippets or an mu-plugin). It does nothing unless the URL contains ?ted_mode=onupdate, and then fires one update_checkout from inside the first updated_checkout, like the delivery-date plugins do:
    add_action( 'wp_footer', function () {
    	if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || ! isset( $_GET['ted_mode'] ) ) {
    		return;
    	}
    	?>
    	<script>
    	jQuery( function ( $ ) {
    		$( document.body ).one( 'updated_checkout', function () {
    			$( document.body ).trigger( 'update_checkout' );
    		} );
    	} );
    	</script>
    	<?php
    }, 100 );
  3. On trunk, add a product to the cart and open the checkout with ?ted_mode=onupdate. Select the Square credit card method and run document.querySelectorAll('#wc-square-credit-card-container iframe.sq-card-component').length in the console. It returns 2 and two card fields are visible. Note that the race depends on the server answering the checkout update faster than Square builds the card. On a slow local site it may not show, in which case delay Square's requests (e.g. a Playwright route that holds squarecdn.com/squareupsandbox.com requests for ~1.5s).
  4. Switch to this branch and repeat step 3. It returns 1, with a single card field, every time.
  5. Load the checkout without the query param and confirm a single card field renders normally.
  6. Change the billing country and postcode a few times to force several checkout updates. After each update there is still exactly one card field and it accepts input.
  7. Place an order with the sandbox test card 4111 1111 1111 1111. The order should be placed with no errors.
  8. Reload with ?ted_mode=onupdate, select Square, and click Place order straight away, before the card field has appeared. A general payment error is shown, the form is not left blocked, and there are no console errors. Fill the card in and place the order. It succeeds.
  9. Check My Account > Add payment method shows a single card field and saves a card.
  10. Check the block checkout still renders the Square card field once and can place an order.
  11. Repeat steps 4 and 7 in Firefox, Safari and Edge, and on a mobile browser.

Changelog entry

Fix - Prevent the Square credit card field from rendering twice on the classic checkout when the checkout updates while the card form is loading.

@github-actions

Copy link
Copy Markdown

✅ WordPress Plugin Check Report

✅ Status: Passed

📊 Report

All checks passed! No errors or warnings found.


🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Payment-lifecycle timing changes need deterministic regression coverage and human review of cleanup serialization and compatibility.

2 open issues
What changed in this PR

Prevents duplicate Square card fields during overlapping classic-checkout updates. Block checkout remains unchanged.

Changes:

  • Serializes card setup and teardown, queuing overlapping refreshes.
  • Improves failed-build cleanup and stale-container handling.
  • Handles submission before the card field is ready.
File Description
src/​js/​frontend/​wc-square.js Adds lifecycle guards, queued refreshes, cleanup, and payment-readiness checks.

Review effort: Balanced

Comment thread src/js/frontend/wc-square.js Resolved
Comment thread src/js/frontend/wc-square.js Resolved

@iamdharmesh iamdharmesh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR @faisal-alvi. This looks great overall. Could you please check on 2 feedback added by Copilot? once those addressed this will be ready for the next steps.

@faisal-alvi

Copy link
Copy Markdown
Collaborator Author

@iamdharmesh Thanks. I've addressed both Copilot comments:

  • A failed card build is now cleaned up before the queued refresh runs.
  • I added e2e coverage in a94.credit-card-form-lifecycle.spec.js. It checks for a single card field when the checkout updates while the card is loading, and that clicking Place order before the card is ready shows an error and can be retried.

I also made the fix smaller. I dropped the extra Place order check, since trunk already handles that case and the check could have blocked saved-card payments.

Tested locally: trunk shows 2 card fields every time, this branch shows 1. Classic checkout, block checkout and changing the address all still place orders.

@qasumitbagthariya

Copy link
Copy Markdown
Collaborator

QA Update: Test Pass

Scenario trunk SQUARE-355
Credit card iframes, classic checkout 2 (4 of 4 runs) 1 (5 of 5 runs)
Plain checkout, no query param 1 1

Steps

✅ Steps 3–4: the duplicate card reproduces on trunk and is gone on the branch.
✅ Step 5: plain checkout shows a single card field.
✅ Step 6: five checkout updates (country and postcode) left exactly one card field each time, and it accepted input.
✅ Step 7: sandbox card 4111… placed order with no errors.
✅ Step 8: an early Place order click (0 card fields at click time) showed an error and did not leave the form blocked. The card then mounted, and placing the order with it worked.
✅ Step 9: Add payment method showed one card field and saved a card (tokenization enabled for the test).
✅ Step 10: block checkout showed one card field, and an order was placed.
✅ Step 11: passed in [browsers tested: Firefox / Safari ].


@faisal-alvi @iamdharmesh
New ticket
#581

Finding outside this PR’s scope: the Square Gift Card field also renders twice under the same trigger.

URL Gift card iframes, trunk Gift card iframes, SQUARE-355
/old-checkout/ 1 1
/old-checkout/?ted_mode=onupdate 2 (3 of 3 runs) 2
image

🖥 Testing Environment

Details
  • WordPress: 7.1.4
  • Theme: Storefront 4.6.2
  • Theme: Twenty Twenty-Five 1.4
  • WooCommerce - 11.2.0
  • PHP: 8.0.30
  • Web Server: Nginx 1.20.2
  • Browser: Chrome,safari,firefox
  • OS: macOS 15.3.2
  • Branch: SQUARE-355

@iamdharmesh iamdharmesh added this to the 5.5.3 milestone Nov 2, 2026
@qasumitbagthariya

Copy link
Copy Markdown
Collaborator

Regression / Smoke Test Report ✅

Tested with Archive File created via "php woorelease.phar build repo_URL" (Composer version 2.8.4, npm version 10.8.2, node version 20.18.1)

Status- Working expected with Plugin Archive/Zip file same as fix specific branch.

Testing Environment

Details
  • WordPress: 7.1.4
  • Theme: Twenty Twenty-Five 1.4
  • WooCommerce - 11.2.0
  • PHP: 8.0.30
  • Web Server: Nginx 1.20.2
  • Browser: Chrome
  • OS: macOS 15.3.2
  • Branch: smoke-testing

🔁 Next Step- Ready to Merge 🚀

@iamdharmesh
iamdharmesh marked this pull request as ready for review November 4, 2026 12:42
@iamdharmesh
iamdharmesh merged commit 5b38483 into trunk Nov 4, 2026
6 of 10 checks passed
@iamdharmesh
iamdharmesh deleted the SQUARE-355 branch November 4, 2026 12:43
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.

4 participants