From 54d8a9b5a906972ad16ee799bc2e9db4a9ba02be Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 10 May 2023 08:05:44 +0300 Subject: [PATCH 01/16] Use order currency instead of shop currency on order-pay page --- .../ppcp-button/src/Assets/SmartButton.php | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 9fbff3bdf..1aae5e3fa 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Assets; use Exception; use Psr\Log\LoggerInterface; +use WC_Order; use WC_Product; use WC_Product_Variation; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; @@ -766,8 +767,6 @@ class SmartButton implements SmartButtonInterface { * @throws NotFoundException If a setting hasn't been found. */ public function script_data(): array { - global $wp; - $is_free_trial_cart = $this->is_free_trial_cart(); $url_params = $this->url_params(); @@ -898,7 +897,7 @@ class SmartButton implements SmartButtonInterface { // phpcs:ignore WordPress.WP.I18n 'shipping_field' => _x( 'Shipping %s', 'checkout-validation', 'woocommerce' ), ), - 'order_id' => 'pay-now' === $this->context() ? absint( $wp->query_vars['order-pay'] ) : 0, + 'order_id' => 'pay-now' === $this->context() ? $this->get_order_pay_id() : 0, 'single_product_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'product' ), 'mini_cart_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' ), 'basic_checkout_validation_enabled' => $this->basic_checkout_validation_enabled, @@ -959,6 +958,19 @@ class SmartButton implements SmartButtonInterface { $params['buyer-country'] = WC()->customer->get_billing_country(); } + if ( 'pay-now' === $this->context() ) { + $wc_order_id = $this->get_order_pay_id(); + if ( $wc_order_id ) { + $wc_order = wc_get_order( $wc_order_id ); + if ( $wc_order instanceof WC_Order ) { + $currency = $wc_order->get_currency(); + if ( $currency ) { + $params['currency'] = $currency; + } + } + } + } + $disable_funding = $this->settings->has( 'disable_funding' ) ? $this->settings->get( 'disable_funding' ) : array(); @@ -1349,4 +1361,19 @@ class SmartButton implements SmartButtonInterface { return false; } + + /** + * Returns the ID of WC order on the order-pay page, or 0. + * + * @return int + */ + protected function get_order_pay_id(): int { + global $wp; + + if ( ! isset( $wp->query_vars['order-pay'] ) ) { + return 0; + } + + return absint( $wp->query_vars['order-pay'] ); + } } From 6ddc364eb2626bf7bdadf64754b1c2fedc5b14d1 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 11 May 2023 15:14:33 +0200 Subject: [PATCH 02/16] Cast $column to string before sending PCP-1675 --- modules/ppcp-wc-gateway/src/WCGatewayModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index f19a244e6..ccdbc9b1e 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -615,7 +615,7 @@ class WCGatewayModule implements ModuleInterface { * @var OrderTablePaymentStatusColumn $payment_status_column */ $payment_status_column = $container->get( 'wcgateway.admin.orders-payment-status-column' ); - $payment_status_column->render( $column, intval( $wc_order_id ) ); + $payment_status_column->render( (string) $column, intval( $wc_order_id ) ); }, 10, 2 From 0322aac244cb79b95a7498124518d3ca4ebdbe23 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 17 May 2023 17:21:24 +0300 Subject: [PATCH 03/16] Trigger WC checkout_error event Triggering WC checkout_error event for our validation, similarly to the WC submit ajax handler. --- .../js/modules/ActionHandler/CheckoutActionHandler.js | 3 +++ .../js/modules/ActionHandler/FreeTrialHandler.js | 4 ++++ modules/ppcp-button/resources/js/modules/ErrorHandler.js | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js b/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js index 7c1b6f238..46378596d 100644 --- a/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js +++ b/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js @@ -102,6 +102,9 @@ class CheckoutActionHandler { } else { errorHandler.message(data.data.message); } + + // fire WC event for other plugins + jQuery( document.body ).trigger( 'checkout_error' , [ errorHandler.currentHtml() ] ); } throw {type: 'create-order-error', data: data.data}; diff --git a/modules/ppcp-button/resources/js/modules/ActionHandler/FreeTrialHandler.js b/modules/ppcp-button/resources/js/modules/ActionHandler/FreeTrialHandler.js index 878e20207..5486576ff 100644 --- a/modules/ppcp-button/resources/js/modules/ActionHandler/FreeTrialHandler.js +++ b/modules/ppcp-button/resources/js/modules/ActionHandler/FreeTrialHandler.js @@ -40,6 +40,10 @@ class FreeTrialHandler { if (errors.length > 0) { this.spinner.unblock(); this.errorHandler.messages(errors); + + // fire WC event for other plugins + jQuery( document.body ).trigger( 'checkout_error' , [ this.errorHandler.currentHtml() ] ); + return; } } catch (error) { diff --git a/modules/ppcp-button/resources/js/modules/ErrorHandler.js b/modules/ppcp-button/resources/js/modules/ErrorHandler.js index 726ef2074..6048360b2 100644 --- a/modules/ppcp-button/resources/js/modules/ErrorHandler.js +++ b/modules/ppcp-button/resources/js/modules/ErrorHandler.js @@ -40,6 +40,15 @@ class ErrorHandler { this._scrollToMessages(); } + /** + * @returns {String} + */ + currentHtml() + { + const messageContainer = this._getMessageContainer(); + return messageContainer.outerHTML; + } + /** * @private * @param {String} text From ad5af164462b6044f2a4ef33942d5fb1994dd43b Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Tue, 30 May 2023 13:57:21 +0200 Subject: [PATCH 04/16] Update wording in buttons previews PCP-1713 --- .../Settings/Fields/pay-later-tab-fields.php | 18 ++++++++++-------- .../Fields/paypal-smart-button-fields.php | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php b/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php index fab61ca83..b3af60d5b 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php +++ b/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php @@ -33,11 +33,13 @@ return function ( ContainerInterface $container, array $fields ): array { $selected_country = $container->get( 'api.shop.country' ); $default_messaging_flex_color = $selected_country === 'US' ? 'white-no-border' : 'white'; - - $render_preview_element = function ( string $id, string $type ): string { + $button_message = __( 'Pay Later Button Preview', 'woocommerce-paypal-payments' ); + $messaging_message = __( 'Pay Later Messaging Preview', 'woocommerce-paypal-payments' ); + $default_message = __( 'Preview', 'woocommerce-paypal-payments' ); + $render_preview_element = function ( string $id, string $type, string $message ): string { return '
-

' . __( 'Preview', 'woocommerce-paypal-payments' ) . '

+

' . $message . '

'; }; @@ -82,7 +84,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_button_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpPayLaterButtonPreview', 'button' ), + 'text' => $render_preview_element( 'ppcpPayLaterButtonPreview', 'button', $button_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, @@ -245,7 +247,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_general_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpGeneralMessagePreview', 'message' ), + 'text' => $render_preview_element( 'ppcpGeneralMessagePreview', 'message', $messaging_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, @@ -369,7 +371,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_product_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpProductMessagePreview', 'message' ), + 'text' => $render_preview_element( 'ppcpProductMessagePreview', 'message', $default_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, @@ -493,7 +495,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_cart_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpCartMessagePreview', 'message' ), + 'text' => $render_preview_element( 'ppcpCartMessagePreview', 'message', $default_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, @@ -617,7 +619,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_checkout_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpCheckoutMessagePreview', 'message' ), + 'text' => $render_preview_element( 'ppcpCheckoutMessagePreview', 'message', $default_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, diff --git a/modules/ppcp-wc-gateway/src/Settings/Fields/paypal-smart-button-fields.php b/modules/ppcp-wc-gateway/src/Settings/Fields/paypal-smart-button-fields.php index f064b1e7b..82fa1d5f3 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Fields/paypal-smart-button-fields.php +++ b/modules/ppcp-wc-gateway/src/Settings/Fields/paypal-smart-button-fields.php @@ -31,7 +31,7 @@ return function ( ContainerInterface $container, array $fields ): array { $render_preview_element = function ( string $id ): string { return '
-

' . __( 'Preview', 'woocommerce-paypal-payments' ) . '

+

' . __( 'Button Styling Preview', 'woocommerce-paypal-payments' ) . '

'; }; From 3cd9e6829b0a27a9e327bc511eae5f5a9949d98e Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 9 May 2023 17:24:03 +0300 Subject: [PATCH 05/16] Do not exclude free items --- modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php b/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php index 9f8f12c5e..e66de1c7e 100644 --- a/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php +++ b/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php @@ -110,7 +110,7 @@ class PurchaseUnitFactory { $items = array_filter( $this->item_factory->from_wc_order( $order ), function ( Item $item ): bool { - return $item->unit_amount()->value() > 0; + return $item->unit_amount()->value() >= 0; } ); $shipping = $this->shipping_factory->from_wc_order( $order ); @@ -166,7 +166,7 @@ class PurchaseUnitFactory { $items = array_filter( $this->item_factory->from_wc_cart( $cart ), function ( Item $item ): bool { - return $item->unit_amount()->value() > 0; + return $item->unit_amount()->value() >= 0; } ); From 729251495f332ba39b96b5721538f209b5a5ed9a Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 9 May 2023 12:01:26 +0300 Subject: [PATCH 06/16] Handle empty acdc fields --- .../js/modules/Renderer/CreditCardRenderer.js | 18 +++++++++++++++++- modules/ppcp-button/src/Assets/SmartButton.php | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js b/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js index 0ddafdd15..ac0d62841 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js @@ -10,6 +10,7 @@ class CreditCardRenderer { this.spinner = spinner; this.cardValid = false; this.formValid = false; + this.emptyFields = new Set(['number', 'cvv', 'expirationDate']); this.currentHostedFieldsInstance = null; } @@ -138,6 +139,12 @@ class CreditCardRenderer { this.formValid = formValid; }); + hostedFields.on('empty', (event) => { + this.emptyFields.add(event.emittedBy); + }); + hostedFields.on('notEmpty', (event) => { + this.emptyFields.delete(event.emittedBy); + }); show(buttonSelector); @@ -249,7 +256,16 @@ class CreditCardRenderer { }); } else { this.spinner.unblock(); - const message = ! this.cardValid ? this.defaultConfig.hosted_fields.labels.card_not_supported : this.defaultConfig.hosted_fields.labels.fields_not_valid; + + let message = this.defaultConfig.labels.error.generic; + if (this.emptyFields.size > 0) { + message = this.defaultConfig.hosted_fields.labels.fields_empty; + } else if (!this.cardValid) { + message = this.defaultConfig.hosted_fields.labels.card_not_supported; + } else if (!this.formValid) { + message = this.defaultConfig.hosted_fields.labels.fields_not_valid; + } + this.errorHandler.message(message); } } diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 7f07c332a..bc4e4d82b 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -893,6 +893,10 @@ class SmartButton implements SmartButtonInterface { 'credit_card_number' => '', 'cvv' => '', 'mm_yy' => __( 'MM/YY', 'woocommerce-paypal-payments' ), + 'fields_empty' => __( + 'Card payment details are missing. Please fill in all required fields.', + 'woocommerce-paypal-payments' + ), 'fields_not_valid' => __( 'Unfortunately, your credit card details are not valid.', 'woocommerce-paypal-payments' From 04bc1ba37f3ddf78bd224fdccd469075091d9936 Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 9 May 2023 12:02:57 +0300 Subject: [PATCH 07/16] Fix js error --- .../resources/js/modules/Renderer/CreditCardRenderer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js b/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js index ac0d62841..fcd20a609 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/CreditCardRenderer.js @@ -131,7 +131,7 @@ class CreditCardRenderer { return event.fields[key].isValid; }); - const className = this._cardNumberFiledCLassNameByCardType(event.cards[0].type); + const className = event.cards.length ? this._cardNumberFiledCLassNameByCardType(event.cards[0].type) : ''; event.fields.number.isValid ? cardNumber.classList.add(className) : this._recreateElementClassAttribute(cardNumber, cardNumberField.className); From c0368e9ada0a9fcf9306ec13199d4f388c519827 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 8 May 2023 09:51:32 +0300 Subject: [PATCH 08/16] Do not show broken card button gateway when no checkout location Removes the card button gateway together with the PayPal gateway when disabled in checkout (since it gets broken anyway). Also refactored the admin notice about disabled gateway to add a message about disabled location. --- modules/ppcp-wc-gateway/services.php | 3 +- .../src/Checkout/DisableGateways.php | 7 +- .../GatewayWithoutPayPalAdminNotice.php | 83 ++++++++++++++----- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 901c1f94a..f5a745076 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -223,7 +223,8 @@ return array( $container->get( 'onboarding.state' ), $container->get( 'wcgateway.settings' ), $container->get( 'wcgateway.is-wc-payments-page' ), - $container->get( 'wcgateway.is-ppcp-settings-page' ) + $container->get( 'wcgateway.is-ppcp-settings-page' ), + $container->get( 'wcgateway.settings.status' ) ); }, 'wcgateway.notice.authorize-order-action' => diff --git a/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php b/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php index bbd9b30d3..a842fc28c 100644 --- a/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php +++ b/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php @@ -82,8 +82,11 @@ class DisableGateways { unset( $methods[ CreditCardGateway::ID ] ); } - if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) && ! $this->session_handler->order() && is_checkout() ) { - unset( $methods[ PayPalGateway::ID ] ); + if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) { + unset( $methods[ CardButtonGateway::ID ] ); + if ( ! $this->session_handler->order() && is_checkout() ) { + unset( $methods[ PayPalGateway::ID ] ); + } } if ( ! $this->needs_to_disable_gateways() ) { diff --git a/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php index 08a20651b..81b057423 100644 --- a/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php @@ -13,11 +13,16 @@ use WC_Payment_Gateway; use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; +use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; /** * Creates the admin message about the gateway being enabled without the PayPal gateway. */ class GatewayWithoutPayPalAdminNotice { + private const NOTICE_OK = ''; + private const NOTICE_DISABLED_GATEWAY = 'disabled_gateway'; + private const NOTICE_DISABLED_LOCATION = 'disabled_location'; + /** * The gateway ID. * @@ -53,27 +58,37 @@ class GatewayWithoutPayPalAdminNotice { */ private $is_ppcp_settings_page; + /** + * The Settings status helper. + * + * @var SettingsStatus|null + */ + protected $settings_status; + /** * ConnectAdminNotice constructor. * - * @param string $id The gateway ID. - * @param State $state The state. - * @param ContainerInterface $settings The settings. - * @param bool $is_payments_page Whether the current page is the WC payment page. - * @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page. + * @param string $id The gateway ID. + * @param State $state The state. + * @param ContainerInterface $settings The settings. + * @param bool $is_payments_page Whether the current page is the WC payment page. + * @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page. + * @param SettingsStatus|null $settings_status The Settings status helper. */ public function __construct( string $id, State $state, ContainerInterface $settings, bool $is_payments_page, - bool $is_ppcp_settings_page + bool $is_ppcp_settings_page, + ?SettingsStatus $settings_status = null ) { $this->id = $id; $this->state = $state; $this->settings = $settings; $this->is_payments_page = $is_payments_page; $this->is_ppcp_settings_page = $is_ppcp_settings_page; + $this->settings_status = $settings_status; } /** @@ -82,8 +97,25 @@ class GatewayWithoutPayPalAdminNotice { * @return Message|null */ public function message(): ?Message { - if ( ! $this->should_display() ) { - return null; + $notice_type = $this->check(); + + switch ( $notice_type ) { + case self::NOTICE_DISABLED_GATEWAY: + /* translators: %1$s the gateway name, %2$s URL. */ + $text = __( + '%1$s cannot be used without the PayPal gateway. Enable the PayPal gateway.', + 'woocommerce-paypal-payments' + ); + break; + case self::NOTICE_DISABLED_LOCATION: + /* translators: %1$s the gateway name, %2$s URL. */ + $text = __( + '%1$s cannot be used without enabling the Checkout location for the PayPal gateway. Enable the Checkout location.', + 'woocommerce-paypal-payments' + ); + break; + default: + return null; } $gateway = $this->get_gateway(); @@ -94,11 +126,7 @@ class GatewayWithoutPayPalAdminNotice { $name = $gateway->get_method_title(); $message = sprintf( - /* translators: %1$s the gateway name, %2$s URL. */ - __( - '%1$s cannot be used without the PayPal gateway. Enable the PayPal gateway.', - 'woocommerce-paypal-payments' - ), + $text, $name, admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway' ) ); @@ -106,22 +134,33 @@ class GatewayWithoutPayPalAdminNotice { } /** - * Whether the message should be displayed. + * Checks whether one of the messages should be displayed. * - * @return bool + * @return string One of the NOTICE_* constants. */ - protected function should_display(): bool { + protected function check(): string { if ( State::STATE_ONBOARDED !== $this->state->current_state() || ( ! $this->is_payments_page && ! $this->is_ppcp_settings_page ) ) { - return false; - } - if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) { - return false; + return self::NOTICE_OK; } - $gateway = $this->get_gateway(); + $gateway = $this->get_gateway(); + $gateway_enabled = $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) ); - return $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) ); + if ( ! $gateway_enabled ) { + return self::NOTICE_OK; + } + + $paypal_enabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ); + if ( ! $paypal_enabled ) { + return self::NOTICE_DISABLED_GATEWAY; + } + + if ( $this->settings_status && ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) { + return self::NOTICE_DISABLED_LOCATION; + } + + return self::NOTICE_OK; } /** From f31fbb89160038c4c096db6d7f6e2dc72edc2e3a Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Sun, 11 Jun 2023 11:52:15 +0200 Subject: [PATCH 09/16] Remove default preview message --- .../src/Settings/Fields/pay-later-tab-fields.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php b/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php index b3af60d5b..f87a7285e 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php +++ b/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php @@ -25,7 +25,7 @@ return function ( ContainerInterface $container, array $fields ): array { $settings = $container->get( 'wcgateway.settings' ); assert( $settings instanceof Settings ); - $vault_enabled = $settings->has( 'vault_enabled' ) && $settings->get( 'vault_enabled' ); + $vault_enabled = false;//$settings->has( 'vault_enabled' ) && $settings->get( 'vault_enabled' ); $pay_later_messaging_enabled_label = $vault_enabled ? __( "You have PayPal vaulting enabled, that's why Pay Later options are unavailable now. You cannot use both features at the same time.", 'woocommerce-paypal-payments' ) @@ -35,7 +35,6 @@ return function ( ContainerInterface $container, array $fields ): array { $default_messaging_flex_color = $selected_country === 'US' ? 'white-no-border' : 'white'; $button_message = __( 'Pay Later Button Preview', 'woocommerce-paypal-payments' ); $messaging_message = __( 'Pay Later Messaging Preview', 'woocommerce-paypal-payments' ); - $default_message = __( 'Preview', 'woocommerce-paypal-payments' ); $render_preview_element = function ( string $id, string $type, string $message ): string { return '
@@ -371,7 +370,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_product_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpProductMessagePreview', 'message', $default_message ), + 'text' => $render_preview_element( 'ppcpProductMessagePreview', 'message', $messaging_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, @@ -495,7 +494,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_cart_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpCartMessagePreview', 'message', $default_message ), + 'text' => $render_preview_element( 'ppcpCartMessagePreview', 'message', $messaging_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, @@ -619,7 +618,7 @@ return function ( ContainerInterface $container, array $fields ): array { ), 'pay_later_checkout_message_preview' => array( 'type' => 'ppcp-text', - 'text' => $render_preview_element( 'ppcpCheckoutMessagePreview', 'message', $default_message ), + 'text' => $render_preview_element( 'ppcpCheckoutMessagePreview', 'message', $messaging_message ), 'screens' => array( State::STATE_ONBOARDED ), 'requirements' => array( 'messages' ), 'gateway' => Settings::PAY_LATER_TAB_ID, From d3249b140a108b16ce2d19740e2aa3b5e7c5e37d Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Wed, 14 Jun 2023 12:15:20 +0200 Subject: [PATCH 10/16] 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 11/16] 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 12/16] 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 13/16] 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 14/16] 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 @@ Date: Tue, 20 Jun 2023 10:30:15 +0300 Subject: [PATCH 15/16] Display funding source on the admin order page Now the payment method title on the admin order editing page includes the funding source instead of just "PayPal" the same as in the order list. --- .../src/Gateway/PayPalGateway.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 0a5835c31..c089ce00e 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -271,6 +271,27 @@ class PayPalGateway extends \WC_Payment_Gateway { $this->order_endpoint = $order_endpoint; } + /** + * Return the gateway's title. + * + * @return string + */ + public function get_title() { + if ( is_admin() ) { + // $theorder and other things for retrieving the order or post info are not available + // in the constructor, so must do it here. + global $theorder; + if ( $theorder instanceof WC_Order ) { + $payment_method_title = $theorder->get_payment_method_title(); + if ( $payment_method_title ) { + $this->title = $payment_method_title; + } + } + } + + return parent::get_title(); + } + /** * Whether the Gateway needs to be setup. * From f4c3caedcf0033d8b7cbcc2a77a820926ed302d9 Mon Sep 17 00:00:00 2001 From: Alex P Date: Sun, 25 Jun 2023 12:50:55 +0300 Subject: [PATCH 16/16] Refresh dcc/pui status caches on plugin update Since #1273 we started clearing these caches on plugin updates. But we are updating these caches only on some admin pages ( https://github.com/woocommerce/woocommerce-paypal-payments/blob/583dea8d99f60f710f54f6d689c5f57694562180/modules/ppcp-wc-gateway/src/WCGatewayModule.php#L421-L428 ) to avoid the API calls on every request in some cases, so DCC may become hidden if not visited these pages after update. Maybe we can do it more frequently now after 92ae16f99. But for now simply refreshing the caches during plugin update. --- modules/ppcp-wc-gateway/src/WCGatewayModule.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index f49fb8f08..db26bad78 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -278,6 +278,15 @@ class WCGatewayModule implements ModuleInterface { $settings->set( 'products_dcc_enabled', false ); $settings->set( 'products_pui_enabled', false ); $settings->persist(); + + // Update caches. + $dcc_status = $c->get( 'wcgateway.helper.dcc-product-status' ); + assert( $dcc_status instanceof DCCProductStatus ); + $dcc_status->dcc_is_active(); + + $pui_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' ); + assert( $pui_status instanceof PayUponInvoiceProductStatus ); + $pui_status->pui_is_active(); } );