Skip to content

Commit

Permalink
Fix/7.6.0 constructor injection upgrade bug (#8794)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshheald authored May 7, 2024
1 parent 5134e4c commit 1d69163
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
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() );
$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;
}

/**
* 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

0 comments on commit 1d69163

Please sign in to comment.