Skip to content

Commit

Permalink
Merge pull request #297 from alma/feature/ecom-2128-wc-implement-php-…
Browse files Browse the repository at this point in the history
…client-endpoints-into-woocommerce

Add gather cms data
  • Loading branch information
joyet-simon authored Dec 6, 2024
2 parents 88b66f3 + b6c8bbf commit cb65297
Show file tree
Hide file tree
Showing 14 changed files with 686 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"ext-openssl": "*"
},
"require-dev": {
Expand Down
57 changes: 46 additions & 11 deletions src/includes/AlmaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,66 @@
* @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 bool keys_validity Flag to indicate id the current keys are working
* @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
* @property string live_merchant_name Alma LIVE merchant name
* @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
* @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.
*
Expand All @@ -95,7 +130,7 @@ class AlmaSettings {
public $allowed_fee_plans = array();

/**
* The logger.
* The logger.
*
* @var AlmaLogger
*/
Expand Down Expand Up @@ -273,7 +308,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" );
}

Expand Down
4 changes: 1 addition & 3 deletions src/includes/Builders/Helpers/SecurityHelperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Alma\Woocommerce\Builders\Helpers;

use Alma\Woocommerce\Helpers\ProductHelper;
use Alma\Woocommerce\Helpers\SecurityHelper;
use Alma\Woocommerce\Traits\BuilderTrait;

Expand All @@ -31,8 +30,7 @@ class SecurityHelperBuilder {
*/
public function get_instance() {
return new SecurityHelper(
$this->get_alma_logger(),
$this->get_payment_validator()
$this->get_alma_logger()
);
}
}
46 changes: 28 additions & 18 deletions src/includes/Gateways/AlmaPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Alma\Woocommerce\Helpers\SettingsHelper;
use Alma\Woocommerce\Helpers\TemplateLoaderHelper;
use Alma\Woocommerce\Helpers\ToolsHelper;
use Alma\Woocommerce\Services\CollectCmsDataService;

/**
* AlmaPaymentGateway
Expand Down Expand Up @@ -192,31 +193,39 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway {
*/
protected $alma_plan_helper;

/**
* Collect CMS data service.
*
* @var CollectCmsDataService
*/
protected $collect_cms_data_service;

/**
* Construct.
*
* @param bool $check_basics Check the basics requirement.
* @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->collect_cms_data_service = new CollectCmsDataService();

$this->plugin_factory = new PluginFactory();
$this->cart_factory = new CartFactory();
Expand Down Expand Up @@ -420,6 +429,7 @@ public function manage_credentials() {
}

$this->alma_settings->init_allowed_fee_plans();
$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
Expand Down
4 changes: 2 additions & 2 deletions src/includes/Helpers/PaymentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 );
Expand Down
29 changes: 23 additions & 6 deletions src/includes/Helpers/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand All @@ -78,6 +87,14 @@ public function add_hooks() {
'handle_ipn_callback',
)
);

add_action(
ToolsHelper::action_for_webhook( CollectCmsDataService::COLLECT_URL ),
array(
$this->collect_cms_data_service,
'handle_collect_cms_data',
)
);
}

/**
Expand Down
46 changes: 31 additions & 15 deletions src/includes/Helpers/SecurityHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

/**
Expand All @@ -53,9 +45,33 @@ 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 ( ! RequestUtils::isHmacValidated( $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( $payment_id, $api_key, $signature ) ) {
if ( ! RequestUtils::isHmacValidated( $merchant_id, $api_key, $signature ) ) {
throw new AlmaInvalidSignatureException( '[ALMA] Invalid signature' );
}
}
Expand Down
Loading

0 comments on commit cb65297

Please sign in to comment.