-
Notifications
You must be signed in to change notification settings - Fork 72
refactor: ECE buttons and settings consistency #11182
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
base: develop
Are you sure you want to change the base?
Changes from 15 commits
4dbf584
99d7397
4b8f863
7488f8a
fd0ca7f
5759d10
3c0c31d
1e28c15
7f8b80d
fd4be11
d4d1780
c0e776a
fb4d171
f405a88
b40c8e7
54d4f9d
7147008
61940e5
a1e0b3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: update | ||
|
|
||
| refactor: Google Pay/Apple Pay settings storage consistency |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,11 +49,11 @@ class WC_Payments_Apple_Pay_Registration { | |
| private $domain_name; | ||
|
|
||
| /** | ||
| * Stores Apple Pay domain verification issues. | ||
| * Option name for storing Apple Pay domain verification errors. | ||
| * | ||
| * @var string | ||
| */ | ||
| private $apple_pay_verify_notice; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the previous implementation, the error message was stored as an attribute on the class. I modified the implementation to store the error message in a new |
||
| const APPLE_PAY_DOMAIN_ERROR_OPTION = 'wcpay_apple_pay_domain_error'; | ||
|
|
||
| /** | ||
| * Initialize class actions. | ||
|
|
@@ -63,11 +63,10 @@ class WC_Payments_Apple_Pay_Registration { | |
| * @param WC_Payment_Gateway_WCPay $gateway WooCommerce Payments gateway. | ||
| */ | ||
| public function __construct( WC_Payments_API_Client $payments_api_client, WC_Payments_Account $account, WC_Payment_Gateway_WCPay $gateway ) { | ||
| $this->domain_name = wp_parse_url( get_site_url(), PHP_URL_HOST ); | ||
| $this->apple_pay_verify_notice = ''; | ||
| $this->payments_api_client = $payments_api_client; | ||
| $this->account = $account; | ||
| $this->gateway = $gateway; | ||
| $this->domain_name = wp_parse_url( get_site_url(), PHP_URL_HOST ); | ||
| $this->payments_api_client = $payments_api_client; | ||
| $this->account = $account; | ||
| $this->gateway = $gateway; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -88,30 +87,44 @@ public function init() { | |
| add_action( 'admin_init', [ $this, 'verify_domain_on_domain_name_change' ] ); | ||
|
|
||
| add_action( 'woocommerce_woocommerce_payments_admin_notices', [ $this, 'display_error_notice' ] ); | ||
| add_action( 'add_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 ); | ||
| add_action( 'update_option_woocommerce_woocommerce_payments_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 ); | ||
|
|
||
| // Listen to Apple Pay gateway settings changes for domain verification. | ||
| add_action( 'add_option_woocommerce_woocommerce_payments_apple_pay_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 ); | ||
| add_action( 'update_option_woocommerce_woocommerce_payments_apple_pay_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 ); | ||
| } | ||
|
|
||
| /** | ||
| * Whether the gateway and Express Checkout Buttons (prerequisites for Apple Pay) are enabled. | ||
| * Whether Apple Pay is enabled. | ||
| * | ||
| * Checks both the main gateway and the Apple Pay gateway are enabled. | ||
| * | ||
| * @return bool Whether Apple Pay required settings are enabled. | ||
| * @return bool Whether Apple Pay is enabled. | ||
| */ | ||
| private function is_enabled() { | ||
| return $this->gateway->is_enabled() && 'yes' === $this->gateway->get_option( 'payment_request' ); | ||
| // Check if the main gateway is enabled. | ||
| if ( ! $this->gateway->is_enabled() ) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the Apple Pay gateway is enabled. | ||
| $apple_pay_gateway = WC_Payments::get_payment_gateway_by_id( 'apple_pay' ); | ||
|
|
||
| if ( ! $apple_pay_gateway ) { | ||
| return false; | ||
| } | ||
|
|
||
| return $apple_pay_gateway->is_enabled(); | ||
| } | ||
|
|
||
| /** | ||
| * Whether the gateway and Express Checkout Buttons were enabled in previous settings. | ||
| * Whether Apple Pay was enabled in previous settings. | ||
| * | ||
| * @param array|null $prev_settings Gateway settings. | ||
| * @param array|null $prev_settings Apple Pay gateway settings. | ||
| * | ||
| * @return bool Whether Apple Pay required settings are enabled. | ||
| * @return bool Whether Apple Pay was enabled. | ||
| */ | ||
| private function was_enabled( $prev_settings ) { | ||
| $gateway_enabled = 'yes' === ( $prev_settings['enabled'] ?? 'no' ); | ||
| $payment_request_enabled = 'yes' === ( $prev_settings['payment_request'] ?? 'no' ); | ||
| return $gateway_enabled && $payment_request_enabled; | ||
| return 'yes' === ( $prev_settings['enabled'] ?? 'no' ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -153,6 +166,7 @@ public function register_domain() { | |
| if ( isset( $registration_response['id'] ) && ( isset( $registration_response['apple_pay']['status'] ) && 'active' === $registration_response['apple_pay']['status'] ) ) { | ||
| $this->gateway->update_option( 'apple_pay_verified_domain', $this->domain_name ); | ||
| $this->gateway->update_option( 'apple_pay_domain_set', 'yes' ); | ||
| delete_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION ); | ||
|
|
||
| Logger::log( __( 'Your domain has been verified with Apple Pay!', 'woocommerce-payments' ) ); | ||
| Tracker::track_admin( | ||
|
|
@@ -170,11 +184,10 @@ public function register_domain() { | |
| } catch ( API_Exception $e ) { | ||
| $error = $e->getMessage(); | ||
| } | ||
| // Display error message in notice. | ||
| $this->apple_pay_verify_notice = $error; | ||
|
|
||
| $this->gateway->update_option( 'apple_pay_verified_domain', $this->domain_name ); | ||
| $this->gateway->update_option( 'apple_pay_domain_set', 'no' ); | ||
| update_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION, $error ); | ||
|
|
||
| Logger::log( 'Error registering domain with Apple: ' . $error ); | ||
| Tracker::track_admin( | ||
|
|
@@ -192,7 +205,7 @@ public function register_domain() { | |
| */ | ||
| public function verify_domain_if_configured() { | ||
| // If Express Checkout Buttons are not enabled, | ||
| // do not attempt to register domain. | ||
| // do not attempt to register the domain. | ||
| if ( ! $this->is_enabled() ) { | ||
| return; | ||
| } | ||
|
|
@@ -232,29 +245,36 @@ public function display_error_notice() { | |
| return; | ||
| } | ||
|
|
||
| $empty_notice = empty( $this->apple_pay_verify_notice ); | ||
| $domain_set = $this->gateway->get_option( 'apple_pay_domain_set' ); | ||
| $error_notice = get_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION, '' ); | ||
| $empty_notice = empty( $error_notice ); | ||
|
|
||
| // Don't display error notice if verification notice is empty and | ||
| // apple_pay_domain_set option equals to '' or 'yes'. | ||
| if ( $empty_notice && 'no' !== $domain_set ) { | ||
| return; | ||
| } | ||
|
|
||
| // Clear the error after retrieving it so it only displays once. | ||
| if ( ! $empty_notice ) { | ||
| delete_option( self::APPLE_PAY_DOMAIN_ERROR_OPTION ); | ||
| } | ||
|
|
||
| /** | ||
| * Apple pay is enabled by default and domain verification initializes | ||
| * when setting screen is displayed. So if domain verification is not set, | ||
| * something went wrong so lets notify user. | ||
| */ | ||
| $allowed_html = [ | ||
| $allowed_html = [ | ||
| 'a' => [ | ||
| 'href' => [], | ||
| 'title' => [], | ||
| ], | ||
| ]; | ||
| $payment_request_button_text = __( 'Express checkouts:', 'woocommerce-payments' ); | ||
| $verification_failed_without_error = __( 'Apple Pay domain verification failed.', 'woocommerce-payments' ); | ||
| $verification_failed_with_error = __( 'Apple Pay domain verification failed with the following error:', 'woocommerce-payments' ); | ||
| $check_log_text = WC_Payments_Utils::esc_interpolated_html( | ||
| $verification_failed = $empty_notice | ||
| ? __( 'Apple Pay domain verification failed.', 'woocommerce-payments' ) | ||
| : __( 'Apple Pay domain verification failed with the following error:', 'woocommerce-payments' ); | ||
| $check_log_text = WC_Payments_Utils::esc_interpolated_html( | ||
| /* translators: a: Link to the logs page */ | ||
| __( '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' ), | ||
| [ | ||
|
|
@@ -271,20 +291,14 @@ public function display_error_notice() { | |
|
|
||
| ?> | ||
| <div class="notice notice-error apple-pay-message"> | ||
| <?php if ( $empty_notice ) : ?> | ||
| <p> | ||
| <strong><?php echo esc_html( $payment_request_button_text ); ?></strong> | ||
| <?php echo esc_html( $verification_failed_without_error ); ?> | ||
| <?php echo $learn_more_text; /* @codingStandardsIgnoreLine */ ?> | ||
| </p> | ||
| <?php else : ?> | ||
| <p> | ||
| <strong><?php echo esc_html( $payment_request_button_text ); ?></strong> | ||
| <?php echo esc_html( $verification_failed_with_error ); ?> | ||
| <?php echo $learn_more_text; /* @codingStandardsIgnoreLine */ ?> | ||
| </p> | ||
| <p><i><?php echo wp_kses( make_clickable( esc_html( $this->apple_pay_verify_notice ) ), $allowed_html ); ?></i></p> | ||
| <?php endif; ?> | ||
| <p> | ||
| <strong><?php esc_html_e( 'Express checkouts:', 'woocommerce-payments' ); ?></strong> | ||
| <?php echo esc_html( $verification_failed ); ?> | ||
| <?php echo $learn_more_text; /* @codingStandardsIgnoreLine */ ?> | ||
| </p> | ||
| <?php if ( ! $empty_notice ) : ?> | ||
| <p><i><?php echo wp_kses( make_clickable( esc_html( $error_notice ) ), $allowed_html ); ?></i></p> | ||
| <?php endif; ?> | ||
| <p><?php echo $check_log_text; /* @codingStandardsIgnoreLine */ ?></p> | ||
| </div> | ||
| <?php | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed - the
payment_requestsetting got deleted in favor of theenabledflag on the Google Pay/Apple Pay payment method definitions