From 6960bdf0e2b4c63911edc82b5f4ff6e1926ec915 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Mon, 16 Dec 2024 19:49:46 +0100 Subject: [PATCH] Add decorators and strategies for request --- src/Payment/MollieObject.php | 33 ++--- src/Payment/MollieOrder.php | 122 +----------------- src/Payment/MolliePayment.php | 70 +--------- src/Payment/PaymentModule.php | 42 ++---- ...dSequenceTypeForSubscriptionsDecorator.php | 53 ++++++++ .../Decorators/ApplePaytokenDecorator.php | 22 ++++ .../Request/Decorators/CardTokenDecorator.php | 20 +++ .../Decorators/CustomerBirthdateDecorator.php | 45 +++++++ .../Decorators/RequestDecoratorInterface.php | 10 ++ .../Decorators/StoreCustomerDecorator.php | 32 +++++ src/Payment/Request/RequestFactory.php | 34 +++++ .../Strategies/OrderRequestStrategy.php | 93 +++++++++++++ .../Strategies/PaymentRequestStrategy.php | 92 +++++++++++++ .../Strategies/RequestStrategyInterface.php | 9 ++ src/Payment/inc/services.php | 104 +++++++++++++++ 15 files changed, 538 insertions(+), 243 deletions(-) create mode 100644 src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php create mode 100644 src/Payment/Request/Decorators/ApplePaytokenDecorator.php create mode 100644 src/Payment/Request/Decorators/CardTokenDecorator.php create mode 100644 src/Payment/Request/Decorators/CustomerBirthdateDecorator.php create mode 100644 src/Payment/Request/Decorators/RequestDecoratorInterface.php create mode 100644 src/Payment/Request/Decorators/StoreCustomerDecorator.php create mode 100644 src/Payment/Request/RequestFactory.php create mode 100644 src/Payment/Request/Strategies/OrderRequestStrategy.php create mode 100644 src/Payment/Request/Strategies/PaymentRequestStrategy.php create mode 100644 src/Payment/Request/Strategies/RequestStrategyInterface.php create mode 100644 src/Payment/inc/services.php diff --git a/src/Payment/MollieObject.php b/src/Payment/MollieObject.php index edf3179c..6a507b09 100644 --- a/src/Payment/MollieObject.php +++ b/src/Payment/MollieObject.php @@ -9,6 +9,7 @@ use Mollie\Api\Resources\Order; use Mollie\Api\Resources\Payment; use Mollie\WooCommerce\Gateway\MolliePaymentGatewayHandler; +use Mollie\WooCommerce\Payment\Request\RequestFactory; use Mollie\WooCommerce\PaymentMethods\Voucher; use Mollie\WooCommerce\SDK\Api; use Mollie\WooCommerce\Settings\Settings; @@ -55,8 +56,17 @@ class MollieObject * @var null */ private $paymentMethod; - - public function __construct($data, Logger $logger, PaymentFactory $paymentFactory, Api $apiHelper, Settings $settingsHelper, string $pluginId) + protected RequestFactory $requestFactory; + + public function __construct( + $data, + Logger $logger, + PaymentFactory $paymentFactory, + Api $apiHelper, + Settings $settingsHelper, + string $pluginId, + RequestFactory $requestFactory + ) { $this->data = $data; $this->logger = $logger; @@ -67,6 +77,7 @@ public function __construct($data, Logger $logger, PaymentFactory $paymentFactor $base_location = wc_get_base_location(); static::$shop_country = $base_location['country']; $this->paymentMethod = null; + $this->requestFactory = $requestFactory; } public function data() @@ -657,24 +668,6 @@ protected function addMandateIdMetaToFirstPaymentSubscriptionOrder( } } } - - protected function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway, $paymentRequestData): array - { - if ($this->dataHelper->isSubscription($orderId) || $this->dataHelper->isWcSubscription($orderId)) { - $disable_automatic_payments = apply_filters($this->pluginId . '_is_automatic_payment_disabled', false); - $supports_subscriptions = $gateway->supports('subscriptions'); - - if ($supports_subscriptions == true && $disable_automatic_payments == false) { - $paymentRequestData = $this->addSequenceTypeFirst($paymentRequestData); - } - } - return $paymentRequestData; - } - - public function addSequenceTypeFirst($paymentRequestData) - { - } - /** * @param $order */ diff --git a/src/Payment/MollieOrder.php b/src/Payment/MollieOrder.php index 19127c25..dc9be0a8 100644 --- a/src/Payment/MollieOrder.php +++ b/src/Payment/MollieOrder.php @@ -90,95 +90,10 @@ public function getPaymentObject($paymentId, $testMode = false, $useCache = true */ public function getPaymentRequestData($order, $customerId, $voucherDefaultCategory = Voucher::NO_CATEGORY) { - $settingsHelper = $this->settingsHelper; - $paymentLocale = $settingsHelper->getPaymentLocale(); - $storeCustomer = $settingsHelper->shouldStoreCustomer(); - - $gateway = wc_get_payment_gateway_by_order($order); - - if (! $gateway || ! ( $gateway instanceof PaymentGateway )) { - return [ 'result' => 'failure' ]; - } - - $gatewayId = $gateway->id; - $selectedIssuer = $this->getSelectedIssuer($gatewayId); - $returnUrl = $gateway->get_return_url($order); - $returnUrl = $this->getReturnUrl($order, $returnUrl); - $webhookUrl = $this->getWebhookUrl($order, $gatewayId); - $isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton'; - $billingAddress = null; - if (!$isPayPalExpressOrder) { - $billingAddress = $this->createBillingAddress($order); - $shippingAddress = $this->createShippingAddress($order); - } - - // Generate order lines for Mollie Orders - $orderLinesHelper = $this->orderLines; - $orderLines = $orderLinesHelper->order_lines($order, $voucherDefaultCategory); - $methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); - - // Build the Mollie order data - $paymentRequestData = [ - 'amount' => [ - 'currency' => $this->dataHelper->getOrderCurrency($order), - 'value' => $this->dataHelper->formatCurrencyValue( - $order->get_total(), - $this->dataHelper->getOrderCurrency($order) - ), - ], - 'redirectUrl' => $returnUrl, - 'webhookUrl' => $webhookUrl, - 'method' => $methodId, - 'payment' => [ - 'issuer' => $selectedIssuer, - ], - 'locale' => $paymentLocale, - 'billingAddress' => $billingAddress, - 'metadata' => apply_filters( - $this->pluginId . '_payment_object_metadata', - [ - 'order_id' => $order->get_id(), - 'order_number' => $order->get_order_number(), - ] - ), - 'lines' => $orderLines['lines'], - 'orderNumber' => $order->get_order_number(), - ]; - - $paymentRequestData = $this->addSequenceTypeForSubscriptionsFirstPayments($order->get_id(), $gateway, $paymentRequestData); - - // Only add shippingAddress if all required fields are set - if ( - !empty($shippingAddress->streetAndNumber) - && !empty($shippingAddress->postalCode) - && !empty($shippingAddress->city) - && !empty($shippingAddress->country) - ) { - $paymentRequestData['shippingAddress'] = $shippingAddress; - } - - // Only store customer at Mollie if setting is enabled - if ($storeCustomer) { - $paymentRequestData['payment']['customerId'] = $customerId; - } - - $cardToken = mollieWooCommerceCardToken(); - if ($cardToken && isset($paymentRequestData['payment'])) { - $paymentRequestData['payment']['cardToken'] = $cardToken; - } - //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - $applePayToken = wc_clean(wp_unslash($_POST["token"] ?? '')); - if ($applePayToken && isset($paymentRequestData['payment'])) { - $encodedApplePayToken = wp_json_encode($applePayToken); - $paymentRequestData['payment']['applePayPaymentToken'] = $encodedApplePayToken; - } - $customerBirthdate = $this->getCustomerBirthdate($order); - if ($customerBirthdate) { - $paymentRequestData['consumerDateOfBirth'] = $customerBirthdate; - } - return $paymentRequestData; + return $this->requestFactory->createRequest('order', $order, $customerId); } + public function setActiveMolliePayment($orderId) { self::$paymentId = $this->getMolliePaymentIdFromPaymentObject(); @@ -248,12 +163,6 @@ public function getMollieCustomerIbanDetailsFromPaymentObject($payment = null) return $ibanDetails; } - public function addSequenceTypeFirst($paymentRequestData) - { - $paymentRequestData['payment']['sequenceType'] = 'first'; - return $paymentRequestData; - } - /** * @param WC_Order $order * @param Order $payment @@ -1005,31 +914,4 @@ protected function processOrderItemsRefund( // drop item from array unset($items[$item->get_id()]); } - - protected function getCustomerBirthdate($order) - { - $gateway = wc_get_payment_gateway_by_order($order); - if (!$gateway || !isset($gateway->id)) { - return null; - } - if (strpos($gateway->id, 'mollie_wc_gateway_') === false) { - return null; - } - $additionalFields = $this->paymentMethod->getProperty('additionalFields'); - $methodId = $additionalFields && in_array('birthdate', $additionalFields, true); - if ($methodId) { - $optionName = 'billing_birthdate_' . $this->paymentMethod->getProperty('id'); - //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - $fieldPosted = wc_clean(wp_unslash($_POST[$optionName] ?? '')); - if ($fieldPosted === '' || !is_string($fieldPosted)) { - return null; - } - - $order->update_meta_data($optionName, $fieldPosted); - $order->save(); - $format = "Y-m-d"; - return gmdate($format, (int) strtotime($fieldPosted)); - } - return null; - } } diff --git a/src/Payment/MolliePayment.php b/src/Payment/MolliePayment.php index 70368dad..c6a7f808 100644 --- a/src/Payment/MolliePayment.php +++ b/src/Payment/MolliePayment.php @@ -67,76 +67,8 @@ public function getPaymentObject($paymentId, $testMode = false, $useCache = true */ public function getPaymentRequestData($order, $customerId, $voucherDefaultCategory = Voucher::NO_CATEGORY) { - $settingsHelper = $this->settingsHelper; - $optionName = $this->pluginId . '_' . 'api_payment_description'; - $option = get_option($optionName); - $paymentDescription = $this->getPaymentDescription($order, $option); - $paymentLocale = $settingsHelper->getPaymentLocale(); - $storeCustomer = $settingsHelper->shouldStoreCustomer(); - - $gateway = wc_get_payment_gateway_by_order($order); - - if (!$gateway || !($gateway instanceof PaymentGateway)) { - return ['result' => 'failure']; - } - - $gatewayId = $gateway->id; - $selectedIssuer = $this->getSelectedIssuer($gatewayId); - $returnUrl = $gateway->get_return_url($order); - $returnUrl = $this->getReturnUrl($order, $returnUrl); - $webhookUrl = $this->getWebhookUrl($order, $gatewayId); - $orderId = $order->get_id(); - - $paymentRequestData = [ - 'amount' => [ - 'currency' => $this->dataHelper - ->getOrderCurrency($order), - 'value' => $this->dataHelper - ->formatCurrencyValue( - $order->get_total(), - $this->dataHelper->getOrderCurrency( - $order - ) - ), - ], - 'description' => $paymentDescription, - 'redirectUrl' => $returnUrl, - 'webhookUrl' => $webhookUrl, - 'method' => $this->paymentMethod->getProperty('id'), - 'issuer' => $selectedIssuer, - 'locale' => $paymentLocale, - 'metadata' => apply_filters( - $this->pluginId . '_payment_object_metadata', - [ - 'order_id' => $order->get_id(), - ] - ), - ]; - - $paymentRequestData = $this->addSequenceTypeForSubscriptionsFirstPayments($order->get_id(), $gateway, $paymentRequestData); - - if ($storeCustomer) { - $paymentRequestData['customerId'] = $customerId; - } - - $cardToken = mollieWooCommerceCardToken(); - if ($cardToken) { - $paymentRequestData['cardToken'] = $cardToken; - } - //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - $applePayToken = wc_clean(wp_unslash($_POST["token"] ?? '')); - if ($applePayToken) { - $encodedApplePayToken = wp_json_encode($applePayToken); - $paymentRequestData['applePayPaymentToken'] = $encodedApplePayToken; - } - $paymentRequestData = $this->addCustomRequestFields($order, $paymentRequestData); - return $paymentRequestData; - } + return $this->requestFactory->createRequest('payment', $order, $customerId); - public function addSequenceTypeFirst($paymentRequestData) - { - $paymentRequestData['sequenceType'] = 'first'; - return $paymentRequestData; } /** diff --git a/src/Payment/PaymentModule.php b/src/Payment/PaymentModule.php index f07d0d47..4fd78cd2 100644 --- a/src/Payment/PaymentModule.php +++ b/src/Payment/PaymentModule.php @@ -50,40 +50,14 @@ class PaymentModule implements ServiceModule, ExecutableModule public function services(): array { - return [ - OrderLines::class => static function (ContainerInterface $container): OrderLines { - $data = $container->get('settings.data_helper'); - $pluginId = $container->get('shared.plugin_id'); - return new OrderLines($data, $pluginId); - }, - PaymentFactory::class => static function (ContainerInterface $container): PaymentFactory { - $settingsHelper = $container->get('settings.settings_helper'); - assert($settingsHelper instanceof Settings); - $apiHelper = $container->get('SDK.api_helper'); - assert($apiHelper instanceof Api); - $data = $container->get('settings.data_helper'); - assert($data instanceof Data); - $pluginId = $container->get('shared.plugin_id'); - $logger = $container->get(Logger::class); - assert($logger instanceof Logger); - $orderLines = $container->get(OrderLines::class); - return new PaymentFactory($data, $apiHelper, $settingsHelper, $pluginId, $logger, $orderLines); - }, - MollieObject::class => static function (ContainerInterface $container): MollieObject { - $logger = $container->get(Logger::class); - assert($logger instanceof Logger); - $data = $container->get('settings.data_helper'); - assert($data instanceof Data); - $apiHelper = $container->get('SDK.api_helper'); - assert($apiHelper instanceof Api); - $pluginId = $container->get('shared.plugin_id'); - $paymentFactory = $container->get(PaymentFactory::class); - assert($paymentFactory instanceof PaymentFactory); - $settingsHelper = $container->get('settings.settings_helper'); - assert($settingsHelper instanceof Settings); - return new MollieObject($data, $logger, $paymentFactory, $apiHelper, $settingsHelper, $pluginId); - }, - ]; + static $services; + + if ($services === null) { + + $services = require_once __DIR__ . '/inc/services.php'; + } + + return $services(); } public function run(ContainerInterface $container): bool diff --git a/src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php b/src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php new file mode 100644 index 00000000..61227cd0 --- /dev/null +++ b/src/Payment/Request/Decorators/AddSequenceTypeForSubscriptionsDecorator.php @@ -0,0 +1,53 @@ +dataHelper = $dataHelper; + $this->pluginId = $pluginId; + } + + public function decorate(array $requestData, WC_Order $order, $context): array + { + $gateway = wc_get_payment_gateway_by_order($order); + if ($gateway) { + $requestData = $this->addSequenceTypeForSubscriptionsFirstPayments($order->get_id(), $gateway, $requestData, $context); + } + return $requestData; + } + + private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway, $requestData, $context): array + { + if ($this->dataHelper->isSubscription($orderId) || $this->dataHelper->isWcSubscription($orderId)) { + $disable_automatic_payments = apply_filters($this->pluginId . '_is_automatic_payment_disabled', false); + $supports_subscriptions = $gateway->supports('subscriptions'); + + if ($supports_subscriptions == true && $disable_automatic_payments == false) { + $requestData = $this->addSequenceTypeFirst($requestData, $context); + } + } + return $requestData; + } + + private function addSequenceTypeFirst($requestData, $context) + { + if($context === 'order') { + $requestData['payment']['sequenceType'] = 'first'; + } elseif ($context === 'payment') { + $requestData['sequenceType'] = 'first'; + } + return $requestData; + } +} diff --git a/src/Payment/Request/Decorators/ApplePaytokenDecorator.php b/src/Payment/Request/Decorators/ApplePaytokenDecorator.php new file mode 100644 index 00000000..d13b14c5 --- /dev/null +++ b/src/Payment/Request/Decorators/ApplePaytokenDecorator.php @@ -0,0 +1,22 @@ +paymentMethods = $paymentMethods; + } + + public function decorate(array $requestData, WC_Order $order): array + { + $gateway = wc_get_payment_gateway_by_order($order); + if (!$gateway || !isset($gateway->id)) { + return $requestData; + } + if (strpos($gateway->id, 'mollie_wc_gateway_') === false) { + return $requestData; + } + $paymentMethodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); + $paymentMethod = $this->paymentMethods[$paymentMethodId]; + $additionalFields = $paymentMethod->getProperty('additionalFields'); + $methodId = $additionalFields && in_array('birthdate', $additionalFields, true); + if ($methodId) { + $optionName = 'billing_birthdate_' . $paymentMethod->getProperty('id'); + //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + $fieldPosted = wc_clean(wp_unslash($_POST[$optionName] ?? '')); + if ($fieldPosted === '' || !is_string($fieldPosted)) { + return $requestData; + } + + $order->update_meta_data($optionName, $fieldPosted); + $order->save(); + $format = "Y-m-d"; + $requestData['consumerDateOfBirth'] = gmdate($format, (int) strtotime($fieldPosted)); + } + return $requestData; + } +} diff --git a/src/Payment/Request/Decorators/RequestDecoratorInterface.php b/src/Payment/Request/Decorators/RequestDecoratorInterface.php new file mode 100644 index 00000000..cbf0f7d3 --- /dev/null +++ b/src/Payment/Request/Decorators/RequestDecoratorInterface.php @@ -0,0 +1,10 @@ +settingsHelper = $settingsHelper; + } + + + public function decorate(array $requestData, WC_Order $order): array + { + + $storeCustomer = $this->settingsHelper->shouldStoreCustomer(); + $customerId = $order->get_meta('_mollie_customer_id'); + + if ($storeCustomer && $customerId) { + $requestData['payment']['customerId'] = $customerId; + } + + return $requestData; + } +} diff --git a/src/Payment/Request/RequestFactory.php b/src/Payment/Request/RequestFactory.php new file mode 100644 index 00000000..37ea4f55 --- /dev/null +++ b/src/Payment/Request/RequestFactory.php @@ -0,0 +1,34 @@ +container = $container; + } + + /** + * Create a request based on the type. + * + * @param string $type 'order' or 'payment'. + * @param WC_Order $order The WooCommerce order object. + * @param string $customerId Customer ID for the request. + * @return array The generated request data. + */ + public function createRequest(string $type, WC_Order $order, string $customerId): array { + // Use the container to fetch the appropriate strategy. + $strategy = $this->container->get("request.strategy.{$type}"); + + if (!$strategy instanceof RequestStrategyInterface) { + throw new \InvalidArgumentException("Invalid strategy for type: {$type}"); + } + + return $strategy->createRequest($order, $customerId); + } +} diff --git a/src/Payment/Request/Strategies/OrderRequestStrategy.php b/src/Payment/Request/Strategies/OrderRequestStrategy.php new file mode 100644 index 00000000..6f0f55b3 --- /dev/null +++ b/src/Payment/Request/Strategies/OrderRequestStrategy.php @@ -0,0 +1,93 @@ +dataHelper = $dataHelper; + $this->settingsHelper = $settingsHelper; + $this->decorators = $decorators; + } + public function createRequest(WC_Order $order, string $customerId): array + { + $settingsHelper = $this->settingsHelper; + + $gateway = wc_get_payment_gateway_by_order($order); + + if (! $gateway || ! ( $gateway instanceof PaymentGateway )) { + return [ 'result' => 'failure' ]; + } + + $gatewayId = $gateway->id; + $paymentLocale = $settingsHelper->getPaymentLocale(); + $selectedIssuer = $this->getSelectedIssuer($gatewayId); + $returnUrl = $gateway->get_return_url($order); + $returnUrl = $this->getReturnUrl($order, $returnUrl); + $webhookUrl = $this->getWebhookUrl($order, $gatewayId); + $isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton'; + $billingAddress = null; + if (!$isPayPalExpressOrder) { + $billingAddress = $this->createBillingAddress($order); + $shippingAddress = $this->createShippingAddress($order); + } + // Only add shippingAddress if all required fields are set + if ( + !empty($shippingAddress->streetAndNumber) + && !empty($shippingAddress->postalCode) + && !empty($shippingAddress->city) + && !empty($shippingAddress->country) + ) { + $requestData['shippingAddress'] = $shippingAddress; + } + + // Generate order lines for Mollie Orders + $orderLinesHelper = $this->orderLines; + $orderLines = $orderLinesHelper->order_lines($order, $voucherDefaultCategory); + $methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); + + // Build the Mollie order data + $requestData = [ + 'amount' => [ + 'currency' => $this->dataHelper->getOrderCurrency($order), + 'value' => $this->dataHelper->formatCurrencyValue( + $order->get_total(), + $this->dataHelper->getOrderCurrency($order) + ), + ], + 'redirectUrl' => $returnUrl, + 'webhookUrl' => $webhookUrl, + 'method' => $methodId, + 'payment' => [ + 'issuer' => $selectedIssuer, + ], + 'locale' => $paymentLocale, + 'billingAddress' => $billingAddress, + 'metadata' => apply_filters( + $this->pluginId . '_payment_object_metadata', + [ + 'order_id' => $order->get_id(), + 'order_number' => $order->get_order_number(), + ] + ), + 'lines' => $orderLines['lines'], + 'orderNumber' => $order->get_order_number(), + ]; + + + $context = 'order'; + foreach ($this->decorators as $decorator) { + $requestData = $decorator->decorate($requestData, $order, $context); + } + + return $requestData; + } +} diff --git a/src/Payment/Request/Strategies/PaymentRequestStrategy.php b/src/Payment/Request/Strategies/PaymentRequestStrategy.php new file mode 100644 index 00000000..52fd8675 --- /dev/null +++ b/src/Payment/Request/Strategies/PaymentRequestStrategy.php @@ -0,0 +1,92 @@ +dataHelper = $dataHelper; + $this->settingsHelper = $settingsHelper; + $this->decorators = $decorators; + } + public function createRequest(WC_Order $order, string $customerId): array + { + + $gateway = wc_get_payment_gateway_by_order($order); + + if (!$gateway || !($gateway instanceof PaymentGateway)) { + return ['result' => 'failure']; + } + $settingsHelper = $this->settingsHelper; + $optionName = $this->pluginId . '_' . 'api_payment_description'; + $option = get_option($optionName); + $paymentDescription = $this->getPaymentDescription($order, $option); + $paymentLocale = $settingsHelper->getPaymentLocale(); + $storeCustomer = $settingsHelper->shouldStoreCustomer(); + + $gatewayId = $gateway->id; + $selectedIssuer = $this->getSelectedIssuer($gatewayId); + $returnUrl = $gateway->get_return_url($order); + $returnUrl = $this->getReturnUrl($order, $returnUrl); + $webhookUrl = $this->getWebhookUrl($order, $gatewayId); + $orderId = $order->get_id(); + + $paymentRequestData = [ + 'amount' => [ + 'currency' => $this->dataHelper + ->getOrderCurrency($order), + 'value' => $this->dataHelper + ->formatCurrencyValue( + $order->get_total(), + $this->dataHelper->getOrderCurrency( + $order + ) + ), + ], + 'description' => $paymentDescription, + 'redirectUrl' => $returnUrl, + 'webhookUrl' => $webhookUrl, + 'method' => $this->paymentMethod->getProperty('id'), + 'issuer' => $selectedIssuer, + 'locale' => $paymentLocale, + 'metadata' => apply_filters( + $this->pluginId . '_payment_object_metadata', + [ + 'order_id' => $order->get_id(), + ] + ), + ]; + + $paymentRequestData = $this->addSequenceTypeForSubscriptionsFirstPayments($order->get_id(), $gateway, $paymentRequestData); + + if ($storeCustomer) { + $paymentRequestData['customerId'] = $customerId; + } + + $cardToken = mollieWooCommerceCardToken(); + if ($cardToken) { + $paymentRequestData['cardToken'] = $cardToken; + } + //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + $applePayToken = wc_clean(wp_unslash($_POST["token"] ?? '')); + if ($applePayToken) { + $encodedApplePayToken = wp_json_encode($applePayToken); + $paymentRequestData['applePayPaymentToken'] = $encodedApplePayToken; + } + $paymentRequestData = $this->addCustomRequestFields($order, $paymentRequestData); + $context = 'payment'; + foreach ($this->decorators as $decorator) { + $requestData = $decorator->decorate($requestData, $order, $context); + } + + return $requestData; + } +} diff --git a/src/Payment/Request/Strategies/RequestStrategyInterface.php b/src/Payment/Request/Strategies/RequestStrategyInterface.php new file mode 100644 index 00000000..0f95ea8d --- /dev/null +++ b/src/Payment/Request/Strategies/RequestStrategyInterface.php @@ -0,0 +1,9 @@ + static function (ContainerInterface $container): OrderLines { + $data = $container->get('settings.data_helper'); + $pluginId = $container->get('shared.plugin_id'); + return new OrderLines($data, $pluginId); + }, + PaymentFactory::class => static function (ContainerInterface $container): PaymentFactory { + $settingsHelper = $container->get('settings.settings_helper'); + assert($settingsHelper instanceof Settings); + $apiHelper = $container->get('SDK.api_helper'); + assert($apiHelper instanceof Api); + $data = $container->get('settings.data_helper'); + assert($data instanceof Data); + $pluginId = $container->get('shared.plugin_id'); + $logger = $container->get(Logger::class); + assert($logger instanceof Logger); + $orderLines = $container->get(OrderLines::class); + return new PaymentFactory($data, $apiHelper, $settingsHelper, $pluginId, $logger, $orderLines); + }, + RequestFactory::class => static function (ContainerInterface $container): RequestFactory { + return new RequestFactory($container); + }, + CustomerBirthdateDecorator::class => static function (ContainerInterface $container): CustomerBirthdateDecorator { + return new CustomerBirthdateDecorator($container->get('payment_methods')); + }, + ApplePayTokenDecorator::class => static function (): ApplePayTokenDecorator { + return new ApplePayTokenDecorator(); + }, + CardTokenDecorator::class => static function (): CardTokenDecorator { + return new CardTokenDecorator(); + }, + StoreCustomerDecorator::class => static function (ContainerInterface $container): StoreCustomerDecorator { + return new StoreCustomerDecorator($container->get('settings.settings_helper')); + }, + AddSequenceTypeForSubscriptionsDecorator::class => static function (ContainerInterface $container): AddSequenceTypeForSubscriptionsDecorator { + $dataHelper = $container->get('settings.data_helper'); + $pluginId = $container->get('shared.plugin_id'); + return new AddSequenceTypeForSubscriptionsDecorator($dataHelper, $pluginId); + }, + + 'request.strategy.order' => static function (ContainerInterface $container): RequestStrategyInterface { + $dataHelper = $container->get('settings.data_helper'); + $settingsHelper = $container->get('settings.settings_helper'); + return new OrderRequestStrategy( + $dataHelper, + $settingsHelper, + [ + $container->get(CustomerBirthdateDecorator::class), + $container->get(ApplePayTokenDecorator::class), + $container->get(CardTokenDecorator::class), + $container->get(StoreCustomerDecorator::class), + + ] + ); + }, + 'request.strategy.payment' => static function (ContainerInterface $container): RequestStrategyInterface { + $dataHelper = $container->get('settings.data_helper'); + $settingsHelper = $container->get('settings.settings_helper'); + $decorators = [ + + ]; + return new PaymentRequestStrategy($dataHelper, $settingsHelper, $decorators); + }, + MollieObject::class => static function (ContainerInterface $container): MollieObject { + $logger = $container->get(Logger::class); + assert($logger instanceof Logger); + $data = $container->get('settings.data_helper'); + assert($data instanceof Data); + $apiHelper = $container->get('SDK.api_helper'); + assert($apiHelper instanceof Api); + $pluginId = $container->get('shared.plugin_id'); + $paymentFactory = $container->get(PaymentFactory::class); + assert($paymentFactory instanceof PaymentFactory); + $settingsHelper = $container->get('settings.settings_helper'); + assert($settingsHelper instanceof Settings); + $requestFactory = $container->get(RequestFactory::class); + return new MollieObject($data, $logger, $paymentFactory, $apiHelper, $settingsHelper, $pluginId, $requestFactory); + }, + ]; +};