Skip to content

Commit

Permalink
Add Compatibility Service (#7778)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepearson authored Dec 14, 2023
1 parent 7a7a05e commit b0fc45f
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog/update-4163-compatibility-service
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Adding Compatibility Service to assist with flagging possible compatibility issues in the future.
61 changes: 61 additions & 0 deletions includes/class-compatibility-service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Compatibility_Service class
*
* @package WooCommerce\Payments
*/

namespace WCPay;

use WC_Payments_API_Client;
use WCPay\Exceptions\API_Exception;

defined( 'ABSPATH' ) || exit; // block direct access.

/**
* Class to send compatibility data to the server.
*/
class Compatibility_Service {
/**
* Client for making requests to the WooCommerce Payments API
*
* @var WC_Payments_API_Client
*/
private $payments_api_client;

/**
* Constructor for Compatibility_Service.
*
* @param WC_Payments_API_Client $payments_api_client WooCommerce Payments API client.
*/
public function __construct( WC_Payments_API_Client $payments_api_client ) {
$this->payments_api_client = $payments_api_client;
}

/**
* Initializes this class's WP hooks.
*
* @return void
*/
public function init_hooks() {
add_action( 'woocommerce_payments_account_refreshed', [ $this, 'update_compatibility_data' ] );
}

/**
* Gets the data we need to confirm compatibility and sends it to the server.
*
* @return void
*/
public function update_compatibility_data() {
try {
$this->payments_api_client->update_compatibility_data(
[
'woopayments_version' => WCPAY_VERSION_NUMBER,
'woocommerce_version' => WC_VERSION,
]
);
} catch ( API_Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// The exception is already logged if logging is on, nothing else needed.
}
}
}
11 changes: 11 additions & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use WCPay\Internal\Service\OrderService;
use WCPay\WooPay\WooPay_Scheduler;
use WCPay\WooPay\WooPay_Session;
use WCPay\Compatibility_Service;

/**
* Main class for the WooPayments extension. Its responsibility is to initialize the extension.
Expand Down Expand Up @@ -295,6 +296,13 @@ class WC_Payments {
*/
private static $incentives_service;

/**
* Instance of Compatibility_Service, created in init function
*
* @var Compatibility_Service
*/
private static $compatibility_service;

/**
* Entry point to the initialization logic.
*/
Expand Down Expand Up @@ -463,6 +471,7 @@ public static function init() {
include_once __DIR__ . '/core/service/class-wc-payments-customer-service-api.php';
include_once __DIR__ . '/class-duplicate-payment-prevention-service.php';
include_once __DIR__ . '/class-wc-payments-incentives-service.php';
include_once __DIR__ . '/class-compatibility-service.php';
include_once __DIR__ . '/multi-currency/wc-payments-multi-currency.php';

self::$woopay_checkout_service = new Checkout_Service();
Expand Down Expand Up @@ -497,6 +506,7 @@ public static function init() {
self::$woopay_tracker = new WooPay_Tracker( self::get_wc_payments_http() );
self::$incentives_service = new WC_Payments_Incentives_Service( self::$database_cache );
self::$duplicate_payment_prevention_service = new Duplicate_Payment_Prevention_Service();
self::$compatibility_service = new Compatibility_Service( self::$api_client );

( new WooPay_Scheduler( self::$api_client ) )->init();

Expand All @@ -505,6 +515,7 @@ public static function init() {
self::$fraud_service->init_hooks();
self::$onboarding_service->init_hooks();
self::$incentives_service->init_hooks();
self::$compatibility_service->init_hooks();

self::$legacy_card_gateway = new CC_Payment_Gateway( self::$api_client, self::$account, self::$customer_service, self::$token_service, self::$action_scheduler_service, self::$failed_transaction_rate_limiter, self::$order_service, self::$duplicate_payment_prevention_service, self::$localization_service, self::$fraud_service );

Expand Down
1 change: 1 addition & 0 deletions includes/core/server/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ abstract class Request {
WC_Payments_API_Client::AUTHORIZATIONS_API => 'authorizations',
WC_Payments_API_Client::FRAUD_OUTCOMES_API => 'fraud_outcomes',
WC_Payments_API_Client::FRAUD_RULESET_API => 'fraud_ruleset',
WC_Payments_API_Client::COMPATIBILITY_API => 'compatibility',
];

/**
Expand Down
22 changes: 22 additions & 0 deletions includes/wc-payment-api/class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class WC_Payments_API_Client {
const FRAUD_SERVICES_API = 'accounts/fraud_services';
const FRAUD_OUTCOMES_API = 'fraud_outcomes';
const FRAUD_RULESET_API = 'fraud_ruleset';
const COMPATIBILITY_API = 'compatibility';

/**
* Common keys in API requests/responses that we might want to redact.
Expand Down Expand Up @@ -1703,6 +1704,27 @@ public function get_onboarding_po_eligible( array $business_info, array $store_i
);
}

/**
* Sends the compatibility data to the server to be saved to the account.
*
* @param array $compatibility_data The array containing the data.
*
* @return array HTTP response on success.
*
* @throws API_Exception - If not connected or request failed.
*/
public function update_compatibility_data( $compatibility_data ) {
$response = $this->request(
[
'compatibility_data' => $compatibility_data,
],
self::COMPATIBILITY_API,
self::POST
);

return $response;
}

/**
* Sends a request object.
*
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/test-class-compatibility-service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Class Compatibility_Service_Test
*
* @package WooCommerce\Payments\Tests
*/

use PHPUnit\Framework\MockObject\MockObject;
use WCPay\Compatibility_Service;

/**
* Unit tests related to the Compatibility_Service class.
*/
class Compatibility_Service_Test extends WCPAY_UnitTestCase {
/**
* Mock WC_Payments_API_Client.
*
* @var WC_Payments_API_Client|MockObject
*/
private $mock_api_client;

/**
* Compatibility_Service.
*
* @var Compatibility_Service
*/
private $compatibility_service;

/**
* Pre-test setup
*/
public function set_up() {
parent::set_up();

$this->mock_api_client = $this->createMock( WC_Payments_API_Client::class );
$this->compatibility_service = new Compatibility_Service( $this->mock_api_client );
$this->compatibility_service->init_hooks();
}

public function test_registers_woocommerce_filters_properly() {
$priority = has_filter( 'woocommerce_payments_account_refreshed', [ $this->compatibility_service, 'update_compatibility_data' ] );
$this->assertEquals( 10, $priority );
}

public function test_update_compatibility_data() {
// Arrange: Create the expected value to be passed to update_compatibility_data.
$expected = [
'woopayments_version' => WCPAY_VERSION_NUMBER,
'woocommerce_version' => WC_VERSION,
];

// Arrange/Assert: Set the expectations for update_compatibility_data.
$this->mock_api_client
->expects( $this->once() )
->method( 'update_compatibility_data' )
->with( $expected );

// Act: Call the method we're testing.
$this->compatibility_service->update_compatibility_data();
}
}
29 changes: 28 additions & 1 deletion tests/unit/wc-payment-api/test-class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ public function test_get_disputes_summary_success() {
$this->assertSame( 12, $disputes_summary['data']['count'] );
}

public function get_onboarding_po_eligible() {
public function test_get_onboarding_po_eligible() {
$this->set_http_mock_response(
200,
[
Expand Down Expand Up @@ -1203,6 +1203,33 @@ public function test_request_doesnt_retry_get_without_idempotency_header_on_netw
[ [], 'intentions', 'GET' ]
);
}

public function test_update_compatibility_data() {
// Arrange: Set expectation and return for remote_request.
$this->mock_http_client
->expects( $this->once() )
->method( 'remote_request' )
->willReturn(
[
'body' => wp_json_encode( [ 'result' => 'success' ] ),
'response' => [
'code' => 200,
'message' => 'OK',
],
]
);

// Act: Get the result of updating the data.
$result = $this->payments_api_client->update_compatibility_data(
[
'woocommerce_core_version' => WC_VERSION,
]
);

// Assert: Confirm we get the expected response.
$this->assertSame( 'success', $result['result'] );
}

/**
* Set up http mock response.
*
Expand Down

0 comments on commit b0fc45f

Please sign in to comment.