Skip to content

Commit

Permalink
Ensure Link disabling when WooPay is simultaneously enabled by default (
Browse files Browse the repository at this point in the history
#8327)

Co-authored-by: Timur Karimov <[email protected]>
  • Loading branch information
2 people authored and cesarcosta99 committed Mar 6, 2024
1 parent 6b8c0fd commit 61ac490
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add migration script to cover situations with Link and WooPay both enabled after plugin update.
10 changes: 2 additions & 8 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = [];
Expand Down
71 changes: 71 additions & 0 deletions includes/migrations/class-link-woopay-mutual-exclusion-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Class Link_WooPay_Mutual_Exclusion_Handler
*
* @package WooCommerce\Payments
*/

namespace WCPay\Migrations;

use WC_Payment_Gateway_WCPay;
use WC_Payments_Features;

defined( 'ABSPATH' ) || exit;

/**
* Class Link_WooPay_Mutual_Exclusion_Handler
*
* In version 7.3.0, the logic responsible for disabling Stripe Link if WooPay is by default enabled, is moved from the gateways registration step to the migration.
*
* @since 7.3.0
*/
class Link_WooPay_Mutual_Exclusion_Handler {

/**
* Version in which this migration was introduced.
*
* @var string
*/
const VERSION_SINCE = '7.3.0';

/**
* WCPay gateway.
*
* @var WC_Payment_Gateway_WCPay
*/
private $gateway;

/**
* Link_WooPay_Mutual_Exclusion_Handler constructor.
*
* @param WC_Payment_Gateway_WCPay $gateway WCPay gateway.
*/
public function __construct( WC_Payment_Gateway_WCPay $gateway ) {
$this->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 );
}
}
}

0 comments on commit 61ac490

Please sign in to comment.