From 84f138a28dd27c5228556feb42614e0e101e6817 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 5 Jul 2024 16:21:16 +0200 Subject: [PATCH 1/4] Move wc subscriptions support to `ppcp-wc-subscriptions` module and remove conditionals based on setting enabled --- .../src/Gateway/CardButtonGateway.php | 26 ++--- .../src/Gateway/CreditCardGateway.php | 41 ++------ .../src/Gateway/PayPalGateway.php | 42 ++------- .../src/WcSubscriptionsModule.php | 94 +++++++++++++++++++ 4 files changed, 120 insertions(+), 83 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php b/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php index 58bc4abfe..3ba41da15 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php @@ -185,29 +185,15 @@ class CardButtonGateway extends \WC_Payment_Gateway { $this->paypal_checkout_url_factory = $paypal_checkout_url_factory; $this->order_button_text = $place_order_button_text; - $this->supports = array( - 'refunds', + $default_support = array( 'products', + 'refunds', ); - if ( - ( $this->config->has( 'vault_enabled' ) && $this->config->get( 'vault_enabled' ) ) - || ( $this->config->has( 'subscriptions_mode' ) && $this->config->get( 'subscriptions_mode' ) === 'subscriptions_api' ) - ) { - array_push( - $this->supports, - 'subscriptions', - 'subscription_cancellation', - 'subscription_suspension', - 'subscription_reactivation', - 'subscription_amount_changes', - 'subscription_date_changes', - 'subscription_payment_method_change', - 'subscription_payment_method_change_customer', - 'subscription_payment_method_change_admin', - 'multiple_subscriptions' - ); - } + $this->supports = array_merge( + $default_support, + apply_filters( 'woocommerce_paypal_payments_card_button_gateway_supports', array() ) + ); $this->method_title = __( 'Standard Card Button', 'woocommerce-paypal-payments' ); $this->method_description = __( 'The separate payment gateway with the card button. If disabled, the button is included in the PayPal gateway.', 'woocommerce-paypal-payments' ); diff --git a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php index cea67537f..b2ce4cbc5 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php @@ -240,38 +240,17 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { $this->wc_payment_tokens = $wc_payment_tokens; $this->logger = $logger; - if ( $state->current_state() === State::STATE_ONBOARDED ) { - $this->supports = array( 'refunds' ); - } - if ( $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' ) ) { - $this->supports = array( - 'refunds', - 'products', - ); + $default_support = array( + 'products', + 'refunds', + 'tokenization', + 'add_payment_method', + ); - if ( $this->config->has( 'vault_enabled_dcc' ) && $this->config->get( 'vault_enabled_dcc' ) ) { - $supports = apply_filters( - 'woocommerce_paypal_payments_credit_card_gateway_vault_supports', - array( - 'subscriptions', - 'subscription_cancellation', - 'subscription_suspension', - 'subscription_reactivation', - 'subscription_amount_changes', - 'subscription_date_changes', - 'subscription_payment_method_change', - 'subscription_payment_method_change_customer', - 'subscription_payment_method_change_admin', - 'multiple_subscriptions', - ) - ); - - $this->supports = array_merge( - $this->supports, - $supports - ); - } - } + $this->supports = array_merge( + $default_support, + apply_filters( 'woocommerce_paypal_payments_credit_card_gateway_supports', array() ) + ); $this->method_title = __( 'Advanced Card Processing', diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 6c6568b8d..4a00b8d5e 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -271,39 +271,17 @@ class PayPalGateway extends \WC_Payment_Gateway { $this->vault_v3_enabled = $vault_v3_enabled; $this->wc_payment_tokens = $wc_payment_tokens; - if ( $this->onboarded ) { - $this->supports = array( 'refunds', 'tokenization' ); - } - if ( $this->config->has( 'enabled' ) && $this->config->get( 'enabled' ) ) { - $this->supports = array( - 'refunds', - 'products', - ); + $default_support = array( + 'products', + 'refunds', + 'tokenization', + 'add_payment_method', + ); - if ( - ( $this->config->has( 'vault_enabled' ) && $this->config->get( 'vault_enabled' ) ) - || ( $this->config->has( 'subscriptions_mode' ) && $this->config->get( 'subscriptions_mode' ) === 'subscriptions_api' ) - ) { - array_push( - $this->supports, - 'tokenization', - 'subscriptions', - 'subscription_cancellation', - 'subscription_suspension', - 'subscription_reactivation', - 'subscription_amount_changes', - 'subscription_date_changes', - 'subscription_payment_method_change', - 'subscription_payment_method_change_customer', - 'subscription_payment_method_change_admin', - 'multiple_subscriptions' - ); - } elseif ( $this->config->has( 'subscriptions_mode' ) && $this->config->get( 'subscriptions_mode' ) === 'subscriptions_api' ) { - $this->supports[] = 'gateway_scheduled_payments'; - } elseif ( $this->config->has( 'vault_enabled_dcc' ) && $this->config->get( 'vault_enabled_dcc' ) ) { - $this->supports[] = 'tokenization'; - } - } + $this->supports = array_merge( + $default_support, + apply_filters( 'woocommerce_paypal_payments_paypal_gateway_supports', array() ) + ); $this->method_title = $this->define_method_title(); $this->method_description = $this->define_method_description(); diff --git a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php index 9d19dc0ac..9d98ea33e 100644 --- a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php +++ b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php @@ -49,6 +49,8 @@ class WcSubscriptionsModule implements ModuleInterface { * {@inheritDoc} */ public function run( ContainerInterface $c ): void { + $this->add_gateways_support( $c ); + add_action( 'woocommerce_scheduled_subscription_payment_' . PayPalGateway::ID, /** @@ -372,4 +374,96 @@ class WcSubscriptionsModule implements ModuleInterface { return $default_fields; } + + /** + * Groups all filters for adding WC Subscriptions gateway support. + * + * @param ContainerInterface $c The container. + * @return void + */ + private function add_gateways_support( ContainerInterface $c ): void { + /** + * Add WC Subscriptions plugin support for PayPal gateway. + */ + add_filter( + 'woocommerce_paypal_payments_paypal_gateway_supports', + function ( array $supports ) use ( $c ): array { + $subscriptions_helper = $c->get( 'wc-subscriptions.helper' ); + assert( $subscriptions_helper instanceof SubscriptionHelper ); + + if ( $subscriptions_helper->plugin_is_active() ) { + $supports = array( + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + } + + return $supports; + } + ); + + /** + * Add WC Subscriptions plugin support for Credit Card gateway. + */ + add_filter( + 'woocommerce_paypal_payments_credit_card_gateway_supports', + function ( array $supports ) use ( $c ): array { + $subscriptions_helper = $c->get( 'wc-subscriptions.helper' ); + assert( $subscriptions_helper instanceof SubscriptionHelper ); + + if ( $subscriptions_helper->plugin_is_active() ) { + $supports = array( + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + } + + return $supports; + } + ); + + /** + * Add WC Subscriptions plugin support for Card Button gateway. + */ + add_filter( + 'woocommerce_paypal_payments_card_button_gateway_supports', + function ( array $supports ) use ( $c ): array { + $subscriptions_helper = $c->get( 'wc-subscriptions.helper' ); + assert( $subscriptions_helper instanceof SubscriptionHelper ); + + if ( $subscriptions_helper->plugin_is_active() ) { + $supports = array( + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + } + + return $supports; + } + ); + } } From e66d733ef5b25af0facc26c9662c2c952a790b65 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 5 Jul 2024 16:44:34 +0200 Subject: [PATCH 2/4] Remove redundant comments --- .../ppcp-wc-subscriptions/src/WcSubscriptionsModule.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php index 9d98ea33e..af5d1c0f7 100644 --- a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php +++ b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php @@ -382,9 +382,6 @@ class WcSubscriptionsModule implements ModuleInterface { * @return void */ private function add_gateways_support( ContainerInterface $c ): void { - /** - * Add WC Subscriptions plugin support for PayPal gateway. - */ add_filter( 'woocommerce_paypal_payments_paypal_gateway_supports', function ( array $supports ) use ( $c ): array { @@ -410,9 +407,6 @@ class WcSubscriptionsModule implements ModuleInterface { } ); - /** - * Add WC Subscriptions plugin support for Credit Card gateway. - */ add_filter( 'woocommerce_paypal_payments_credit_card_gateway_supports', function ( array $supports ) use ( $c ): array { @@ -438,9 +432,6 @@ class WcSubscriptionsModule implements ModuleInterface { } ); - /** - * Add WC Subscriptions plugin support for Card Button gateway. - */ add_filter( 'woocommerce_paypal_payments_card_button_gateway_supports', function ( array $supports ) use ( $c ): array { From fadb78dc3281c22d9da31779ba37060a7995f3fb Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 5 Jul 2024 16:58:02 +0200 Subject: [PATCH 3/4] Add `gateway_scheduled_payments` support when subscription mode is PayPal subscriptions --- .../ppcp-wc-subscriptions/src/WcSubscriptionsModule.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php index af5d1c0f7..9a7c45028 100644 --- a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php +++ b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php @@ -388,6 +388,9 @@ class WcSubscriptionsModule implements ModuleInterface { $subscriptions_helper = $c->get( 'wc-subscriptions.helper' ); assert( $subscriptions_helper instanceof SubscriptionHelper ); + $settings = $c->get( 'wcgateway.settings' ); + assert( $settings instanceof Settings ); + if ( $subscriptions_helper->plugin_is_active() ) { $supports = array( 'subscriptions', @@ -401,6 +404,10 @@ class WcSubscriptionsModule implements ModuleInterface { 'subscription_payment_method_change_admin', 'multiple_subscriptions', ); + + if ( $settings->has( 'subscriptions_mode' ) && $settings->get( 'subscriptions_mode' ) === 'subscriptions_api' ) { + $supports[] = 'gateway_scheduled_payments'; + } } return $supports; From 0ee9907dc9486a866dc51dabb18afe804295c5db Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 9 Jul 2024 15:43:46 +0200 Subject: [PATCH 4/4] Add `gateway_scheduled_payments` support for PayPal subscriptions at subscription level --- .../src/WcSubscriptionsModule.php | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php index 9a7c45028..df31b45bc 100644 --- a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php +++ b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php @@ -230,6 +230,31 @@ class WcSubscriptionsModule implements ModuleInterface { $endpoint->handle_request(); } ); + + // Remove `gateway_scheduled_payments` feature support for non PayPal Subscriptions at subscription level. + add_filter( + 'woocommerce_subscription_payment_gateway_supports', + /** + * Param types removed to avoid third-party issues. + * + * @psalm-suppress MissingClosureParamType + */ + function( $is_supported, $feature, $subscription ) { + if ( + $subscription->get_payment_method() === PayPalGateway::ID + && $feature === 'gateway_scheduled_payments' + ) { + $subscription_connected = $subscription->get_meta( 'ppcp_subscription' ) ?? ''; + if ( ! $subscription_connected ) { + $is_supported = false; + } + } + + return $is_supported; + }, + 10, + 3 + ); } /** @@ -403,11 +428,8 @@ class WcSubscriptionsModule implements ModuleInterface { 'subscription_payment_method_change_customer', 'subscription_payment_method_change_admin', 'multiple_subscriptions', + 'gateway_scheduled_payments', ); - - if ( $settings->has( 'subscriptions_mode' ) && $settings->get( 'subscriptions_mode' ) === 'subscriptions_api' ) { - $supports[] = 'gateway_scheduled_payments'; - } } return $supports;