From d3249b140a108b16ce2d19740e2aa3b5e7c5e37d Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Wed, 14 Jun 2023 12:15:20 +0200 Subject: [PATCH 1/5] Add unsupported currency admin notice --- modules/ppcp-wc-gateway/services.php | 6 ++ .../Notice/UnsupportedCurrencyAdminNotice.php | 81 +++++++++++++++++++ .../ppcp-wc-gateway/src/WCGatewayModule.php | 8 ++ 3 files changed, 95 insertions(+) create mode 100644 modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 901c1f94a..429191589 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -57,6 +57,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use WooCommerce\PayPalCommerce\WcGateway\Notice\GatewayWithoutPayPalAdminNotice; +use WooCommerce\PayPalCommerce\WcGateway\Notice\UnsupportedCurrencyAdminNotice; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; @@ -208,6 +209,11 @@ return array( $settings = $container->get( 'wcgateway.settings' ); return new ConnectAdminNotice( $state, $settings ); }, + 'wcgateway.notice.currency-unsupported' => static function (ContainerInterface $container): UnsupportedCurrencyAdminNotice { + $state = $container->get('onboarding.state'); + $settings = $container->get('wcgateway.settings'); + return new UnsupportedCurrencyAdminNotice($state, $settings); + }, 'wcgateway.notice.dcc-without-paypal' => static function ( ContainerInterface $container ): GatewayWithoutPayPalAdminNotice { return new GatewayWithoutPayPalAdminNotice( CreditCardGateway::ID, diff --git a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php new file mode 100644 index 000000000..600b64f9b --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php @@ -0,0 +1,81 @@ +state = $state; + $this->settings = $settings; + } + + /** + * Returns the message. + * + * @return Message|null + */ + public function unsupported_currency_message() { + if ( ! $this->should_display() ) { + return null; + } + + $message = sprintf( + /* translators: %1$s the gateway name. */ + __( + 'Attention: Your current WooCommerce store currency is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the PayPal currency support page for more information on supported currencies.', + 'woocommerce-paypal-payments' + ), + "https://developer.paypal.com/api/rest/reference/currency-codes/" + ); + return new Message( $message, 'warning' ); + } + + /** + * Whether the message should display. + * + * @return bool + */ + protected function should_display(): bool { + return $this->state->current_state() === State::STATE_ONBOARDED && ! $this->currency_supported(); + } + + private function currency_supported() + { + //TODO - get the currency from the settings + } +} diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index f49fb8f08..9f3282b58 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -39,6 +39,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use WooCommerce\PayPalCommerce\WcGateway\Notice\GatewayWithoutPayPalAdminNotice; +use WooCommerce\PayPalCommerce\WcGateway\Notice\UnsupportedCurrencyAdminNotice; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Settings\HeaderRenderer; use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer; @@ -197,6 +198,13 @@ class WCGatewayModule implements ModuleInterface { $notices[] = $connect_message; } + $notice = $c->get( 'wcgateway.notice.currency-unsupported' ); + assert( $notice instanceof UnsupportedCurrencyAdminNotice ); + $unsupported_currency_message = $notice->unsupported_currency_message(); + if ( $unsupported_currency_message ) { + $notices[] = $unsupported_currency_message; + } + foreach ( array( $c->get( 'wcgateway.notice.dcc-without-paypal' ), $c->get( 'wcgateway.notice.card-button-without-paypal' ), From 013ffb52137b140ed4b8a9a6126e02ef21382c4d Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Wed, 14 Jun 2023 12:36:55 +0200 Subject: [PATCH 2/5] Check supported currencies in notice method --- modules/ppcp-wc-gateway/services.php | 9 +++--- .../Notice/UnsupportedCurrencyAdminNotice.php | 28 ++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 429191589..069b8c539 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -209,10 +209,11 @@ return array( $settings = $container->get( 'wcgateway.settings' ); return new ConnectAdminNotice( $state, $settings ); }, - 'wcgateway.notice.currency-unsupported' => static function (ContainerInterface $container): UnsupportedCurrencyAdminNotice { - $state = $container->get('onboarding.state'); - $settings = $container->get('wcgateway.settings'); - return new UnsupportedCurrencyAdminNotice($state, $settings); + 'wcgateway.notice.currency-unsupported' => static function ( ContainerInterface $container ): UnsupportedCurrencyAdminNotice { + $state = $container->get( 'onboarding.state' ); + $settings = $container->get( 'wcgateway.settings' ); + $supported_currencies = $container->get( 'api.supported-currencies' ); + return new UnsupportedCurrencyAdminNotice( $state, $settings, $supported_currencies ); }, 'wcgateway.notice.dcc-without-paypal' => static function ( ContainerInterface $container ): GatewayWithoutPayPalAdminNotice { return new GatewayWithoutPayPalAdminNotice( diff --git a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php index 600b64f9b..89c177b01 100644 --- a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php @@ -32,16 +32,24 @@ class UnsupportedCurrencyAdminNotice { * @var ContainerInterface */ private $settings; + /** + * The supported currencies. + * + * @var array + */ + private $supported_currencies; /** * ConnectAdminNotice constructor. * * @param State $state The state. * @param ContainerInterface $settings The settings. + * @param array $supported_currencies The supported currencies. */ - public function __construct( State $state, ContainerInterface $settings ) { - $this->state = $state; - $this->settings = $settings; + public function __construct( State $state, ContainerInterface $settings, array $supported_currencies ) { + $this->state = $state; + $this->settings = $settings; + $this->supported_currencies = $supported_currencies; } /** @@ -60,7 +68,7 @@ class UnsupportedCurrencyAdminNotice { 'Attention: Your current WooCommerce store currency is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the PayPal currency support page for more information on supported currencies.', 'woocommerce-paypal-payments' ), - "https://developer.paypal.com/api/rest/reference/currency-codes/" + 'https://developer.paypal.com/api/rest/reference/currency-codes/' ); return new Message( $message, 'warning' ); } @@ -74,8 +82,14 @@ class UnsupportedCurrencyAdminNotice { return $this->state->current_state() === State::STATE_ONBOARDED && ! $this->currency_supported(); } - private function currency_supported() - { - //TODO - get the currency from the settings + /** + * Whether the currency is supported by PayPal. + * + * @return bool + */ + private function currency_supported(): bool { + $currency = get_woocommerce_currency(); + $supported_currencies = $this->supported_currencies; + return in_array( $currency, $supported_currencies, true ); } } From 356fc386982e42dd3c8bb92facdcab58e87696d0 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 15 Jun 2023 11:39:29 +0200 Subject: [PATCH 3/5] Use api currency service --- modules/ppcp-wc-gateway/services.php | 4 +-- .../Notice/UnsupportedCurrencyAdminNotice.php | 25 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 069b8c539..de4b6ee63 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -211,9 +211,9 @@ return array( }, 'wcgateway.notice.currency-unsupported' => static function ( ContainerInterface $container ): UnsupportedCurrencyAdminNotice { $state = $container->get( 'onboarding.state' ); - $settings = $container->get( 'wcgateway.settings' ); + $shop_currency = $container->get( 'api.shop.currency' ); $supported_currencies = $container->get( 'api.supported-currencies' ); - return new UnsupportedCurrencyAdminNotice( $state, $settings, $supported_currencies ); + return new UnsupportedCurrencyAdminNotice( $state, $shop_currency, $supported_currencies ); }, 'wcgateway.notice.dcc-without-paypal' => static function ( ContainerInterface $container ): GatewayWithoutPayPalAdminNotice { return new GatewayWithoutPayPalAdminNotice( diff --git a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php index 89c177b01..1f8b3b98f 100644 --- a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php @@ -26,12 +26,6 @@ class UnsupportedCurrencyAdminNotice { */ private $state; - /** - * The settings. - * - * @var ContainerInterface - */ - private $settings; /** * The supported currencies. * @@ -39,16 +33,23 @@ class UnsupportedCurrencyAdminNotice { */ private $supported_currencies; + /** + * The shop currency. + * + * @var string + */ + private $shop_currency; + /** * ConnectAdminNotice constructor. * - * @param State $state The state. - * @param ContainerInterface $settings The settings. - * @param array $supported_currencies The supported currencies. + * @param State $state The state. + * @param string $shop_currency The shop currency. + * @param array $supported_currencies The supported currencies. */ - public function __construct( State $state, ContainerInterface $settings, array $supported_currencies ) { + public function __construct( State $state, string $shop_currency, array $supported_currencies ) { $this->state = $state; - $this->settings = $settings; + $this->shop_currency = $shop_currency; $this->supported_currencies = $supported_currencies; } @@ -88,7 +89,7 @@ class UnsupportedCurrencyAdminNotice { * @return bool */ private function currency_supported(): bool { - $currency = get_woocommerce_currency(); + $currency = $this->shop_currency; $supported_currencies = $this->supported_currencies; return in_array( $currency, $supported_currencies, true ); } From 9b697ddaa2fc82d35f374d07a06fa091b9ec2da6 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Mon, 19 Jun 2023 08:12:32 +0200 Subject: [PATCH 4/5] Add currency to message line --- .../src/Notice/UnsupportedCurrencyAdminNotice.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php index 1f8b3b98f..3da5a3b6f 100644 --- a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php @@ -64,11 +64,12 @@ class UnsupportedCurrencyAdminNotice { } $message = sprintf( - /* translators: %1$s the gateway name. */ + /* translators: %1$s the shop currency, 2$s the gateway name. */ __( - 'Attention: Your current WooCommerce store currency is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the PayPal currency support page for more information on supported currencies.', + 'Attention: Your current WooCommerce store currency (%1$s) is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the PayPal currency support page for more information on supported currencies.', 'woocommerce-paypal-payments' ), + $this->shop_currency, 'https://developer.paypal.com/api/rest/reference/currency-codes/' ); return new Message( $message, 'warning' ); From a87748e44ce4907edf51f2ba604a0f26e285c245 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 19 Jun 2023 09:27:37 +0300 Subject: [PATCH 5/5] Fix phpdoc --- .../src/Notice/UnsupportedCurrencyAdminNotice.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php index 3da5a3b6f..27ef14f79 100644 --- a/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/UnsupportedCurrencyAdminNotice.php @@ -1,6 +1,6 @@