Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/7.6.0 constructor injection upgrade bug #8794

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/fix-7.6.0-constructor-injection-upgrade-bug
Original file line number Diff line number Diff line change
@@ -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.


13 changes: 2 additions & 11 deletions includes/admin/class-wc-rest-payments-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,19 @@ 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.
*
* @param WC_Payments_API_Client $api_client WooCommerce Payments API client.
* @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;
}

/**
Expand Down Expand Up @@ -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() );
gpressutto5 marked this conversation as resolved.
Show resolved Hide resolved
$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 );
Expand Down
22 changes: 21 additions & 1 deletion includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
gpressutto5 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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(
Expand Down
Loading