Skip to content

Commit

Permalink
misc: fix warning during plugin update due to problems with initializ…
Browse files Browse the repository at this point in the history
…ation (#8728)

Co-authored-by: Timur Karimov <[email protected]>
Co-authored-by: Timur Karimov <[email protected]>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent c9a8761 commit 3c6bba1
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 65 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-fatals-on-plugin-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Avoid warnings about fatal error during plugin update due to problems with plugin initialization.
27 changes: 7 additions & 20 deletions includes/admin/class-wc-rest-payments-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use WCPay\Constants\Country_Code;
use WCPay\Fraud_Prevention\Fraud_Risk_Tools;
use WCPay\Constants\Track_Events;
use WCPay\Duplicates_Detection_Service;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -37,34 +36,22 @@ class WC_REST_Payments_Settings_Controller extends WC_Payments_REST_Controller {
*/
protected $account;


/**
* Duplicates detection service.
*
* @var Duplicates_Detection_Service
*/
private $duplicates_detection_service;


/**
* WC_REST_Payments_Settings_Controller constructor.
*
* @param WC_Payments_API_Client $api_client WC_Payments_API_Client instance.
* @param WC_Payment_Gateway_WCPay $wcpay_gateway WC_Payment_Gateway_WCPay instance.
* @param WC_Payments_Account $account Account class instance.
* @param Duplicates_Detection_Service $duplicates_detection_service Duplicates detection service.
* @param WC_Payments_API_Client $api_client WC_Payments_API_Client instance.
* @param WC_Payment_Gateway_WCPay $wcpay_gateway WC_Payment_Gateway_WCPay instance.
* @param WC_Payments_Account $account Account class instance.
*/
public function __construct(
WC_Payments_API_Client $api_client,
WC_Payment_Gateway_WCPay $wcpay_gateway,
WC_Payments_Account $account,
Duplicates_Detection_Service $duplicates_detection_service
WC_Payments_Account $account
) {
parent::__construct( $api_client );

$this->wcpay_gateway = $wcpay_gateway;
$this->account = $account;
$this->duplicates_detection_service = $duplicates_detection_service;
$this->wcpay_gateway = $wcpay_gateway;
$this->account = $account;
}

/**
Expand Down Expand Up @@ -486,7 +473,7 @@ public function get_settings(): WP_REST_Response {
'enabled_payment_method_ids' => $enabled_payment_methods,
'available_payment_method_ids' => $available_upe_payment_methods,
'payment_method_statuses' => $this->wcpay_gateway->get_upe_enabled_payment_method_statuses(),
'duplicated_payment_method_ids' => $this->duplicates_detection_service->find_duplicates(),
'duplicated_payment_method_ids' => $this->wcpay_gateway->find_duplicates(),
'is_wcpay_enabled' => $this->wcpay_gateway->is_enabled(),
'is_manual_capture_enabled' => 'yes' === $this->wcpay_gateway->get_option( 'manual_capture' ),
'is_test_mode_enabled' => WC_Payments::mode()->is_test(),
Expand Down
1 change: 0 additions & 1 deletion includes/class-duplicates-detection-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
exit; // Exit if accessed directly.
}

use Exception;
use WC_Payments;
use WCPay\Payment_Methods\Affirm_Payment_Method;
use WCPay\Payment_Methods\Afterpay_Payment_Method;
Expand Down
42 changes: 31 additions & 11 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use WCPay\Core\Server\Request\List_Charge_Refunds;
use WCPay\Core\Server\Request\Refund_Charge;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Duplicates_Detection_Service;
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
use WCPay\Fraud_Prevention\Fraud_Risk_Tools;
use WCPay\Internal\Payment\State\AuthenticationRequiredState;
Expand Down Expand Up @@ -196,6 +197,13 @@ class WC_Payment_Gateway_WCPay extends WC_Payment_Gateway_CC {
*/
protected $duplicate_payment_prevention_service;

/**
* Duplicate payment methods detection service
*
* @var Duplicates_Detection_Service
*/
protected $duplicate_payment_methods_detection_service;

/**
* WC_Payments_Localization_Service instance.
*
Expand Down Expand Up @@ -246,6 +254,7 @@ class WC_Payment_Gateway_WCPay extends WC_Payment_Gateway_CC {
* @param Duplicate_Payment_Prevention_Service $duplicate_payment_prevention_service - Service for preventing duplicate payments.
* @param WC_Payments_Localization_Service $localization_service - Localization service instance.
* @param WC_Payments_Fraud_Service $fraud_service - Fraud service instance.
* @param Duplicates_Detection_Service $duplicate_payment_methods_detection_service - Service for finding duplicate enabled payment methods.
*/
public function __construct(
WC_Payments_API_Client $payments_api_client,
Expand All @@ -259,22 +268,24 @@ public function __construct(
WC_Payments_Order_Service $order_service,
Duplicate_Payment_Prevention_Service $duplicate_payment_prevention_service,
WC_Payments_Localization_Service $localization_service,
WC_Payments_Fraud_Service $fraud_service
WC_Payments_Fraud_Service $fraud_service,
Duplicates_Detection_Service $duplicate_payment_methods_detection_service
) {
$this->payment_methods = $payment_methods;
$this->payment_method = $payment_method;
$this->stripe_id = $payment_method->get_id();

$this->payments_api_client = $payments_api_client;
$this->account = $account;
$this->customer_service = $customer_service;
$this->token_service = $token_service;
$this->action_scheduler_service = $action_scheduler_service;
$this->failed_transaction_rate_limiter = $failed_transaction_rate_limiter;
$this->order_service = $order_service;
$this->duplicate_payment_prevention_service = $duplicate_payment_prevention_service;
$this->localization_service = $localization_service;
$this->fraud_service = $fraud_service;
$this->payments_api_client = $payments_api_client;
$this->account = $account;
$this->customer_service = $customer_service;
$this->token_service = $token_service;
$this->action_scheduler_service = $action_scheduler_service;
$this->failed_transaction_rate_limiter = $failed_transaction_rate_limiter;
$this->order_service = $order_service;
$this->duplicate_payment_prevention_service = $duplicate_payment_prevention_service;
$this->localization_service = $localization_service;
$this->fraud_service = $fraud_service;
$this->duplicate_payment_methods_detection_service = $duplicate_payment_methods_detection_service;

$this->id = static::GATEWAY_ID;
$this->icon = $this->get_theme_icon();
Expand Down Expand Up @@ -4346,6 +4357,15 @@ public function get_method_description() {
return $description;
}

/**
* Calls duplicate payment methods detection service to find duplicates.
* This method acts as a wrapper. The approach should be reverted once
* https://github.com/Automattic/woocommerce-payments/issues/7464 is resolved.
*/
public function find_duplicates() {
return $this->duplicate_payment_methods_detection_service->find_duplicates();
}

// Start: Deprecated functions.

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public static function init() {
foreach ( $payment_methods as $payment_method ) {
self::$payment_method_map[ $payment_method->get_id() ] = $payment_method;

$split_gateway = new WC_Payment_Gateway_WCPay( self::$api_client, self::$account, self::$customer_service, self::$token_service, self::$action_scheduler_service, $payment_method, $payment_methods, self::$failed_transaction_rate_limiter, self::$order_service, self::$duplicate_payment_prevention_service, self::$localization_service, self::$fraud_service );
$split_gateway = new WC_Payment_Gateway_WCPay( self::$api_client, self::$account, self::$customer_service, self::$token_service, self::$action_scheduler_service, $payment_method, $payment_methods, self::$failed_transaction_rate_limiter, self::$order_service, self::$duplicate_payment_prevention_service, self::$localization_service, self::$fraud_service, self::$duplicates_detection_service );

// Card gateway hooks are registered once below.
if ( 'card' !== $payment_method->get_id() ) {
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public static function init_rest_api() {
$reporting_controller->register_routes();

include_once WCPAY_ABSPATH . 'includes/admin/class-wc-rest-payments-settings-controller.php';
$settings_controller = new WC_REST_Payments_Settings_Controller( self::$api_client, self::get_gateway(), self::$account, self::$duplicates_detection_service );
$settings_controller = new WC_REST_Payments_Settings_Controller( self::$api_client, self::get_gateway(), self::$account );
$settings_controller->register_routes();

include_once WCPAY_ABSPATH . 'includes/admin/class-wc-rest-payments-reader-controller.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ public function set_up() {
$order_service,
$mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service
$this->mock_fraud_service,
$this->mock_duplicates_detection_service
);
$this->controller = new WC_REST_Payments_Settings_Controller( $this->mock_api_client, $this->gateway, $this->mock_wcpay_account, $this->mock_duplicates_detection_service );
$this->controller = new WC_REST_Payments_Settings_Controller( $this->mock_api_client, $this->gateway, $this->mock_wcpay_account );

$this->mock_api_client
->method( 'is_server_connected' )
Expand Down
25 changes: 14 additions & 11 deletions tests/unit/admin/test-class-wc-rest-payments-tos-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WCPay\Core\Server\Request\Add_Account_Tos_Agreement;
use WCPay\Database_Cache;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Duplicates_Detection_Service;
use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;

Expand Down Expand Up @@ -55,16 +56,17 @@ public function set_up() {
->disableOriginalConstructor()
->getMock();

$mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
$mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
$mock_db_cache = $this->createMock( Database_Cache::class );
$mock_session_service = $this->createMock( WC_Payments_Session_Service::class );
$customer_service = new WC_Payments_Customer_Service( $mock_api_client, $mock_wcpay_account, $mock_db_cache, $mock_session_service );
$token_service = new WC_Payments_Token_Service( $mock_api_client, $customer_service );
$order_service = new WC_Payments_Order_Service( $this->createMock( WC_Payments_API_Client::class ) );
$action_scheduler_service = new WC_Payments_Action_Scheduler_Service( $mock_api_client, $order_service );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
$mock_payment_method = $this->createMock( CC_Payment_Method::class );
$mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
$mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
$mock_db_cache = $this->createMock( Database_Cache::class );
$mock_session_service = $this->createMock( WC_Payments_Session_Service::class );
$customer_service = new WC_Payments_Customer_Service( $mock_api_client, $mock_wcpay_account, $mock_db_cache, $mock_session_service );
$token_service = new WC_Payments_Token_Service( $mock_api_client, $customer_service );
$order_service = new WC_Payments_Order_Service( $this->createMock( WC_Payments_API_Client::class ) );
$action_scheduler_service = new WC_Payments_Action_Scheduler_Service( $mock_api_client, $order_service );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
$mock_payment_method = $this->createMock( CC_Payment_Method::class );
$mock_duplicates_detection_service = $this->createMock( Duplicates_Detection_Service::class );

$this->gateway = new WC_Payment_Gateway_WCPay(
$mock_api_client,
Expand All @@ -78,7 +80,8 @@ public function set_up() {
$order_service,
$mock_dpps,
$this->createMock( WC_Payments_Localization_Service::class ),
$mock_fraud_service
$mock_fraud_service,
$mock_duplicates_detection_service
);
$this->controller = new WC_REST_Payments_Tos_Controller( $mock_api_client, $this->gateway, $mock_wcpay_account );

Expand Down
17 changes: 14 additions & 3 deletions tests/unit/payment-methods/test-class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use WC_Payments_Localization_Service;
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
use WCPay\Database_Cache;
use WCPay\Duplicates_Detection_Service;
use WCPay\Internal\Service\Level3Service;
use WCPay\Internal\Service\OrderService;

Expand Down Expand Up @@ -170,6 +171,13 @@ class UPE_Payment_Gateway_Test extends WCPAY_UnitTestCase {
*/
private $mock_fraud_service;

/**
* Mock Duplicates Detection Service.
*
* @var Duplicates_Detection_Service
*/
private $mock_duplicates_detection_service;

/**
* Pre-test setup
*/
Expand Down Expand Up @@ -230,8 +238,9 @@ public function set_up() {

$this->mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );

$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
$this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
$this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
$this->mock_duplicates_detection_service = $this->createMock( Duplicates_Detection_Service::class );

$this->mock_payment_methods = [];
$payment_method_classes = [
Expand Down Expand Up @@ -294,6 +303,7 @@ public function set_up() {
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
]
)
->setMethods(
Expand Down Expand Up @@ -961,7 +971,8 @@ public function test_get_upe_available_payment_methods( $payment_methods, $expec
$this->mock_order_service,
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service
$this->mock_fraud_service,
$this->mock_duplicates_detection_service
);

$this->assertEquals( $expected_result, $gateway->get_upe_available_payment_methods() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use WC_Payments_Localization_Service;
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
use WCPay\Database_Cache;
use WCPay\Duplicates_Detection_Service;
use WCPay\Internal\Service\Level3Service;
use WCPay\Internal\Service\OrderService;
/**
Expand Down Expand Up @@ -158,6 +159,13 @@ class UPE_Split_Payment_Gateway_Test extends WCPAY_UnitTestCase {
*/
private $mock_fraud_service;

/**
* Mock Duplicates Detection Service.
*
* @var Duplicates_Detection_Service
*/
private $mock_duplicates_detection_service;

/**
* Mapping for payment ID to payment method.
*
Expand Down Expand Up @@ -247,8 +255,9 @@ public function set_up() {

$this->mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );

$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
$this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
$this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
$this->mock_duplicates_detection_service = $this->createMock( Duplicates_Detection_Service::class );

// Arrange: Define a $_POST array which includes the payment method,
// so that get_payment_method_from_request() does not throw error.
Expand Down Expand Up @@ -281,6 +290,7 @@ public function set_up() {
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
]
)
->setMethods(
Expand Down Expand Up @@ -1060,6 +1070,7 @@ public function test_get_payment_methods_with_request_context() {
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
]
)
->setMethods( [ 'get_payment_methods_from_gateway_id' ] )
Expand Down Expand Up @@ -1105,6 +1116,7 @@ public function test_get_payment_methods_without_request_context() {
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
]
)
->setMethods( [ 'get_payment_methods_from_gateway_id' ] )
Expand Down Expand Up @@ -1149,6 +1161,7 @@ public function test_get_payment_methods_without_request_context_or_token() {
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
]
)
->setMethods(
Expand Down Expand Up @@ -1202,6 +1215,7 @@ public function test_get_payment_methods_from_gateway_id_upe() {
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
]
)
->onlyMethods(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
use WCPay\Constants\Payment_Method;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Duplicates_Detection_Service;
use WCPay\Session_Rate_Limiter;
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
use WCPay\Payment_Methods\CC_Payment_Method;
Expand Down Expand Up @@ -153,6 +154,7 @@ public function set_up() {
$mock_dpps,
$this->createMock( WC_Payments_Localization_Service::class ),
$this->createMock( WC_Payments_Fraud_Service::class ),
$this->createMock( Duplicates_Detection_Service::class ),
]
)
->setMethods(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use WCPay\Exceptions\Connection_Exception;
use WCPay\Session_Rate_Limiter;
use WCPay\Constants\Payment_Method;
use WCPay\Duplicates_Detection_Service;
use WCPay\Payment_Methods\CC_Payment_Method;

// Need to use WC_Mock_Data_Store.
Expand Down Expand Up @@ -167,6 +168,7 @@ public function set_up() {
$this->mock_dpps,
$this->createMock( WC_Payments_Localization_Service::class ),
$this->createMock( WC_Payments_Fraud_Service::class ),
$this->createMock( Duplicates_Detection_Service::class ),
]
)
->setMethods(
Expand Down
Loading

0 comments on commit 3c6bba1

Please sign in to comment.