From dc2dbaed92eae93bdf2cb2209f19d8e7241c78aa Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Wed, 31 Jul 2024 14:04:48 +0200
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Show=20Fastlane=20warning=20when=20?=
=?UTF-8?q?DCC=20is=20disabled?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-axo/extensions.php | 1 +
modules/ppcp-axo/services.php | 11 ++++-
.../src/Helper/SettingsNoticeGenerator.php | 49 +++++++++++++++++++
3 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/modules/ppcp-axo/extensions.php b/modules/ppcp-axo/extensions.php
index 3bee1d391..5b2ac3214 100644
--- a/modules/ppcp-axo/extensions.php
+++ b/modules/ppcp-axo/extensions.php
@@ -119,6 +119,7 @@ return array(
'html' => implode(
'',
array(
+ $container->get( 'axo.settings-conflict-notice' ),
$container->get( 'axo.shipping-config-notice' ),
$container->get( 'axo.checkout-config-notice' ),
$container->get( 'axo.incompatible-plugins-notice' ),
diff --git a/modules/ppcp-axo/services.php b/modules/ppcp-axo/services.php
index 31e448bfd..5b468835f 100644
--- a/modules/ppcp-axo/services.php
+++ b/modules/ppcp-axo/services.php
@@ -16,7 +16,6 @@ use WooCommerce\PayPalCommerce\Axo\Helper\SettingsNoticeGenerator;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
-use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
return array(
@@ -164,6 +163,16 @@ return array(
);
},
+ 'axo.settings-conflict-notice' => static function ( ContainerInterface $container ) : string {
+ $settings_notice_generator = $container->get( 'axo.helpers.settings-notice-generator' );
+ assert( $settings_notice_generator instanceof SettingsNoticeGenerator );
+
+ $settings = $container->get( 'wcgateway.settings' );
+ assert( $settings instanceof Settings );
+
+ return $settings_notice_generator->generate_settings_conflict_notice( $settings );
+ },
+
'axo.checkout-config-notice' => static function ( ContainerInterface $container ) : string {
$settings_notice_generator = $container->get( 'axo.helpers.settings-notice-generator' );
assert( $settings_notice_generator instanceof SettingsNoticeGenerator );
diff --git a/modules/ppcp-axo/src/Helper/SettingsNoticeGenerator.php b/modules/ppcp-axo/src/Helper/SettingsNoticeGenerator.php
index f375e2943..503e135cb 100644
--- a/modules/ppcp-axo/src/Helper/SettingsNoticeGenerator.php
+++ b/modules/ppcp-axo/src/Helper/SettingsNoticeGenerator.php
@@ -11,11 +11,32 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Axo\Helper;
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
+use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
+use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
/**
* Class SettingsNoticeGenerator
*/
class SettingsNoticeGenerator {
+ /**
+ * Generates the full HTML of the notification.
+ *
+ * @param string $message HTML of the inner message contents.
+ * @param bool $is_error Whether the provided message is an error. Affects the notice color.
+ *
+ * @return string The full HTML code of the notification, or an empty string.
+ */
+ private function render_notice( string $message, bool $is_error = false ) : string {
+ if ( ! $message ) {
+ return '';
+ }
+
+ return sprintf(
+ '',
+ $is_error ? 'ppcp-notice-error' : '',
+ $message
+ );
+ }
/**
* Generates the checkout notice.
@@ -127,4 +148,32 @@ class SettingsNoticeGenerator {
return '';
}
+
+ /**
+ * Generates a warning notice with instructions on conflicting plugin-internal settings.
+ *
+ * @param Settings $settings The plugin settings container, which is checked for conflicting
+ * values.
+ * @return string
+ */
+ public function generate_settings_conflict_notice( Settings $settings ) : string {
+ $notice_content = '';
+ $is_dcc_enabled = false;
+
+ try {
+ $is_dcc_enabled = $settings->has( 'dcc_enabled' ) && $settings->get( 'dcc_enabled' );
+ // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
+ } catch ( NotFoundException $ignored ) {
+ // Never happens.
+ }
+
+ if ( ! $is_dcc_enabled ) {
+ $notice_content = __(
+ 'Warning: To enable Fastlane and accelerate payments, the Advanced Card Processing payment method must also be enabled.',
+ 'woocommerce-paypal-payments'
+ );
+ }
+
+ return $this->render_notice( $notice_content, true );
+ }
}