@@ -49,11 +49,11 @@ class WC_Payments_Apple_Pay_Registration {
4949 private $ domain_name ;
5050
5151 /**
52- * Stores Apple Pay domain verification issues .
52+ * Option name for storing Apple Pay domain verification errors .
5353 *
5454 * @var string
5555 */
56- private $ apple_pay_verify_notice ;
56+ const APPLE_PAY_DOMAIN_ERROR_OPTION = ' wcpay_apple_pay_domain_error ' ;
5757
5858 /**
5959 * Initialize class actions.
@@ -63,11 +63,10 @@ class WC_Payments_Apple_Pay_Registration {
6363 * @param WC_Payment_Gateway_WCPay $gateway WooCommerce Payments gateway.
6464 */
6565 public function __construct ( WC_Payments_API_Client $ payments_api_client , WC_Payments_Account $ account , WC_Payment_Gateway_WCPay $ gateway ) {
66- $ this ->domain_name = wp_parse_url ( get_site_url (), PHP_URL_HOST );
67- $ this ->apple_pay_verify_notice = '' ;
68- $ this ->payments_api_client = $ payments_api_client ;
69- $ this ->account = $ account ;
70- $ this ->gateway = $ gateway ;
66+ $ this ->domain_name = wp_parse_url ( get_site_url (), PHP_URL_HOST );
67+ $ this ->payments_api_client = $ payments_api_client ;
68+ $ this ->account = $ account ;
69+ $ this ->gateway = $ gateway ;
7170 }
7271
7372 /**
@@ -88,30 +87,47 @@ public function init() {
8887 add_action ( 'admin_init ' , [ $ this , 'verify_domain_on_domain_name_change ' ] );
8988
9089 add_action ( 'woocommerce_woocommerce_payments_admin_notices ' , [ $ this , 'display_error_notice ' ] );
91- add_action ( 'add_option_woocommerce_woocommerce_payments_settings ' , [ $ this , 'verify_domain_on_new_settings ' ], 10 , 2 );
92- add_action ( 'update_option_woocommerce_woocommerce_payments_settings ' , [ $ this , 'verify_domain_on_updated_settings ' ], 10 , 2 );
90+
91+ // Listen to Apple Pay gateway settings changes for domain verification.
92+ add_action ( 'add_option_woocommerce_woocommerce_payments_apple_pay_settings ' , [ $ this , 'verify_domain_on_new_settings ' ], 10 , 2 );
93+ add_action ( 'update_option_woocommerce_woocommerce_payments_apple_pay_settings ' , [ $ this , 'verify_domain_on_updated_settings ' ], 10 , 2 );
94+
95+ // Also listen to main gateway settings changes, since it's a prerequisite for Apple Pay.
96+ add_action ( 'update_option_woocommerce_woocommerce_payments_settings ' , [ $ this , 'verify_domain_on_updated_main_gateway_settings ' ], 10 , 2 );
9397 }
9498
9599 /**
96- * Whether the gateway and Express Checkout Buttons (prerequisites for Apple Pay) are enabled.
100+ * Whether Apple Pay is enabled.
101+ *
102+ * Checks both the main gateway and the Apple Pay gateway are enabled.
97103 *
98- * @return bool Whether Apple Pay required settings are enabled.
104+ * @return bool Whether Apple Pay is enabled.
99105 */
100106 private function is_enabled () {
101- return $ this ->gateway ->is_enabled () && 'yes ' === $ this ->gateway ->get_option ( 'payment_request ' );
107+ // Check if the main gateway is enabled.
108+ if ( ! $ this ->gateway ->is_enabled () ) {
109+ return false ;
110+ }
111+
112+ // Check if the Apple Pay gateway is enabled.
113+ $ apple_pay_gateway = WC_Payments::get_payment_gateway_by_id ( 'apple_pay ' );
114+
115+ if ( ! $ apple_pay_gateway ) {
116+ return false ;
117+ }
118+
119+ return $ apple_pay_gateway ->is_enabled ();
102120 }
103121
104122 /**
105- * Whether the gateway and Express Checkout Buttons were enabled in previous settings.
123+ * Whether Apple Pay was enabled in previous settings.
106124 *
107- * @param array|null $prev_settings Gateway settings.
125+ * @param array|null $prev_settings Apple Pay gateway settings.
108126 *
109- * @return bool Whether Apple Pay required settings are enabled.
127+ * @return bool Whether Apple Pay was enabled.
110128 */
111129 private function was_enabled ( $ prev_settings ) {
112- $ gateway_enabled = 'yes ' === ( $ prev_settings ['enabled ' ] ?? 'no ' );
113- $ payment_request_enabled = 'yes ' === ( $ prev_settings ['payment_request ' ] ?? 'no ' );
114- return $ gateway_enabled && $ payment_request_enabled ;
130+ return 'yes ' === ( $ prev_settings ['enabled ' ] ?? 'no ' );
115131 }
116132
117133 /**
@@ -153,6 +169,7 @@ public function register_domain() {
153169 if ( isset ( $ registration_response ['id ' ] ) && ( isset ( $ registration_response ['apple_pay ' ]['status ' ] ) && 'active ' === $ registration_response ['apple_pay ' ]['status ' ] ) ) {
154170 $ this ->gateway ->update_option ( 'apple_pay_verified_domain ' , $ this ->domain_name );
155171 $ this ->gateway ->update_option ( 'apple_pay_domain_set ' , 'yes ' );
172+ delete_option ( self ::APPLE_PAY_DOMAIN_ERROR_OPTION );
156173
157174 Logger::log ( __ ( 'Your domain has been verified with Apple Pay! ' , 'woocommerce-payments ' ) );
158175 Tracker::track_admin (
@@ -170,11 +187,10 @@ public function register_domain() {
170187 } catch ( API_Exception $ e ) {
171188 $ error = $ e ->getMessage ();
172189 }
173- // Display error message in notice.
174- $ this ->apple_pay_verify_notice = $ error ;
175190
176191 $ this ->gateway ->update_option ( 'apple_pay_verified_domain ' , $ this ->domain_name );
177192 $ this ->gateway ->update_option ( 'apple_pay_domain_set ' , 'no ' );
193+ update_option ( self ::APPLE_PAY_DOMAIN_ERROR_OPTION , $ error );
178194
179195 Logger::log ( 'Error registering domain with Apple: ' . $ error );
180196 Tracker::track_admin (
@@ -192,7 +208,7 @@ public function register_domain() {
192208 */
193209 public function verify_domain_if_configured () {
194210 // If Express Checkout Buttons are not enabled,
195- // do not attempt to register domain.
211+ // do not attempt to register the domain.
196212 if ( ! $ this ->is_enabled () ) {
197213 return ;
198214 }
@@ -218,12 +234,27 @@ public function verify_domain_on_new_settings( $option, $settings ) {
218234 * @param array $settings Settings after update.
219235 */
220236 public function verify_domain_on_updated_settings ( $ prev_settings , $ settings ) {
221- // If Gateway or Express Checkout Buttons weren 't enabled, then might need to verify now.
237+ // If Apple Pay wasn 't enabled, then might need to verify now.
222238 if ( ! $ this ->was_enabled ( $ prev_settings ) ) {
223239 $ this ->verify_domain_if_configured ();
224240 }
225241 }
226242
243+ /**
244+ * Conditionally process the Apple Pay domain verification after main gateway settings are updated.
245+ *
246+ * @param array $prev_settings Settings before update.
247+ * @param array $settings Settings after update.
248+ */
249+ public function verify_domain_on_updated_main_gateway_settings ( $ prev_settings , $ settings ) {
250+ $ was_main_gateway_enabled = 'yes ' === ( $ prev_settings ['enabled ' ] ?? 'no ' );
251+
252+ // If main gateway wasn't enabled before, might need to verify now.
253+ if ( ! $ was_main_gateway_enabled ) {
254+ $ this ->verify_domain_if_configured ();
255+ }
256+ }
257+
227258 /**
228259 * Display Apple Pay registration errors.
229260 */
@@ -232,29 +263,36 @@ public function display_error_notice() {
232263 return ;
233264 }
234265
235- $ empty_notice = empty ( $ this ->apple_pay_verify_notice );
236266 $ domain_set = $ this ->gateway ->get_option ( 'apple_pay_domain_set ' );
267+ $ error_notice = get_option ( self ::APPLE_PAY_DOMAIN_ERROR_OPTION , '' );
268+ $ empty_notice = empty ( $ error_notice );
269+
237270 // Don't display error notice if verification notice is empty and
238271 // apple_pay_domain_set option equals to '' or 'yes'.
239272 if ( $ empty_notice && 'no ' !== $ domain_set ) {
240273 return ;
241274 }
242275
276+ // Clear the error after retrieving it so it only displays once.
277+ if ( ! $ empty_notice ) {
278+ delete_option ( self ::APPLE_PAY_DOMAIN_ERROR_OPTION );
279+ }
280+
243281 /**
244282 * Apple pay is enabled by default and domain verification initializes
245283 * when setting screen is displayed. So if domain verification is not set,
246284 * something went wrong so lets notify user.
247285 */
248- $ allowed_html = [
286+ $ allowed_html = [
249287 'a ' => [
250288 'href ' => [],
251289 'title ' => [],
252290 ],
253291 ];
254- $ payment_request_button_text = __ ( ' Express checkouts: ' , ' woocommerce-payments ' );
255- $ verification_failed_without_error = __ ( 'Apple Pay domain verification failed. ' , 'woocommerce-payments ' );
256- $ verification_failed_with_error = __ ( 'Apple Pay domain verification failed with the following error: ' , 'woocommerce-payments ' );
257- $ check_log_text = WC_Payments_Utils::esc_interpolated_html (
292+ $ verification_failed = $ empty_notice
293+ ? __ ( 'Apple Pay domain verification failed. ' , 'woocommerce-payments ' )
294+ : __ ( 'Apple Pay domain verification failed with the following error: ' , 'woocommerce-payments ' );
295+ $ check_log_text = WC_Payments_Utils::esc_interpolated_html (
258296 /* translators: a: Link to the logs page */
259297 __ ( 'Please check the <a>logs</a> for more details on this issue. Debug log must be enabled under <strong>Advanced settings</strong> to see recorded logs. ' , 'woocommerce-payments ' ),
260298 [
@@ -271,20 +309,14 @@ public function display_error_notice() {
271309
272310 ?>
273311 <div class="notice notice-error apple-pay-message">
274- <?php if ( $ empty_notice ) : ?>
275- <p>
276- <strong><?php echo esc_html ( $ payment_request_button_text ); ?> </strong>
277- <?php echo esc_html ( $ verification_failed_without_error ); ?>
278- <?php echo $ learn_more_text ; /* @codingStandardsIgnoreLine */ ?>
279- </p>
280- <?php else : ?>
281- <p>
282- <strong><?php echo esc_html ( $ payment_request_button_text ); ?> </strong>
283- <?php echo esc_html ( $ verification_failed_with_error ); ?>
284- <?php echo $ learn_more_text ; /* @codingStandardsIgnoreLine */ ?>
285- </p>
286- <p><i><?php echo wp_kses ( make_clickable ( esc_html ( $ this ->apple_pay_verify_notice ) ), $ allowed_html ); ?> </i></p>
287- <?php endif ; ?>
312+ <p>
313+ <strong><?php esc_html_e ( 'Express checkouts: ' , 'woocommerce-payments ' ); ?> </strong>
314+ <?php echo esc_html ( $ verification_failed ); ?>
315+ <?php echo $ learn_more_text ; /* @codingStandardsIgnoreLine */ ?>
316+ </p>
317+ <?php if ( ! $ empty_notice ) : ?>
318+ <p><i><?php echo wp_kses ( make_clickable ( esc_html ( $ error_notice ) ), $ allowed_html ); ?> </i></p>
319+ <?php endif ; ?>
288320 <p><?php echo $ check_log_text ; /* @codingStandardsIgnoreLine */ ?> </p>
289321 </div>
290322 <?php
0 commit comments