diff --git a/changelog/migration-stripe-link-disablement-with-woopay-enabled b/changelog/migration-stripe-link-disablement-with-woopay-enabled new file mode 100644 index 00000000000..169afd3ee5e --- /dev/null +++ b/changelog/migration-stripe-link-disablement-with-woopay-enabled @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add migration script to cover situations with Link and WooPay both enabled after plugin update. diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index 2f73ad342e6..a0f14bd38aa 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -587,11 +587,13 @@ public static function init() { require_once __DIR__ . '/migrations/class-allowed-payment-request-button-sizes-update.php'; 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-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', [ '\WCPay\Migrations\Delete_Active_WooPay_Webhook', 'maybe_delete' ] ); include_once WCPAY_ABSPATH . '/includes/class-wc-payments-explicit-price-formatter.php'; @@ -737,14 +739,6 @@ public static function get_plugin_headers() { public static function register_gateway( $gateways ) { $payment_methods = array_keys( self::get_payment_method_map() ); - $key = array_search( 'link', $payment_methods, true ); - - if ( false !== $key && WC_Payments_Features::is_woopay_enabled() ) { - unset( $payment_methods[ $key ] ); - - self::get_gateway()->update_option( 'upe_enabled_payment_method_ids', $payment_methods ); - } - $gateways[] = self::$card_gateway; $all_gateways = []; $reusable_methods = []; diff --git a/includes/migrations/class-link-woopay-mutual-exclusion-handler.php b/includes/migrations/class-link-woopay-mutual-exclusion-handler.php new file mode 100644 index 00000000000..53284365adb --- /dev/null +++ b/includes/migrations/class-link-woopay-mutual-exclusion-handler.php @@ -0,0 +1,71 @@ +gateway = $gateway; + } + + /** + * Only execute the migration if not applied yet. + */ + public function maybe_migrate() { + $previous_version = get_option( 'woocommerce_woocommerce_payments_version' ); + if ( version_compare( self::VERSION_SINCE, $previous_version, '>' ) ) { + $this->migrate(); + } + } + + /** + * Does the actual migration as described in the class docblock. + */ + private function migrate() { + // check if both Stripe Link and WooPay are enabled and if so - disable Stripe Link. + $enabled_payment_methods = $this->gateway->get_payment_method_ids_enabled_at_checkout(); + $enabled_stripe_link_index = array_search( 'link', $enabled_payment_methods, true ); + + if ( false !== $enabled_stripe_link_index && WC_Payments_Features::is_woopay_enabled() ) { + unset( $enabled_payment_methods[ $enabled_stripe_link_index ] ); + + $this->gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods ); + } + } +}