From ea433e309cf336e3f46d23288f9d565012535a4c Mon Sep 17 00:00:00 2001 From: Timur Karimov Date: Mon, 18 Mar 2024 22:47:51 +0100 Subject: [PATCH] Add settings object for every gateway (#8414) Co-authored-by: Timur Karimov --- changelog/fix-payment-methods-rendering | 4 + ...s-wc-rest-payments-settings-controller.php | 24 ++++-- includes/class-wc-payments.php | 11 +++ .../class-gateway-settings-sync.php | 79 +++++++++++++++++++ ...s-wc-rest-payments-settings-controller.php | 12 +-- 5 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 changelog/fix-payment-methods-rendering create mode 100644 includes/migrations/class-gateway-settings-sync.php diff --git a/changelog/fix-payment-methods-rendering b/changelog/fix-payment-methods-rendering new file mode 100644 index 00000000000..86b7c023aef --- /dev/null +++ b/changelog/fix-payment-methods-rendering @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Add settings object for every gateway diff --git a/includes/admin/class-wc-rest-payments-settings-controller.php b/includes/admin/class-wc-rest-payments-settings-controller.php index 55e56214142..f680f14ea32 100644 --- a/includes/admin/class-wc-rest-payments-settings-controller.php +++ b/includes/admin/class-wc-rest-payments-settings-controller.php @@ -606,11 +606,11 @@ function ( $payment_method ) use ( $available_payment_methods ) { ) ); - if ( function_exists( 'wc_admin_record_tracks_event' ) ) { - $active_payment_methods = $this->wcpay_gateway->get_upe_enabled_payment_method_ids(); - $disabled_payment_methods = array_diff( $active_payment_methods, $payment_method_ids_to_enable ); - $enabled_payment_methods = array_diff( $payment_method_ids_to_enable, $active_payment_methods ); + $active_payment_methods = $this->wcpay_gateway->get_upe_enabled_payment_method_ids(); + $disabled_payment_methods = array_diff( $active_payment_methods, $payment_method_ids_to_enable ); + $enabled_payment_methods = array_diff( $payment_method_ids_to_enable, $active_payment_methods ); + if ( function_exists( 'wc_admin_record_tracks_event' ) ) { foreach ( $disabled_payment_methods as $disabled_payment_method ) { wc_admin_record_tracks_event( Track_Events::PAYMENT_METHOD_DISABLED, @@ -630,7 +630,21 @@ function ( $payment_method ) use ( $available_payment_methods ) { } } - $this->wcpay_gateway->update_option( 'upe_enabled_payment_method_ids', $payment_method_ids_to_enable ); + foreach ( $enabled_payment_methods as $payment_method_id ) { + $gateway = WC_Payments::get_payment_gateway_by_id( $payment_method_id ); + $gateway->enable(); + } + + foreach ( $disabled_payment_methods as $payment_method_id ) { + $gateway = WC_Payments::get_payment_gateway_by_id( $payment_method_id ); + $gateway->disable(); + } + + // Keep the enabled payment method IDs list synchronized across gateway setting objects unless we remove this list with all dependencies. + foreach ( WC_Payments::get_payment_gateway_map() as $payment_gateway ) { + $payment_gateway->update_option( 'upe_enabled_payment_method_ids', $payment_method_ids_to_enable ); + } + if ( $payment_method_ids_to_enable ) { $this->request_unrequested_payment_methods( $payment_method_ids_to_enable ); } diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index 42d19928d3d..524e917b0ab 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -588,12 +588,14 @@ public static function init() { require_once __DIR__ . '/migrations/class-update-service-data-from-server.php'; require_once __DIR__ . '/migrations/class-additional-payment-methods-admin-notes-removal.php'; require_once __DIR__ . '/migrations/class-link-woopay-mutual-exclusion-handler.php'; + require_once __DIR__ . '/migrations/class-gateway-settings-sync.php'; require_once __DIR__ . '/migrations/class-delete-active-woopay-webhook.php'; add_action( 'woocommerce_woocommerce_payments_updated', [ new Allowed_Payment_Request_Button_Types_Update( self::get_gateway() ), 'maybe_migrate' ] ); add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Allowed_Payment_Request_Button_Sizes_Update( self::get_gateway() ), 'maybe_migrate' ] ); add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Update_Service_Data_From_Server( self::get_account_service() ), 'maybe_migrate' ] ); add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Additional_Payment_Methods_Admin_Notes_Removal(), 'maybe_migrate' ] ); add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Link_WooPay_Mutual_Exclusion_Handler( self::get_gateway() ), 'maybe_migrate' ] ); + add_action( 'woocommerce_woocommerce_payments_updated', [ new \WCPay\Migrations\Gateway_Settings_Sync( self::get_gateway(), self::get_payment_gateway_map() ), 'maybe_sync' ] ); add_action( 'woocommerce_woocommerce_payments_updated', [ '\WCPay\Migrations\Delete_Active_WooPay_Webhook', 'maybe_delete' ] ); include_once WCPAY_ABSPATH . '/includes/class-wc-payments-explicit-price-formatter.php'; @@ -1135,6 +1137,15 @@ public static function get_payment_method_map() { return self::$payment_method_map; } + /** + * Returns Payment Gateway map. + * + * @return array + */ + public static function get_payment_gateway_map() { + return self::$payment_gateway_map; + } + /** * Returns the WC_Payment_Gateway_WCPay instance * diff --git a/includes/migrations/class-gateway-settings-sync.php b/includes/migrations/class-gateway-settings-sync.php new file mode 100644 index 00000000000..5f1c32abcc6 --- /dev/null +++ b/includes/migrations/class-gateway-settings-sync.php @@ -0,0 +1,79 @@ +main_gateway = $main_gateway; + $this->all_registered_gateways = $all_registered_gateways; + } + + /** + * Checks whether we should trigger the event. + */ + public function maybe_sync() { + $previous_version = get_option( 'woocommerce_woocommerce_payments_version' ); + if ( version_compare( self::VERSION_SINCE, $previous_version, '>' ) ) { + $this->sync(); + } + } + + /** + * Deletes the active webhook. + */ + private function sync() { + $enabled_payment_methods = $this->main_gateway->get_option( 'upe_enabled_payment_method_ids', [] ); + + foreach ( $this->all_registered_gateways as $gateway ) { + if ( in_array( $gateway->get_stripe_id(), $enabled_payment_methods, true ) ) { + $gateway->enable(); + $gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods ); + } else { + $gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods ); + } + } + } +} diff --git a/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php b/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php index 0f8714c6b6b..7acffe22944 100644 --- a/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php +++ b/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php @@ -350,15 +350,15 @@ public function test_update_settings_returns_error_on_non_bool_is_wcpay_enabled_ $this->assertEquals( 400, $response->get_status() ); } - public function test_update_settings_saves_enabled_payment_methods() { - $this->gateway->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD ] ); + public function test_timur_testing() { + WC_Payments::get_gateway()->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD ] ); - $request = new WP_REST_Request(); - $request->set_param( 'enabled_payment_method_ids', [ Payment_Method::CARD, Payment_Method::GIROPAY ] ); + $request = new WP_REST_Request(); + $request->set_param( 'enabled_payment_method_ids', [ Payment_Method::CARD, Payment_Method::GIROPAY ] ); - $this->controller->update_settings( $request ); + $this->controller->update_settings( $request ); - $this->assertEquals( [ Payment_Method::CARD, Payment_Method::GIROPAY ], $this->gateway->get_option( 'upe_enabled_payment_method_ids' ) ); + $this->assertEquals( [ Payment_Method::CARD, Payment_Method::GIROPAY ], WC_Payments::get_gateway()->get_option( 'upe_enabled_payment_method_ids' ) ); } public function test_update_settings_fails_if_user_cannot_manage_woocommerce() {