88use WCPay \Database_Cache ;
99
1010/**
11- * QIT development tools for WooCommerce Payments E2E testing.
11+ * QIT development tools for WooPayments E2E testing.
1212 * Provides Jetpack connection setup for QIT environments.
1313 */
1414class WP_CLI_QIT_Dev_Command {
@@ -45,16 +45,22 @@ public function qit_setup( array $args, array $assoc_args ): void {
4545 $ blog_token = isset ( $ assoc_args ['blog_token ' ] ) ? (string ) $ assoc_args ['blog_token ' ] : '123.ABC.QIT ' ;
4646 $ user_token = isset ( $ assoc_args ['user_token ' ] ) ? (string ) $ assoc_args ['user_token ' ] : '123.ABC.QIT.1 ' ;
4747
48+ // Force test mode BEFORE any other operations (since this is a test account).
49+ $ this ->force_test_mode ();
50+
4851 // Set up Jetpack connection.
4952 $ this ->setup_jetpack_connection ( $ blog_id , $ blog_token , $ user_token );
5053
51- // Refresh account data to get real account info from server.
54+ // Enable dev mode (like WCP Dev Tools plugin does).
55+ $ this ->enable_dev_mode ();
56+
57+ // Refresh account data to get real account info from server (like regular E2E tests).
5258 if ( class_exists ( 'WC_Payments ' ) ) {
5359 $ this ->refresh_account_data ();
5460 }
5561
56- \WP_CLI ::success ( "QIT WCPay setup completed for blog ID {$ blog_id }" );
57- \WP_CLI ::line ( 'Account data will be fetched from server based on Jetpack connection. ' );
62+ \WP_CLI ::success ( "QIT WooPayments setup completed for blog ID {$ blog_id }" );
63+ \WP_CLI ::line ( 'Account data will be fetched from server based on Jetpack connection ' );
5864 }
5965
6066 /**
@@ -76,7 +82,42 @@ private function setup_jetpack_connection( int $blog_id, string $blog_token, str
7682 }
7783
7884 /**
79- * Refreshes WCPay account data from the server.
85+ * Enables WCP development mode like the WCP Dev Tools plugin.
86+ */
87+ private function enable_dev_mode (): void {
88+ // Enable dev mode like WCP Dev Tools plugin does.
89+ update_option ( 'wcpaydev_dev_mode ' , '1 ' );
90+
91+ // Add the dev mode filter like WCP Dev Tools plugin does.
92+ add_filter ( 'wcpay_dev_mode ' , '__return_true ' );
93+
94+ \WP_CLI ::log ( 'Enabled WCPay dev mode with filter ' );
95+ }
96+
97+ /**
98+ * Forces WCP test mode by setting filters and gateway settings.
99+ */
100+ private function force_test_mode (): void {
101+ // Force test mode onboarding and test mode since we're using a test account.
102+ add_filter ( 'wcpay_test_mode_onboarding ' , '__return_true ' );
103+ add_filter ( 'wcpay_test_mode ' , '__return_true ' );
104+
105+ // Also try setting the gateway settings to enable test mode.
106+ $ gateway_settings = get_option ( 'woocommerce_woocommerce_payments_settings ' , [] );
107+ $ gateway_settings ['test_mode ' ] = 'yes ' ;
108+ update_option ( 'woocommerce_woocommerce_payments_settings ' , $ gateway_settings );
109+
110+ // CRITICAL: Use WC_Payments_Onboarding_Service to set test mode (this sets test_mode_onboarding).
111+ if ( class_exists ( 'WC_Payments_Onboarding_Service ' ) ) {
112+ \WC_Payments_Onboarding_Service::set_test_mode ( true );
113+ \WP_CLI ::log ( 'Set WC_Payments_Onboarding_Service test mode - this enables test_mode_onboarding ' );
114+ }
115+
116+ \WP_CLI ::log ( 'Forced WCPay test mode for test account (filters + gateway settings + onboarding service) ' );
117+ }
118+
119+ /**
120+ * Refreshes account data from the WCP server and validates the connection.
80121 */
81122 private function refresh_account_data (): void {
82123 if ( ! class_exists ( 'WC_Payments ' ) ) {
@@ -85,20 +126,37 @@ private function refresh_account_data(): void {
85126 }
86127
87128 try {
88- \WC_Payments::get_account_service ()->refresh_account_data ();
89- \WP_CLI ::log ( 'Account data refreshed from server ' );
129+ $ account_service = \WC_Payments::get_account_service ();
130+ \WP_CLI ::log ( 'Attempting to refresh account data... ' );
131+
132+ $ result = $ account_service ->refresh_account_data ();
133+
134+ // Check if data was actually set.
135+ $ database_cache = \WC_Payments::get_database_cache ();
136+ $ account_data = $ database_cache ? $ database_cache ->get ( Database_Cache::ACCOUNT_KEY ) : null ;
137+
138+ if ( $ account_data ) {
139+ \WP_CLI ::log ( 'Account data refreshed successfully from server ' );
140+ // Verify key fields exist without exposing sensitive data.
141+ $ has_account_id = isset ( $ account_data ['account_id ' ] ) && ! empty ( $ account_data ['account_id ' ] );
142+ $ has_keys = isset ( $ account_data ['live_publishable_key ' ] ) || isset ( $ account_data ['test_publishable_key ' ] );
143+ $ status = $ account_data ['status ' ] ?? 'unknown ' ;
144+ \WP_CLI ::log ( 'Account validation: ID= ' . ( $ has_account_id ? 'present ' : 'missing ' ) . ', Keys= ' . ( $ has_keys ? 'present ' : 'missing ' ) . ', Status= ' . $ status );
145+ } else {
146+ \WP_CLI ::warning ( 'Account refresh completed but no account data cached - connection may be invalid ' );
147+ }
90148 } catch ( \Exception $ e ) {
91- \WP_CLI ::log ( 'Account refresh failed (expected in local dev) : ' . $ e ->getMessage () );
149+ \WP_CLI ::warning ( 'Account refresh failed: ' . $ e ->getMessage () );
92150 }
93151 }
94152
95153 /**
96- * Shows QIT WCPay connection status for debugging.
154+ * Shows QIT WooPayments connection status for debugging.
97155 *
98156 * @when after_wp_load
99157 */
100158 public function qit_status (): void {
101- \WP_CLI ::line ( '=== QIT WCPay Connection Status === ' );
159+ \WP_CLI ::line ( '=== QIT WooPayments Connection Status === ' );
102160
103161 if ( class_exists ( 'Jetpack_Options ' ) ) {
104162 $ blog_id = Jetpack_Options::get_option ( 'id ' );
@@ -113,6 +171,6 @@ public function qit_status(): void {
113171 }
114172 }
115173
116- \WP_CLI ::line ( 'Dev Mode: ' . ( get_option ( 'wcpay_dev_mode ' ) ? 'Enabled ' : 'Disabled ' ) );
174+ \WP_CLI ::line ( 'Dev Mode: ' . ( get_option ( 'wcpaydev_dev_mode ' ) ? 'Enabled ' : 'Disabled ' ) );
117175 }
118176}
0 commit comments