diff --git a/.github/workflows/aqua.yml b/.github/workflows/aqua.yml index 97fbda06..633e7bc4 100644 --- a/.github/workflows/aqua.yml +++ b/.github/workflows/aqua.yml @@ -16,7 +16,7 @@ jobs: - name: Run Aqua scanner uses: docker://aquasec/aqua-scanner with: - args: trivy fs --sast --reachability --scanners config,vuln,secret . + args: trivy fs --sast --reachability --scanners misconfig,vuln,secret . # To customize which severities add the following flag: --severity UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL # To enable SAST scanning, add: --sast # To enable reachability scanning, add: --reachability diff --git a/CHANGELOG.md b/CHANGELOG.md index fc25c1e9..9e574b3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,28 @@ -Changelog -========= +# Changelog + +## v5.5.0 - 2024-07-29 + +### Changes + +### 🚀 New Features + +- feature: Compatibility woocommerce 9.0.2 wordpress 6.5.5 (#269) +- feature: Gather cart data from pnx and deferred payments (#265) +- feature: Compatibility woocommerce 8.9.2 (#260) +- feature: Compatibility woocommerce 8.8.2 wordpress 6.5 (#248) + +#### Contributors + +@Benjamin-Freoua-Alma, @ClaireAlma, @FranceBe, @carine-bonnafous, @gdraynz, @joyet-simon and @remic-alma + +## v5.4.0 -v5.4.0 ------- * feature: Compatibility Woocommerce Blocks * feature: compatibility Woocommerce 8.7.0 * feature: Compatibility WordPress 6.4.2 -v5.3.0 ------- +## v5.3.0 + * feature: In page with Deferred payments is available * feature: compatibility Woocommerce 8.2.2 * feature: Compatibility WordPress 6.4.1 @@ -18,75 +32,75 @@ v5.3.0 * fix: default plan selection * fix: add flag security for SOC -v5.2.1 ------- +## v5.2.1 + * fix: shortcode widget for XSS injection -v5.2.0 ------- +## v5.2.0 + * feature : HPOS compatibility * feat: Update translations * fix: widget XSS -v5.1.4 ------- +## v5.1.4 + * feat: compatibility Woocommerce 8.2.1 * feat: change fee wording in the BO * fix: In page installment plans - -v5.1.3 ------- + +## v5.1.3 + * fix: update issues from version 1.* and 2.* * feat: compatibility Woocommerce 8.2.0 * feat: Compatibility WordPress 6.3.1 * feat: Update Widget version 3.3.8 -v5.1.2 ------- +## v5.1.2 + * fix: svn marketplace activation issue -v5.1.1 ------- -* fix: css incompatibility with plugin Yith Woocommerce Checkout Manager +## v5.1.1 + +* fix: css incompatibility with plugin Yith Woocommerce Checkout Manager + +## v5.1.0 -v5.1.0 ------- * feature : In Page checkout for all merchants * feat: Update Widget version 3.3.6 -v5.0.6 ------- +## v5.0.6 + * hotfix: Show form errors on checkout in-page when some information are missing for the form * hotfix: Add method payment description for In Page methods -v5.0.5 ------- +## v5.0.5 + * hotfix: fix compatibility betwen In-Page mode and some plugins(colissimo, ...) -v5.0.4 ------- +## v5.0.4 + * feat: compatibility Woocommerce 8.1.1 * change : PHP client version 1.11.2 * fix: compatibility with WC_Checkout::process_checkout() -v5.0.3 ------- +## v5.0.3 + * fix : Incompatibility with Woocommerce Paypal Plugin -v5.0.2 ------- +## v5.0.2 + * fix : In-Page creation order * feat: Compatibility WordPress 6.3.1 -v5.0.1 ------- +## v5.0.1 + * fix: missing shipping information when In Page is activated * fix: missing client information when In Page is activated * fix: loading of the translation files * feat: Compatibility Woocommerce 8.0.3 -v5.0.0 ------- +## v5.0.0 + * feat: In Page checkout * feat: Separate the gateways in the checkout page * feat: Compatibility Woocommerce 8.0.1 @@ -94,20 +108,20 @@ v5.0.0 * feat: Compatibility WordPress 6.3 * feat: Update Widget version 3.3.5 -v4.3.4 ------- +## v4.3.4 + * fix: wrong variable in mismatch -v4.3.3 ------- +## v4.3.3 + * fix: prevent to call Eligibility api with an amount to 0~~ * fix: wrong variable name * fix: Change the return code (500 to 200)on Ipn callback when it's a mismatch or potential fraud * feat: upgrade PHP client to 1.11.1 * feat: Compatibility woocommerce 7.9.0 -v4.3.2 ------- +## v4.3.2 + * fix: SEPA deprecated test * fix: Restore HTML description in gateway * fix: Improve auto-update @@ -117,39 +131,39 @@ v4.3.2 * feat: Compatibility woocommerce 7.8.2 * feat: New widget v3.3.4 -v4.3.1 ------- +## v4.3.1 + * fix: Warning: Cannot modify header information - headers already sent -v4.3.0 ------- +## v4.3.0 + * feat: Implement Pay Now * feat: Compatibility woocommerce 7.8.0 * feat: Update Widget to 3.3.3 -v4.2.5 ------- +## v4.2.5 + * fix: fix the accordion css on checkout * feat: Update widget to 3.3.2 -v4.2.4 ------- +## v4.2.4 + * feat: Allow to customize the gateway title -v4.2.3 ------- +## v4.2.3 + * hotfix: don't do the basic gateway checks during update process -v4.2.2 ------- +## v4.2.2 + * hotfix: Prevent to send multi update thread -v4.2.1 ------- +## v4.2.1 + * hotfix: Plugin update when the gateway is disabled -v4.2.0 ------- +## v4.2.0 + * feat: Share of checkout feature * feat: Encryption Key feature in database * feat: Gather the gateway in checkout (design) @@ -157,21 +171,17 @@ v4.2.0 * feat: Send basket order details for credit * feat: Implement a template system -v4.1.2 ------- +## v4.1.2 * fix: nonce error log issue * feat: Update widget * feat: compatibility with woocommerce 7.6 - -v4.1.1 ------- +## v4.1.1 * fix: PHP 8.0 and 8.1 compatibility -v4.1.0 ------- +## v4.1.0 * feat: compatibility WordPress 6.2 * feat: add logger to the php client @@ -185,37 +195,31 @@ v4.1.0 * fix: migration between versions of alma module * fix: implement best practices Woocommerce and WordPress -v3.2.2 ------- +## v3.2.2 * feat: update widget to 3.1.0 -v3.2.1 ------- +## v3.2.1 * feat: update widget to 3.0.4 * fix: nonce double check issue -v3.2.0 ------- +## v3.2.0 * feat: rebranding 2022 -v3.1.2 ------- +## v3.1.2 * fix: widget translations * fix: php8 compatibility -v3.1.1 ------- +## v3.1.1 * fix: correct bad button text for refund * fix: correcting issue of multiple times the same order note * fix: correcting wording issue in case of order not have a transaction_id -v3.1.0 ------- +## v3.1.0 * feat: implement B2B fields (INT-714 #65) * refact: widget injection (INT-589 #57) @@ -223,54 +227,46 @@ v3.1.0 * feat: refund from woocommerce back-office (INT-510 #69) * fix: alma should be hidden on checkout when test key is selected & user is not admin (INT-586 #71) -v3.0.0 ------- +## v3.0.0 * feat: add nonce to secure checkout form * feat: add script to retrieve widget files * fix: data sanitizing * fix: upd payment fields validation notice messages -v2.6.0 ------- +## v2.6.0 * feat: add BO dynamic check variation * feat: add BO technical fields section * feat: add payment upon shipping * fix: names inversion between shipping & billing addresses -v2.5.2 ------- +## v2.5.2 * feat: ignore & remove composer.lock * fix: allow widget to be injected in builder -v2.5.1 ------- +## v2.5.1 * fix: admin test on unavailable var for other admin pages * fix: check cart eligibility without customer -v2.5.0 ------- +## v2.5.0 * feat: back-office i18n custom fields -v2.4.1 ------- +## v2.4.1 * fix: issue on payment method order on checkout page -v2.4.0 ------- +## v2.4.0 * feat: split payments * fix: model get_cart on null in wysiwyg builder * refactor: Remove the unnecessary boolean literal * refactor: reduce cognitive complexity -v2.3.0 ------- +## v2.3.0 * feat(widget): upgrade CDN from 1.x to 2.x * feat(widget): alma API mode @@ -278,13 +274,11 @@ v2.3.0 * feat(widget): locale * fix: filter eligible feeplans in checkout -v2.2.1 ------- +## v2.2.1 * fix: send user_locale in Alma checkout -v2.2.0 ------- +## v2.2.0 * ci: add shell script bumper * feat: add IT & DE languages @@ -293,8 +287,7 @@ v2.2.0 * fix: update translations without space on go_to_log * fix: widget handler replace excluding by including tax -v2.1.1 ------- +## v2.1.1 * fix: remove upgrade process * fix: usage of woo deprecated get price func @@ -303,8 +296,7 @@ v2.1.1 * refactor: remove unnecessary brackets into strings * word(typo): fix all doc block typo -v2.1.0 ------- +## v2.1.0 * [i18n] allow plugin to works with all locales * [i18n] add locale & advanced address fields on payment creation @@ -313,8 +305,7 @@ v2.1.0 * minor refactorings * enhance docker env -v2.0.0 ------- +## v2.0.0 * Add inline Pay Later (Eligibility V2) * Add dedicated DB migrations processor @@ -324,14 +315,12 @@ v2.0.0 * Root tree refactorisation * Re-design back-office fee plans configuration -v1.3.1 ------- +## v1.3.1 * Avoid Fatal error on (not found) product badge injection * Increase debug log on non displayed bages -v1.3.0 ------- +## v1.3.0 * Remove PHP warnings & do not display widget on out-stock or un-priced product * Minor refactoring @@ -341,24 +330,20 @@ v1.3.0 * Add filter to override locale on checkout payment ALMA API call * Add widget shortcodes -v1.2.3 ------- +## v1.2.3 * Use unpkg.com instead of unpkg.io -v1.2.2 ------- +## v1.2.2 * Do not display a radio button at checkout, when there's only one payment plan available -v1.2.1 ------- +## v1.2.1 * Fixes default min/max amount values being converted to cents multiple times in a row * Improves xdebug configuration -v1.2.0 ------- +## v1.2.0 * Allows merchant to activate 2-, 3- a 4-installment plans, with min and max allowed amounts per plan * Displays an Alma widget on product pages and cart page to inform customers of eligible payment plans @@ -370,73 +355,64 @@ v1.2.0 * Fixes many bugs * Dependencies update -v1.1.7 ------- +## v1.1.7 * Do not require Live key in Test mode and vice versa * Include cancel url, order reference & order URLs in payment data * Don't show the eligibility message in case of API error * Stop checking in vendor dependencies – they'll be included in each release's ZIP file - -v1.1.6 ------- +## v1.1.6 * Dependencies update -v1.1.5 -------- +## v1.1.5 * Fixes display on cart option that was always on * Tested against latest versions of Wordpress/WooCommerce -v1.1.4 -------- +## v1.1.4 * Fixes various warnings when WooCommerce isn't activated * Updates dependencies * Fixes errors when Alma's API is not available -* Attempt to fix problem triggered when calling the Wordpress API +* Attempt to fix problem triggered when calling the Wordpress API -v1.1.3 -------- +## v1.1.3 * Fixes warning when `excluded_products_list` is not set -v1.1.2 ------- +## v1.1.2 * Adds hidden option to be able to choose installments count to use for created payments -v1.1.1 ------- +## v1.1.1 * Fixes bug occurring in `wp-admin/nav-menus.php` -v1.1.0 ------- +## v1.1.0 * Fixes case of "alma" in includes path -* Adds possibility to exclude product categories from installment payments, to help enforce contractual restrictions +* Adds possibility to exclude product categories from installment payments, to help enforce contractual restrictions (i.e. Alma cannot be used to sell/buy virtual/downloadable products such as gift cards, subscriptions, ...) -v1.0.1 ------- +## v1.0.1 + Let's start following semver. * Switches logo file to SVG * Uses latest Eligibility check behaviour to remove one extra call to the API * Adds User-Agent string containing the module's version, WordPress version, WooCommerce, PHP client and PHP versions, -to all requests going to Alma's API. + to all requests going to Alma's API. * Adds support for Alma's IPN callback mechanism -v1.0.0 ------- +## v1.0.0 + This version evolved for a while without any version bump 🤷‍♂️ Features in the latest push to this release: * Module can be configured in Test and Live mode; Test mode only shows Alma's payment method to visitors who are also -administrators of the shop + administrators of the shop * A message displays below the cart to indicate whether the purchase is eligible to monthly installments * The module adds a payment method to the checkout, which redirects the user to Alma's payment page. -If everything goes right (i.e. Customer doesn't cancel, pays the right amount, ... ), the order is validated upon customer return. + If everything goes right (i.e. Customer doesn't cancel, pays the right amount, ... ), the order is validated upon customer return. diff --git a/README.md b/README.md index 8512f872..a3bb36c2 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ - Tags: payments, payment gateway, woocommerce, ecommerce, e-commerce, sell, woo commerce, alma, monthly payments, split payments - Requires at least Wordpress: 4.4 - Requires at least Woocommerce: 3.0.0 -- Tested up to Wordpress: 6.4.2 -- Tested up to Woocommerce: 8.7.0 +- Tested up to Wordpress: 6.5.5 +- Tested up to Woocommerce: 9.0.2 - Requires PHP: 5.6 -- Stable tag: 5.4.0 +- Stable tag: 5.5.0 - License: GPLv3 - License URI: https://www.gnu.org/licenses/gpl-3.0.html - Support: support@getalma.eu diff --git a/cz.yaml b/cz.yaml new file mode 100644 index 00000000..00ed9833 --- /dev/null +++ b/cz.yaml @@ -0,0 +1,7 @@ +--- +commitizen: + name: cz_conventional_commits + tag_format: v$version + update_changelog_on_bump: true + version: 5.4.0 + version_scheme: semver diff --git a/readme.txt b/readme.txt index dcd810c8..1bc0fa6c 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: payments, BNPL, woocommerce, ecommerce, e-commerce, payment gateway, sell, Requires at least: 4.4 Tested up to: 6.3 Requires PHP: 5.6 -Stable tag: 5.4.0 +Stable tag: 5.5.0 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html @@ -51,6 +51,21 @@ You can find more documentation on our [website](https://docs.almapay.com/docs/w == Changelog == +## v5.5.0 - 2024-07-29 + +### Changes + +### 🚀 New Features + +- feature: Compatibility woocommerce 9.0.2 wordpress 6.5.5 (#269) +- feature: Gather cart data from pnx and deferred payments (#265) +- feature: Compatibility woocommerce 8.9.2 (#260) +- feature: Compatibility woocommerce 8.8.2 wordpress 6.5 (#248) + +#### Contributors + +@Benjamin-Freoua-Alma, @ClaireAlma, @FranceBe, @carine-bonnafous, @gdraynz, @joyet-simon and @remic-alma + v5.4.0 ------ * feature: Compatibility Woocommerce Blocks diff --git a/src/alma-gateway-for-woocommerce.php b/src/alma-gateway-for-woocommerce.php index ba1871b7..8a82499c 100644 --- a/src/alma-gateway-for-woocommerce.php +++ b/src/alma-gateway-for-woocommerce.php @@ -3,7 +3,7 @@ * Plugin Name: Alma - Pay in installments or later for WooCommerce * Plugin URI: https://docs.almapay.com/docs/woocommerce * Description: Install Alma and boost your sales! It's simple and guaranteed, your cash flow is secured. 0 commitment, 0 subscription, 0 risk. - * Version: 5.4.0 + * Version: 5.5.0 * Author: Alma * Author URI: https://almapay.com * License: GNU General Public License v3.0 @@ -12,12 +12,12 @@ * Domain Path: /languages * Requires at least: 4.4 * Requires PHP: 5.6 - * Tested up to: 6.4.2 + * Tested up to: 6.5.5 * * @package Alma_Gateway_For_Woocommerce * * WC requires at least: 2.6 - * WC tested up to: 8.7.0 + * WC tested up to: 9.0.2 * * Alma Payment Gateway for WooCommerce is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,7 +38,7 @@ } if ( ! defined( 'ALMA_VERSION' ) ) { - define( 'ALMA_VERSION', '5.4.0' ); + define( 'ALMA_VERSION', '5.5.0' ); } if ( ! defined( 'ALMA_PLUGIN_FILE' ) ) { define( 'ALMA_PLUGIN_FILE', __FILE__ ); diff --git a/src/includes/Admin/Helpers/CheckLegalHelper.php b/src/includes/Admin/Helpers/CheckLegalHelper.php index dea89366..4953de5f 100644 --- a/src/includes/Admin/Helpers/CheckLegalHelper.php +++ b/src/includes/Admin/Helpers/CheckLegalHelper.php @@ -41,12 +41,20 @@ class CheckLegalHelper { */ protected $settings; + /** + * The asset helper. + * + * @var AssetsHelper + */ + protected $asset_helper; + /** * Constructor. */ public function __construct() { - $this->logger = new AlmaLogger(); - $this->settings = new AlmaSettings(); + $this->logger = new AlmaLogger(); + $this->settings = new AlmaSettings(); + $this->asset_helper = new AssetsHelper(); } /** @@ -203,7 +211,7 @@ public function alma_admin_soc_success_message() { return sprintf( // translators: %s: Admin settings url. __( 'The settings have been saved. Refresh the page when ready.', 'alma-gateway-for-woocommerce' ), - esc_url( AssetsHelper::get_admin_setting_url() ) + esc_url( $this->asset_helper->get_admin_setting_url() ) ); } diff --git a/src/includes/Admin/Helpers/FormFieldsHelper.php b/src/includes/Admin/Helpers/FormFieldsHelper.php index 163fd107..a657cbd5 100644 --- a/src/includes/Admin/Helpers/FormFieldsHelper.php +++ b/src/includes/Admin/Helpers/FormFieldsHelper.php @@ -17,16 +17,16 @@ use Alma\API\Entities\FeePlan; use Alma\Woocommerce\Admin\Builders\FormHtmlBuilder; +use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Builders\Helpers\ToolsHelperBuilder; use Alma\Woocommerce\Helpers\AssetsHelper; use Alma\Woocommerce\Helpers\BlockHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\FeePlanHelper; -use Alma\Woocommerce\Helpers\GeneralHelper; use Alma\Woocommerce\Helpers\InternationalizationHelper; +use Alma\Woocommerce\Helpers\PluginHelper; use Alma\Woocommerce\Helpers\ToolsHelper; use Alma\Woocommerce\Services\PaymentUponTriggerService; -use Alma\Woocommerce\AlmaSettings; -use Alma\Woocommerce\Helpers\PluginHelper; /** @@ -70,16 +70,32 @@ class FormFieldsHelper { */ protected $block_helper; + /** + * Tools helper. + * + * @var ToolsHelper + */ + protected $tools_helper; + + /** + * Internationalization Helper. + * + * @var InternationalizationHelper + */ + protected $internationalization_helper; /** * Constructor. */ public function __construct() { - $this->settings_helper = new AlmaSettings(); - $this->payment_upon_trigger = new PaymentUponTriggerService(); - $this->fee_plan_helper = new FeePlanHelper(); - $this->plugin_helper = new PluginHelper(); - $this->block_helper = new BlockHelper(); + $this->settings_helper = new AlmaSettings(); + $this->payment_upon_trigger = new PaymentUponTriggerService(); + $this->fee_plan_helper = new FeePlanHelper(); + $this->plugin_helper = new PluginHelper(); + $this->block_helper = new BlockHelper(); + $tools_helper_builder = new ToolsHelperBuilder(); + $this->tools_helper = $tools_helper_builder->get_instance(); + $this->internationalization_helper = new InternationalizationHelper(); } @@ -309,11 +325,11 @@ protected function init_fee_plan_fields( FeePlan $fee_plan, $default_settings, $ $toggle_key = 'enabled_' . $key; $class = 'alma_fee_plan alma_fee_plan_' . $key; $css = $selected ? '' : 'display: none;'; - $default_min_amount = ToolsHelper::alma_price_from_cents( $this->fee_plan_helper->get_min_purchase_amount( $fee_plan ) ); - $default_max_amount = ToolsHelper::alma_price_from_cents( $fee_plan->max_purchase_amount ); - $merchant_fee_fixed = ToolsHelper::alma_price_from_cents( $fee_plan->merchant_fee_fixed ); + $default_min_amount = $this->tools_helper->alma_price_from_cents( $this->fee_plan_helper->get_min_purchase_amount( $fee_plan ) ); + $default_max_amount = $this->tools_helper->alma_price_from_cents( $fee_plan->max_purchase_amount ); + $merchant_fee_fixed = $this->tools_helper->alma_price_from_cents( $fee_plan->merchant_fee_fixed ); $merchant_fee_variable = $fee_plan->merchant_fee_variable / 100; // percent. - $customer_fee_fixed = ToolsHelper::alma_price_from_cents( $fee_plan->customer_fee_fixed ); + $customer_fee_fixed = $this->tools_helper->alma_price_from_cents( $fee_plan->customer_fee_fixed ); $customer_fee_variable = $fee_plan->customer_fee_variable / 100; // percent. $customer_lending_rate = $fee_plan->customer_lending_rate / 100; // percent. $default_enabled = $default_settings['selected_fee_plan'] === $key ? 'yes' : 'no'; @@ -568,7 +584,7 @@ public function init_general_settings_fields( array $default_settings ) { ), ); - $field_cart_not_eligible_message_gift_cards = InternationalizationHelper::generate_i18n_field( + $field_cart_not_eligible_message_gift_cards = $this->internationalization_helper->generate_i18n_field( 'cart_not_eligible_message_gift_cards', array( 'title' => __( 'Non-eligibility message for excluded products', 'alma-gateway-for-woocommerce' ), @@ -619,7 +635,7 @@ protected function get_custom_fields_payment_method( $payment_method_name, $titl ), ); - $field_payment_method_title = InternationalizationHelper::generate_i18n_field( + $field_payment_method_title = $this->internationalization_helper->generate_i18n_field( 'title_' . $blocks . $payment_method_name, array( 'title' => __( 'Title', 'alma-gateway-for-woocommerce' ), @@ -629,7 +645,7 @@ protected function get_custom_fields_payment_method( $payment_method_name, $titl $default_settings[ 'title_' . $blocks . $payment_method_name ] ); - $field_payment_method_description = InternationalizationHelper::generate_i18n_field( + $field_payment_method_description = $this->internationalization_helper->generate_i18n_field( 'description_' . $blocks . $payment_method_name, array( 'title' => __( 'Description', 'alma-gateway-for-woocommerce' ), @@ -686,7 +702,7 @@ public function init_payment_upon_trigger_fields( $default_settings ) { 'title' => __( 'Trigger typology', 'alma-gateway-for-woocommerce' ), 'description' => __( 'Text that will appear in the payments schedule and in the customer\'s payment authorization email.', 'alma-gateway-for-woocommerce' ), 'default' => $default_settings['payment_upon_trigger_display_text'], - 'options' => GeneralHelper::get_display_texts_keys_and_values(), + 'options' => $this->internalionalization_helper->get_display_texts_keys_and_values(), ), 'payment_upon_trigger_event' => array( 'type' => 'select', diff --git a/src/includes/Admin/Helpers/FormHelper.php b/src/includes/Admin/Helpers/FormHelper.php index c074dfaf..cdb5ef2f 100644 --- a/src/includes/Admin/Helpers/FormHelper.php +++ b/src/includes/Admin/Helpers/FormHelper.php @@ -15,7 +15,9 @@ die( 'Not allowed' ); // Exit if accessed directly. } -use Alma\Woocommerce\Helpers\SettingsHelper as AlmaHelperSettings; +use Alma\Woocommerce\Builders\Helpers\SettingsHelperBuilder; +use Alma\Woocommerce\Helpers\InternationalizationHelper; +use Alma\Woocommerce\Helpers\SettingsHelper; /** * FormHelper. @@ -29,11 +31,31 @@ class FormHelper { */ protected $form_fields_helper; + + /** + * The Settings Helper. + * + * @var SettingsHelper + */ + protected $settings_helper; + + /** + * The internationalization Helper. + * + * @var InternationalizationHelper + */ + protected $internationalization_helper; + + /** * Constructor. */ public function __construct() { - $this->form_fields_helper = new FormFieldsHelper(); + $this->form_fields_helper = new FormFieldsHelper(); + $this->internationalization_helper = new InternationalizationHelper(); + + $settings_helper_builder = new SettingsHelperBuilder(); + $this->settings_helper = $settings_helper_builder->get_instance(); } /** @@ -44,7 +66,7 @@ public function __construct() { * @return array[] */ public function init_form_fields( $show_alma_fee_plans ) { - $default_settings = AlmaHelperSettings::default_settings(); + $default_settings = $this->settings_helper->default_settings(); if ( ! $show_alma_fee_plans ) { return array_merge( diff --git a/src/includes/Admin/Helpers/GeneralHelper.php b/src/includes/Admin/Helpers/GeneralHelper.php index 75243dcb..36d492de 100644 --- a/src/includes/Admin/Helpers/GeneralHelper.php +++ b/src/includes/Admin/Helpers/GeneralHelper.php @@ -22,6 +22,19 @@ */ class GeneralHelper { + /** + * Internalionalization Helper. + * + * @var InternationalizationHelper + */ + protected $internalization_helper; + + /** + * Constructor + */ + public function __construct() { + $this->internalization_helper = new InternationalizationHelper(); + } /** * Returns if the language code in parameter matches the current admin page language. * @@ -29,8 +42,8 @@ class GeneralHelper { * * @return bool */ - public static function is_lang_selected( $code_lang ) { - if ( self::get_current_admin_page_language() === $code_lang ) { + public function is_lang_selected( $code_lang ) { + if ( $this->get_current_admin_page_language() === $code_lang ) { return true; } @@ -42,14 +55,14 @@ public static function is_lang_selected( $code_lang ) { * * @return string */ - public static function get_current_admin_page_language() { + public function get_current_admin_page_language() { $current_admin_page_language = get_locale(); if ( defined( 'ICL_LANGUAGE_CODE' ) ) { $current_admin_page_language = \ICL_LANGUAGE_CODE; } - return InternationalizationHelper::map_locale( $current_admin_page_language ); + return $this->internalization_helper->map_locale( $current_admin_page_language ); } } diff --git a/src/includes/Admin/Helpers/RefundHelper.php b/src/includes/Admin/Helpers/RefundHelper.php index 3aab8990..c0972bcb 100644 --- a/src/includes/Admin/Helpers/RefundHelper.php +++ b/src/includes/Admin/Helpers/RefundHelper.php @@ -15,10 +15,11 @@ die( 'Not allowed' ); // Exit if accessed directly. } -use Alma\Woocommerce\Helpers\ToolsHelper; -use Alma\Woocommerce\AlmaSettings; use Alma\Woocommerce\AlmaLogger; +use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Builders\Helpers\ToolsHelperBuilder; use Alma\Woocommerce\Helpers\ConstantsHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; /** * RefundHelper @@ -58,9 +59,10 @@ class RefundHelper { * Constructor. */ public function __construct() { - $this->logger = new AlmaLogger(); - $this->alma_settings = new AlmaSettings(); - $this->tool_helper = new ToolsHelper(); + $this->logger = new AlmaLogger(); + $this->alma_settings = new AlmaSettings(); + $tools_helper_builder = new ToolsHelperBuilder(); + $this->tool_helper = $tools_helper_builder->get_instance(); } /** diff --git a/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php b/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php index 7eada83e..a4c5b125 100644 --- a/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php +++ b/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php @@ -12,6 +12,7 @@ namespace Alma\Woocommerce\Admin\Helpers; use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Builders\Helpers\ToolsHelperBuilder; use Alma\Woocommerce\Exceptions\ApiSocLastUpdateDatesException; use Alma\Woocommerce\Helpers\OrderHelper; use Alma\Woocommerce\Helpers\ToolsHelper; @@ -59,7 +60,8 @@ class ShareOfCheckoutHelper { public function __construct() { $this->alma_settings = new AlmaSettings(); $this->order_helper = new OrderHelper(); - $this->tool_helper = new ToolsHelper(); + $tools_helper_builder = new ToolsHelperBuilder(); + $this->tool_helper = $tools_helper_builder->get_instance(); $this->check_legal_helper = new CheckLegalHelper(); } diff --git a/src/includes/AlmaLogger.php b/src/includes/AlmaLogger.php index ef4a6753..64de376a 100644 --- a/src/includes/AlmaLogger.php +++ b/src/includes/AlmaLogger.php @@ -9,6 +9,7 @@ namespace Alma\Woocommerce; +use Alma\Woocommerce\Factories\VersionFactory; use Psr\Log\AbstractLogger; use Psr\Log\LogLevel; @@ -26,6 +27,20 @@ class AlmaLogger extends AbstractLogger { const LOG_HANDLE = 'alma'; + /** + * The version factory. + * + * @var VersionFactory + */ + protected $version_factory; + + + /** + * Construct. + */ + public function __construct() { + $this->version_factory = new VersionFactory(); + } /** * Logs with an arbitrary level. * @@ -43,7 +58,7 @@ public function log( $level, $message, array $context = array() ) { // phpcs:ign return; } - $logger = self::get_logger(); + $logger = $this->get_logger(); $levels = array( LogLevel::DEBUG => 'debug', @@ -56,7 +71,7 @@ public function log( $level, $message, array $context = array() ) { // phpcs:ign LogLevel::EMERGENCY => 'emergency', ); - if ( version_compare( \wc()->version, '3.0', '<' ) ) { + if ( version_compare( $this->version_factory->get_version(), '3.0', '<' ) ) { $level = strtoupper( $levels[ $level ] ); $message = "[$level] " . $message; $logger->add( self::LOG_HANDLE, $message ); @@ -89,12 +104,12 @@ protected function can_log() { * * @return \WC_Logger */ - private static function get_logger() { - if ( version_compare( \wc()->version, '3.0', '<' ) ) { + private function get_logger() { + if ( version_compare( $this->version_factory->get_version(), '3.0', '<' ) ) { return new \WC_Logger(); - } else { - return \wc_get_logger(); } + + return \wc_get_logger(); } /** diff --git a/src/includes/AlmaPlugin.php b/src/includes/AlmaPlugin.php index c3b59c09..d77b7544 100644 --- a/src/includes/AlmaPlugin.php +++ b/src/includes/AlmaPlugin.php @@ -16,6 +16,7 @@ use Alma\Woocommerce\Admin\Services\NoticesService; use Alma\Woocommerce\Exceptions\RequirementsException; use Alma\Woocommerce\Exceptions\VersionDeprecated; +use Alma\Woocommerce\Factories\VersionFactory; use Alma\Woocommerce\Gateways\Inpage\InPageGateway; use Alma\Woocommerce\Gateways\Inpage\PayLaterGateway; use Alma\Woocommerce\Gateways\Standard\PayMoreThanFourGateway; @@ -63,6 +64,13 @@ class AlmaPlugin { */ protected $plugin_helper; + /** + * The version factory. + * + * @var VersionFactory + */ + protected $version_factory; + /** * Protected constructor to prevent creating a new instance of the * *Singleton* via the `new` operator from outside of this class. @@ -72,6 +80,7 @@ protected function __construct() { $this->migration_helper = new MigrationHelper(); $this->admin_notices = new NoticesService(); $this->plugin_helper = new PluginHelper(); + $this->version_factory = new VersionFactory(); $this->load_plugin_textdomain(); @@ -126,7 +135,7 @@ protected function check_dependencies() { throw new RequirementsException( __( 'Alma requires WooCommerce to be activated', 'alma-gateway-for-woocommerce' ) ); } - if ( version_compare( wc()->version, '3.0.0', '<' ) ) { + if ( version_compare( $this->version_factory->get_version(), '3.0.0', '<' ) ) { throw new RequirementsException( __( 'Alma requires WooCommerce version 3.0.0 or greater', 'alma-gateway-for-woocommerce' ) ); } diff --git a/src/includes/AlmaSettings.php b/src/includes/AlmaSettings.php index d6012def..53151736 100644 --- a/src/includes/AlmaSettings.php +++ b/src/includes/AlmaSettings.php @@ -15,12 +15,13 @@ use Alma\API\Client; use Alma\API\DependenciesError; -use Alma\API\Endpoints\Results\Eligibility; use Alma\API\Entities\FeePlan; use Alma\API\Entities\Payment; use Alma\API\ParamsError; use Alma\API\RequestError; +use Alma\Woocommerce\Builders\Helpers\SettingsHelperBuilder; use Alma\Woocommerce\Exceptions\ActivationException; +use Alma\Woocommerce\Exceptions\AlmaException; use Alma\Woocommerce\Exceptions\ApiCreatePaymentsException; use Alma\Woocommerce\Exceptions\ApiFetchPaymentsException; use Alma\Woocommerce\Exceptions\ApiFullRefundException; @@ -32,18 +33,15 @@ use Alma\Woocommerce\Exceptions\ApiShareOfCheckoutException; use Alma\Woocommerce\Exceptions\ApiSocLastUpdateDatesException; use Alma\Woocommerce\Exceptions\ApiTriggerPaymentsException; -use Alma\Woocommerce\Exceptions\AlmaException; use Alma\Woocommerce\Exceptions\PlansDefinitionException; use Alma\Woocommerce\Exceptions\WrongCredentialsException; -use Alma\Woocommerce\Helpers\CartHelper; +use Alma\Woocommerce\Factories\PluginFactory; +use Alma\Woocommerce\Factories\VersionFactory; use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\EncryptorHelper; use Alma\Woocommerce\Helpers\FeePlanHelper; -use Alma\Woocommerce\Helpers\GeneralHelper; use Alma\Woocommerce\Helpers\InternationalizationHelper; -use Alma\Woocommerce\Helpers\PaymentHelper; -use Alma\Woocommerce\Helpers\SettingsHelper as AlmaHelperSettings; -use Alma\Woocommerce\Helpers\PlanBuilderHelper; +use Alma\Woocommerce\Helpers\SettingsHelper; /** * Handles settings retrieval from the settings API. @@ -108,13 +106,6 @@ class AlmaSettings { public $alma_client; - /** - * Eligibilities - * - * @var Eligibility|Eligibility[]|array - */ - protected $eligibilities; - /** * The encryptor. * @@ -131,21 +122,47 @@ class AlmaSettings { public $fee_plan_helper; /** - * The cart helper. + * Internationalization Helper. + * + * @var InternationalizationHelper + */ + protected $internationalization_helper; + + /** + * Settings Helper. + * + * @var SettingsHelper + */ + protected $settings_helper; + + /** + * The version factory. * - * @var CartHelper + * @var VersionFactory */ - public $cart_helper; + protected $version_factory; + /** + * The plugin factory. + * + * @var PluginFactory + */ + protected $plugin_factory; + /** * Constructor. */ public function __construct() { - $this->logger = new AlmaLogger(); - $this->encryptor_helper = new EncryptorHelper(); - $this->fee_plan_helper = new FeePlanHelper(); - $this->cart_helper = new CartHelper(); + $this->logger = new AlmaLogger(); + $this->encryptor_helper = new EncryptorHelper(); + $this->fee_plan_helper = new FeePlanHelper(); + $this->internationalization_helper = new InternationalizationHelper(); + $this->version_factory = new VersionFactory(); + $this->plugin_factory = new PluginFactory(); + + $settings_helper_builder = new SettingsHelperBuilder(); + $this->settings_helper = $settings_helper_builder->get_instance(); $this->load_settings(); } @@ -186,7 +203,7 @@ public function get_settings() { $settings['allowed_fee_plans'] = unserialize( $settings['allowed_fee_plans'] ); // phpcs:ignore } - return array_merge( AlmaHelperSettings::default_settings(), $settings ); + return array_merge( $this->settings_helper->default_settings(), $settings ); } /** @@ -420,12 +437,12 @@ public function get_description( $payment_method, $is_blocks = false ) { * @return string */ public function get_i18n( $key ) { - if ( InternationalizationHelper::is_site_multilingual() ) { + if ( $this->internationalization_helper->is_site_multilingual() ) { if ( $this->{$key . '_' . get_locale()} ) { return $this->{$key . '_' . get_locale()}; } - return InternationalizationHelper::get_translated_text( - AlmaHelperSettings::default_settings()[ $key ], + return $this->internationalization_helper->get_translated_text( + $this->settings_helper->default_settings()[ $key ], get_locale() ); } @@ -531,7 +548,7 @@ public function get_alma_client() { ); $this->alma_client->addUserAgentComponent( 'WordPress', get_bloginfo( 'version' ) ); - $this->alma_client->addUserAgentComponent( 'WooCommerce', wc()->version ); + $this->alma_client->addUserAgentComponent( 'WooCommerce', $this->version_factory->get_version() ); $this->alma_client->addUserAgentComponent( 'Alma for WooCommerce', ALMA_VERSION ); return; @@ -827,7 +844,7 @@ public function init_allowed_fee_plans() { if ( ! count( $fee_plans ) ) { $message = __( 'Alma encountered an error when fetching the fee plans.', 'alma-gateway-for-woocommerce' ); - alma_plugin()->admin_notices->add_admin_notice( 'error_get_fee', 'notice notice-error', $message, true ); + $this->plugin_factory->add_admin_notice( 'error_get_fee', 'notice notice-error', $message, true ); throw new ApiPlansException( $message ); } @@ -944,60 +961,6 @@ public function has_pnx_plus_4() { return false; } - /** - * Get eligible plans keys for current cart. - * - * @param array $cart_eligibilities The eligibilities. - * @param string|null $gateway_id The gateway id. - * @return array - */ - public function get_eligible_plans_keys_for_cart( $cart_eligibilities = array(), $gateway_id = null ) { - $alma_plan_builder = new PlanBuilderHelper(); - if ( empty( $cart_eligibilities ) ) { - $cart_eligibilities = $this->get_cart_eligibilities(); - } - - $eligibilities = array_filter( - $this->get_eligible_plans_keys( $this->cart_helper->get_total_in_cents() ), - function ( $key ) use ( $cart_eligibilities ) { - if ( is_array( $cart_eligibilities ) ) { - return array_key_exists( $key, $cart_eligibilities ); - } - - return property_exists( $cart_eligibilities, $key ); - } - ); - - return $alma_plan_builder->order_plans( $eligibilities, $gateway_id ); - } - - /** - * Get eligibilities from cart. - * - * @return Eligibility|Eligibility[]|array - */ - public function get_cart_eligibilities() { - $amount = $this->cart_helper->get_total_in_cents(); - - if ( 0 === $amount ) { - return array(); - } - - if ( ! $this->eligibilities ) { - - try { - $this->get_alma_client(); - $this->eligibilities = $this->alma_client->payments->eligibility( PaymentHelper::get_eligibility_payload_from_cart() ); - } catch ( \Exception $error ) { - $this->logger->error( $error->getMessage(), $error->getTrace() ); - - return array(); - } - } - - return $this->eligibilities; - } - /** * Gets eligible plans keys for amount. * @@ -1068,33 +1031,6 @@ public function should_display_plan( $plan_key, $gateway_id ) { } } - /** - * Get Eligibility / Payment formatted eligible plans definitions for current cart. - * - * @return array - */ - public function get_eligible_plans_for_cart() { - $amount = $this->cart_helper->get_total_in_cents(); - - return array_values( - array_map( - function ( $plan ) use ( $amount ) { - unset( $plan['max_amount'] ); - unset( $plan['min_amount'] ); - if ( isset( $plan['deferred_months'] ) && 0 === $plan['deferred_months'] ) { - unset( $plan['deferred_months'] ); - } - if ( isset( $plan['deferred_days'] ) && 0 === $plan['deferred_days'] ) { - unset( $plan['deferred_days'] ); - } - - return $plan; - }, - $this->get_eligible_plans_definitions( $amount ) - ) - ); - } - /** * Gets eligible plans definitions for amount. * @@ -1117,7 +1053,7 @@ function ( $plan ) use ( $amount ) { * @return string */ public function get_display_text() { - return GeneralHelper::get_display_texts_keys_and_values() [ $this->payment_upon_trigger_display_text ]; + return $this->internationalization_helper->get_display_texts_keys_and_values() [ $this->payment_upon_trigger_display_text ]; } /** diff --git a/src/includes/Blocks/AlmaBlock.php b/src/includes/Blocks/AlmaBlock.php index f93140e2..666ce406 100644 --- a/src/includes/Blocks/AlmaBlock.php +++ b/src/includes/Blocks/AlmaBlock.php @@ -12,13 +12,16 @@ namespace Alma\Woocommerce\Blocks; use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Builders\Helpers\CartHelperBuilder; +use Alma\Woocommerce\Builders\Helpers\GatewayHelperBuilder; +use Alma\Woocommerce\Builders\Helpers\PlanHelperBuilder; +use Alma\Woocommerce\Helpers\AssetsHelper; use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\CheckoutHelper; +use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\GatewayHelper; +use Alma\Woocommerce\Helpers\PlanHelper; use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; -use Alma\Woocommerce\Helpers\AssetsHelper; -use Alma\Woocommerce\Helpers\ConstantsHelper; -use Alma\Woocommerce\Helpers\PlanBuilderHelper; if ( ! defined( 'ABSPATH' ) ) { die( 'Not allowed' ); // Exit if accessed directly. @@ -60,9 +63,9 @@ class AlmaBlock extends AbstractPaymentMethodType { /** * The plan builder. * - * @var PlanBuilderHelper + * @var PlanHelper */ - protected $alma_plan_builder; + protected $alma_plan_helper; /** * Initialize. @@ -70,12 +73,16 @@ class AlmaBlock extends AbstractPaymentMethodType { * @return void */ public function initialize() { - $this->settings = get_option( AlmaSettings::OPTIONS_KEY, array() ); - $this->gateway_helper = new GatewayHelper(); - $this->alma_settings = new AlmaSettings(); - $this->checkout_helper = new CheckoutHelper(); - $this->cart_helper = new CartHelper(); - $this->alma_plan_builder = new PlanBuilderHelper(); + $this->settings = get_option( AlmaSettings::OPTIONS_KEY, array() ); + $gateway_helper_builder = new GatewayHelperBuilder(); + $this->gateway_helper = $gateway_helper_builder->get_instance(); + $this->alma_settings = new AlmaSettings(); + $this->checkout_helper = new CheckoutHelper(); + $cart_helper_builder = new CartHelperBuilder(); + $this->cart_helper = $cart_helper_builder->get_instance(); + + $alma_plan_builder = new PlanHelperBuilder(); + $this->alma_plan_helper = $alma_plan_builder->get_instance(); } /** @@ -149,12 +156,13 @@ public function get_payment_method_data() { $nonce_value = $this->checkout_helper->create_nonce_value( $gateway_id ); // We get the eligibilites. - $eligibilities = $this->alma_settings->get_cart_eligibilities(); - $eligible_plans = $this->alma_settings->get_eligible_plans_keys_for_cart( $eligibilities, $gateway_id ); + $eligibilities = $this->cart_helper->get_cart_eligibilities(); + $eligible_plans = $this->cart_helper->get_eligible_plans_keys_for_cart( $eligibilities ); + $eligible_plans_ordered = $this->alma_plan_helper->order_plans( $eligible_plans, $gateway_id ); - $plans = $this->alma_plan_builder->get_plans_by_keys( $eligible_plans, $eligibilities ); + $plans = $this->alma_plan_helper->get_plans_by_keys( $eligible_plans_ordered, $eligibilities ); - $default_plan = $this->gateway_helper->get_default_plan( $eligible_plans ); + $default_plan = $this->gateway_helper->get_default_plan( $eligible_plans_ordered ); $is_in_page = $this->gateway_helper->is_in_page_gateway( $gateway_id ); diff --git a/src/includes/Builders/Factories/CustomerFactoryBuilder.php b/src/includes/Builders/Factories/CustomerFactoryBuilder.php new file mode 100644 index 00000000..8f8f2f5b --- /dev/null +++ b/src/includes/Builders/Factories/CustomerFactoryBuilder.php @@ -0,0 +1,38 @@ +get_php_factory() + ); + } +} diff --git a/src/includes/Builders/Helpers/CartHelperBuilder.php b/src/includes/Builders/Helpers/CartHelperBuilder.php new file mode 100644 index 00000000..bd7cb84f --- /dev/null +++ b/src/includes/Builders/Helpers/CartHelperBuilder.php @@ -0,0 +1,44 @@ +get_tools_helper(), + $this->get_session_factory(), + $this->get_version_factory(), + $this->get_cart_factory(), + $this->get_alma_settings(), + $this->get_alma_logger(), + $this->get_customer_helper() + ); + } +} diff --git a/src/includes/Builders/Helpers/CustomerHelperBuilder.php b/src/includes/Builders/Helpers/CustomerHelperBuilder.php new file mode 100644 index 00000000..1876c371 --- /dev/null +++ b/src/includes/Builders/Helpers/CustomerHelperBuilder.php @@ -0,0 +1,36 @@ +get_customer_factory() ); + } +} diff --git a/src/includes/Builders/Helpers/GatewayHelperBuilder.php b/src/includes/Builders/Helpers/GatewayHelperBuilder.php new file mode 100644 index 00000000..7887b8e5 --- /dev/null +++ b/src/includes/Builders/Helpers/GatewayHelperBuilder.php @@ -0,0 +1,45 @@ +get_alma_settings(), + $this->get_payment_helper(), + $this->get_checkout_helper(), + $this->get_cart_factory(), + $this->get_product_helper(), + $this->get_core_factory(), + $this->get_cart_helper(), + $this->get_php_helper() + ); + } +} diff --git a/src/includes/Builders/Helpers/PlanHelperBuilder.php b/src/includes/Builders/Helpers/PlanHelperBuilder.php new file mode 100644 index 00000000..4c166245 --- /dev/null +++ b/src/includes/Builders/Helpers/PlanHelperBuilder.php @@ -0,0 +1,41 @@ +get_alma_settings(), + $this->get_gateway_helper(), + $this->get_template_loader_helper(), + $this->get_price_factory() + ); + } +} diff --git a/src/includes/Builders/Helpers/ProductHelperBuilder.php b/src/includes/Builders/Helpers/ProductHelperBuilder.php new file mode 100644 index 00000000..5b1e8e23 --- /dev/null +++ b/src/includes/Builders/Helpers/ProductHelperBuilder.php @@ -0,0 +1,41 @@ +get_alma_logger(), + $this->get_alma_settings(), + $this->get_cart_factory(), + $this->get_core_factory() + ); + } +} diff --git a/src/includes/Builders/Helpers/SettingsHelperBuilder.php b/src/includes/Builders/Helpers/SettingsHelperBuilder.php new file mode 100644 index 00000000..399ca4d0 --- /dev/null +++ b/src/includes/Builders/Helpers/SettingsHelperBuilder.php @@ -0,0 +1,42 @@ +get_internalionalization_helper(), + $this->get_version_factory(), + $this->get_tools_helper(), + $this->get_assets_helper(), + $this->get_plugin_factory() + ); + } +} diff --git a/src/includes/Builders/Helpers/ToolsHelperBuilder.php b/src/includes/Builders/Helpers/ToolsHelperBuilder.php new file mode 100644 index 00000000..5800ae86 --- /dev/null +++ b/src/includes/Builders/Helpers/ToolsHelperBuilder.php @@ -0,0 +1,40 @@ +get_alma_logger(), + $this->get_price_factory(), + $this->get_currency_factory() + ); + } +} diff --git a/src/includes/Exceptions/ActivationException.php b/src/includes/Exceptions/ActivationException.php index 4d5f2551..d1de9cdf 100644 --- a/src/includes/Exceptions/ActivationException.php +++ b/src/includes/Exceptions/ActivationException.php @@ -27,10 +27,12 @@ class ActivationException extends AlmaException { * @param string $environment The environment. */ public function __construct( $environment ) { + $asset_helper = new AssetsHelper(); + $message = sprintf( // translators: %s: Alma dashboard url. __( 'Your Alma account needs to be activated before you can use Alma on your shop.
Go to your Alma dashboard to activate your account.
Refresh the page when ready.', 'alma-gateway-for-woocommerce' ), AssetsHelper::get_alma_dashboard_url( $environment, 'settings' ), - esc_url( AssetsHelper::get_admin_setting_url() ) + esc_url( $asset_helper->get_admin_setting_url() ) ); parent::__construct( $message ); diff --git a/src/includes/Factories/CartFactory.php b/src/includes/Factories/CartFactory.php new file mode 100644 index 00000000..e178a578 --- /dev/null +++ b/src/includes/Factories/CartFactory.php @@ -0,0 +1,46 @@ +cart; + } + + /** + * Get the cart items + * + * @return array + */ + public function get_cart_items() { + $cart = $this->get_cart(); + + if ( ! $cart ) { + return array(); + } + + return $cart->get_cart(); + } +} diff --git a/src/includes/Factories/CoreFactory.php b/src/includes/Factories/CoreFactory.php new file mode 100644 index 00000000..4b9d2268 --- /dev/null +++ b/src/includes/Factories/CoreFactory.php @@ -0,0 +1,48 @@ +php_factory = $php_factory; + } + /** + * Get the customer. + * + * @return \WC_Customer|null + */ + public function get_customer() { + return wc()->customer; + } + + /** + * Get the first name. + * + * @return string|null The first name. + */ + public function get_first_name() { + $customer = $this->get_customer(); + + if ( ! $customer ) { + return null; + } + + return $customer->get_first_name(); + } + + /** + * Get the last name. + * + * @return string|null The last name. + */ + public function get_last_name() { + $customer = $this->get_customer(); + + if ( ! $customer ) { + return null; + } + + return $customer->get_last_name(); + } + + /** + * Get the email. + * + * @return string|null The email. + */ + public function get_email() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_email(); + } + + /** + * Get the billing phone. + * + * @return string|null The billing phone. + */ + public function get_billing_phone() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_phone(); + } + + /** + * Get the billing first name. + * + * @return string|null The billing first name. + */ + public function get_billing_first_name() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_first_name(); + } + + /** + * Get the shipping first name. + * + * @return string|null The shipping first name. + */ + public function get_shipping_first_name() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_first_name(); + } + + /** + * Get the billing last name. + * + * @return string|null The billing last name. + */ + public function get_billing_last_name() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_last_name(); + } + + /** + * Get the shipping last name. + * + * @return string|null The shipping last name. + */ + public function get_shipping_last_name() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_last_name(); + } + + /** + * Get the billing address. + * + * @return string|null The billing address. + */ + public function get_billing_address() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_address(); + } + + /** + * Get the shipping address. + * + * @return string|null The shipping address. + */ + public function get_shipping_address() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_address(); + } + + + /** + * Get the billing address 2 . + * + * @return string|null The billing address 2. + */ + public function get_billing_address_2() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_address_2(); + } + + + /** + * Get the billing shipping 2 . + * + * @return string|null The shipping address 2. + */ + public function get_shipping_address_2() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_address_2(); + } + + /** + * + * Get the billing post code. + * + * @return string|null The billing post code. + */ + public function get_billing_postcode() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_postcode(); + } + + + /** + * + * Get the shipping post code. + * + * @return string|null The shipping post code. + */ + public function get_shipping_postcode() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_postcode(); + } + + + /** + * + * Get the billing city. + * + * @return string|null The billing city. + */ + public function get_billing_city() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_city(); + } + + + /** + * + * Get the shipping city. + * + * @return string|null The shipping city. + */ + public function get_shipping_city() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_city(); + } + + /** + * + * Get the billing country. + * + * @return string|null The billing country. + */ + public function get_billing_country() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_country(); + } + + /** + * + * Get the shipping country. + * + * @return string|null The billing country. + */ + public function get_shipping_country() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_shipping_country(); + } + + /** + * + * Get the billing email. + * + * @return string|null The billing email. + */ + public function get_billing_email() { + $customer = $this->get_customer(); + if ( ! $customer ) { + return null; + } + + return $customer->get_billing_email(); + } + + /** + * Call a method. + * + * @param string $method The method. + * + * @return null|string|false + */ + public function call_method( $method ) { + $customer = $this->get_customer(); + if ( ! $customer ) { + return false; + } + + if ( $this->php_factory->method_exists( $customer, $method ) ) { + return $customer->$method(); + } + + return false; + } + +} diff --git a/src/includes/Factories/PHPFactory.php b/src/includes/Factories/PHPFactory.php new file mode 100644 index 00000000..71b60b1e --- /dev/null +++ b/src/includes/Factories/PHPFactory.php @@ -0,0 +1,35 @@ +admin_notices; + } + + /*** + * Allow this class and other classes to add slug keyed notices (to avoid duplication). + * + * @param string $slug Unique slug. + * @param string $class Css class. + * @param string $message The message. + * @param bool $dismissible Is this dismissible. + * + * @codeCoverageIgnore + * + * @return void + */ + public function add_admin_notice( $slug, $class, $message, $dismissible = false ) { + $this->get_plugin_admin_notice()->add_admin_notice( $slug, $class, $message, $dismissible ); + } +} diff --git a/src/includes/Factories/PriceFactory.php b/src/includes/Factories/PriceFactory.php new file mode 100644 index 00000000..3ed8a091 --- /dev/null +++ b/src/includes/Factories/PriceFactory.php @@ -0,0 +1,58 @@ +session; + } +} diff --git a/src/includes/Factories/VersionFactory.php b/src/includes/Factories/VersionFactory.php new file mode 100644 index 00000000..3551b219 --- /dev/null +++ b/src/includes/Factories/VersionFactory.php @@ -0,0 +1,30 @@ +version; + } +} diff --git a/src/includes/Gateways/AlmaPaymentGateway.php b/src/includes/Gateways/AlmaPaymentGateway.php index d8dde15d..38856bc1 100644 --- a/src/includes/Gateways/AlmaPaymentGateway.php +++ b/src/includes/Gateways/AlmaPaymentGateway.php @@ -18,25 +18,31 @@ use Alma\Woocommerce\Admin\Helpers\GeneralHelper as AdminGeneralHelper; use Alma\Woocommerce\Admin\Helpers\ShareOfCheckoutHelper; use Alma\Woocommerce\AlmaLogger; +use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Builders\Helpers\CartHelperBuilder; +use Alma\Woocommerce\Builders\Helpers\GatewayHelperBuilder; +use Alma\Woocommerce\Builders\Helpers\PlanHelperBuilder; +use Alma\Woocommerce\Builders\Helpers\SettingsHelperBuilder; +use Alma\Woocommerce\Builders\Helpers\ToolsHelperBuilder; use Alma\Woocommerce\Exceptions\ApiClientException; use Alma\Woocommerce\Exceptions\ApiMerchantsException; use Alma\Woocommerce\Exceptions\ApiPlansException; use Alma\Woocommerce\Exceptions\NoCredentialsException; +use Alma\Woocommerce\Factories\CartFactory; +use Alma\Woocommerce\Factories\PluginFactory; use Alma\Woocommerce\Helpers\AssetsHelper; +use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\CheckoutHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\EncryptorHelper; use Alma\Woocommerce\Helpers\GatewayHelper; -use Alma\Woocommerce\Helpers\GeneralHelper; use Alma\Woocommerce\Helpers\OrderHelper; use Alma\Woocommerce\Helpers\PaymentHelper; +use Alma\Woocommerce\Helpers\PlanHelper; use Alma\Woocommerce\Helpers\PluginHelper; use Alma\Woocommerce\Helpers\SettingsHelper; -use Alma\Woocommerce\Helpers\CartHelper; -use Alma\Woocommerce\Helpers\ToolsHelper; -use Alma\Woocommerce\Helpers\PlanBuilderHelper; use Alma\Woocommerce\Helpers\TemplateLoaderHelper; -use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Helpers\ToolsHelper; /** * AlmaPaymentGateway @@ -79,13 +85,6 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { */ public $checkout_helper; - /** - * The general helper. - * - * @var GeneralHelper - */ - public $general_helper; - /** * The assets helper. * @@ -93,13 +92,6 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { */ public $scripts_helper; - /** - * The plan builder. - * - * @var PlanBuilderHelper - */ - public $plan_builder; - /** * The encryptor. * @@ -163,6 +155,43 @@ class AlmaPaymentGateway extends \WC_Payment_Gateway { */ protected $cart_helper; + /** + * The settings helper. + * + * @var SettingsHelper + */ + protected $settings_helper; + + + /** + * The asset helper. + * + * @var AssetsHelper + */ + protected $asset_helper; + + + /** + * The plugin factory. + * + * @var PluginFactory + */ + protected $plugin_factory; + + /** + * The cart factory. + * + * @var CartFactory + */ + protected $cart_factory; + + /** + * Alma plan builder. + * + * @var PlanHelper + */ + protected $alma_plan_helper; + /** * Construct. * @@ -174,26 +203,38 @@ public function __construct( $check_basics = true ) { $this->alma_settings = new AlmaSettings(); $this->check_legal_helper = new CheckLegalHelper(); $this->checkout_helper = new CheckoutHelper(); - $this->gateway_helper = new GatewayHelper(); - $this->general_helper = new GeneralHelper(); + $gateway_helper_builder = new GatewayHelperBuilder(); + $this->gateway_helper = $gateway_helper_builder->get_instance(); $this->scripts_helper = new AssetsHelper(); - $this->plan_builder = new PlanBuilderHelper(); $this->encryption_helper = new EncryptorHelper(); - $this->tool_helper = new ToolsHelper(); + $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->cart_helper = new CartHelper(); - $this->id = $this->get_gateway_id(); - $this->method_title = __( 'Payment in instalments and deferred with Alma - 2x 3x 4x', 'alma-gateway-for-woocommerce' ); - $this->method_description = __( 'Install Alma and boost your sales! It\'s simple and guaranteed, your cash flow is secured. 0 commitment, 0 subscription, 0 risk.', 'alma-gateway-for-woocommerce' ); + $this->asset_helper = new AssetsHelper(); + $alma_plan_builder = new PlanHelperBuilder(); + $this->alma_plan_helper = $alma_plan_builder->get_instance(); + + $this->plugin_factory = new PluginFactory(); + $this->cart_factory = new CartFactory(); + + $settings_helper_builder = new SettingsHelperBuilder(); + $this->settings_helper = $settings_helper_builder->get_instance(); + + $cart_helper_builder = new CartHelperBuilder(); + $this->cart_helper = $cart_helper_builder->get_instance(); + + $this->id = $this->get_gateway_id(); + $this->method_title = __( 'Payment in instalments and deferred with Alma - 2x 3x 4x', 'alma-gateway-for-woocommerce' ); + $this->method_description = __( 'Install Alma and boost your sales! It\'s simple and guaranteed, your cash flow is secured. 0 commitment, 0 subscription, 0 risk.', 'alma-gateway-for-woocommerce' ); if ( $check_basics ) { $this->check_activation(); - SettingsHelper::check_alma_keys( $this->alma_settings->has_keys(), false ); + $this->settings_helper->check_alma_keys( $this->alma_settings->has_keys(), false ); $this->add_filters(); $this->gateway_helper->add_actions(); $this->init_admin_form(); @@ -217,7 +258,7 @@ public function add_filters() { ); add_filter( 'woocommerce_gateway_title', array( $this->gateway_helper, 'woocommerce_gateway_title' ), 10, 2 ); - add_filter( 'allowed_redirect_hosts', array( $this->general_helper, 'alma_domains_whitelist' ) ); + add_filter( 'allowed_redirect_hosts', array( $this->asset_helper, 'alma_domains_whitelist' ) ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, @@ -247,9 +288,9 @@ public function check_activation() { $message = sprintf( // translators: %s: Admin settings url. __( "Thanks for installing Alma! Start by activating Alma's payment method, then set it up to get started.", 'alma-gateway-for-woocommerce' ), - esc_url( AssetsHelper::get_admin_setting_url( false ) ) + esc_url( $this->asset_helper->get_admin_setting_url( false ) ) ); - alma_plugin()->admin_notices->add_admin_notice( 'no_alma_enabled', 'notice notice-warning', $message ); + $this->plugin_factory->add_admin_notice( 'no_alma_enabled', 'notice notice-warning', $message ); } } @@ -267,8 +308,8 @@ public function check_activation() { public function get_option( $key, $empty_value = null ) { $option = parent::get_option( $key, $empty_value ); - if ( ToolsHelper::is_amount_plan_key( $key ) ) { - return strval( ToolsHelper::alma_price_from_cents( $option ) ); + if ( $this->tool_helper->is_amount_plan_key( $key ) ) { + return strval( $this->tool_helper->alma_price_from_cents( $option ) ); } return $option; @@ -280,13 +321,12 @@ public function get_option( $key, $empty_value = null ) { * @return bool Is available. */ public function is_available() { - $tools = new ToolsHelper(); global $wp; if ( ! $this->alma_settings->is_allowed_to_see_alma( wp_get_current_user() ) || is_admin() - || ! $tools->check_currency() + || ! $this->tool_helper->check_currency() || is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'cart' ) || ! empty( $wp->query_vars['order-pay'] ) @@ -296,7 +336,7 @@ public function is_available() { } if ( - wc()->cart === null + $this->cart_factory->get_cart() === null || ! is_checkout() ) { return parent::is_available(); @@ -310,8 +350,9 @@ public function is_available() { return false; } - $eligibilities = $this->alma_settings->get_cart_eligibilities(); - $eligible_plans = $this->alma_settings->get_eligible_plans_keys_for_cart( $eligibilities ); + $eligibilities = $this->cart_helper->get_cart_eligibilities(); + $eligible_plans = $this->cart_helper->get_eligible_plans_keys_for_cart( $eligibilities ); + $eligible_plans = $this->alma_plan_helper->order_plans( $eligible_plans ); $is_eligible = false; @@ -362,7 +403,7 @@ public function init_admin_form() { * @throws NoCredentialsException No credentials Api excetption. */ public function manage_credentials() { - SettingsHelper::check_alma_keys( $this->alma_settings->has_keys() ); + $this->settings_helper->check_alma_keys( $this->alma_settings->has_keys() ); $this->init_alma_client(); @@ -398,11 +439,11 @@ protected function init_alma_client() { $message = sprintf( // translators: %1$s: Admin settings url, %2$s: Admin logs url. __( 'Error while initializing Alma API client.
Activate debug mode and check logs for more details.', 'alma-gateway-for-woocommerce' ), - esc_url( AssetsHelper::get_admin_setting_url() ), + esc_url( $this->asset_helper->get_admin_setting_url() ), esc_url( AssetsHelper::get_admin_logs_url() ) ); - alma_plugin()->admin_notices->add_admin_notice( 'client_keys_build', 'notice notice-error', $message ); + $this->plugin_factory->add_admin_notice( 'client_keys_build', 'notice notice-error', $message ); throw new ApiClientException( $message ); } @@ -419,7 +460,7 @@ protected function init_alma_merchant() { // We try to get the merchants. $this->alma_settings->get_alma_merchant_id(); } catch ( \Exception $e ) { - alma_plugin()->admin_notices->add_admin_notice( 'error_keys', 'notice notice-error', $e->getMessage(), true ); + $this->plugin_factory->add_admin_notice( 'error_keys', 'notice notice-error', $e->getMessage(), true ); throw new ApiMerchantsException( $e ); } @@ -541,7 +582,7 @@ public function clean_credentials( $post_data ) { ) || $this->alma_settings->settings['environment'] !== $post_data['woocommerce_alma_environment'] ) { - $this->alma_settings->settings = SettingsHelper::reset_plans( $this->alma_settings->settings ); + $this->alma_settings->settings = $this->settings_helper->reset_plans( $this->alma_settings->settings ); } } @@ -552,7 +593,7 @@ protected function convert_amounts_to_cents() { $post_data = $this->get_post_data(); foreach ( $this->get_form_fields() as $key => $field ) { - if ( ToolsHelper::is_amount_plan_key( $key ) ) { + if ( $this->tool_helper->is_amount_plan_key( $key ) ) { try { $amount = $this->get_field_value( $key, $field, $post_data ); $this->alma_settings->settings[ $key ] = $this->tool_helper->alma_price_to_cents( $amount ); @@ -575,6 +616,8 @@ protected function convert_amounts_to_cents() { * @noinspection PhpUnused */ public function generate_text_alma_i18n_html( $key, $data ) { + $admin_general_helper = new AdminGeneralHelper(); + $field_key = $this->get_field_key( $key ); $defaults = array( 'title' => '', @@ -615,7 +658,7 @@ public function generate_text_alma_i18n_html( $key, $data ) {