From ffb2de496daff371f4353aabecb6f05c6cbc0272 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Wed, 14 Feb 2024 18:17:03 +0000
Subject: [PATCH] Refactor AXO module
---
modules/ppcp-axo/extensions.php | 19 +-
modules/ppcp-axo/services.php | 21 +++
modules/ppcp-axo/src/Assets/AxoManager.php | 166 ++++++++++++++++++
modules/ppcp-axo/src/AxoModule.php | 44 ++---
modules/ppcp-axo/src/Gateway/AxoGateway.php | 21 ++-
.../src/Helper/PropertiesDictionary.php | 20 ++-
.../ppcp-wc-gateway/resources/css/common.scss | 27 +--
7 files changed, 258 insertions(+), 60 deletions(-)
create mode 100644 modules/ppcp-axo/src/Assets/AxoManager.php
diff --git a/modules/ppcp-axo/extensions.php b/modules/ppcp-axo/extensions.php
index 0d0b7665c..942c856f1 100644
--- a/modules/ppcp-axo/extensions.php
+++ b/modules/ppcp-axo/extensions.php
@@ -84,6 +84,23 @@ return array(
'requirements' => array(),
'gateway' => 'paypal',
),
+ 'axo_email_widget' => array(
+ 'title' => __( 'Email Widget', 'woocommerce-paypal-payments' ),
+ 'type' => 'select',
+ 'desc_tip' => true,
+ 'description' => __(
+ 'This controls if the Hosted Email Widget should be used.',
+ 'woocommerce-paypal-payments'
+ ),
+ 'classes' => array( 'ppcp-field-indent' ),
+ 'class' => array(),
+ 'input_class' => array( 'wc-enhanced-select' ),
+ 'default' => 'pay',
+ 'options' => PropertiesDictionary::email_widget_options(),
+ 'screens' => array( State::STATE_ONBOARDED ),
+ 'gateway' => 'paypal',
+ 'requirements' => array(),
+ ),
'axo_address_widget' => array(
'title' => __( 'Address Widget', 'woocommerce-paypal-payments' ),
'type' => 'select',
@@ -101,7 +118,7 @@ return array(
'gateway' => 'paypal',
'requirements' => array(),
),
- 'axo_payment_widget' => array(
+ 'axo_payment_widget' => array(
'title' => __( 'Payment Widget', 'woocommerce-paypal-payments' ),
'type' => 'select',
'desc_tip' => true,
diff --git a/modules/ppcp-axo/services.php b/modules/ppcp-axo/services.php
index 76c87944c..0e11e2742 100644
--- a/modules/ppcp-axo/services.php
+++ b/modules/ppcp-axo/services.php
@@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Axo;
+use WooCommerce\PayPalCommerce\Axo\Assets\AxoManager;
+use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
@@ -24,4 +26,23 @@ return array(
);
},
+ 'axo.manager' => static function ( ContainerInterface $container ): AxoManager {
+ return new AxoManager(
+ $container->get( 'axo.url' ),
+ $container->get( 'ppcp.asset-version' ),
+ $container->get( 'session.handler' ),
+ $container->get( 'wcgateway.settings' ),
+ $container->get( 'onboarding.environment' ),
+ $container->get( 'wcgateway.settings.status' ),
+ $container->get( 'api.shop.currency' ),
+ $container->get( 'woocommerce.logger.woocommerce' )
+ );
+ },
+
+ 'axo.gateway' => static function ( ContainerInterface $container ): AxoGateway {
+ return new AxoGateway(
+ $container->get( 'wcgateway.settings' )
+ );
+ },
+
);
diff --git a/modules/ppcp-axo/src/Assets/AxoManager.php b/modules/ppcp-axo/src/Assets/AxoManager.php
new file mode 100644
index 000000000..af4c8fd14
--- /dev/null
+++ b/modules/ppcp-axo/src/Assets/AxoManager.php
@@ -0,0 +1,166 @@
+module_url = $module_url;
+ $this->version = $version;
+ $this->session_handler = $session_handler;
+ $this->settings = $settings;
+ $this->environment = $environment;
+ $this->settings_status = $settings_status;
+ $this->currency = $currency;
+ $this->logger = $logger;
+ }
+
+ /**
+ * Enqueues scripts/styles.
+ *
+ * @return void
+ */
+ public function enqueue() {
+
+ // Register styles.
+ wp_register_style(
+ 'wc-ppcp-axo',
+ untrailingslashit( $this->module_url ) . '/assets/css/styles.css',
+ array(),
+ $this->version
+ );
+ wp_enqueue_style( 'wc-ppcp-axo' );
+
+ // Register scripts.
+ wp_register_script(
+ 'wc-ppcp-axo',
+ untrailingslashit( $this->module_url ) . '/assets/js/boot.js',
+ array(),
+ $this->version,
+ true
+ );
+ wp_enqueue_script( 'wc-ppcp-axo' );
+
+ wp_localize_script(
+ 'wc-ppcp-axo',
+ 'wc_ppcp_axo',
+ $this->script_data()
+ );
+ }
+
+ /**
+ * The configuration for AXO.
+ *
+ * @return array
+ */
+ private function script_data() {
+ $email_widget = $this->settings->has( 'axo_email_widget' ) ? $this->settings->get( 'axo_email_widget' ) : null;
+ $address_widget = $this->settings->has( 'axo_address_widget' ) ? $this->settings->get( 'axo_address_widget' ) : null;
+ $payment_widget = $this->settings->has( 'axo_payment_widget' ) ? $this->settings->get( 'axo_payment_widget' ) : null;
+
+ return array(
+ 'widgets' => array(
+ 'email' => $email_widget ?: 'render',
+ 'address' => $address_widget ?: 'render',
+ 'payment' => $payment_widget ?: 'render',
+ )
+ );
+ }
+
+}
diff --git a/modules/ppcp-axo/src/AxoModule.php b/modules/ppcp-axo/src/AxoModule.php
index 3561c707b..42e55282f 100644
--- a/modules/ppcp-axo/src/AxoModule.php
+++ b/modules/ppcp-axo/src/AxoModule.php
@@ -9,7 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Axo;
-use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
+use WooCommerce\PayPalCommerce\Axo\Assets\AxoManager;
+use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@@ -37,11 +38,7 @@ class AxoModule implements ModuleInterface {
add_filter(
'woocommerce_payment_gateways',
function ( $methods ) use ( $c ): array {
- $settings = $c->get( 'wcgateway.settings' );
-
- $methods[] = new AxoGateway(
- $settings
- );
+ $methods[] = $c->get('axo.gateway');
return $methods;
},
1,
@@ -69,36 +66,15 @@ class AxoModule implements ModuleInterface {
add_action(
'wp_enqueue_scripts',
static function () use ( $c ) {
- $module_url = $c->get( 'axo.url' );
- $version = '1';
+ $manager = $c->get( 'axo.manager' );
+ assert( $manager instanceof AxoManager );
- // Register styles.
- wp_register_style(
- 'wc-ppcp-axo',
- untrailingslashit( $module_url ) . '/assets/css/styles.css',
- array(),
- $version
- );
- wp_enqueue_style( 'wc-ppcp-axo' );
-
- // Register scripts.
- wp_register_script(
- 'wc-ppcp-axo',
- untrailingslashit( $module_url ) . '/assets/js/boot.js',
- array(),
- $version,
- true
- );
- wp_enqueue_script( 'wc-ppcp-axo' );
-
- wp_localize_script(
- 'wc-ppcp-axo',
- 'wc_ppcp_axo',
- array(
- // TODO
- )
- );
+ $smart_button = $c->get( 'button.smart-button' );
+ assert( $smart_button instanceof SmartButtonInterface );
+ if ( $smart_button->should_load_ppcp_script() ) {
+ $manager->enqueue();
+ }
}
);
diff --git a/modules/ppcp-axo/src/Gateway/AxoGateway.php b/modules/ppcp-axo/src/Gateway/AxoGateway.php
index 9e8bd5817..f35825a01 100644
--- a/modules/ppcp-axo/src/Gateway/AxoGateway.php
+++ b/modules/ppcp-axo/src/Gateway/AxoGateway.php
@@ -24,24 +24,30 @@ class AxoGateway extends WC_Payment_Gateway {
*
* @var ContainerInterface
*/
- protected $config;
+ protected $ppcp_settings;
/**
* AXOGateway constructor.
*
- * @param ContainerInterface $config The settings.
+ * @param ContainerInterface $ppcp_settings The settings.
*/
public function __construct(
- ContainerInterface $config
+ ContainerInterface $ppcp_settings
) {
- $this->config = $config;
+ $this->ppcp_settings = $ppcp_settings;
$this->id = self::ID;
$this->method_title = __( 'Fastlane Debit & Credit Cards', 'woocommerce-paypal-payments' );
$this->method_description = __( 'Fastlane Debit & Credit Cards', 'woocommerce-paypal-payments' );
- $this->title = $this->get_option( 'title', $this->method_title );
+ $is_axo_enabled = $this->ppcp_settings->has( 'axo_enabled' ) && $this->ppcp_settings->get( 'axo_enabled' );
+ $this->update_option( 'enabled', $is_axo_enabled ? 'yes' : 'no' );
+
+ $this->title = $this->ppcp_settings->has( 'axo_gateway_title' )
+ ? $this->ppcp_settings->get( 'axo_gateway_title' )
+ : $this->get_option( 'title', $this->method_title );
+
$this->description = $this->get_option( 'description', __( '', 'woocommerce-paypal-payments' ) );
$this->init_form_fields();
@@ -63,9 +69,6 @@ class AxoGateway extends WC_Payment_Gateway {
// $this->icon = esc_url( $this->module_url ) . 'assets/images/axo.svg'; // TODO
// $this->environment = $environment;
-
- $is_axo_enabled = $this->config->has( 'axo_enabled' ) && $this->config->get( 'axo_enabled' );
- $this->update_option( 'enabled', $is_axo_enabled ? 'yes' : 'no' );
}
/**
@@ -117,7 +120,7 @@ class AxoGateway extends WC_Payment_Gateway {
// */
// private function is_enabled(): bool {
// return true;
-// //return $this->config->has( 'axo_enabled' ) && $this->config->get( 'axo_enabled' ); // TODO
+// //return $this->ppcp_settings->has( 'axo_enabled' ) && $this->ppcp_settings->get( 'axo_enabled' ); // TODO
// }
/**
diff --git a/modules/ppcp-axo/src/Helper/PropertiesDictionary.php b/modules/ppcp-axo/src/Helper/PropertiesDictionary.php
index e1fc70ece..714e635f5 100644
--- a/modules/ppcp-axo/src/Helper/PropertiesDictionary.php
+++ b/modules/ppcp-axo/src/Helper/PropertiesDictionary.php
@@ -14,6 +14,18 @@ namespace WooCommerce\PayPalCommerce\Axo\Helper;
*/
class PropertiesDictionary {
+ /**
+ * Returns the possible list of possible email widget options.
+ *
+ * @return array
+ */
+ public static function email_widget_options(): array {
+ return array(
+ 'render' => __( 'Render email input', 'woocommerce-paypal-payments' ),
+ 'use_widget' => __( 'Use email widget', 'woocommerce-paypal-payments' ),
+ );
+ }
+
/**
* Returns the possible list of possible address widget options.
*
@@ -21,8 +33,8 @@ class PropertiesDictionary {
*/
public static function address_widget_options(): array {
return array(
- 'render' => __( 'Render address options.', 'woocommerce-paypal-payments' ),
- 'use_widget' => __( 'Use address widget.', 'woocommerce-paypal-payments' ),
+ 'render' => __( 'Render address options list', 'woocommerce-paypal-payments' ),
+ 'use_widget' => __( 'Use address widget', 'woocommerce-paypal-payments' ),
);
}
@@ -33,8 +45,8 @@ class PropertiesDictionary {
*/
public static function payment_widget_options(): array {
return array(
- 'render' => __( 'Render payment options.', 'woocommerce-paypal-payments' ),
- 'use_widget' => __( 'Use payment widget.', 'woocommerce-paypal-payments' ),
+ 'render' => __( 'Render payment options list', 'woocommerce-paypal-payments' ),
+ 'use_widget' => __( 'Use payment widget', 'woocommerce-paypal-payments' ),
);
}
diff --git a/modules/ppcp-wc-gateway/resources/css/common.scss b/modules/ppcp-wc-gateway/resources/css/common.scss
index 8217bd3b3..3cc34ae01 100644
--- a/modules/ppcp-wc-gateway/resources/css/common.scss
+++ b/modules/ppcp-wc-gateway/resources/css/common.scss
@@ -11,9 +11,21 @@
opacity: 0.5;
}
-.ppcp-field-indent {
- th {
- padding-left: 20px;
+.ppcp-settings-field {
+ border-left: 1px solid transparent;
+ border-right: 1px solid transparent;
+
+ &.ppcp-field-indent {
+ background-color: #f9f9f9;
+ border: 1px solid #c3c4c7;
+
+ th {
+ padding-left: 20px;
+ }
+
+ & + .ppcp-field-indent {
+ border-top: 2px solid transparent;
+ }
}
}
@@ -39,12 +51,3 @@
font-weight: bold;
}
}
-
-.ppcp-field-indent {
- background-color: #f9f9f9;
- border: 1px solid #c3c4c7;
-
- & + .ppcp-field-indent {
- border-top: 2px solid transparent;
- }
-}