diff --git a/modules/ppcp-googlepay/services.php b/modules/ppcp-googlepay/services.php index ae86d129b..6997976bf 100644 --- a/modules/ppcp-googlepay/services.php +++ b/modules/ppcp-googlepay/services.php @@ -941,7 +941,6 @@ return array( 'googlepay.wc-gateway' => static function ( ContainerInterface $container ): GooglePayGateway { return new GooglePayGateway( $container->get( 'wcgateway.order-processor' ), - $container->get( 'wc-subscriptions.helper' ), $container->get( 'api.factory.paypal-checkout-url' ), $container->get( 'wcgateway.processor.refunds' ), $container->get( 'wcgateway.transaction-url-provider' ), diff --git a/modules/ppcp-googlepay/src/GooglePayGateway.php b/modules/ppcp-googlepay/src/GooglePayGateway.php index 144d5da9e..6b02560b2 100644 --- a/modules/ppcp-googlepay/src/GooglePayGateway.php +++ b/modules/ppcp-googlepay/src/GooglePayGateway.php @@ -21,7 +21,6 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\ProcessPaymentTrait; use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; -use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; /** * Class GooglePayGateway @@ -38,13 +37,6 @@ class GooglePayGateway extends WC_Payment_Gateway { */ protected $order_processor; - /** - * The subscription helper. - * - * @var SubscriptionHelper - */ - protected $subscription_helper; - /** * The function return the PayPal checkout URL for the given order ID. * @@ -77,7 +69,6 @@ class GooglePayGateway extends WC_Payment_Gateway { * GooglePayGateway constructor. * * @param OrderProcessor $order_processor The Order Processor. - * @param SubscriptionHelper $subscription_helper The subscription helper. * @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID. * @param RefundProcessor $refund_processor The Refund Processor. * @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order. @@ -85,7 +76,6 @@ class GooglePayGateway extends WC_Payment_Gateway { */ public function __construct( OrderProcessor $order_processor, - SubscriptionHelper $subscription_helper, callable $paypal_checkout_url_factory, RefundProcessor $refund_processor, TransactionUrlProvider $transaction_url_provider, @@ -102,7 +92,6 @@ class GooglePayGateway extends WC_Payment_Gateway { $this->init_form_fields(); $this->init_settings(); $this->order_processor = $order_processor; - $this->subscription_helper = $subscription_helper; $this->paypal_checkout_url_factory = $paypal_checkout_url_factory; $this->refund_processor = $refund_processor; $this->transaction_url_provider = $transaction_url_provider; @@ -155,28 +144,7 @@ class GooglePayGateway extends WC_Payment_Gateway { ); } - /** - * If customer has chosen change Subscription payment. - */ - if ( $this->subscription_helper->has_subscription( $order_id ) && $this->subscription_helper->is_subscription_change_payment() ) { - // phpcs:ignore WordPress.Security.NonceVerification.Missing - $saved_paypal_payment = wc_clean( wp_unslash( $_POST['saved_paypal_payment'] ?? '' ) ); - if ( $saved_paypal_payment ) { - $wc_order->update_meta_data( 'payment_token_id', $saved_paypal_payment ); - $wc_order->save(); - - return $this->handle_payment_success( $wc_order ); - } - } - - /** - * If the WC_Order is paid through the approved webhook. - */ - //phpcs:disable WordPress.Security.NonceVerification.Recommended - if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) { - return $this->handle_payment_success( $wc_order ); - } - //phpcs:enable WordPress.Security.NonceVerification.Recommended + do_action( 'woocommerce_paypal_payments_before_process_order', $wc_order ); try { try { diff --git a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php index df31b45bc..f2ccf3f0f 100644 --- a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php +++ b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php @@ -23,6 +23,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\ProcessPaymentTrait; use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod; @@ -33,7 +34,7 @@ use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; */ class WcSubscriptionsModule implements ModuleInterface { - use TransactionIdHandlingTrait; + use TransactionIdHandlingTrait, ProcessPaymentTrait; /** * {@inheritDoc} @@ -255,6 +256,23 @@ class WcSubscriptionsModule implements ModuleInterface { 10, 3 ); + + add_action( 'woocommerce_paypal_payments_before_process_order', function(WC_Order $wc_order) use ( $c ) { + $subscriptions_helper = $c->get( 'wc-subscriptions.helper' ); + assert( $subscriptions_helper instanceof SubscriptionHelper ); + + // If the customer has chosen to change the subscription payment. + if ( $subscriptions_helper->has_subscription( $wc_order->get_id() ) && $subscriptions_helper->is_subscription_change_payment() ) { + // phpcs:ignore WordPress.Security.NonceVerification.Missing + $saved_paypal_payment = wc_clean( wp_unslash( $_POST['saved_paypal_payment'] ?? '' ) ); + if ( $saved_paypal_payment ) { + $wc_order->update_meta_data( 'payment_token_id', $saved_paypal_payment ); + $wc_order->save(); + + return $this->handle_payment_success( $wc_order ); + } + } + }); } /** diff --git a/modules/ppcp-webhooks/src/WebhookModule.php b/modules/ppcp-webhooks/src/WebhookModule.php index 2458e5b4d..f6343f0c2 100644 --- a/modules/ppcp-webhooks/src/WebhookModule.php +++ b/modules/ppcp-webhooks/src/WebhookModule.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Webhooks; +use WC_Order; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; @@ -16,6 +17,7 @@ use Exception; use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\ProcessPaymentTrait; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulateEndpoint; @@ -27,6 +29,8 @@ use WooCommerce\PayPalCommerce\Webhooks\Status\Assets\WebhooksStatusPageAssets; */ class WebhookModule implements ModuleInterface { + use ProcessPaymentTrait; + /** * {@inheritDoc} */ @@ -158,6 +162,15 @@ class WebhookModule implements ModuleInterface { ); } ); + + // If the WC_Order is paid through the approved webhook. + add_action( 'woocommerce_paypal_payments_before_process_order', function(WC_Order $wc_order) { + //phpcs:disable WordPress.Security.NonceVerification.Recommended + if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) { + return $this->handle_payment_success( $wc_order ); + } + //phpcs:enable + }); } /**