From 51a6ffad2def66ae70d55322f9e0bbe915582e9b Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 2 Jan 2025 18:46:27 +0100 Subject: [PATCH] Unlink payment processor from deprecated helpers --- src/Gateway/DeprecatedGatewayBuilder.php | 5 -- src/Gateway/GatewayModule.php | 4 +- src/Gateway/MolliePaymentGatewayHandler.php | 8 --- src/Gateway/inc/services.php | 41 +++++++++++-- src/Payment/PaymentProcessor.php | 58 ++++++++----------- .../Decorators/StoreCustomerDecorator.php | 2 +- .../Strategies/OrderRequestStrategy.php | 2 +- .../MollieSepaRecurringGatewayHandler.php | 2 - .../MollieSubscriptionGatewayHandler.php | 34 +---------- .../Functional/Payment/PaymentServiceTest.php | 18 +++--- 10 files changed, 77 insertions(+), 97 deletions(-) diff --git a/src/Gateway/DeprecatedGatewayBuilder.php b/src/Gateway/DeprecatedGatewayBuilder.php index 8db665d7..e2b16275 100644 --- a/src/Gateway/DeprecatedGatewayBuilder.php +++ b/src/Gateway/DeprecatedGatewayBuilder.php @@ -26,8 +26,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container): assert($logger instanceof Logger); $notice = $container->get(FrontendNotice::class); assert($notice instanceof FrontendNotice); - $paymentProcessor = $container->get(PaymentProcessor::class); - assert($paymentProcessor instanceof PaymentProcessor); $mollieOrderService = $container->get(MollieOrderService::class); assert($mollieOrderService instanceof MollieOrderService); $HttpResponseService = $container->get('SDK.HttpResponse'); @@ -62,7 +60,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container): $gateways[$key] = new MollieSepaRecurringGatewayHandler( $directDebit, $paymentMethod, - $paymentProcessor, $orderInstructionsManager, $mollieOrderService, $data, @@ -78,7 +75,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container): } elseif ($paymentMethod->getProperty('Subscription')) { $gateways[$key] = new MollieSubscriptionGatewayHandler( $paymentMethod, - $paymentProcessor, $orderInstructionsManager, $mollieOrderService, $data, @@ -94,7 +90,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container): } else { $gateways[$key] = new MolliePaymentGatewayHandler( $paymentMethod, - $paymentProcessor, $orderInstructionsManager, $mollieOrderService, $data, diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index 1bad2cfb..e5c3225a 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -94,7 +94,9 @@ static function ($gateways) use ($container) { if (!has_action('woocommerce_thankyou_' . $gateway->id)) { $thankyouPageService($gateway); } - + // Add subscription payment hooks + $isSubscriptionPaymentService = $container->get('gateway.hooks.isSubscriptionPayment'); + $isSubscriptionPaymentService($gateway); } return $gateways; }, diff --git a/src/Gateway/MolliePaymentGatewayHandler.php b/src/Gateway/MolliePaymentGatewayHandler.php index 6f4b952b..f140634a 100644 --- a/src/Gateway/MolliePaymentGatewayHandler.php +++ b/src/Gateway/MolliePaymentGatewayHandler.php @@ -90,7 +90,6 @@ class MolliePaymentGatewayHandler */ public function __construct( PaymentMethodI $paymentMethod, - PaymentProcessor $paymentProcessor, OrderInstructionsManager $orderInstructionsProcessor, MollieOrderService $mollieOrderService, Data $dataService, @@ -105,7 +104,6 @@ public function __construct( $this->paymentMethod = $paymentMethod; $this->logger = $logger; $this->notice = $notice; - $this->paymentProcessor = $paymentProcessor; $this->orderInstructionsManager = $orderInstructionsProcessor; $this->mollieOrderService = $mollieOrderService; $this->httpResponse = $httpResponse; @@ -141,7 +139,6 @@ public function __construct( if ($this->enabled === 'yes' && $this->paymentMethod->getProperty('filtersOnBuild')) { $this->paymentMethod->filtersOnBuild(); } - //$this->refundProcessor = new RefundProcessor($this); } public function paymentMethod(): PaymentMethodI @@ -149,11 +146,6 @@ public function paymentMethod(): PaymentMethodI return $this->paymentMethod; } - public function paymentService() - { - return $this->paymentProcessor; - } - public function dataService() { return $this->dataService; diff --git a/src/Gateway/inc/services.php b/src/Gateway/inc/services.php index c34cec01..030a11b5 100644 --- a/src/Gateway/inc/services.php +++ b/src/Gateway/inc/services.php @@ -149,7 +149,8 @@ $paymentCheckoutRedirectService = $container->get(PaymentCheckoutRedirectService::class); assert($paymentCheckoutRedirectService instanceof PaymentCheckoutRedirectService); $voucherDefaultCategory = $container->get('voucher.defaultCategory'); - return new PaymentProcessor($notice, $logger, $paymentFactory, $data, $api, $settings, $pluginId, $paymentCheckoutRedirectService, $voucherDefaultCategory); + $deprecatedGatewayInstances = $container->get('__deprecated.gateway_helpers'); + return new PaymentProcessor($notice, $logger, $paymentFactory, $data, $api, $settings, $pluginId, $paymentCheckoutRedirectService, $voucherDefaultCategory, $deprecatedGatewayInstances); }, OrderInstructionsManager::class => static function (): OrderInstructionsManager { return new OrderInstructionsManager(); @@ -352,6 +353,38 @@ function ($order, $sent_to_admin, $plain_text) use ($instructionsManager, $payme ); }; }, + 'gateway.hooks.isSubscriptionPayment' => static function (ContainerInterface $container) { + return static function (PaymentGateway $paymentGateway) use ($container) { + $pluginId = $container->get('shared.plugin_id'); + $dataHelper = $container->get('settings.data_helper'); + if ($paymentGateway->supports('subscriptions')) { + add_filter( + $pluginId . '_is_subscription_payment', + function ($isSubscription, $orderId) use ($pluginId, $dataHelper) { + if ($dataHelper->isWcSubscription($orderId)) { + add_filter( + $pluginId . '_is_automatic_payment_disabled', + static function ($filteredOption) { + if ( + 'yes' == get_option( + \WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments' + ) + ) { + return true; + } + return $filteredOption; + } + ); + return true; + } + return $isSubscription; + }, + 10, + 2 + ); + } + }; + }, ]; $paymentMethods = SharedDataDictionary::GATEWAY_CLASSNAMES; @@ -364,11 +397,7 @@ function ($order, $sent_to_admin, $plain_text) use ($instructionsManager, $payme return $container->get('payment_gateways.noop_payment_request_validator'); }; $dynamicServices["payment_gateway.$gatewayId.payment_processor"] = static function (ContainerInterface $container) use ($gatewayId): PaymentProcessor { - $oldGatewayInstances = $container->get('__deprecated.gateway_helpers'); - $deprecatedGatewayHelper = $oldGatewayInstances[$gatewayId]; - $paymentProcessor = $container->get(PaymentProcessor::class); - $paymentProcessor->setGateway($deprecatedGatewayHelper); - return $paymentProcessor; + return $container->get(PaymentProcessor::class); }; $dynamicServices["payment_gateway.$gatewayId.refund_processor"] = static function (ContainerInterface $container) use ($gatewayId): RefundProcessorInterface { $getProperty = $container->get('gateway.getMethodPropertyByGatewayId'); diff --git a/src/Payment/PaymentProcessor.php b/src/Payment/PaymentProcessor.php index 878adb0a..cea555ff 100644 --- a/src/Payment/PaymentProcessor.php +++ b/src/Payment/PaymentProcessor.php @@ -4,6 +4,7 @@ namespace Mollie\WooCommerce\Payment; +use Inpsyde\PaymentGateway\PaymentGateway; use Inpsyde\PaymentGateway\PaymentProcessorInterface; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Payment; @@ -53,6 +54,8 @@ class PaymentProcessor implements PaymentProcessorInterface * @var string */ protected $voucherDefaultCategory; + private PaymentGateway $gateway; + private array $deprecatedGatewayInstances; /** * PaymentProcessor constructor. @@ -66,7 +69,8 @@ public function __construct( Settings $settingsHelper, string $pluginId, PaymentCheckoutRedirectService $paymentCheckoutRedirectService, - string $voucherDefaultCategory + string $voucherDefaultCategory, + array $deprecatedGatewayInstances ) { $this->notice = $notice; @@ -78,17 +82,23 @@ public function __construct( $this->pluginId = $pluginId; $this->paymentCheckoutRedirectService = $paymentCheckoutRedirectService; $this->voucherDefaultCategory = $voucherDefaultCategory; + $this->deprecatedGatewayInstances = $deprecatedGatewayInstances; } - public function setGateway($gateway) + public function setGatewayHelper(string $paymentGatewayId) { - $this->deprecatedGatewayHelper = $gateway; + $this->deprecatedGatewayHelper = $this->deprecatedGatewayInstances[$paymentGatewayId]; + } + public function setGateway(PaymentGateway $gateway) + { + $this->gateway = $gateway; } public function processPayment($order, $paymentGateway): array { $orderId = $order->get_id(); - $this->handleSubscriptions($paymentGateway, $orderId); + $this->setGateway($paymentGateway); + $this->setGatewayHelper($paymentGateway->id); $redirectUrl = $paymentGateway->get_return_url($order); $paymentMethod = $this->deprecatedGatewayHelper->paymentMethod(); @@ -339,7 +349,7 @@ protected function processAsMollieOrder( $data = array_filter($paymentRequestData); $data = apply_filters( - 'woocommerce_' . $this->deprecatedGatewayHelper->id . '_args', + 'woocommerce_' . $this->gateway->id . '_args', $data, $order ); @@ -465,12 +475,12 @@ protected function processAsMolliePayment( $data = array_filter($paymentRequestData); $data = apply_filters( - 'woocommerce_' . $this->deprecatedGatewayHelper->id . '_args', + 'woocommerce_' . $this->gateway->id . '_args', $data, $order ); $data = apply_filters( - 'woocommerce_' . $this->deprecatedGatewayHelper->id . 'payment_args', + 'woocommerce_' . $this->gateway->id . 'payment_args', $data, $order ); @@ -527,8 +537,6 @@ protected function processPaymentForMollie( $customer_id, $apiKey ) { - - $paymentMethod = $this->deprecatedGatewayHelper->paymentMethod(); // // PROCESS REGULAR PAYMENT AS MOLLIE ORDER // @@ -536,9 +544,9 @@ protected function processPaymentForMollie( // if the capture is set to manual, and this is a credit card payment, we need to create a payment instead of an order $captureType = get_option('mollie-payments-for-woocommerce_place_payment_onhold'); - if ($captureType === 'later_capture' && $this->deprecatedGatewayHelper->id === 'mollie_wc_gateway_creditcard') { + if ($captureType === 'later_capture' && $this->gateway->id === 'mollie_wc_gateway_creditcard') { $this->logger->debug( - "{$this->deprecatedGatewayHelper->id}: Create payment for order {$orderId} capture set to manual", + "{$this->gateway->id}: Create payment for order {$orderId} capture set to manual", [true] ); @@ -550,7 +558,7 @@ protected function processPaymentForMollie( return $paymentObject; } $this->logger->debug( - "{$this->deprecatedGatewayHelper->id}: Create Mollie payment object for order {$orderId}", + "{$this->gateway->id}: Create Mollie payment object for order {$orderId}", [true] ); @@ -642,7 +650,7 @@ public function updateOrderStatus(\WC_Order $order, $new_status, $note = '', $re protected function noValidMandateForSubsSwitchFailure($orderId): void { $this->logger->debug( - $this->deprecatedGatewayHelper->id . ': Subscription switch failed, no valid mandate for order #' . $orderId + $this->gateway->id . ': Subscription switch failed, no valid mandate for order #' . $orderId ); $this->notice->addNotice( 'error', @@ -669,11 +677,11 @@ protected function subsSwitchCompleted($order): array ) ); - $this->logger->debug($this->deprecatedGatewayHelper->id . ': Subscription switch completed, valid mandate for order #' . $order->get_id()); + $this->logger->debug($this->gateway->id . ': Subscription switch completed, valid mandate for order #' . $order->get_id()); return [ 'result' => 'success', - 'redirect' => $this->deprecatedGatewayHelper->get_return_url($order), + 'redirect' => $this->gateway->get_return_url($order), ]; } @@ -691,7 +699,7 @@ protected function processValidMandate($order, ?string $customerId, $apiKey): bo ); $paymentRequestData = $paymentObject->getPaymentRequestData($order, $customerId); $data = array_filter($paymentRequestData); - $data = apply_filters('woocommerce_' . $this->deprecatedGatewayHelper->id . '_args', $data, $order); + $data = apply_filters('woocommerce_' . $this->gateway->id . '_args', $data, $order); $mandates = $this->apiHelper->getApiClient($apiKey)->customers->get($customerId)->mandates(); $validMandate = false; @@ -711,7 +719,7 @@ protected function processSubscriptionSwitch(WC_Order $order, int $orderId, ?str // PROCESS SUBSCRIPTION SWITCH - If this is a subscription switch and customer has a valid mandate, process the order internally // try { - $this->logger->debug($this->deprecatedGatewayHelper->id . ': Subscription switch started, fetching mandate(s) for order #' . $orderId); + $this->logger->debug($this->gateway->id . ': Subscription switch started, fetching mandate(s) for order #' . $orderId); $validMandate = $this->processValidMandate($order, $customerId, $apiKey); if ($validMandate) { return $this->subsSwitchCompleted($order); @@ -925,20 +933,4 @@ public function handleMollieFraudRejection(ApiException $e): void ); } } - - /** - * @param \Inpsyde\PaymentGateway\PaymentGateway $paymentGateway - * @param int|null $orderId - * @return void - */ - public function handleSubscriptions(\Inpsyde\PaymentGateway\PaymentGateway $paymentGateway, ?int $orderId): void - { - if ($paymentGateway->supports('subscriptions')) { - $this->deprecatedGatewayHelper->addWcSubscriptionsFiltersForPayment(); - $isSubscription = $this->dataHelper->isSubscription($orderId); - if ($isSubscription) { - $this->deprecatedGatewayHelper->isSubscriptionPayment = true; - } - } - } } diff --git a/src/Payment/Request/Decorators/StoreCustomerDecorator.php b/src/Payment/Request/Decorators/StoreCustomerDecorator.php index 8180dcea..68f71ad0 100644 --- a/src/Payment/Request/Decorators/StoreCustomerDecorator.php +++ b/src/Payment/Request/Decorators/StoreCustomerDecorator.php @@ -20,7 +20,7 @@ public function decorate(array $requestData, WC_Order $order, $context = null): $storeCustomer = $this->settingsHelper->shouldStoreCustomer(); if (!$storeCustomer) { if ($context === 'order') { - unset($requestData['payment']['customerId']); + unset($requestData['payment']['customerId']); } elseif ($context === 'payment') { unset($requestData['customerId']); } diff --git a/src/Payment/Request/Strategies/OrderRequestStrategy.php b/src/Payment/Request/Strategies/OrderRequestStrategy.php index bbb89e21..fba1eb80 100644 --- a/src/Payment/Request/Strategies/OrderRequestStrategy.php +++ b/src/Payment/Request/Strategies/OrderRequestStrategy.php @@ -54,7 +54,7 @@ public function createRequest(WC_Order $order, $customerId): array 'orderNumber' => $order->get_order_number(), 'payment' => [ 'customerId' => $customerId, - ] + ], ]; $context = 'order'; diff --git a/src/Subscription/MollieSepaRecurringGatewayHandler.php b/src/Subscription/MollieSepaRecurringGatewayHandler.php index 5512acd6..497560e7 100644 --- a/src/Subscription/MollieSepaRecurringGatewayHandler.php +++ b/src/Subscription/MollieSepaRecurringGatewayHandler.php @@ -36,7 +36,6 @@ class MollieSepaRecurringGatewayHandler extends MollieSubscriptionGatewayHandler public function __construct( PaymentMethodI $directDebitPaymentMethod, PaymentMethodI $paymentMethod, - PaymentProcessor $paymentProcessor, OrderInstructionsManager $orderInstructionsService, MollieOrderService $mollieOrderService, Data $dataService, @@ -52,7 +51,6 @@ public function __construct( parent::__construct( $paymentMethod, - $paymentProcessor, $orderInstructionsService, $mollieOrderService, $dataService, diff --git a/src/Subscription/MollieSubscriptionGatewayHandler.php b/src/Subscription/MollieSubscriptionGatewayHandler.php index c77895a5..25daf819 100644 --- a/src/Subscription/MollieSubscriptionGatewayHandler.php +++ b/src/Subscription/MollieSubscriptionGatewayHandler.php @@ -38,7 +38,6 @@ class MollieSubscriptionGatewayHandler extends MolliePaymentGatewayHandler 'mollie_wc_gateway_sofort', ]; protected const DIRECTDEBIT = Constants::DIRECTDEBIT; - public $isSubscriptionPayment = false; protected $apiHelper; protected $settingsHelper; /** @@ -51,7 +50,6 @@ class MollieSubscriptionGatewayHandler extends MolliePaymentGatewayHandler */ public function __construct( PaymentMethodI $paymentMethod, - PaymentProcessor $paymentProcessor, OrderInstructionsManager $orderInstructionsProcessor, MollieOrderService $mollieOrderService, Data $dataService, @@ -67,7 +65,6 @@ public function __construct( parent::__construct( $paymentMethod, - $paymentProcessor, $orderInstructionsProcessor, $mollieOrderService, $dataService, @@ -211,7 +208,7 @@ public function scheduled_subscription_payment($renewal_total, WC_Order $renewal do_action($this->pluginId . '_before_renewal_payment_created', $renewal_order); $this->logger->debug($gateway->id . ': Try to create renewal payment for renewal order ' . $renewal_order_id); - $this->paymentProcessor->setGateway($this); + //$this->paymentProcessor->setGatewayHelper($gateway->id); $initial_order_status = $this->paymentMethod->getInitialOrderStatus(); // Overwrite plugin-wide @@ -559,35 +556,6 @@ public function update_failing_payment_method($subscription, $renewal_order) $subscription->save(); } - //TODO this is still used in the paymentProcessor through the deprecatedGateway - public function addWcSubscriptionsFiltersForPayment(): void - { - add_filter( - $this->pluginId . '_is_subscription_payment', - function ($isSubscription, $orderId) { - if ($this->dataService->isWcSubscription($orderId)) { - add_filter( - $this->pluginId . '_is_automatic_payment_disabled', - static function ($filteredOption) { - if ( - 'yes' == get_option( - \WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments' - ) - ) { - return true; - } - return $filteredOption; - } - ); - return true; - } - return $isSubscription; - }, - 10, - 2 - ); - } - /** * @param $mollie_customer_id * @param $mollie_payment_id diff --git a/tests/php/Functional/Payment/PaymentServiceTest.php b/tests/php/Functional/Payment/PaymentServiceTest.php index f5ec9bfe..35f81db7 100644 --- a/tests/php/Functional/Payment/PaymentServiceTest.php +++ b/tests/php/Functional/Payment/PaymentServiceTest.php @@ -76,6 +76,7 @@ public function processPayment_Order_success(){ ); $apiClientMock->orders = $orderEndpoints; $voucherDefaultCategory = Voucher::NO_CATEGORY; + $deprecatedGatewayHelper = $this->mollieGateway($paymentMethodId, $testee); $testee = new PaymentProcessor( $this->helperMocks->noticeMock(), $this->helperMocks->loggerMock(), @@ -85,15 +86,16 @@ public function processPayment_Order_success(){ $this->helperMocks->settingsHelper(), $this->helperMocks->pluginId(), $this->paymentCheckoutService($apiClientMock), - $voucherDefaultCategory + $voucherDefaultCategory, + ['mollie_wc_gateway_ideal' => $deprecatedGatewayHelper] ); $gateway = $this->helperMocks->genericPaymentGatewayMock(); $gateway->method('supports')->willReturnMap([ ['subscriptions', false] ]); $gateway->method('get_return_url')->willReturn($processPaymentRedirect); - $deprecatedGatewayHelper = $this->mollieGateway($paymentMethodId, $testee); - $testee->setGateway($deprecatedGatewayHelper); + + $testee->setGatewayHelper($gateway->id); stubs( [ @@ -155,7 +157,8 @@ public function processAsMollieOrder_BailsIf_FraudException() $apiClientMock = $this->createMock(MollieApiClient::class); $apiClientMock->orders = $orderEndpointsMock; $voucherDefaultCategory = Voucher::NO_CATEGORY; - + $gateway = $this->helperMocks->genericPaymentGatewayMock(); + $deprecatedGatewayHelper = $this->mollieGateway($paymentMethodId, $testee); $testee = new PaymentProcessor( $this->helperMocks->noticeMock(), $this->helperMocks->loggerMock(), @@ -165,10 +168,11 @@ public function processAsMollieOrder_BailsIf_FraudException() $this->helperMocks->settingsHelper(), $this->helperMocks->pluginId(), $this->paymentCheckoutService($apiClientMock), - $voucherDefaultCategory + $voucherDefaultCategory, + ['mollie_wc_gateway_ideal' => $deprecatedGatewayHelper] ); - $gateway = $this->mollieGateway($paymentMethodId, $testee); - $testee->setGateway($gateway); + + $testee->setGatewayHelper($gateway->id); $wcOrderId = 1; $wcOrderKey = 'wc_order_hxZniP1zDcnM8'; $wcOrder = $this->wcOrder($wcOrderId, $wcOrderKey);