From 62869ac4db9145b7ecd8ad6c031042c8263ce951 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:56:31 +0100 Subject: [PATCH 1/7] feat: add gather cms data --- src/composer.json | 2 +- src/includes/AlmaSettings.php | 53 +++- src/includes/Gateways/AlmaPaymentGateway.php | 46 ++-- src/includes/Helpers/ConstantsHelper.php | 2 + src/includes/Helpers/PaymentHelper.php | 4 +- src/includes/Helpers/PluginHelper.php | 29 ++- src/includes/Helpers/SecurityHelper.php | 20 ++ .../Services/CollectCmsDataService.php | 227 ++++++++++++++++++ .../IntegrationConfigurationUrlService.php | 81 +++++++ src/includes/WcProxy/FunctionsProxy.php | 27 +++ src/includes/WcProxy/OptionProxy.php | 28 +++ src/includes/WcProxy/ThemeProxy.php | 44 ++++ src/tests/Helpers/PaymentHelperTest.php | 65 ----- src/tests/Helpers/SecurityHelperTest.php | 21 +- .../Services/CollectCmsDataServiceTest.php | 156 ++++++++++++ ...IntegrationConfigurationUrlServiceTest.php | 38 +++ 16 files changed, 741 insertions(+), 102 deletions(-) create mode 100644 src/includes/Services/CollectCmsDataService.php create mode 100644 src/includes/Services/IntegrationConfigurationUrlService.php create mode 100644 src/includes/WcProxy/FunctionsProxy.php create mode 100644 src/includes/WcProxy/OptionProxy.php create mode 100644 src/includes/WcProxy/ThemeProxy.php delete mode 100644 src/tests/Helpers/PaymentHelperTest.php create mode 100644 src/tests/Services/CollectCmsDataServiceTest.php create mode 100644 src/tests/Services/IntegrationConfigurationUrlServiceTest.php diff --git a/src/composer.json b/src/composer.json index 15c264d6..9458bd52 100644 --- a/src/composer.json +++ b/src/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1", - "alma/alma-php-client": ">=2.2.0", + "alma/alma-php-client": ">=2.3.0", "ext-openssl": "*" }, "require-dev": { diff --git a/src/includes/AlmaSettings.php b/src/includes/AlmaSettings.php index 18624d45..58d0b775 100644 --- a/src/includes/AlmaSettings.php +++ b/src/includes/AlmaSettings.php @@ -52,12 +52,8 @@ * @property string live_api_key Live api key * @property string test_api_key Test api key * @property string enabled Wp-bool-eq (yes or no) - * @property string debug Wp-bool-eq (yes or no) - * @property string display_product_eligibility Wp-bool-eq (yes or no) - * @property string display_cart_eligibility Wp-bool-eq (yes or no) * @property string environment Live or test * @property bool keys_validity Flag to indicate id the current keys are working - * @property string selected_fee_plan Admin dashboard fee_plan in edition mode. * @property string test_merchant_id Alma TEST merchant ID * @property string test_merchant_name Alma TEST merchant name * @property string live_merchant_id Alma LIVE merchant ID @@ -65,18 +61,57 @@ * @property string variable_product_price_query_selector Css query selector * @property string variable_product_sale_price_query_selector Css query selector for variable discounted products * @property string variable_product_check_variations_event JS event for product variation change - * @property array excluded_products_list Wp Categories excluded slug's list * @property string share_of_checkout_enabled Bool for share of checkout acceptance (yes or no) * @property string share_of_checkout_enabled_date String Date when the merchant did accept the share of checkout * @property string share_of_checkout_last_sharing_date String Date when we sent the data to Alma - * @property bool display_in_page Bool if In Page is activated * @property bool use_blocks_template Bool if we want to use a blocks template */ class AlmaSettings { - const OPTIONS_KEY = 'wc_alma_settings'; // Generated by WooCommerce in WC_Settings_API::get_option_key(). + /** + * Wp-bool-eq (yes or no). + * + * @var string + */ + public $debug; + + /** + * Wp-bool-eq (yes or no). + * + * @var string + */ + public $display_cart_eligibility; + + /** + * Bool if In Page is activated. + * + * @var bool + */ + public $display_in_page; + + /** + * Wp-bool-eq (yes or no) + * + * @var string + */ + public $display_product_eligibility; + + /** + * Wp Categories excluded slug's list + * + * @var array + */ + public $excluded_products_list; + + /** + * Admin dashboard fee_plan in edition mode. + * + * @var string + */ + public $selected_fee_plan; + /** * Setting values from get_option. * @@ -92,7 +127,7 @@ class AlmaSettings { public $allowed_fee_plans = array(); /** - * The logger. + * The logger. * * @var AlmaLogger */ @@ -300,7 +335,7 @@ public function get_enabled_plans_definitions() { * * @return bool */ - protected function is_plan_enabled( $key ) { + public function is_plan_enabled( $key ) { return 'yes' === $this->__get( "enabled_$key" ); } diff --git a/src/includes/Gateways/AlmaPaymentGateway.php b/src/includes/Gateways/AlmaPaymentGateway.php index 38856bc1..20060457 100644 --- a/src/includes/Gateways/AlmaPaymentGateway.php +++ b/src/includes/Gateways/AlmaPaymentGateway.php @@ -43,6 +43,7 @@ use Alma\Woocommerce\Helpers\SettingsHelper; use Alma\Woocommerce\Helpers\TemplateLoaderHelper; use Alma\Woocommerce\Helpers\ToolsHelper; +use Alma\Woocommerce\Services\IntegrationConfigurationUrlService; /** * AlmaPaymentGateway @@ -192,6 +193,13 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { */ protected $alma_plan_helper; + /** + * The integration configuration url service. + * + * @var IntegrationConfigurationUrlService + */ + protected $integration_configuration_url_service; + /** * Construct. * @@ -199,24 +207,25 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { * @throws NoCredentialsException The exception. */ public function __construct( $check_basics = true ) { - $this->logger = new AlmaLogger(); - $this->alma_settings = new AlmaSettings(); - $this->check_legal_helper = new CheckLegalHelper(); - $this->checkout_helper = new CheckoutHelper(); - $gateway_helper_builder = new GatewayHelperBuilder(); - $this->gateway_helper = $gateway_helper_builder->get_instance(); - $this->scripts_helper = new AssetsHelper(); - $this->encryption_helper = new EncryptorHelper(); - $tools_helper_builder = new ToolsHelperBuilder(); - $this->tool_helper = $tools_helper_builder->get_instance(); - $this->alma_payment_helper = new PaymentHelper(); - $this->order_helper = new OrderHelper(); - $this->template_loader = new TemplateLoaderHelper(); - $this->soc_helper = new ShareOfCheckoutHelper(); - $this->plugin_helper = new PluginHelper(); - $this->asset_helper = new AssetsHelper(); - $alma_plan_builder = new PlanHelperBuilder(); - $this->alma_plan_helper = $alma_plan_builder->get_instance(); + $this->logger = new AlmaLogger(); + $this->alma_settings = new AlmaSettings(); + $this->check_legal_helper = new CheckLegalHelper(); + $this->checkout_helper = new CheckoutHelper(); + $gateway_helper_builder = new GatewayHelperBuilder(); + $this->gateway_helper = $gateway_helper_builder->get_instance(); + $this->scripts_helper = new AssetsHelper(); + $this->encryption_helper = new EncryptorHelper(); + $tools_helper_builder = new ToolsHelperBuilder(); + $this->tool_helper = $tools_helper_builder->get_instance(); + $this->alma_payment_helper = new PaymentHelper(); + $this->order_helper = new OrderHelper(); + $this->template_loader = new TemplateLoaderHelper(); + $this->soc_helper = new ShareOfCheckoutHelper(); + $this->plugin_helper = new PluginHelper(); + $this->asset_helper = new AssetsHelper(); + $alma_plan_builder = new PlanHelperBuilder(); + $this->alma_plan_helper = $alma_plan_builder->get_instance(); + $this->integration_configuration_url_service = new IntegrationConfigurationUrlService( $this->alma_settings, $this->tool_helper, $this->logger ); $this->plugin_factory = new PluginFactory(); $this->cart_factory = new CartFactory(); @@ -420,6 +429,7 @@ public function manage_credentials() { } $this->alma_settings->init_allowed_fee_plans(); + $this->integration_configuration_url_service->send(); // Save of the fee plans. update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->alma_settings->settings ), 'yes' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound diff --git a/src/includes/Helpers/ConstantsHelper.php b/src/includes/Helpers/ConstantsHelper.php index ab0bd92a..3960046d 100644 --- a/src/includes/Helpers/ConstantsHelper.php +++ b/src/includes/Helpers/ConstantsHelper.php @@ -73,6 +73,8 @@ class ConstantsHelper { const ERROR = 'error'; + const COLLECT_URL = 'alma_collect_data_url'; + /** * The payment status * diff --git a/src/includes/Helpers/PaymentHelper.php b/src/includes/Helpers/PaymentHelper.php index 514179bd..a921d145 100644 --- a/src/includes/Helpers/PaymentHelper.php +++ b/src/includes/Helpers/PaymentHelper.php @@ -141,7 +141,7 @@ public function handle_ipn_callback() { if ( ! array_key_exists( 'HTTP_X_ALMA_SIGNATURE', $_SERVER ) ) { $this->logger->error( 'Header key X-Alma-Signature doesn\'t exist' ); - wp_send_json( array( 'error' => 'Header key X-Alma-Signature doesn\'t exist' ), 500 ); + wp_send_json( array( 'error' => 'Header key X-Alma-Signature doesn\'t exist' ), 403 ); } try { @@ -153,7 +153,7 @@ public function handle_ipn_callback() { $this->logger->info( '[ALMA] IPN signature is valid' ); } catch ( AlmaInvalidSignatureException $e ) { $this->logger->error( $e->getMessage() ); - wp_send_json( array( 'error' => $e->getMessage() ), 500 ); + wp_send_json( array( 'error' => $e->getMessage() ), 403 ); } $this->validate_payment_from_ipn( $payment_id ); diff --git a/src/includes/Helpers/PluginHelper.php b/src/includes/Helpers/PluginHelper.php index 41fb0bd2..5660eebe 100644 --- a/src/includes/Helpers/PluginHelper.php +++ b/src/includes/Helpers/PluginHelper.php @@ -13,15 +13,16 @@ use Alma\Woocommerce\Admin\Helpers\CheckLegalHelper; use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Blocks\Standard\PayLaterBlock; +use Alma\Woocommerce\Blocks\Standard\PayMoreThanFourBlock; +use Alma\Woocommerce\Blocks\Standard\PayNowBlock; +use Alma\Woocommerce\Blocks\Standard\StandardBlock; use Alma\Woocommerce\Handlers\CartHandler; use Alma\Woocommerce\Handlers\ProductHandler; +use Alma\Woocommerce\Services\CollectCmsDataService; use Alma\Woocommerce\Services\PaymentUponTriggerService; use Alma\Woocommerce\Services\RefundService; use Alma\Woocommerce\Services\ShareOfCheckoutService; -use Alma\Woocommerce\Blocks\Standard\PayLaterBlock; -use Alma\Woocommerce\Blocks\Standard\PayMoreThanFourBlock; -use Alma\Woocommerce\Blocks\Standard\StandardBlock; -use Alma\Woocommerce\Blocks\Standard\PayNowBlock; if ( ! defined( 'ABSPATH' ) ) { exit; @@ -47,13 +48,21 @@ class PluginHelper { */ protected $block_helper; + /** + * The collect cms data helper + * + * @var CollectCmsDataService + */ + protected $collect_cms_data_service; + /** * Constructor. */ public function __construct() { - $this->order_helper = new OrderHelper(); - $this->block_helper = new BlockHelper(); + $this->order_helper = new OrderHelper(); + $this->block_helper = new BlockHelper(); + $this->collect_cms_data_service = new CollectCmsDataService(); } /** @@ -78,6 +87,14 @@ public function add_hooks() { 'handle_ipn_callback', ) ); + + add_action( + ToolsHelper::action_for_webhook( ConstantsHelper::COLLECT_URL ), + array( + $this->collect_cms_data_service, + 'handle_collect_cms_data', + ) + ); } /** diff --git a/src/includes/Helpers/SecurityHelper.php b/src/includes/Helpers/SecurityHelper.php index bd891ee1..d7c23021 100644 --- a/src/includes/Helpers/SecurityHelper.php +++ b/src/includes/Helpers/SecurityHelper.php @@ -59,4 +59,24 @@ public function validate_ipn_signature( $payment_id, $api_key, $signature ) { throw new AlmaInvalidSignatureException( '[ALMA] Invalid signature' ); } } + + /** + * Validate the collect data signature + * + * @param string $merchant_id merchant id. + * @param string $api_key The API key. + * @param string $signature The signature. + * + * @return void + * + * @throws AlmaInvalidSignatureException If the signature is invalid. + */ + public function validate_collect_data_signature( $merchant_id, $api_key, $signature ) { + if ( empty( $merchant_id ) || empty( $api_key ) || empty( $signature ) ) { + throw new AlmaInvalidSignatureException( sprintf( '[ALMA] Missing required parameters, merchant_id: %s, api_key: %s, signature: %s', $merchant_id, $api_key, $signature ) ); + } + if ( ! $this->payment_validator->isHmacValidated( $merchant_id, $api_key, $signature ) ) { + throw new AlmaInvalidSignatureException( '[ALMA] Invalid signature' ); + } + } } diff --git a/src/includes/Services/CollectCmsDataService.php b/src/includes/Services/CollectCmsDataService.php new file mode 100644 index 00000000..c20e420f --- /dev/null +++ b/src/includes/Services/CollectCmsDataService.php @@ -0,0 +1,227 @@ +alma_settings = isset( $alma_settings ) ? $alma_settings : new AlmaSettings(); + $this->alma_logger = isset( $alma_logger ) ? $alma_logger : new AlmaLogger(); + $this->payload_formatter = isset( $payload_formatter ) ? $payload_formatter : new PayloadFormatter(); + $this->security_helper = isset( $security_helper ) ? $security_helper : ( new SecurityHelperBuilder() )->get_instance(); + $this->option_proxy = isset( $option_proxy ) ? $option_proxy : new OptionProxy(); + $this->theme_proxy = isset( $theme_proxy ) ? $theme_proxy : new ThemeProxy(); + $this->functions_proxy = isset( $functions_proxy ) ? $functions_proxy : new FunctionsProxy(); + } + + /** + * Handle collect cms data + * + * @return void + */ + public function handle_collect_cms_data() { + if ( ! array_key_exists( 'HTTP_X_ALMA_SIGNATURE', $_SERVER ) ) { + $this->alma_logger->error( 'Header key X-Alma-Signature doesn\'t exist' ); + $this->functions_proxy->send_http_response( array( 'error' => 'Header key X-Alma-Signature doesn\'t exist' ), 403 ); + return; + } + + try { + $this->security_helper->validate_collect_data_signature( $this->alma_settings->get_active_merchant_id(), $this->alma_settings->get_active_api_key(), $_SERVER['HTTP_X_ALMA_SIGNATURE'] ); + $this->functions_proxy->send_http_response( $this->payload_formatter->formatConfigurationPayload( $this->get_cms_info(), $this->get_cms_features() ), 200 ); + } catch ( AlmaInvalidSignatureException $e ) { + $this->alma_logger->error( $e->getMessage() ); + $this->functions_proxy->send_http_response( array( 'error' => $e->getMessage() ), 403 ); + } + } + + /** + * Get CMS features. + * + * @return CmsFeatures + */ + private function get_cms_features() { + try { + $this->alma_settings->get_alma_client(); + } catch ( DependenciesError $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } catch ( ParamsError $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } catch ( AlmaException $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } + return new CmsFeatures( + array( + 'alma_enabled' => $this->alma_settings->is_enabled(), + 'widget_cart_activated' => 'yes' === $this->alma_settings->display_cart_eligibility, + 'widget_product_activated' => 'yes' === $this->alma_settings->display_product_eligibility, + 'used_fee_plans' => $this->format_fee_plans(), + 'in_page_activated' => $this->alma_settings->display_in_page, + 'log_activated' => $this->alma_settings->debug, + 'excluded_categories' => $this->alma_settings->excluded_products_list, + 'is_multisite' => is_multisite(), + ) + ); + } + + /** + * Get CMS info. + * + * @return CmsInfo + */ + private function get_cms_info() { + $active_plugins = $this->option_proxy->get_option( 'active_plugins' ); + + return new CmsInfo( + array( + 'cms_name' => 'WooCommerce', + 'cms_version' => $this->option_proxy->get_option( 'woocommerce_version' ), + 'third_parties_plugins' => $this->format_third_party_modules( $active_plugins ), + 'theme_name' => $this->theme_proxy->get_name(), + 'theme_version' => $this->theme_proxy->get_version(), + 'language_name' => 'PHP', + 'language_version' => phpversion(), + 'alma_plugin_version' => ALMA_VERSION, + 'alma_sdk_version' => Client::VERSION, + 'alma_sdk_name' => 'alma/alma-php-client', + ) + ); + } + + /** + * Format third party modules. + * + * @param array $active_plugins active plugins. + * @return array + */ + private function format_third_party_modules( $active_plugins ) { + $third_party_modules = array(); + foreach ( $active_plugins as $plugin_data ) { + $third_party_modules[] = array( 'name' => $plugin_data ); + } + + return $third_party_modules; + } + + /** + * Format fee plans. + * + * @return array + */ + private function format_fee_plans() { + $plans = array(); + + if ( empty( $this->alma_settings->allowed_fee_plans ) ) { + return $plans; + } + + foreach ( $this->alma_settings->allowed_fee_plans as $fee_plan ) { + $plan_key = $fee_plan->getPlanKey(); + + $plans[ $plan_key ] = array( + 'enabled' => $this->alma_settings->is_plan_enabled( $plan_key ), + 'min_amount' => $this->alma_settings->get_min_amount( $plan_key ), + 'max_amount' => $this->alma_settings->get_max_amount( $plan_key ), + ); + } + + uksort( $plans, array( $this->alma_settings->fee_plan_helper, 'alma_usort_plans_keys' ) ); + + return $plans; + } + +} diff --git a/src/includes/Services/IntegrationConfigurationUrlService.php b/src/includes/Services/IntegrationConfigurationUrlService.php new file mode 100644 index 00000000..f29b238b --- /dev/null +++ b/src/includes/Services/IntegrationConfigurationUrlService.php @@ -0,0 +1,81 @@ +alma_settings = $alma_settings; + $this->tool_helper = $tool_helper; + $this->alma_logger = $alma_logger; + } + + /** + * Send collect config url to Alma + * + * @return void + */ + public function send() { + try { + $this->alma_settings->get_alma_client(); + $this->alma_settings->alma_client->configuration->sendIntegrationsConfigurationsUrl( $this->tool_helper->url_for_webhook( ConstantsHelper::COLLECT_URL ) ); + } catch ( AlmaException $e ) { + $this->alma_logger->warning( 'Error while sending integrations configurations URL to Alma: ' . $e->getMessage() ); + } catch ( DependenciesError $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } catch ( ParamsError $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } catch ( \Alma\Woocommerce\Exceptions\AlmaException $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } + } + + +} diff --git a/src/includes/WcProxy/FunctionsProxy.php b/src/includes/WcProxy/FunctionsProxy.php new file mode 100644 index 00000000..9b36dde0 --- /dev/null +++ b/src/includes/WcProxy/FunctionsProxy.php @@ -0,0 +1,27 @@ +get_theme()->get( 'Name' ); + } + + /** + * Get theme version. + * + * @return string + */ + public function get_version() { + return $this->get_theme()->get( 'Version' ); + } + +} diff --git a/src/tests/Helpers/PaymentHelperTest.php b/src/tests/Helpers/PaymentHelperTest.php deleted file mode 100644 index 5a00a931..00000000 --- a/src/tests/Helpers/PaymentHelperTest.php +++ /dev/null @@ -1,65 +0,0 @@ -logger = \Mockery::mock(AlmaLogger::class); - $trigger = \Mockery::mock(PaymentUponTriggerService::class); - $settings = \Mockery::mock(AlmaSettings::class); - $tool_helper = \Mockery::mock(ToolsHelper::class); - $cart_helper = \Mockery::mock(CartHelper::class); - $order_helper = \Mockery::mock(OrderHelper::class); - $product_helper = \Mockery::mock(ProductHelper::class); - $security_helper = \Mockery::mock(SecurityHelper::class); - $this->payment_helper = new PaymentHelper( - $this->logger, - $trigger, - $settings, - $tool_helper, - $cart_helper, - $order_helper, - $product_helper, - $security_helper - ); - } - - public function tear_down() - { - parent::tear_down(); - \Mockery::close(); - } - - public function test() - { - // TODO: Need to add test or remove this Test file - $this->assertTrue(true); - } -} diff --git a/src/tests/Helpers/SecurityHelperTest.php b/src/tests/Helpers/SecurityHelperTest.php index 4d02d773..13274d62 100644 --- a/src/tests/Helpers/SecurityHelperTest.php +++ b/src/tests/Helpers/SecurityHelperTest.php @@ -48,7 +48,7 @@ public function tear_down() \Mockery::close(); // Ferme Mockery après chaque test } - public function test_validate_ipn_throw_Invalide_signature_exception_for_bad_params() + public function test_validate_ipn_throw_invalid_signature_exception_for_bad_params() { $signature = 'bad_signature'; $payment_id = 'payment_id'; @@ -66,4 +66,23 @@ public function test_validate_ipn_signature() $this->payment_validator->shouldReceive('isHmacValidated')->with($payment_id, $api_key, $signature)->andReturn(true); $this->assertNull($this->security_helper->validate_ipn_signature($payment_id, $api_key, $signature)); } + + public function test_validate_collect_data_signature_throw_invalid_signature_exception_for_bad_params() + { + $signature = 'bad_signature'; + $merchant_id = 'merchant_id'; + $api_key = 'api_key'; + $this->payment_validator->shouldReceive('isHmacValidated')->andReturn(false); + $this->expectException(AlmaInvalidSignatureException::class); + $this->security_helper->validate_collect_data_signature($merchant_id, $api_key, $signature); + } + + public function test_validate_collect_data_signature() + { + $signature = 'good_signature'; + $merchant_id = 'valid_merchant_id'; + $api_key = 'valid_api_key'; + $this->payment_validator->shouldReceive('isHmacValidated')->with($merchant_id, $api_key, $signature)->andReturn(true); + $this->assertNull($this->security_helper->validate_collect_data_signature($merchant_id, $api_key, $signature)); + } } diff --git a/src/tests/Services/CollectCmsDataServiceTest.php b/src/tests/Services/CollectCmsDataServiceTest.php new file mode 100644 index 00000000..848d5e09 --- /dev/null +++ b/src/tests/Services/CollectCmsDataServiceTest.php @@ -0,0 +1,156 @@ +alma_logger_mock = $this->createMock(AlmaLogger::class); + $this->alma_settings_mock = $this->createMock(AlmaSettings::class); + $this->alma_settings_mock->method('is_enabled')->willReturn(true); + $this->alma_settings_mock->display_cart_eligibility = 'yes'; + $this->alma_settings_mock->display_product_eligibility = 'no'; + $this->alma_settings_mock->display_in_page = false; + $this->alma_settings_mock->debug = true; + $this->alma_settings_mock->method('is_plan_enabled')->willReturn(true); + $this->alma_settings_mock->method('get_min_amount')->willReturn(0); + $this->alma_settings_mock->method('get_max_amount')->willReturn(1000); + $this->fee_plan_mock = $this->createMock(FeePlan::class); + $this->fee_plan_mock->method('getPlanKey')->willReturn('general_1_0_0'); + $this->alma_settings_mock->allowed_fee_plans = [$this->fee_plan_mock]; + $this->alma_settings_mock->fee_plan_helper = $this->createMock(FeePlanHelper::class); + $this->alma_settings_mock->excluded_products_list = []; + $this->security_helper_mock = $this->createMock(SecurityHelper::class); + $this->payload_formatter_mock = $this->createMock(PayloadFormatter::class); + $this->option_proxy_mock = $this->createMock(OptionProxy::class); + $valueMap = [ + ['active_plugins', false, []], + ['woocommerce_version', false, 'v.4.2'], + ]; + $this->option_proxy_mock->method('get_option')->willReturnMap($valueMap); + $this->theme_proxy_mock = $this->createMock(ThemeProxy::class); + $this->theme_proxy_mock->method('get_name')->willReturn('Storefront'); + $this->theme_proxy_mock->method('get_version')->willReturn('v.4.5'); + $this->functions_proxy_mock = $this->createMock(FunctionsProxy::class); + + $this->collect_cms_data_service = new CollectCmsDataService( + $this->alma_settings_mock, + $this->alma_logger_mock, + $this->payload_formatter_mock, + $this->security_helper_mock, + $this->option_proxy_mock, + $this->theme_proxy_mock, + $this->functions_proxy_mock + ); + } + + public function test_handle_collect_cms_data_without_signature_header() + { + $this->alma_logger_mock->expects($this->once()) + ->method('error') + ->with("Header key X-Alma-Signature doesn't exist"); + + $this->functions_proxy_mock->expects($this->once()) + ->method('send_http_response') + ->with(array('error' => 'Header key X-Alma-Signature doesn\'t exist'), 403); + $this->security_helper_mock->expects($this->never())->method('validate_collect_data_signature'); + + $this->assertNull($this->collect_cms_data_service->handle_collect_cms_data()); + } + + public function test_handle_collect_cms_data_with_invalid_signature() + { + $_SERVER['HTTP_X_ALMA_SIGNATURE'] = 'invalid_signature'; + + $this->security_helper_mock->expects($this->once()) + ->method('validate_collect_data_signature') + ->willThrowException(new AlmaInvalidSignatureException("Invalid signature")); + + $this->alma_logger_mock->expects($this->once()) + ->method('error') + ->with("Invalid signature"); + + $this->functions_proxy_mock->expects($this->once()) + ->method('send_http_response') + ->with(array('error' => 'Invalid signature'), 403); + + $this->payload_formatter_mock->expects($this->never())->method('formatConfigurationPayload'); + + $this->assertNull($this->collect_cms_data_service->handle_collect_cms_data()); + + } + + public function test_handle_collect_cms_data_with_valid_signature() + { + $_SERVER['HTTP_X_ALMA_SIGNATURE'] = 'valid_signature'; + + $this->security_helper_mock->expects($this->once()) + ->method('validate_collect_data_signature'); + + $cms_info = new CmsInfo([ + 'cms_name' => 'WooCommerce', + 'cms_version' => 'v.4.2', + 'third_parties_plugins' => [], + 'theme_name' => 'Storefront', + 'theme_version' => 'v.4.5', + 'language_name' => 'PHP', + 'language_version' => phpversion(), + 'alma_plugin_version' => ALMA_VERSION, + 'alma_sdk_version' => Client::VERSION, + 'alma_sdk_name' => 'alma/alma-php-client', + ]); + + $cms_features = new CmsFeatures([ + 'alma_enabled' => true, + 'widget_cart_activated' => true, + 'widget_product_activated' => false, + 'used_fee_plans' => ['general_1_0_0' => ['enabled' => true, 'min_amount' => 0, 'max_amount' => 1000]], + 'in_page_activated' => false, + 'log_activated' => true, + 'excluded_categories' => [], + 'is_multisite' => is_multisite(), + ]); + + $payload_formatter_return = [ + 'cms_info' => $cms_info->getProperties(), + 'cms_features' => $cms_features->getProperties() + ]; + $this->payload_formatter_mock->expects($this->once()) + ->method('formatConfigurationPayload') + ->with($cms_info, $cms_features) + ->willReturn($payload_formatter_return); + + $this->functions_proxy_mock->expects($this->once()) + ->method('send_http_response') + ->with($payload_formatter_return, 200); + + $this->assertNull($this->collect_cms_data_service->handle_collect_cms_data()); + } +} \ No newline at end of file diff --git a/src/tests/Services/IntegrationConfigurationUrlServiceTest.php b/src/tests/Services/IntegrationConfigurationUrlServiceTest.php new file mode 100644 index 00000000..02ab9ada --- /dev/null +++ b/src/tests/Services/IntegrationConfigurationUrlServiceTest.php @@ -0,0 +1,38 @@ +createMock(AlmaSettings::class); + $tool_helper = $this->createMock(ToolsHelper::class); + $alma_logger = $this->createMock(AlmaLogger::class); + + $alma_settings->alma_client = $this->createMock(Client::class); + $alma_settings->alma_client->configuration = $this->createMock(Configuration::class); + + $tool_helper->expects($this->once()) + ->method('url_for_webhook') + ->with(ConstantsHelper::COLLECT_URL) + ->willReturn('http://example.com/woocommerce_api_alma_collect_data_url'); + + $alma_settings->alma_client->configuration->expects($this->once()) + ->method('sendIntegrationsConfigurationsUrl') + ->with('http://example.com/woocommerce_api_alma_collect_data_url'); + + $service = new IntegrationConfigurationUrlService($alma_settings, $tool_helper, $alma_logger); + assertNull($service->send()); + } +} \ No newline at end of file From 4abf9eeb5da979d6279b9f0fa3c593161329f66b Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:33:06 +0100 Subject: [PATCH 2/7] fix: add new line at end of file --- src/tests/Services/CollectCmsDataServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/Services/CollectCmsDataServiceTest.php b/src/tests/Services/CollectCmsDataServiceTest.php index 848d5e09..f91971fc 100644 --- a/src/tests/Services/CollectCmsDataServiceTest.php +++ b/src/tests/Services/CollectCmsDataServiceTest.php @@ -153,4 +153,4 @@ public function test_handle_collect_cms_data_with_valid_signature() $this->assertNull($this->collect_cms_data_service->handle_collect_cms_data()); } -} \ No newline at end of file +} From 1d602180bb7b847afea933dc6f2a45330c62f675 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:34:04 +0100 Subject: [PATCH 3/7] fix: add new line at end of file --- src/tests/Services/IntegrationConfigurationUrlServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/Services/IntegrationConfigurationUrlServiceTest.php b/src/tests/Services/IntegrationConfigurationUrlServiceTest.php index 02ab9ada..14fb9859 100644 --- a/src/tests/Services/IntegrationConfigurationUrlServiceTest.php +++ b/src/tests/Services/IntegrationConfigurationUrlServiceTest.php @@ -35,4 +35,4 @@ public function test_send() $service = new IntegrationConfigurationUrlService($alma_settings, $tool_helper, $alma_logger); assertNull($service->send()); } -} \ No newline at end of file +} From 74815f6eeb571f2971e99943815c3f90c45caad4 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 29 Nov 2024 09:26:12 +0100 Subject: [PATCH 4/7] fix: remove useless code --- .../Helpers/SecurityHelperBuilder.php | 3 +- src/includes/Helpers/SecurityHelper.php | 30 ++++++++----------- .../Services/CollectCmsDataService.php | 15 ++++------ src/includes/WcProxy/ThemeProxy.php | 2 +- src/tests/Helpers/SecurityHelperTest.php | 25 +++++----------- 5 files changed, 27 insertions(+), 48 deletions(-) diff --git a/src/includes/Builders/Helpers/SecurityHelperBuilder.php b/src/includes/Builders/Helpers/SecurityHelperBuilder.php index 51bf6702..c4b0c5cd 100644 --- a/src/includes/Builders/Helpers/SecurityHelperBuilder.php +++ b/src/includes/Builders/Helpers/SecurityHelperBuilder.php @@ -31,8 +31,7 @@ class SecurityHelperBuilder { */ public function get_instance() { return new SecurityHelper( - $this->get_alma_logger(), - $this->get_payment_validator() + $this->get_alma_logger() ); } } diff --git a/src/includes/Helpers/SecurityHelper.php b/src/includes/Helpers/SecurityHelper.php index d7c23021..c2429a29 100644 --- a/src/includes/Helpers/SecurityHelper.php +++ b/src/includes/Helpers/SecurityHelper.php @@ -7,7 +7,8 @@ namespace Alma\Woocommerce\Helpers; -use Alma\API\Lib\PaymentValidator; +use Alma\API\Lib\RequestUtils; +use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\Exceptions\AlmaInvalidSignatureException; /** @@ -22,22 +23,13 @@ class SecurityHelper { */ protected $logger; - /** - * The payment validator. - * - * @var PaymentValidator - */ - protected $payment_validator; - /** * SecurityHelper constructor. * - * @param AlmaLogger $logger The logger. - * @param PaymentValidator $payment_validator The payment validator. + * @param AlmaLogger $logger The logger. */ - public function __construct( $logger, $payment_validator ) { - $this->logger = $logger; - $this->payment_validator = $payment_validator; + public function __construct( $logger ) { + $this->logger = $logger; } /** @@ -53,9 +45,11 @@ public function __construct( $logger, $payment_validator ) { */ public function validate_ipn_signature( $payment_id, $api_key, $signature ) { if ( empty( $payment_id ) || empty( $api_key ) || empty( $signature ) ) { - throw new AlmaInvalidSignatureException( sprintf( '[ALMA] Missing required parameters, payment_id: %s, api_key: %s, signature: %s', $payment_id, $api_key, $signature ) ); + throw new AlmaInvalidSignatureException( + sprintf( '[ALMA] Missing required parameters, payment_id: %s, api_key: %s, signature: %s', $payment_id, $api_key, $signature ) + ); } - if ( ! $this->payment_validator->isHmacValidated( $payment_id, $api_key, $signature ) ) { + if ( ! RequestUtils::isHmacValidated( $payment_id, $api_key, $signature ) ) { throw new AlmaInvalidSignatureException( '[ALMA] Invalid signature' ); } } @@ -73,9 +67,11 @@ public function validate_ipn_signature( $payment_id, $api_key, $signature ) { */ public function validate_collect_data_signature( $merchant_id, $api_key, $signature ) { if ( empty( $merchant_id ) || empty( $api_key ) || empty( $signature ) ) { - throw new AlmaInvalidSignatureException( sprintf( '[ALMA] Missing required parameters, merchant_id: %s, api_key: %s, signature: %s', $merchant_id, $api_key, $signature ) ); + throw new AlmaInvalidSignatureException( + sprintf( '[ALMA] Missing required parameters, merchant_id: %s, api_key: %s, signature: %s', $merchant_id, $api_key, $signature ) + ); } - if ( ! $this->payment_validator->isHmacValidated( $merchant_id, $api_key, $signature ) ) { + if ( ! RequestUtils::isHmacValidated( $merchant_id, $api_key, $signature ) ) { throw new AlmaInvalidSignatureException( '[ALMA] Invalid signature' ); } } diff --git a/src/includes/Services/CollectCmsDataService.php b/src/includes/Services/CollectCmsDataService.php index c20e420f..10e0031d 100644 --- a/src/includes/Services/CollectCmsDataService.php +++ b/src/includes/Services/CollectCmsDataService.php @@ -121,7 +121,11 @@ public function handle_collect_cms_data() { } try { - $this->security_helper->validate_collect_data_signature( $this->alma_settings->get_active_merchant_id(), $this->alma_settings->get_active_api_key(), $_SERVER['HTTP_X_ALMA_SIGNATURE'] ); + $this->security_helper->validate_collect_data_signature( + $this->alma_settings->get_active_merchant_id(), + $this->alma_settings->get_active_api_key(), + $_SERVER['HTTP_X_ALMA_SIGNATURE'] + ); $this->functions_proxy->send_http_response( $this->payload_formatter->formatConfigurationPayload( $this->get_cms_info(), $this->get_cms_features() ), 200 ); } catch ( AlmaInvalidSignatureException $e ) { $this->alma_logger->error( $e->getMessage() ); @@ -135,15 +139,6 @@ public function handle_collect_cms_data() { * @return CmsFeatures */ private function get_cms_features() { - try { - $this->alma_settings->get_alma_client(); - } catch ( DependenciesError $e ) { - $this->alma_logger->warning( $e->getMessage() ); - } catch ( ParamsError $e ) { - $this->alma_logger->warning( $e->getMessage() ); - } catch ( AlmaException $e ) { - $this->alma_logger->warning( $e->getMessage() ); - } return new CmsFeatures( array( 'alma_enabled' => $this->alma_settings->is_enabled(), diff --git a/src/includes/WcProxy/ThemeProxy.php b/src/includes/WcProxy/ThemeProxy.php index 81aa58f4..9385f8e0 100644 --- a/src/includes/WcProxy/ThemeProxy.php +++ b/src/includes/WcProxy/ThemeProxy.php @@ -19,7 +19,7 @@ class ThemeProxy { * * @return WP_Theme */ - public function get_theme() { + private function get_theme() { return wp_get_theme(); } diff --git a/src/tests/Helpers/SecurityHelperTest.php b/src/tests/Helpers/SecurityHelperTest.php index 13274d62..13ea005f 100644 --- a/src/tests/Helpers/SecurityHelperTest.php +++ b/src/tests/Helpers/SecurityHelperTest.php @@ -9,7 +9,6 @@ namespace Alma\Woocommerce\Tests\Helpers; -use Alma\API\Lib\PaymentValidator; use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\Exceptions\AlmaInvalidSignatureException; use Alma\Woocommerce\Helpers\PaymentHelper; @@ -26,19 +25,13 @@ class SecurityHelperTest extends WP_UnitTestCase * @var PaymentHelper */ protected $logger; - /** - * @var PaymentValidator - */ - protected $payment_validator; public function set_up() { $this->logger = \Mockery::mock(AlmaLogger::class); - $this->payment_validator = \Mockery::mock(PaymentValidator::class); $this->security_helper = new SecurityHelper( - $this->logger, - $this->payment_validator + $this->logger ); } @@ -53,17 +46,15 @@ public function test_validate_ipn_throw_invalid_signature_exception_for_bad_para $signature = 'bad_signature'; $payment_id = 'payment_id'; $api_key = 'api_key'; - $this->payment_validator->shouldReceive('isHmacValidated')->andReturn(false); $this->expectException(AlmaInvalidSignatureException::class); $this->security_helper->validate_ipn_signature($payment_id, $api_key, $signature); } public function test_validate_ipn_signature() { - $signature = 'good_signature'; - $payment_id = 'valid_payment_id'; - $api_key = 'valid_api_key'; - $this->payment_validator->shouldReceive('isHmacValidated')->with($payment_id, $api_key, $signature)->andReturn(true); + $signature = '3dcb1255e432da08a2bd65df2963659bb0b362888500e18c8cf6c5d5958db752'; + $payment_id = 'payment_xxxxx'; + $api_key = 'sk_test_xxxxx'; $this->assertNull($this->security_helper->validate_ipn_signature($payment_id, $api_key, $signature)); } @@ -72,17 +63,15 @@ public function test_validate_collect_data_signature_throw_invalid_signature_exc $signature = 'bad_signature'; $merchant_id = 'merchant_id'; $api_key = 'api_key'; - $this->payment_validator->shouldReceive('isHmacValidated')->andReturn(false); $this->expectException(AlmaInvalidSignatureException::class); $this->security_helper->validate_collect_data_signature($merchant_id, $api_key, $signature); } public function test_validate_collect_data_signature() { - $signature = 'good_signature'; - $merchant_id = 'valid_merchant_id'; - $api_key = 'valid_api_key'; - $this->payment_validator->shouldReceive('isHmacValidated')->with($merchant_id, $api_key, $signature)->andReturn(true); + $signature = '7d572bbfbedb1bde72378691973a67ff52fb56cd9d18b1f0ab3c7e88b119b9d3'; + $merchant_id = 'merchant_xxxxxx'; + $api_key = 'sk_test_xxxxx'; $this->assertNull($this->security_helper->validate_collect_data_signature($merchant_id, $api_key, $signature)); } } From 99c078c9c1e64188f474a8b009a71a1f9178aeb3 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:59:40 +0100 Subject: [PATCH 5/7] feat: remove integration configuration url service --- .../Helpers/SecurityHelperBuilder.php | 1 - src/includes/Gateways/AlmaPaymentGateway.php | 48 +++++------ src/includes/Helpers/ConstantsHelper.php | 2 - src/includes/Helpers/PluginHelper.php | 2 +- .../Services/CollectCmsDataService.php | 48 +++++++++-- .../IntegrationConfigurationUrlService.php | 81 ------------------- .../Services/CollectCmsDataServiceTest.php | 27 ++++++- ...IntegrationConfigurationUrlServiceTest.php | 38 --------- 8 files changed, 90 insertions(+), 157 deletions(-) delete mode 100644 src/includes/Services/IntegrationConfigurationUrlService.php delete mode 100644 src/tests/Services/IntegrationConfigurationUrlServiceTest.php diff --git a/src/includes/Builders/Helpers/SecurityHelperBuilder.php b/src/includes/Builders/Helpers/SecurityHelperBuilder.php index c4b0c5cd..b4809afe 100644 --- a/src/includes/Builders/Helpers/SecurityHelperBuilder.php +++ b/src/includes/Builders/Helpers/SecurityHelperBuilder.php @@ -7,7 +7,6 @@ namespace Alma\Woocommerce\Builders\Helpers; -use Alma\Woocommerce\Helpers\ProductHelper; use Alma\Woocommerce\Helpers\SecurityHelper; use Alma\Woocommerce\Traits\BuilderTrait; diff --git a/src/includes/Gateways/AlmaPaymentGateway.php b/src/includes/Gateways/AlmaPaymentGateway.php index 20060457..2661f397 100644 --- a/src/includes/Gateways/AlmaPaymentGateway.php +++ b/src/includes/Gateways/AlmaPaymentGateway.php @@ -43,7 +43,7 @@ use Alma\Woocommerce\Helpers\SettingsHelper; use Alma\Woocommerce\Helpers\TemplateLoaderHelper; use Alma\Woocommerce\Helpers\ToolsHelper; -use Alma\Woocommerce\Services\IntegrationConfigurationUrlService; +use Alma\Woocommerce\Services\CollectCmsDataService; /** * AlmaPaymentGateway @@ -194,11 +194,11 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { protected $alma_plan_helper; /** - * The integration configuration url service. + * Collect CMS data service. * - * @var IntegrationConfigurationUrlService + * @var CollectCmsDataService */ - protected $integration_configuration_url_service; + protected $collect_cms_data_service; /** * Construct. @@ -207,25 +207,25 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { * @throws NoCredentialsException The exception. */ public function __construct( $check_basics = true ) { - $this->logger = new AlmaLogger(); - $this->alma_settings = new AlmaSettings(); - $this->check_legal_helper = new CheckLegalHelper(); - $this->checkout_helper = new CheckoutHelper(); - $gateway_helper_builder = new GatewayHelperBuilder(); - $this->gateway_helper = $gateway_helper_builder->get_instance(); - $this->scripts_helper = new AssetsHelper(); - $this->encryption_helper = new EncryptorHelper(); - $tools_helper_builder = new ToolsHelperBuilder(); - $this->tool_helper = $tools_helper_builder->get_instance(); - $this->alma_payment_helper = new PaymentHelper(); - $this->order_helper = new OrderHelper(); - $this->template_loader = new TemplateLoaderHelper(); - $this->soc_helper = new ShareOfCheckoutHelper(); - $this->plugin_helper = new PluginHelper(); - $this->asset_helper = new AssetsHelper(); - $alma_plan_builder = new PlanHelperBuilder(); - $this->alma_plan_helper = $alma_plan_builder->get_instance(); - $this->integration_configuration_url_service = new IntegrationConfigurationUrlService( $this->alma_settings, $this->tool_helper, $this->logger ); + $this->logger = new AlmaLogger(); + $this->alma_settings = new AlmaSettings(); + $this->check_legal_helper = new CheckLegalHelper(); + $this->checkout_helper = new CheckoutHelper(); + $gateway_helper_builder = new GatewayHelperBuilder(); + $this->gateway_helper = $gateway_helper_builder->get_instance(); + $this->scripts_helper = new AssetsHelper(); + $this->encryption_helper = new EncryptorHelper(); + $tools_helper_builder = new ToolsHelperBuilder(); + $this->tool_helper = $tools_helper_builder->get_instance(); + $this->alma_payment_helper = new PaymentHelper(); + $this->order_helper = new OrderHelper(); + $this->template_loader = new TemplateLoaderHelper(); + $this->soc_helper = new ShareOfCheckoutHelper(); + $this->plugin_helper = new PluginHelper(); + $this->asset_helper = new AssetsHelper(); + $alma_plan_builder = new PlanHelperBuilder(); + $this->alma_plan_helper = $alma_plan_builder->get_instance(); + $this->collect_cms_data_service = new CollectCmsDataService(); $this->plugin_factory = new PluginFactory(); $this->cart_factory = new CartFactory(); @@ -429,7 +429,7 @@ public function manage_credentials() { } $this->alma_settings->init_allowed_fee_plans(); - $this->integration_configuration_url_service->send(); + $this->collect_cms_data_service->send_url(); // Save of the fee plans. update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->alma_settings->settings ), 'yes' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound diff --git a/src/includes/Helpers/ConstantsHelper.php b/src/includes/Helpers/ConstantsHelper.php index 3960046d..ab0bd92a 100644 --- a/src/includes/Helpers/ConstantsHelper.php +++ b/src/includes/Helpers/ConstantsHelper.php @@ -73,8 +73,6 @@ class ConstantsHelper { const ERROR = 'error'; - const COLLECT_URL = 'alma_collect_data_url'; - /** * The payment status * diff --git a/src/includes/Helpers/PluginHelper.php b/src/includes/Helpers/PluginHelper.php index 5660eebe..6e9100a9 100644 --- a/src/includes/Helpers/PluginHelper.php +++ b/src/includes/Helpers/PluginHelper.php @@ -89,7 +89,7 @@ public function add_hooks() { ); add_action( - ToolsHelper::action_for_webhook( ConstantsHelper::COLLECT_URL ), + ToolsHelper::action_for_webhook( CollectCmsDataService::COLLECT_URL ), array( $this->collect_cms_data_service, 'handle_collect_cms_data', diff --git a/src/includes/Services/CollectCmsDataService.php b/src/includes/Services/CollectCmsDataService.php index 10e0031d..1b292130 100644 --- a/src/includes/Services/CollectCmsDataService.php +++ b/src/includes/Services/CollectCmsDataService.php @@ -13,14 +13,16 @@ use Alma\API\DependenciesError; use Alma\API\Entities\MerchantData\CmsFeatures; use Alma\API\Entities\MerchantData\CmsInfo; +use Alma\API\Exceptions\AlmaException; use Alma\API\Lib\PayloadFormatter; use Alma\API\ParamsError; use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\AlmaSettings; use Alma\Woocommerce\Builders\Helpers\SecurityHelperBuilder; -use Alma\Woocommerce\Exceptions\AlmaException; +use Alma\Woocommerce\Builders\Helpers\ToolsHelperBuilder; use Alma\Woocommerce\Exceptions\AlmaInvalidSignatureException; use Alma\Woocommerce\Helpers\SecurityHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; use Alma\Woocommerce\WcProxy\FunctionsProxy; use Alma\Woocommerce\WcProxy\OptionProxy; use Alma\Woocommerce\WcProxy\ThemeProxy; @@ -30,6 +32,8 @@ */ class CollectCmsDataService { + const COLLECT_URL = 'alma_collect_data_url'; + /** * Alma Settings. * @@ -79,6 +83,13 @@ class CollectCmsDataService { */ protected $functions_proxy; + /** + * Tool Helper. + * + * @var ToolsHelper + */ + protected $tools_helper; + /** * CollectCmsDataService constructor. * @@ -89,6 +100,7 @@ class CollectCmsDataService { * @param OptionProxy $option_proxy Option Proxy. * @param ThemeProxy $theme_proxy Theme Proxy. * @param FunctionsProxy $functions_proxy Functions Proxy. + * @param ToolsHelper $tools_helper Tools Helper. */ public function __construct( $alma_settings = null, @@ -97,7 +109,8 @@ public function __construct( $security_helper = null, $option_proxy = null, $theme_proxy = null, - $functions_proxy = null + $functions_proxy = null, + $tools_helper = null ) { $this->alma_settings = isset( $alma_settings ) ? $alma_settings : new AlmaSettings(); $this->alma_logger = isset( $alma_logger ) ? $alma_logger : new AlmaLogger(); @@ -106,6 +119,27 @@ public function __construct( $this->option_proxy = isset( $option_proxy ) ? $option_proxy : new OptionProxy(); $this->theme_proxy = isset( $theme_proxy ) ? $theme_proxy : new ThemeProxy(); $this->functions_proxy = isset( $functions_proxy ) ? $functions_proxy : new FunctionsProxy(); + $this->tools_helper = isset( $tools_helper ) ? $tools_helper : ( new ToolsHelperBuilder() )->get_instance(); + } + + /** + * Send collect config url to Alma + * + * @return void + */ + public function send_url() { + try { + $this->alma_settings->get_alma_client(); + $this->alma_settings->alma_client->configuration->sendIntegrationsConfigurationsUrl( $this->tools_helper->url_for_webhook( self::COLLECT_URL ) ); + } catch ( AlmaException $e ) { + $this->alma_logger->warning( 'Error while sending integrations configurations URL to Alma: ' . $e->getMessage() ); + } catch ( DependenciesError $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } catch ( ParamsError $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } catch ( \Alma\Woocommerce\Exceptions\AlmaException $e ) { + $this->alma_logger->warning( $e->getMessage() ); + } } /** @@ -146,7 +180,7 @@ private function get_cms_features() { 'widget_product_activated' => 'yes' === $this->alma_settings->display_product_eligibility, 'used_fee_plans' => $this->format_fee_plans(), 'in_page_activated' => $this->alma_settings->display_in_page, - 'log_activated' => $this->alma_settings->debug, + 'log_activated' => 'yes' === $this->alma_settings->debug, 'excluded_categories' => $this->alma_settings->excluded_products_list, 'is_multisite' => is_multisite(), ) @@ -159,13 +193,11 @@ private function get_cms_features() { * @return CmsInfo */ private function get_cms_info() { - $active_plugins = $this->option_proxy->get_option( 'active_plugins' ); - return new CmsInfo( array( 'cms_name' => 'WooCommerce', 'cms_version' => $this->option_proxy->get_option( 'woocommerce_version' ), - 'third_parties_plugins' => $this->format_third_party_modules( $active_plugins ), + 'third_parties_plugins' => $this->format_third_party_modules(), 'theme_name' => $this->theme_proxy->get_name(), 'theme_version' => $this->theme_proxy->get_version(), 'language_name' => 'PHP', @@ -180,10 +212,10 @@ private function get_cms_info() { /** * Format third party modules. * - * @param array $active_plugins active plugins. * @return array */ - private function format_third_party_modules( $active_plugins ) { + private function format_third_party_modules() { + $active_plugins = $this->option_proxy->get_option( 'active_plugins' ); $third_party_modules = array(); foreach ( $active_plugins as $plugin_data ) { $third_party_modules[] = array( 'name' => $plugin_data ); diff --git a/src/includes/Services/IntegrationConfigurationUrlService.php b/src/includes/Services/IntegrationConfigurationUrlService.php deleted file mode 100644 index f29b238b..00000000 --- a/src/includes/Services/IntegrationConfigurationUrlService.php +++ /dev/null @@ -1,81 +0,0 @@ -alma_settings = $alma_settings; - $this->tool_helper = $tool_helper; - $this->alma_logger = $alma_logger; - } - - /** - * Send collect config url to Alma - * - * @return void - */ - public function send() { - try { - $this->alma_settings->get_alma_client(); - $this->alma_settings->alma_client->configuration->sendIntegrationsConfigurationsUrl( $this->tool_helper->url_for_webhook( ConstantsHelper::COLLECT_URL ) ); - } catch ( AlmaException $e ) { - $this->alma_logger->warning( 'Error while sending integrations configurations URL to Alma: ' . $e->getMessage() ); - } catch ( DependenciesError $e ) { - $this->alma_logger->warning( $e->getMessage() ); - } catch ( ParamsError $e ) { - $this->alma_logger->warning( $e->getMessage() ); - } catch ( \Alma\Woocommerce\Exceptions\AlmaException $e ) { - $this->alma_logger->warning( $e->getMessage() ); - } - } - - -} diff --git a/src/tests/Services/CollectCmsDataServiceTest.php b/src/tests/Services/CollectCmsDataServiceTest.php index f91971fc..bdbd3b6e 100644 --- a/src/tests/Services/CollectCmsDataServiceTest.php +++ b/src/tests/Services/CollectCmsDataServiceTest.php @@ -3,6 +3,7 @@ namespace Alma\Woocommerce\Tests\Services; use Alma\API\Client; +use Alma\API\Endpoints\Configuration; use Alma\API\Entities\FeePlan; use Alma\API\Entities\MerchantData\CmsFeatures; use Alma\API\Entities\MerchantData\CmsInfo; @@ -12,11 +13,13 @@ use Alma\Woocommerce\Exceptions\AlmaInvalidSignatureException; use Alma\Woocommerce\Helpers\FeePlanHelper; use Alma\Woocommerce\Helpers\SecurityHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; use Alma\Woocommerce\Services\CollectCmsDataService; use Alma\Woocommerce\WcProxy\FunctionsProxy; use Alma\Woocommerce\WcProxy\OptionProxy; use Alma\Woocommerce\WcProxy\ThemeProxy; use WP_UnitTestCase; +use function PHPUnit\Framework\assertNull; class CollectCmsDataServiceTest extends WP_UnitTestCase { @@ -29,6 +32,7 @@ class CollectCmsDataServiceTest extends WP_UnitTestCase protected $theme_proxy_mock; protected $functions_proxy_mock; protected $fee_plan_mock; + protected $tools_helper_mock; public function set_up() { @@ -38,7 +42,7 @@ public function set_up() $this->alma_settings_mock->display_cart_eligibility = 'yes'; $this->alma_settings_mock->display_product_eligibility = 'no'; $this->alma_settings_mock->display_in_page = false; - $this->alma_settings_mock->debug = true; + $this->alma_settings_mock->debug = "yes"; $this->alma_settings_mock->method('is_plan_enabled')->willReturn(true); $this->alma_settings_mock->method('get_min_amount')->willReturn(0); $this->alma_settings_mock->method('get_max_amount')->willReturn(1000); @@ -59,6 +63,7 @@ public function set_up() $this->theme_proxy_mock->method('get_name')->willReturn('Storefront'); $this->theme_proxy_mock->method('get_version')->willReturn('v.4.5'); $this->functions_proxy_mock = $this->createMock(FunctionsProxy::class); + $this->tools_helper_mock = $this->createMock(ToolsHelper::class); $this->collect_cms_data_service = new CollectCmsDataService( $this->alma_settings_mock, @@ -67,10 +72,28 @@ public function set_up() $this->security_helper_mock, $this->option_proxy_mock, $this->theme_proxy_mock, - $this->functions_proxy_mock + $this->functions_proxy_mock, + $this->tools_helper_mock ); } + public function test_send_url() + { + $this->alma_settings_mock->alma_client = $this->createMock(Client::class); + $this->alma_settings_mock->alma_client->configuration = $this->createMock(Configuration::class); + + $this->tools_helper_mock->expects($this->once()) + ->method('url_for_webhook') + ->with(CollectCmsDataService::COLLECT_URL) + ->willReturn('http://example.com/woocommerce_api_alma_collect_data_url'); + + $this->alma_settings_mock->alma_client->configuration->expects($this->once()) + ->method('sendIntegrationsConfigurationsUrl') + ->with('http://example.com/woocommerce_api_alma_collect_data_url'); + + assertNull($this->collect_cms_data_service->send_url()); + } + public function test_handle_collect_cms_data_without_signature_header() { $this->alma_logger_mock->expects($this->once()) diff --git a/src/tests/Services/IntegrationConfigurationUrlServiceTest.php b/src/tests/Services/IntegrationConfigurationUrlServiceTest.php deleted file mode 100644 index 14fb9859..00000000 --- a/src/tests/Services/IntegrationConfigurationUrlServiceTest.php +++ /dev/null @@ -1,38 +0,0 @@ -createMock(AlmaSettings::class); - $tool_helper = $this->createMock(ToolsHelper::class); - $alma_logger = $this->createMock(AlmaLogger::class); - - $alma_settings->alma_client = $this->createMock(Client::class); - $alma_settings->alma_client->configuration = $this->createMock(Configuration::class); - - $tool_helper->expects($this->once()) - ->method('url_for_webhook') - ->with(ConstantsHelper::COLLECT_URL) - ->willReturn('http://example.com/woocommerce_api_alma_collect_data_url'); - - $alma_settings->alma_client->configuration->expects($this->once()) - ->method('sendIntegrationsConfigurationsUrl') - ->with('http://example.com/woocommerce_api_alma_collect_data_url'); - - $service = new IntegrationConfigurationUrlService($alma_settings, $tool_helper, $alma_logger); - assertNull($service->send()); - } -} From 9f25a65ed01a557cf60ff64a7e7f7ff84c95f46d Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 2 Dec 2024 16:21:12 +0100 Subject: [PATCH 6/7] fix: logger message --- src/composer.json | 2 +- src/includes/Services/CollectCmsDataService.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/composer.json b/src/composer.json index 9458bd52..0356d994 100644 --- a/src/composer.json +++ b/src/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1", - "alma/alma-php-client": ">=2.3.0", + "alma/alma-php-client": ">=2.3.1", "ext-openssl": "*" }, "require-dev": { diff --git a/src/includes/Services/CollectCmsDataService.php b/src/includes/Services/CollectCmsDataService.php index 1b292130..15eaf6fa 100644 --- a/src/includes/Services/CollectCmsDataService.php +++ b/src/includes/Services/CollectCmsDataService.php @@ -134,11 +134,11 @@ public function send_url() { } catch ( AlmaException $e ) { $this->alma_logger->warning( 'Error while sending integrations configurations URL to Alma: ' . $e->getMessage() ); } catch ( DependenciesError $e ) { - $this->alma_logger->warning( $e->getMessage() ); + $this->alma_logger->warning( 'Dependencies error: ' . $e->getMessage() ); } catch ( ParamsError $e ) { - $this->alma_logger->warning( $e->getMessage() ); + $this->alma_logger->warning( 'Params Error: ' . $e->getMessage() ); } catch ( \Alma\Woocommerce\Exceptions\AlmaException $e ) { - $this->alma_logger->warning( $e->getMessage() ); + $this->alma_logger->warning( 'Alma exception: ' . $e->getMessage() ); } } From b6c8bbfbaaf68aa07ea0af5a3aba1c6761584802 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:03:00 +0100 Subject: [PATCH 7/7] fix: return null for plans empty --- src/includes/Services/CollectCmsDataService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/includes/Services/CollectCmsDataService.php b/src/includes/Services/CollectCmsDataService.php index 15eaf6fa..1e5e19c6 100644 --- a/src/includes/Services/CollectCmsDataService.php +++ b/src/includes/Services/CollectCmsDataService.php @@ -227,13 +227,13 @@ private function format_third_party_modules() { /** * Format fee plans. * - * @return array + * @return array | null */ private function format_fee_plans() { $plans = array(); if ( empty( $this->alma_settings->allowed_fee_plans ) ) { - return $plans; + return null; } foreach ( $this->alma_settings->allowed_fee_plans as $fee_plan ) {