diff --git a/src/Payment/MollieSubscription.php b/src/Payment/MollieSubscription.php index 30f1db38..1eb92bb5 100644 --- a/src/Payment/MollieSubscription.php +++ b/src/Payment/MollieSubscription.php @@ -3,6 +3,7 @@ namespace Mollie\WooCommerce\Payment; use Mollie\Api\Types\SequenceType; +use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler; use Mollie\WooCommerce\SDK\Api; use Mollie\WooCommerce\Subscription\MollieSubscriptionGatewayHandler; @@ -13,12 +14,13 @@ class MollieSubscription extends MollieObject * @var mixed */ private $paymentMethod; + protected MiddlewareHandler $middleware; /** * Molliesubscription constructor. * */ - public function __construct($pluginId, Api $apiHelper, $settingsHelper, $dataHelper, $logger, $paymentMethod, $middleware) + public function __construct($pluginId, Api $apiHelper, $settingsHelper, $dataHelper, $logger, $paymentMethod, $middlewareHandler) { $this->pluginId = $pluginId; $this->apiHelper = $apiHelper; @@ -26,7 +28,7 @@ public function __construct($pluginId, Api $apiHelper, $settingsHelper, $dataHel $this->dataHelper = $dataHelper; $this->logger = $logger; $this->paymentMethod = $paymentMethod; - $this->middleware = $middleware; + $this->middleware = $middlewareHandler; } /** * @param $order @@ -67,10 +69,7 @@ public function getRecurringPaymentRequestData($order, $customerId, $initialPaym 'customerId' => $customerId, ]); $context = 'payment'; - foreach ($this->middleware as $field) { - $requestData = $field->decorate($requestData, $order, $context); - } - return $requestData; + return $this->middleware->handle($requestData, $order, $context); } protected function getRecurringPaymentDescription($order, $option, $initialPaymentUsedOrderAPI) diff --git a/src/Payment/Request/Decorators/RequestDecoratorInterface.php b/src/Payment/Request/Decorators/RequestDecoratorInterface.php deleted file mode 100644 index 68736d4e..00000000 --- a/src/Payment/Request/Decorators/RequestDecoratorInterface.php +++ /dev/null @@ -1,10 +0,0 @@ -container = $container; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { $gateway = wc_get_payment_gateway_by_order($order); $methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); @@ -26,15 +26,15 @@ public function decorate(array $requestData, WC_Order $order, $context = null): if (property_exists($paymentMethod, 'paymentAPIfields')) { $paymentAPIfields = $paymentMethod->paymentAPIfields; foreach ($paymentAPIfields as $field) { - $decoratorClass = 'Mollie\\WooCommerce\\Payment\\Decorator\\' . $field; - if (class_exists($decoratorClass)) { - $decorator = $this->container->get($decoratorClass); - if ($decorator instanceof RequestDecoratorInterface) { - $requestData = $decorator->decorate($requestData, $order); + $middlewareClass = 'Mollie\\WooCommerce\\Payment\\Request\\Middleware' . $field; + if (class_exists($middlewareClass)) { + $middleware = $this->container->get($middlewareClass); + if ($middleware instanceof RequestMiddlewareInterface) { + $requestData = $middleware->__invoke($requestData, $order); } } } } - return $requestData; + return $next($requestData, $order, $context); } } diff --git a/src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php b/src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php similarity index 83% rename from src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php rename to src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php index 71c02e98..110c41ff 100644 --- a/src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php +++ b/src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use Mollie\WooCommerce\Shared\Data; use WC_Order; -class AddSequenceTypeForSubscriptionsDecorator implements RequestDecoratorInterface +class AddSequenceTypeForSubscriptionsMiddleware implements RequestMiddlewareInterface { private Data $dataHelper; private string $pluginId; @@ -18,13 +18,13 @@ public function __construct($dataHelper, $pluginId) $this->pluginId = $pluginId; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array { $gateway = wc_get_payment_gateway_by_order($order); if ($gateway) { $requestData = $this->addSequenceTypeForSubscriptionsFirstPayments($order->get_id(), $gateway, $requestData, $context); } - return $requestData; + return $next($requestData, $order, $context); } private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway, $requestData, $context): array diff --git a/src/Payment/Request/Decorators/AddressDecorator.php b/src/Payment/Request/Middleware/AddressMiddleware.php similarity index 96% rename from src/Payment/Request/Decorators/AddressDecorator.php rename to src/Payment/Request/Middleware/AddressMiddleware.php index 683680d0..0649c537 100644 --- a/src/Payment/Request/Decorators/AddressDecorator.php +++ b/src/Payment/Request/Middleware/AddressMiddleware.php @@ -2,18 +2,18 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use stdClass; use WC_Order; -class AddressDecorator implements RequestDecoratorInterface +class AddressMiddleware implements RequestMiddlewareInterface { public const MAXIMAL_LENGTH_ADDRESS = 100; public const MAXIMAL_LENGTH_POSTALCODE = 20; public const MAXIMAL_LENGTH_CITY = 200; public const MAXIMAL_LENGTH_REGION = 200; - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array { $isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton'; $billingAddress = null; @@ -31,8 +31,7 @@ public function decorate(array $requestData, WC_Order $order, $context = null): ) { $requestData['shippingAddress'] = $shippingAddress; } - - return $requestData; + return $next($requestData, $order, $context); } private function createBillingAddress(WC_Order $order) diff --git a/src/Payment/Request/Decorators/ApplePayTokenDecorator.php b/src/Payment/Request/Middleware/ApplePayTokenMiddleware.php similarity index 64% rename from src/Payment/Request/Decorators/ApplePayTokenDecorator.php rename to src/Payment/Request/Middleware/ApplePayTokenMiddleware.php index cff73317..a0a2586c 100644 --- a/src/Payment/Request/Decorators/ApplePayTokenDecorator.php +++ b/src/Payment/Request/Middleware/ApplePayTokenMiddleware.php @@ -2,18 +2,18 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use WC_Order; -class ApplePayTokenDecorator implements RequestDecoratorInterface +class ApplePayTokenMiddleware implements RequestMiddlewareInterface { - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $applePayToken = wc_clean(wp_unslash($_POST["token"] ?? '')); if (!$applePayToken) { - return $requestData; + return $next($requestData, $order, $context); } $encodedApplePayToken = wp_json_encode($applePayToken); if ($context === 'order') { @@ -21,6 +21,6 @@ public function decorate(array $requestData, WC_Order $order, $context = null): } elseif ($context === 'payment') { $requestData['applePayToken'] = $encodedApplePayToken; } - return $requestData; + return $next($requestData, $order, $context); } } diff --git a/src/Payment/Request/Decorators/CardTokenDecorator.php b/src/Payment/Request/Middleware/CardTokenMiddleware.php similarity index 60% rename from src/Payment/Request/Decorators/CardTokenDecorator.php rename to src/Payment/Request/Middleware/CardTokenMiddleware.php index 6ab55441..dc95c269 100644 --- a/src/Payment/Request/Decorators/CardTokenDecorator.php +++ b/src/Payment/Request/Middleware/CardTokenMiddleware.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use WC_Order; -class CardTokenDecorator implements RequestDecoratorInterface +class CardTokenMiddleware implements RequestMiddlewareInterface { - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { $cardToken = mollieWooCommerceCardToken(); if ($cardToken && isset($requestData['payment']) && $context === 'order') { @@ -16,6 +16,6 @@ public function decorate(array $requestData, WC_Order $order, $context = null): } elseif ($cardToken && isset($requestData['payment']) && $context === 'payment') { $requestData['cardToken'] = $cardToken; } - return $requestData; + return $next($requestData, $order, $context); } } diff --git a/src/Payment/Request/Decorators/CustomerBirthdateDecorator.php b/src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php similarity index 81% rename from src/Payment/Request/Decorators/CustomerBirthdateDecorator.php rename to src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php index 9dda9048..9be0d720 100644 --- a/src/Payment/Request/Decorators/CustomerBirthdateDecorator.php +++ b/src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php @@ -1,11 +1,10 @@ paymentMethods = $paymentMethods; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { $gateway = wc_get_payment_gateway_by_order($order); if (!$gateway || !isset($gateway->id)) { @@ -40,6 +39,6 @@ public function decorate(array $requestData, WC_Order $order, $context = null): $format = "Y-m-d"; $requestData['consumerDateOfBirth'] = gmdate($format, (int) strtotime($fieldPosted)); } - return $requestData; + return $next($requestData, $order, $context); } } diff --git a/src/Payment/Request/Middleware/MiddlewareHandler.php b/src/Payment/Request/Middleware/MiddlewareHandler.php new file mode 100644 index 00000000..a22b370d --- /dev/null +++ b/src/Payment/Request/Middleware/MiddlewareHandler.php @@ -0,0 +1,37 @@ +middlewares = $middlewares; + } + + public function handle(array $requestData, WC_Order $order, $context = null): array + { + $middlewareChain = $this->createMiddlewareChain($this->middlewares); + + return $middlewareChain($requestData, $order, $context); + } + + private function createMiddlewareChain(array $middlewares): callable + { + return array_reduce( + array_reverse($middlewares), + function ($next, $middleware) { + return function ($requestData, $order, $context) use ($middleware, $next) { + return $middleware($requestData, $order, $context, $next); + }; + }, + function ($requestData) { + return $requestData; + } + ); + } +} diff --git a/src/Payment/Request/Decorators/OrderLinesDecorator.php b/src/Payment/Request/Middleware/OrderLinesMiddleware.php similarity index 65% rename from src/Payment/Request/Decorators/OrderLinesDecorator.php rename to src/Payment/Request/Middleware/OrderLinesMiddleware.php index 6c419519..ebe5931f 100644 --- a/src/Payment/Request/Decorators/OrderLinesDecorator.php +++ b/src/Payment/Request/Middleware/OrderLinesMiddleware.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use Mollie\WooCommerce\Payment\OrderLines; use WC_Order; -class OrderLinesDecorator implements RequestDecoratorInterface +class OrderLinesMiddleware implements RequestMiddlewareInterface { private OrderLines $orderLines; private string $voucherDefaultCategory; @@ -18,10 +18,10 @@ public function __construct($orderLines, $voucherDefaultCategory) $this->voucherDefaultCategory = $voucherDefaultCategory; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { $orderLines = $this->orderLines->order_lines($order, $this->voucherDefaultCategory); $requestData['lines'] = $orderLines['lines']; - return $requestData; + return $next($requestData, $order, $context); } } diff --git a/src/Payment/Request/Decorators/PaymentDescriptionDecorator.php b/src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php similarity index 93% rename from src/Payment/Request/Decorators/PaymentDescriptionDecorator.php rename to src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php index 2292fa61..1cb29597 100644 --- a/src/Payment/Request/Decorators/PaymentDescriptionDecorator.php +++ b/src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use WC_Order; -class PaymentDescriptionDecorator implements RequestDecoratorInterface +class PaymentDescriptionMiddleware implements RequestMiddlewareInterface { private $dataHelper; @@ -15,14 +15,14 @@ public function __construct($dataHelper) $this->dataHelper = $dataHelper; } - public function decorate(array $requestData, WC_Order $order, string $context = null): array + public function __invoke(array $requestData, WC_Order $order, string $context = null, $next): array { $optionName = $this->dataHelper->getPluginId() . '_' . 'api_payment_description'; $option = get_option($optionName); $paymentDescription = $this->getPaymentDescription($order, $option); $requestData['description'] = $paymentDescription; - return $requestData; + return $next($requestData, $order, $context); } private function getPaymentDescription(WC_Order $order, $option): string diff --git a/src/Payment/Request/Middleware/RequestMiddlewareInterface.php b/src/Payment/Request/Middleware/RequestMiddlewareInterface.php new file mode 100644 index 00000000..51ff8a95 --- /dev/null +++ b/src/Payment/Request/Middleware/RequestMiddlewareInterface.php @@ -0,0 +1,10 @@ +pluginId = $pluginId; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { + $gateway = wc_get_payment_gateway_by_order($order); if (!$gateway) { - return $requestData; + + return $next($requestData, $order, $context); } $gatewayId = $gateway->id; $selectedIssuer = $this->getSelectedIssuer($gatewayId); if (empty($selectedIssuer)) { - return $requestData; + return $next($requestData, $order, $context); } if ($context === 'order') { $requestData['payment']['issuer'] = $selectedIssuer; @@ -33,7 +35,7 @@ public function decorate(array $requestData, WC_Order $order, $context = null): $requestData['issuer'] = $selectedIssuer; } - return $requestData; + return $next($requestData, $order, $context); } private function getSelectedIssuer(string $gatewayId): string diff --git a/src/Payment/Request/Decorators/StoreCustomerDecorator.php b/src/Payment/Request/Middleware/StoreCustomerMiddleware.php similarity index 68% rename from src/Payment/Request/Decorators/StoreCustomerDecorator.php rename to src/Payment/Request/Middleware/StoreCustomerMiddleware.php index 68f71ad0..375c6bbf 100644 --- a/src/Payment/Request/Decorators/StoreCustomerDecorator.php +++ b/src/Payment/Request/Middleware/StoreCustomerMiddleware.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use Mollie\WooCommerce\Settings\Settings; use WC_Order; -class StoreCustomerDecorator implements RequestDecoratorInterface +class StoreCustomerMiddleware implements RequestMiddlewareInterface { private Settings $settingsHelper; public function __construct(Settings $settingsHelper) @@ -15,7 +15,7 @@ public function __construct(Settings $settingsHelper) $this->settingsHelper = $settingsHelper; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { $storeCustomer = $this->settingsHelper->shouldStoreCustomer(); if (!$storeCustomer) { @@ -25,8 +25,6 @@ public function decorate(array $requestData, WC_Order $order, $context = null): unset($requestData['customerId']); } } - - - return $requestData; + return $next($requestData, $order, $context); } } diff --git a/src/Payment/Request/Decorators/UrlDecorator.php b/src/Payment/Request/Middleware/UrlMiddleware.php similarity index 94% rename from src/Payment/Request/Decorators/UrlDecorator.php rename to src/Payment/Request/Middleware/UrlMiddleware.php index 056d6f90..82d03b1a 100644 --- a/src/Payment/Request/Decorators/UrlDecorator.php +++ b/src/Payment/Request/Middleware/UrlMiddleware.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Mollie\WooCommerce\Payment\Request\Decorators; +namespace Mollie\WooCommerce\Payment\Request\Middleware; use WC_Order; -class UrlDecorator implements RequestDecoratorInterface +class UrlMiddleware implements RequestMiddlewareInterface { private $pluginId; /** @@ -20,7 +20,7 @@ public function __construct($pluginId, $logger) $this->logger = $logger; } - public function decorate(array $requestData, WC_Order $order, $context = null): array + public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array { $gateway = wc_get_payment_gateway_by_order($order); if ($gateway) { @@ -31,7 +31,7 @@ public function decorate(array $requestData, WC_Order $order, $context = null): $requestData['redirectUrl'] = $returnUrl; $requestData['webhookUrl'] = $webhookUrl; } - return $requestData; + return $next($requestData, $order, $context); } /** diff --git a/src/Payment/Request/Strategies/OrderRequestStrategy.php b/src/Payment/Request/Strategies/OrderRequestStrategy.php index fba1eb80..6d96719c 100644 --- a/src/Payment/Request/Strategies/OrderRequestStrategy.php +++ b/src/Payment/Request/Strategies/OrderRequestStrategy.php @@ -3,6 +3,7 @@ namespace Mollie\WooCommerce\Payment\Request\Strategies; use Inpsyde\PaymentGateway\PaymentGateway; +use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler; use Mollie\WooCommerce\Settings\Settings; use Mollie\WooCommerce\Shared\Data; use WC_Order; @@ -11,14 +12,14 @@ class OrderRequestStrategy implements RequestStrategyInterface { private Data $dataHelper; private Settings $settingsHelper; - private array $decorators; + private MiddlewareHandler $middlewareHandler; - public function __construct($dataHelper, $settingsHelper, array $decorators) + public function __construct($dataHelper, $settingsHelper, MiddlewareHandler $middlewareHandler) { $this->dataHelper = $dataHelper; $this->settingsHelper = $settingsHelper; - $this->decorators = $decorators; + $this->middlewareHandler = $middlewareHandler; } public function createRequest(WC_Order $order, $customerId): array @@ -58,10 +59,6 @@ public function createRequest(WC_Order $order, $customerId): array ]; $context = 'order'; - foreach ($this->decorators as $decorator) { - $requestData = $decorator->decorate($requestData, $order, $context); - } - - return $requestData; + return $this->middlewareHandler->handle($requestData, $order, $context); } } diff --git a/src/Payment/Request/Strategies/PaymentRequestStrategy.php b/src/Payment/Request/Strategies/PaymentRequestStrategy.php index 097010f4..0ff4e94d 100644 --- a/src/Payment/Request/Strategies/PaymentRequestStrategy.php +++ b/src/Payment/Request/Strategies/PaymentRequestStrategy.php @@ -3,20 +3,21 @@ namespace Mollie\WooCommerce\Payment\Request\Strategies; use Inpsyde\PaymentGateway\PaymentGateway; +use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler; use WC_Order; class PaymentRequestStrategy implements RequestStrategyInterface { private $dataHelper; private $settingsHelper; - private array $decorators; + private MiddlewareHandler $middlewareHandler; - public function __construct($dataHelper, $settingsHelper, array $decorators) + public function __construct($dataHelper, $settingsHelper, MiddlewareHandler $middlewareHandler) { $this->dataHelper = $dataHelper; $this->settingsHelper = $settingsHelper; - $this->decorators = $decorators; + $this->middlewareHandler = $middlewareHandler; } public function createRequest(WC_Order $order, $customerId): array @@ -55,10 +56,6 @@ public function createRequest(WC_Order $order, $customerId): array ]; $context = 'payment'; - foreach ($this->decorators as $decorator) { - $requestData = $decorator->decorate($requestData, $order, $context); - } - - return $requestData; + return $this->middlewareHandler->handle($requestData, $order, $context); } } diff --git a/src/Payment/inc/services.php b/src/Payment/inc/services.php index b38cd493..51fe4625 100644 --- a/src/Payment/inc/services.php +++ b/src/Payment/inc/services.php @@ -8,17 +8,18 @@ use Mollie\WooCommerce\Payment\MolliePayment; use Mollie\WooCommerce\Payment\OrderLines; use Mollie\WooCommerce\Payment\PaymentFactory; -use Mollie\WooCommerce\Payment\Request\Decorators\AddCustomRequestFieldsDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\AddressDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\AddSequenceTypeForSubscriptionsDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\ApplePayTokenDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\CardTokenDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\CustomerBirthdateDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\OrderLinesDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\PaymentDescriptionDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\SelectedIssuerDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\StoreCustomerDecorator; -use Mollie\WooCommerce\Payment\Request\Decorators\UrlDecorator; +use Mollie\WooCommerce\Payment\Request\Middleware\AddCustomRequestFieldsMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\AddressMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\AddSequenceTypeForSubscriptionsMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\ApplePayTokenMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\CardTokenMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\CustomerBirthdateMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler; +use Mollie\WooCommerce\Payment\Request\Middleware\OrderLinesMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\PaymentDescriptionMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\SelectedIssuerMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\StoreCustomerMiddleware; +use Mollie\WooCommerce\Payment\Request\Middleware\UrlMiddleware; use Mollie\WooCommerce\Payment\Request\Strategies\OrderRequestStrategy; use Mollie\WooCommerce\Payment\Request\Strategies\PaymentRequestStrategy; use Mollie\WooCommerce\Payment\Request\RequestFactory; @@ -83,93 +84,98 @@ static function () use ($container) { RequestFactory::class => static function (ContainerInterface $container): RequestFactory { return new RequestFactory($container); }, - CustomerBirthdateDecorator::class => static function (ContainerInterface $container): CustomerBirthdateDecorator { - return new CustomerBirthdateDecorator($container->get('gateway.paymentMethods')); + CustomerBirthdateMiddleware::class => static function (ContainerInterface $container): CustomerBirthdateMiddleware { + return new CustomerBirthdateMiddleware($container->get('gateway.paymentMethods')); }, - ApplePayTokenDecorator::class => static function (): ApplePayTokenDecorator { - return new ApplePayTokenDecorator(); + ApplePayTokenMiddleware::class => static function (): ApplePayTokenMiddleware { + return new ApplePayTokenMiddleware(); }, - CardTokenDecorator::class => static function (): CardTokenDecorator { - return new CardTokenDecorator(); + CardTokenMiddleware::class => static function (): CardTokenMiddleware { + return new CardTokenMiddleware(); }, - StoreCustomerDecorator::class => static function (ContainerInterface $container): StoreCustomerDecorator { - return new StoreCustomerDecorator($container->get('settings.settings_helper')); + StoreCustomerMiddleware::class => static function (ContainerInterface $container): StoreCustomerMiddleware { + return new StoreCustomerMiddleware($container->get('settings.settings_helper')); }, - AddSequenceTypeForSubscriptionsDecorator::class => static function (ContainerInterface $container): AddSequenceTypeForSubscriptionsDecorator { + AddSequenceTypeForSubscriptionsMiddleware::class => static function (ContainerInterface $container): AddSequenceTypeForSubscriptionsMiddleware { $dataHelper = $container->get('settings.data_helper'); $pluginId = $container->get('shared.plugin_id'); - return new AddSequenceTypeForSubscriptionsDecorator($dataHelper, $pluginId); + return new AddSequenceTypeForSubscriptionsMiddleware($dataHelper, $pluginId); }, - OrderLinesDecorator::class => static function (ContainerInterface $container): OrderLinesDecorator { + OrderLinesMiddleware::class => static function (ContainerInterface $container): OrderLinesMiddleware { $orderLines = $container->get(OrderLines::class); $voucherDefaultCategory = $container->get('voucher.defaultCategory'); - return new OrderLinesDecorator($orderLines, $voucherDefaultCategory); + return new OrderLinesMiddleware($orderLines, $voucherDefaultCategory); }, - AddressDecorator::class => static function (): AddressDecorator { - return new AddressDecorator(); + AddressMiddleware::class => static function (): AddressMiddleware { + return new AddressMiddleware(); }, - UrlDecorator::class => static function (ContainerInterface $container): UrlDecorator { - return new UrlDecorator( + UrlMiddleware::class => static function (ContainerInterface $container): UrlMiddleware { + return new UrlMiddleware( $container->get('shared.plugin_id'), $container->get(Logger::class), ); }, - SelectedIssuerDecorator::class => static function (ContainerInterface $container): SelectedIssuerDecorator { + SelectedIssuerMiddleware::class => static function (ContainerInterface $container): SelectedIssuerMiddleware { $pluginId = $container->get('shared.plugin_id'); - return new SelectedIssuerDecorator($pluginId); + return new SelectedIssuerMiddleware($pluginId); }, - PaymentDescriptionDecorator::class => static function (ContainerInterface $container): PaymentDescriptionDecorator { + PaymentDescriptionMiddleware::class => static function (ContainerInterface $container): PaymentDescriptionMiddleware { $dataHelper = $container->get('settings.data_helper'); - return new PaymentDescriptionDecorator($dataHelper); + return new PaymentDescriptionMiddleware($dataHelper); }, - AddCustomRequestFieldsDecorator::class => static function (ContainerInterface $container): AddCustomRequestFieldsDecorator { + AddCustomRequestFieldsMiddleware::class => static function (ContainerInterface $container): AddCustomRequestFieldsMiddleware { $paymentMethods = $container->get('gateway.paymentMethods'); - return new AddCustomRequestFieldsDecorator($paymentMethods, $container); + return new AddCustomRequestFieldsMiddleware($paymentMethods, $container); }, 'request.strategy.order' => static function (ContainerInterface $container): RequestStrategyInterface { $dataHelper = $container->get('settings.data_helper'); $settingsHelper = $container->get('settings.settings_helper'); + $middlewares = [ + $container->get(CustomerBirthdateMiddleware::class), + $container->get(ApplePayTokenMiddleware::class), + $container->get(CardTokenMiddleware::class), + $container->get(StoreCustomerMiddleware::class), + $container->get(AddSequenceTypeForSubscriptionsMiddleware::class), + $container->get(OrderLinesMiddleware::class), + $container->get(AddressMiddleware::class), + $container->get(UrlMiddleware::class), + $container->get(SelectedIssuerMiddleware::class), + ]; + $middlewareHandler = new MiddlewareHandler($middlewares); + return new OrderRequestStrategy( $dataHelper, $settingsHelper, - [ - $container->get(CustomerBirthdateDecorator::class), - $container->get(ApplePayTokenDecorator::class), - $container->get(CardTokenDecorator::class), - $container->get(StoreCustomerDecorator::class), - $container->get(AddSequenceTypeForSubscriptionsDecorator::class), - $container->get(OrderLinesDecorator::class), - $container->get(AddressDecorator::class), - $container->get(UrlDecorator::class), - $container->get(SelectedIssuerDecorator::class), - ] + $middlewareHandler ); }, 'request.strategy.payment' => static function (ContainerInterface $container): RequestStrategyInterface { $dataHelper = $container->get('settings.data_helper'); $settingsHelper = $container->get('settings.settings_helper'); - $issuer = $container->get(SelectedIssuerDecorator::class); - $url = $container->get(UrlDecorator::class); - $sequenceType = $container->get(AddSequenceTypeForSubscriptionsDecorator::class); - $cardToken = $container->get(CardTokenDecorator::class); - $applePayToken = $container->get(ApplePayTokenDecorator::class); - $storeCustomer = $container->get(StoreCustomerDecorator::class); - $paymentDescription = $container->get(PaymentDescriptionDecorator::class); - $addCustomRequestFields = $container->get(AddCustomRequestFieldsDecorator::class); + $issuer = $container->get(SelectedIssuerMiddleware::class); + $url = $container->get(UrlMiddleware::class); + $sequenceType = $container->get(AddSequenceTypeForSubscriptionsMiddleware::class); + $cardToken = $container->get(CardTokenMiddleware::class); + $applePayToken = $container->get(ApplePayTokenMiddleware::class); + $storeCustomer = $container->get(StoreCustomerMiddleware::class); + $paymentDescription = $container->get(PaymentDescriptionMiddleware::class); + $addCustomRequestFields = $container->get(AddCustomRequestFieldsMiddleware::class); + $middlewares = [ + $issuer, + $url, + $sequenceType, + $cardToken, + $applePayToken, + $storeCustomer, + $paymentDescription, + $addCustomRequestFields, + ]; + $middlewareHandler = new MiddlewareHandler($middlewares); return new PaymentRequestStrategy( $dataHelper, $settingsHelper, - [ - $issuer, - $url, - $sequenceType, - $cardToken, - $applePayToken, - $storeCustomer, - $paymentDescription, - $addCustomRequestFields, - ] + $middlewareHandler ); }, ]; diff --git a/src/PaymentMethods/Alma.php b/src/PaymentMethods/Alma.php index 2fb07abf..9139eb17 100644 --- a/src/PaymentMethods/Alma.php +++ b/src/PaymentMethods/Alma.php @@ -23,7 +23,7 @@ protected function getConfig(): array 'confirmationDelayed' => false, 'SEPA' => false, 'paymentAPIfields' => [ - 'AddressDecorator', + 'AddressMiddleware', ], 'docs' => 'https://www.mollie.com/gb/payments/alma', ]; diff --git a/src/Subscription/MollieSubscriptionGatewayHandler.php b/src/Subscription/MollieSubscriptionGatewayHandler.php index 25daf819..a13ddd71 100644 --- a/src/Subscription/MollieSubscriptionGatewayHandler.php +++ b/src/Subscription/MollieSubscriptionGatewayHandler.php @@ -13,7 +13,8 @@ use Mollie\WooCommerce\Payment\PaymentProcessor; use Mollie\WooCommerce\Notice\NoticeInterface; use Mollie\WooCommerce\Payment\MollieOrderService; -use Mollie\WooCommerce\Payment\Request\Decorators\UrlDecorator; +use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler; +use Mollie\WooCommerce\Payment\Request\Middleware\UrlMiddleware; use Mollie\WooCommerce\PaymentMethods\InstructionStrategies\OrderInstructionsManager; use Mollie\WooCommerce\PaymentMethods\PaymentMethodI; use Mollie\WooCommerce\SDK\Api; @@ -77,6 +78,10 @@ public function __construct( ); $this->apiHelper = $apiHelper; + $middlewares = [ + new UrlMiddleware($pluginId, $logger), + ]; + $middlewareHandler = new MiddlewareHandler($middlewares); $this->subscriptionObject = new MollieSubscription( $pluginId, $apiHelper, @@ -84,7 +89,7 @@ public function __construct( $dataService, $logger, $paymentMethod, - new UrlDecorator($pluginId, $logger) + $middlewareHandler ); } diff --git a/tests/php/Functional/Payment/OrderRequestStrategyTest.php b/tests/php/Functional/Payment/OrderRequestStrategyTest.php index bae2271c..9f87bdc5 100644 --- a/tests/php/Functional/Payment/OrderRequestStrategyTest.php +++ b/tests/php/Functional/Payment/OrderRequestStrategyTest.php @@ -6,7 +6,7 @@ use Mockery; use Mollie\Api\MollieApiClient; use Mollie\WooCommerce\Payment\OrderLines; -use Mollie\WooCommerce\Payment\Request\Decorators\OrderLinesDecorator; +use Mollie\WooCommerce\Payment\Request\Decorators\OrderLinesMiddleware; use Mollie\WooCommerce\Payment\Request\Strategies\OrderRequestStrategy; use Mollie\WooCommerce\Payment\Request\Strategies\RequestStrategyInterface; use Mollie\WooCommerceTests\Functional\HelperMocks; @@ -222,7 +222,7 @@ public function executeTest($order) [] ); $orderLines = new OrderLines($this->helperMocks->dataHelper($apiClientMock), $this->helperMocks->pluginId()); - $linesDecorator = new OrderLinesDecorator($orderLines, 'no_category'); + $linesDecorator = new OrderLinesMiddleware($orderLines, 'no_category'); $paymentGateway = $this->helperMocks->genericPaymentGatewayMock(); when('wc_get_payment_gateway_by_order')->justReturn($paymentGateway);