chore: move ECE utilities into separate files#11389
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors Express Checkout Element (ECE) utilities by extracting functionality from a monolithic utils/index.ts file into separate, focused utility files. This improves code organization, maintainability, and makes dependencies clearer. The refactoring also introduces two new utility functions (shouldUseConfirmationTokens and createPaymentCredential) that consolidate previously inline logic.
Changes:
- Extracted utility functions into 8 separate focused files (express-checkout-data.ts, error-messages.ts, login-confirmation.ts, stripe-mode.ts, payment-credentials.ts, confirmation-tokens.ts, button-style-settings.ts, button-appearance.ts)
- Refactored payment credential creation logic in event-handlers.js to use the new
createPaymentCredentialutility function - Updated index.ts to re-export all utilities from their new locations
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| client/express-checkout/utils/express-checkout-data.ts | Contains data access utilities and type definitions for express checkout parameters (moved from index.ts) |
| client/express-checkout/utils/error-messages.ts | Extracts error message parsing utility (moved from index.ts) |
| client/express-checkout/utils/login-confirmation.ts | Contains login confirmation dialog logic with ExpressPaymentType type (moved from index.ts) |
| client/express-checkout/utils/stripe-mode.ts | Contains Stripe Elements mode determination logic (moved from index.ts) |
| client/express-checkout/utils/payment-credentials.ts | New utility that consolidates payment credential creation logic with unified error handling |
| client/express-checkout/utils/confirmation-tokens.ts | New utility that determines whether to use confirmation tokens |
| client/express-checkout/utils/button-style-settings.ts | Contains button style configuration logic (moved from index.ts) |
| client/express-checkout/utils/button-appearance.ts | Contains button appearance configuration logic (moved from index.ts) |
| client/express-checkout/utils/index.ts | Updated to re-export utilities from their new modular locations |
| client/express-checkout/event-handlers.js | Refactored to use new createPaymentCredential and shouldUseConfirmationTokens utilities |
| changelog/chore-utils-modularization | Adds changelog entry for this refactoring |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| export { getExpressCheckoutButtonStyleSettings } from './button-style-settings'; | ||
| export { getStripeElementsMode } from './stripe-mode'; | ||
| export { shouldUseConfirmationTokens } from './confirmation-tokens'; | ||
| export { createPaymentCredential } from './payment-credentials'; |
There was a problem hiding this comment.
For consistency with other exported types (ExpressPaymentType, ButtonAttributesType, WCPayExpressCheckoutParams), consider exporting the PaymentCredentialResult type from the index.ts file. This would make the type available for consumers who want to type-check code that uses createPaymentCredential.
| export { createPaymentCredential } from './payment-credentials'; | |
| export { createPaymentCredential } from './payment-credentials'; | |
| export type { PaymentCredentialResult } from './payment-credentials'; |
| export async function createPaymentCredential( | ||
| stripe: Stripe, | ||
| elements: StripeElements, | ||
| useConfirmationTokens: boolean | ||
| ): Promise< PaymentCredentialResult > { | ||
| if ( useConfirmationTokens ) { | ||
| const { | ||
| confirmationToken, | ||
| error, | ||
| } = await stripe.createConfirmationToken( { elements } ); | ||
| if ( error ) { | ||
| throw error; | ||
| } | ||
| return { id: confirmationToken!.id, type: 'confirmation_token' }; | ||
| } | ||
|
|
||
| const { paymentMethod, error } = await stripe.createPaymentMethod( { | ||
| elements, | ||
| } ); | ||
| if ( error ) { | ||
| throw error; | ||
| } | ||
| return { id: paymentMethod!.id, type: 'payment_method' }; | ||
| } |
There was a problem hiding this comment.
Consider adding unit tests for the newly extracted utility functions, especially createPaymentCredential, shouldUseConfirmationTokens, getStripeElementsMode, and displayLoginConfirmation. While the existing integration tests in event-handlers.test.js cover some of this functionality, dedicated unit tests would provide better isolation and make it easier to test edge cases.
Test the buildOption 1. Jetpack Beta
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:
Note: the build is updated when a new commit is pushed to this PR. |
|
Size Change: +707 B (0%) Total Size: 959 kB
ℹ️ View Unchanged
|
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Fixes #
Changes proposed in this Pull Request
Extracting some of the ECE utilities into separate files so that they can be imported more modularly.
Testing instructions
npm run changelogto add a changelog file, choosepatchto 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.Post merge