diff --git a/changelog/fix-7.6.0-constructor-injection-upgrade-bug b/changelog/fix-7.6.0-constructor-injection-upgrade-bug new file mode 100644 index 00000000000..5cac66c7a73 --- /dev/null +++ b/changelog/fix-7.6.0-constructor-injection-upgrade-bug @@ -0,0 +1,5 @@ +Significance: patch +Type: fix +Comment: a known plugin upgrade issue fixed in the same release that the underlying code was released. + + diff --git a/includes/admin/class-wc-rest-payments-orders-controller.php b/includes/admin/class-wc-rest-payments-orders-controller.php index 10068abaf83..04c86f54197 100644 --- a/includes/admin/class-wc-rest-payments-orders-controller.php +++ b/includes/admin/class-wc-rest-payments-orders-controller.php @@ -47,13 +47,6 @@ class WC_REST_Payments_Orders_Controller extends WC_Payments_REST_Controller { */ private $order_service; - /** - * WC_Payments_Token instance for working with customer tokens - * - * @var WC_Payments_Token_Service - */ - private $token_service; - /** * WC_Payments_REST_Controller constructor. * @@ -61,14 +54,12 @@ class WC_REST_Payments_Orders_Controller extends WC_Payments_REST_Controller { * @param WC_Payment_Gateway_WCPay $gateway WooCommerce Payments payment gateway. * @param WC_Payments_Customer_Service $customer_service Customer class instance. * @param WC_Payments_Order_Service $order_service Order Service class instance. - * @param WC_Payments_Token_Service $token_service Token Service class instance. */ - public function __construct( WC_Payments_API_Client $api_client, WC_Payment_Gateway_WCPay $gateway, WC_Payments_Customer_Service $customer_service, WC_Payments_Order_Service $order_service, WC_Payments_Token_Service $token_service ) { + public function __construct( WC_Payments_API_Client $api_client, WC_Payment_Gateway_WCPay $gateway, WC_Payments_Customer_Service $customer_service, WC_Payments_Order_Service $order_service ) { parent::__construct( $api_client ); $this->gateway = $gateway; $this->customer_service = $customer_service; $this->order_service = $order_service; - $this->token_service = $token_service; } /** @@ -237,7 +228,7 @@ function_exists( 'wcs_get_subscriptions_for_order' ) && function_exists( 'wcs_is_manual_renewal_required' ) && wcs_order_contains_subscription( $order_id ); if ( $has_subscriptions ) { - $token = $this->token_service->add_payment_method_to_user( $generated_card, $order->get_user() ); + $token = WC_Payments::get_token_service()->add_payment_method_to_user( $generated_card, $order->get_user() ); $this->gateway->add_token_to_order( $order, $token ); foreach ( wcs_get_subscriptions_for_order( $order ) as $subscription ) { $subscription->set_payment_method( WC_Payment_Gateway_WCPay::GATEWAY_ID ); diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index e99fec6dad4..dc82d7a4304 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -992,7 +992,7 @@ public static function init_rest_api() { $conn_tokens_controller->register_routes(); include_once WCPAY_ABSPATH . 'includes/admin/class-wc-rest-payments-orders-controller.php'; - $orders_controller = new WC_REST_Payments_Orders_Controller( self::$api_client, self::get_gateway(), self::$customer_service, self::$order_service, self::$token_service ); + $orders_controller = new WC_REST_Payments_Orders_Controller( self::$api_client, self::get_gateway(), self::$customer_service, self::$order_service ); $orders_controller->register_routes(); include_once WCPAY_ABSPATH . 'includes/admin/class-wc-rest-payments-fraud-outcomes-controller.php'; @@ -1325,6 +1325,26 @@ public static function get_order_service(): WC_Payments_Order_Service { return self::$order_service; } + /** + * Returns the token service instance. + * + * @return WC_Payments_Token_Service + */ + public static function get_token_service(): WC_Payments_Token_Service { + return self::$token_service; + } + + /** + * Sets the token service instance. This is needed only for tests. + * + * @param WC_Payments_Token_Service $token_service Instance of WC_Payments_Token_Service. + * + * @return void + */ + public static function set_token_service( WC_Payments_Token_Service $token_service ) { + self::$token_service = $token_service; + } + /** * Sets the customer service instance. This is needed only for tests. * diff --git a/tests/unit/admin/test-class-wc-rest-payments-orders-controller.php b/tests/unit/admin/test-class-wc-rest-payments-orders-controller.php index c0aded3df5e..48c94e75869 100644 --- a/tests/unit/admin/test-class-wc-rest-payments-orders-controller.php +++ b/tests/unit/admin/test-class-wc-rest-payments-orders-controller.php @@ -49,6 +49,11 @@ class WC_REST_Payments_Orders_Controller_Test extends WCPAY_UnitTestCase { */ private $mock_token_service; + /** + * @var WC_Payments_Token_Service + */ + private $original_token_service; + /** * @var string */ @@ -79,15 +84,21 @@ public function set_up() { ->setMethods( [ 'attach_intent_info_to_order' ] ) ->getMock(); + $this->original_token_service = WC_Payments::get_token_service(); + WC_Payments::set_token_service( $this->mock_token_service ); $this->controller = new WC_REST_Payments_Orders_Controller( $this->mock_api_client, $this->mock_gateway, $this->mock_customer_service, $this->order_service, - $this->mock_token_service ); } + public function tear_down() { + WC_Payments::set_token_service( $this->original_token_service ); + parent::tear_down(); + } + public function test_capture_terminal_payment_success() { $order = $this->create_mock_order(); $mock_intent = WC_Helper_Intention::create_intention(