From 3b90e0521ce9ce8818f907761373a05fb518cea9 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 17 Aug 2022 15:05:36 +0300 Subject: [PATCH 1/4] Hide additional gateways when not onboarded or not available --- .../ppcp-wc-gateway/src/WCGatewayModule.php | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index b6a30b4d1..206ceba0b 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -17,6 +17,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository; +use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer; use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; @@ -296,15 +297,30 @@ class WCGatewayModule implements ModuleInterface { $paypal_gateway_enabled = wc_string_to_bool( $paypal_gateway->get_option( 'enabled' ) ); - $methods[] = $paypal_gateway; - $dcc_applies = $container->get( 'api.helpers.dccapplies' ); + $methods[] = $paypal_gateway; - /** - * The DCC Applies object. - * - * @var DccApplies $dcc_applies - */ - if ( $dcc_applies->for_country_currency() ) { + $onboarding_state = $container->get( 'onboarding.state' ); + assert( $onboarding_state instanceof State ); + + $settings = $container->get( 'wcgateway.settings' ); + assert( $settings instanceof ContainerInterface ); + + $is_our_page = $container->get( 'wcgateway.is-ppcp-settings-page' ); + + if ( $onboarding_state->current_state() !== State::STATE_ONBOARDED ) { + return $methods; + } + + $dcc_applies = $container->get( 'api.helpers.dccapplies' ); + assert( $dcc_applies instanceof DccApplies ); + + if ( $dcc_applies->for_country_currency() && + // Show only if allowed in PayPal account, except when on our settings pages. + // Checking only the cached value instead of the full DCCProductStatus check + // to avoid the API requests all the time. + // So if waiting for account approval, then it will update only when visiting our pages. + ( $is_our_page || ( $settings->has( 'products_dcc_enabled' ) && $settings->get( 'products_dcc_enabled' ) ) ) + ) { $methods[] = $container->get( 'wcgateway.credit-card-gateway' ); } @@ -312,7 +328,11 @@ class WCGatewayModule implements ModuleInterface { $methods[] = $container->get( 'wcgateway.card-button-gateway' ); } - if ( 'DE' === $container->get( 'api.shop.country' ) ) { + $shop_country = $container->get( 'api.shop.country' ); + + if ( 'DE' === $shop_country && + ( $is_our_page || ( $settings->has( 'products_pui_enabled' ) && $settings->get( 'products_pui_enabled' ) ) ) + ) { $methods[] = $container->get( 'wcgateway.pay-upon-invoice-gateway' ); } From d2189d7d22130dd89064a575929ea1fa657edf71 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 17 Aug 2022 15:06:11 +0300 Subject: [PATCH 2/4] Show oxxo only for Mexican shops --- 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 206ceba0b..b748deb2d 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -336,7 +336,7 @@ class WCGatewayModule implements ModuleInterface { $methods[] = $container->get( 'wcgateway.pay-upon-invoice-gateway' ); } - if ( defined( 'PPCP_FLAG_OXXO' ) && PPCP_FLAG_OXXO === true ) { + if ( defined( 'PPCP_FLAG_OXXO' ) && PPCP_FLAG_OXXO === true && 'MX' === $shop_country ) { $methods[] = $container->get( 'wcgateway.oxxo-gateway' ); } From dda243f1c91bb7861ace144905193cef5f839b0c Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 17 Aug 2022 16:16:50 +0300 Subject: [PATCH 3/4] Perform the full dcc/pui status check on the gateway list page --- modules/ppcp-wc-gateway/services.php | 4 +++ .../ppcp-wc-gateway/src/WCGatewayModule.php | 26 ++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 5bd0ffadb..413f4b014 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -141,11 +141,15 @@ return array( $settings = $container->get( 'wcgateway.settings' ); return new DisableGateways( $session_handler, $settings ); }, + 'wcgateway.is-wc-payments-page' => static function ( ContainerInterface $container ): bool { $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; $tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : ''; return 'wc-settings' === $page && 'checkout' === $tab; }, + 'wcgateway.is-wc-gateways-list-page' => static function ( ContainerInterface $container ): bool { + return $container->get( 'wcgateway.is-wc-payments-page' ) && ! isset( $_GET['section'] ); + }, 'wcgateway.is-ppcp-settings-page' => static function ( ContainerInterface $container ): bool { if ( ! $container->get( 'wcgateway.is-wc-payments-page' ) ) { diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index b748deb2d..77356ca5d 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -29,6 +29,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; +use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus; +use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use WooCommerce\PayPalCommerce\WcGateway\Notice\GatewayWithoutPayPalAdminNotice; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; @@ -305,7 +307,8 @@ class WCGatewayModule implements ModuleInterface { $settings = $container->get( 'wcgateway.settings' ); assert( $settings instanceof ContainerInterface ); - $is_our_page = $container->get( 'wcgateway.is-ppcp-settings-page' ); + $is_our_page = $container->get( 'wcgateway.is-ppcp-settings-page' ); + $is_gateways_list_page = $container->get( 'wcgateway.is-wc-gateways-list-page' ); if ( $onboarding_state->current_state() !== State::STATE_ONBOARDED ) { return $methods; @@ -314,12 +317,17 @@ class WCGatewayModule implements ModuleInterface { $dcc_applies = $container->get( 'api.helpers.dccapplies' ); assert( $dcc_applies instanceof DccApplies ); + $dcc_product_status = $container->get( 'wcgateway.helper.dcc-product-status' ); + assert( $dcc_product_status instanceof DCCProductStatus ); + if ( $dcc_applies->for_country_currency() && // Show only if allowed in PayPal account, except when on our settings pages. - // Checking only the cached value instead of the full DCCProductStatus check - // to avoid the API requests all the time. - // So if waiting for account approval, then it will update only when visiting our pages. - ( $is_our_page || ( $settings->has( 'products_dcc_enabled' ) && $settings->get( 'products_dcc_enabled' ) ) ) + // Performing the full DCCProductStatus check only when on the gateway list page + // to avoid sending the API requests all the time. + ( $is_our_page || + ( $is_gateways_list_page && $dcc_product_status->dcc_is_active() ) || + ( $settings->has( 'products_dcc_enabled' ) && $settings->get( 'products_dcc_enabled' ) ) + ) ) { $methods[] = $container->get( 'wcgateway.credit-card-gateway' ); } @@ -328,10 +336,16 @@ class WCGatewayModule implements ModuleInterface { $methods[] = $container->get( 'wcgateway.card-button-gateway' ); } + $pui_product_status = $container->get( 'wcgateway.pay-upon-invoice-product-status' ); + assert( $pui_product_status instanceof PayUponInvoiceProductStatus ); + $shop_country = $container->get( 'api.shop.country' ); if ( 'DE' === $shop_country && - ( $is_our_page || ( $settings->has( 'products_pui_enabled' ) && $settings->get( 'products_pui_enabled' ) ) ) + ( $is_our_page || + ( $is_gateways_list_page && $pui_product_status->pui_is_active() ) || + ( $settings->has( 'products_pui_enabled' ) && $settings->get( 'products_pui_enabled' ) ) + ) ) { $methods[] = $container->get( 'wcgateway.pay-upon-invoice-gateway' ); } From 8b131d5c37d18d4129353aedee66dd93cfc0d71d Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 17 Aug 2022 16:17:29 +0300 Subject: [PATCH 4/4] Catch all exceptions during dcc/pui status check --- modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php | 7 +++---- .../src/Helper/PayUponInvoiceProductStatus.php | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php index ba48b4b54..c84006a0b 100644 --- a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php @@ -9,9 +9,9 @@ declare( strict_types=1 ); namespace WooCommerce\PayPalCommerce\WcGateway\Helper; +use Throwable; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct; -use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; /** @@ -22,7 +22,7 @@ class DCCProductStatus { /** * Caches the status for the current load. * - * @var string|null + * @var bool|null */ private $current_status_cache; /** @@ -57,7 +57,6 @@ class DCCProductStatus { * Whether the active/subscribed products support DCC. * * @return bool - * @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException Should a setting not be found. */ public function dcc_is_active() : bool { if ( is_bool( $this->current_status_cache ) ) { @@ -70,7 +69,7 @@ class DCCProductStatus { try { $seller_status = $this->partners_endpoint->seller_status(); - } catch ( RuntimeException $error ) { + } catch ( Throwable $error ) { $this->current_status_cache = false; return false; } diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php index 60b72dc7f..6512e4ded 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php @@ -9,11 +9,10 @@ declare( strict_types=1 ); namespace WooCommerce\PayPalCommerce\WcGateway\Helper; +use Throwable; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct; -use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; -use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; /** * Class PayUponInvoiceProductStatus @@ -70,7 +69,7 @@ class PayUponInvoiceProductStatus { try { $seller_status = $this->partners_endpoint->seller_status(); - } catch ( RuntimeException $error ) { + } catch ( Throwable $error ) { $this->current_status_cache = false; return false; }