From 7d17e4a892bcf54173ccd12ca9032865c1b20d45 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 19 Sep 2023 12:19:31 +0200 Subject: [PATCH] Move vaulted subscriptions intent logic to module --- .../src/Endpoint/OrderEndpoint.php | 2 +- .../ppcp-button/src/Assets/SmartButton.php | 8 +++---- .../src/SavedPaymentCheckerModule.php | 21 ++++++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index 3b232c7f6..ad2bfea59 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -194,7 +194,7 @@ class OrderEndpoint { ): Order { $bearer = $this->bearer->bearer(); $data = array( - 'intent' => ( $this->subscription_helper->cart_contains_subscription() || $this->subscription_helper->current_product_is_subscription() ) ? 'AUTHORIZE' : $this->intent, + 'intent' => apply_filters( 'woocommerce_paypal_payments_saved_payment_subscription_intent', $this->intent ), 'purchase_units' => array_map( static function ( PurchaseUnit $item ) use ( $shipping_preference ): array { $data = $item->to_array(); diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index ce75d62d1..a0e0b4e32 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -1547,16 +1547,14 @@ class SmartButton implements SmartButtonInterface { * @throws NotFoundException If intent is not found. */ private function intent(): string { - $intent = ( $this->settings->has( 'intent' ) ) ? $this->settings->get( 'intent' ) : 'capture'; - $product_intent = $this->subscription_helper->current_product_is_subscription() ? 'authorize' : $intent; - $other_context_intent = $this->subscription_helper->cart_contains_subscription() ? 'authorize' : $intent; - $subscription_mode = $this->settings->has( 'subscriptions_mode' ) ? $this->settings->get( 'subscriptions_mode' ) : ''; if ( $this->subscription_helper->need_subscription_intent( $subscription_mode ) ) { return 'subscription'; } - return $this->context() === 'product' ? $product_intent : $other_context_intent; + $intent = $this->settings->has( 'intent' ) ? $this->settings->get( 'intent' ) : 'capture'; + + return strtolower( apply_filters( 'woocommerce_paypal_payments_saved_payment_subscription_intent', $intent ) ); } /** diff --git a/modules/ppcp-saved-payment-checked/src/SavedPaymentCheckerModule.php b/modules/ppcp-saved-payment-checked/src/SavedPaymentCheckerModule.php index 0523fe0a7..0d1cf52b4 100644 --- a/modules/ppcp-saved-payment-checked/src/SavedPaymentCheckerModule.php +++ b/modules/ppcp-saved-payment-checked/src/SavedPaymentCheckerModule.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\SavedPaymentChecker; +use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface; @@ -32,5 +33,23 @@ class SavedPaymentCheckerModule implements ModuleInterface { /** * {@inheritDoc} */ - public function run( ContainerInterface $c ): void {} + public function run( ContainerInterface $c ): void { + + /** + * Set authorize intent for vaulted subscriptions, so we can void if payment not saved. + */ + add_filter( + 'woocommerce_paypal_payments_saved_payment_subscription_intent', + function( string $intent ) use ( $c ) { + $subscription_helper = $c->get( 'subscription.helper' ); + assert( $subscription_helper instanceof SubscriptionHelper ); + + if ( $subscription_helper->cart_contains_subscription() || $subscription_helper->current_product_is_subscription() ) { + return 'AUTHORIZE'; + } + + return $intent; + } + ); + } }