Skip to content

Commit

Permalink
Add settings object for every gateway (#8414)
Browse files Browse the repository at this point in the history
Co-authored-by: Timur Karimov <timurkarimov@timurs-macbook-pro.home>
  • Loading branch information
timur27 and Timur Karimov authored Mar 18, 2024
1 parent 86af532 commit 67e989e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-payment-methods-rendering
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Add settings object for every gateway
24 changes: 19 additions & 5 deletions includes/admin/class-wc-rest-payments-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 );
}
Expand Down
11 changes: 11 additions & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*
Expand Down
79 changes: 79 additions & 0 deletions includes/migrations/class-gateway-settings-sync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Class Gateway_Settings_Sync
*
* @package WooCommerce\Payments
*/

namespace WCPay\Migrations;

use WC_Payment_Gateway_WCPay;

defined( 'ABSPATH' ) || exit;

/**
* Class Delete_Active_WooPay_Webhook
*
* Aligns settings object for every gateway to support new approach of settings handling without the need of using the settings controller.
*/
class Gateway_Settings_Sync {


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

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

/**
* All registered gateways.
*
* @var array
*/
private $all_registered_gateways;

/**
* Gateway_Settings_Sync constructor.
*
* @param WC_Payment_Gateway_WCPay $main_gateway WCPay gateway.
* @param array $all_registered_gateways All registered gateways.
*/
public function __construct( WC_Payment_Gateway_WCPay $main_gateway, $all_registered_gateways ) {
$this->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 );
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 67e989e

Please sign in to comment.