diff --git a/src/Payment/MollieObject.php b/src/Payment/MollieObject.php index 7d40e199..b129d8b5 100644 --- a/src/Payment/MollieObject.php +++ b/src/Payment/MollieObject.php @@ -4,11 +4,9 @@ namespace Mollie\WooCommerce\Payment; -use Inpsyde\PaymentGateway\PaymentGateway; use Mollie\Api\Exceptions\ApiException; 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; @@ -17,7 +15,6 @@ use WC_Order; use WC_Payment_Gateway; use Psr\Log\LoggerInterface as Logger; -use stdClass; class MollieObject { diff --git a/src/Payment/Request/Middleware/AddCustomRequestFieldsMiddleware.php b/src/Payment/Request/Middleware/AddCustomRequestFieldsMiddleware.php index fde0968a..2ac3de1a 100644 --- a/src/Payment/Request/Middleware/AddCustomRequestFieldsMiddleware.php +++ b/src/Payment/Request/Middleware/AddCustomRequestFieldsMiddleware.php @@ -7,18 +7,40 @@ use Psr\Container\ContainerInterface; use WC_Order; +/** + * Class AddCustomRequestFieldsMiddleware + * + * This middleware adds custom request fields to the payment request data. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class AddCustomRequestFieldsMiddleware implements RequestMiddlewareInterface { private array $paymentMethods; private ContainerInterface $container; + /** + * AddCustomRequestFieldsMiddleware constructor. + * + * @param array $paymentMethods An array of available payment methods. + * @param ContainerInterface $container A container for dependency injection. + */ public function __construct($paymentMethods, $container) { $this->paymentMethods = $paymentMethods; $this->container = $container; } - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data to be modified. + * @param WC_Order $order The WooCommerce order object. + * @param mixed $context Additional context for the middleware. + * @param callable $next The next middleware to be called. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, $next): array { $gateway = wc_get_payment_gateway_by_order($order); $methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); diff --git a/src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php b/src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php index 110c41ff..7713b4d9 100644 --- a/src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php +++ b/src/Payment/Request/Middleware/AddSequenceTypeForSubscriptionsMiddleware.php @@ -7,18 +7,43 @@ use Mollie\WooCommerce\Shared\Data; use WC_Order; +/** + * Middleware to add sequence type for subscription payments. + */ class AddSequenceTypeForSubscriptionsMiddleware implements RequestMiddlewareInterface { + /** + * @var Data Helper class for data operations. + */ private Data $dataHelper; + + /** + * @var string Plugin ID. + */ private string $pluginId; + /** + * Constructor. + * + * @param Data $dataHelper Helper class for data operations. + * @param string $pluginId Plugin ID. + */ public function __construct($dataHelper, $pluginId) { $this->dataHelper = $dataHelper; $this->pluginId = $pluginId; } - public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @param callable $next The next middleware to call. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, callable $next): array { $gateway = wc_get_payment_gateway_by_order($order); if ($gateway) { @@ -27,6 +52,15 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, c return $next($requestData, $order, $context); } + /** + * Add sequence type for the first payments of subscriptions. + * + * @param int $orderId The order ID. + * @param WC_Payment_Gateway $gateway The payment gateway. + * @param array $requestData The request data. + * @param string $context The context of the request. + * @return array The modified request data. + */ private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway, $requestData, $context): array { if ($this->dataHelper->isSubscription($orderId) || $this->dataHelper->isWcSubscription($orderId)) { @@ -40,6 +74,13 @@ private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway return $requestData; } + /** + * Add the sequence type 'first' to the request data. + * + * @param array $requestData The request data. + * @param string $context The context of the request. + * @return array The modified request data. + */ private function addSequenceTypeFirst($requestData, $context) { if ($context === 'order') { diff --git a/src/Payment/Request/Middleware/AddressMiddleware.php b/src/Payment/Request/Middleware/AddressMiddleware.php index 0649c537..a9e1498f 100644 --- a/src/Payment/Request/Middleware/AddressMiddleware.php +++ b/src/Payment/Request/Middleware/AddressMiddleware.php @@ -7,13 +7,30 @@ use stdClass; use WC_Order; +/** + * Class AddressMiddleware + * + * This middleware adds address information to the payment request data. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ 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 __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array + + /** + * Invoke the middleware. + * + * @param array $requestData The request data to be modified. + * @param WC_Order $order The WooCommerce order object. + * @param mixed $context Additional context for the middleware. + * @param callable $next The next middleware to be called. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, callable $next): array { $isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton'; $billingAddress = null; @@ -34,7 +51,13 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, c return $next($requestData, $order, $context); } - private function createBillingAddress(WC_Order $order) + /** + * Create the billing address object. + * + * @param WC_Order $order The WooCommerce order object. + * @return stdClass The billing address object. + */ + private function createBillingAddress(WC_Order $order): stdClass { // Setup billing and shipping objects $billingAddress = new stdClass(); @@ -99,7 +122,13 @@ private function createBillingAddress(WC_Order $order) return $billingAddress; } - private function createShippingAddress(WC_Order $order) + /** + * Create the shipping address object. + * + * @param WC_Order $order The WooCommerce order object. + * @return stdClass The shipping address object. + */ + private function createShippingAddress(WC_Order $order): stdClass { $shippingAddress = new stdClass(); // Get user details @@ -113,7 +142,6 @@ private function createShippingAddress(WC_Order $order) ? null : $order->get_billing_email(); // WooCommerce doesn't have a shipping email - // Create shippingAddress object $shippingAddress->streetAndNumber = (ctype_space( $order->get_shipping_address_1() @@ -162,18 +190,29 @@ private function createShippingAddress(WC_Order $order) return $shippingAddress; } - protected function getPhoneNumber($order) + /** + * Get the phone number from the order. + * + * @param WC_Order $order The WooCommerce order object. + * @return string|null The phone number. + */ + protected function getPhoneNumber($order): ?string { - $phone = !empty($order->get_billing_phone()) ? $order->get_billing_phone() : $order->get_shipping_phone(); if (empty($phone)) { //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - $phone = wc_clean(wp_unslash($_POST['billing_phone'] ?? '')); + $phone = wc_clean(wp_unslash($_POST['billing_phone'] ?? '')); } return $phone; } - protected function getFormatedPhoneNumber(string $phone) + /** + * Format the phone number. + * + * @param string $phone The phone number. + * @return string|null The formatted phone number. + */ + protected function getFormatedPhoneNumber(string $phone): ?string { //remove whitespaces and all non numerical characters except + $phone = preg_replace('/[^0-9+]+/', '', $phone); @@ -191,8 +230,10 @@ protected function getFormatedPhoneNumber(string $phone) } /** - * @param $order - * @return string|null + * Get the billing company field. + * + * @param WC_Order $order The WooCommerce order object. + * @return string|null The billing company field. */ public function billingCompanyField($order): ?string { @@ -205,7 +246,13 @@ public function billingCompanyField($order): ?string ); } - private function checkBillieCompanyField($order) + /** + * Check the Billie company field. + * + * @param WC_Order $order The WooCommerce order object. + * @return string|null The Billie company field. + */ + private function checkBillieCompanyField($order): ?string { $gateway = wc_get_payment_gateway_by_order($order); if (!$gateway || !$gateway->id) { @@ -227,14 +274,13 @@ private function checkBillieCompanyField($order) } /** - * Method that shortens the field to a certain length - * - * @param string $field - * @param int $maximalLength + * Method that shortens the field to a certain length. * - * @return null|string + * @param string $field The field to be shortened. + * @param int $maximalLength The maximal length of the field. + * @return string|null The shortened field. */ - protected function maximalFieldLengths($field, $maximalLength) + protected function maximalFieldLengths($field, $maximalLength): ?string { if (!is_string($field)) { return null; diff --git a/src/Payment/Request/Middleware/ApplePayTokenMiddleware.php b/src/Payment/Request/Middleware/ApplePayTokenMiddleware.php index a0a2586c..312f1dd3 100644 --- a/src/Payment/Request/Middleware/ApplePayTokenMiddleware.php +++ b/src/Payment/Request/Middleware/ApplePayTokenMiddleware.php @@ -6,9 +6,21 @@ use WC_Order; +/** + * Middleware to handle Apple Pay token in the request. + */ class ApplePayTokenMiddleware implements RequestMiddlewareInterface { - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @param callable $next The next middleware to call. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, $next): array { // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $applePayToken = wc_clean(wp_unslash($_POST["token"] ?? '')); diff --git a/src/Payment/Request/Middleware/CardTokenMiddleware.php b/src/Payment/Request/Middleware/CardTokenMiddleware.php index dc95c269..6efa6fe5 100644 --- a/src/Payment/Request/Middleware/CardTokenMiddleware.php +++ b/src/Payment/Request/Middleware/CardTokenMiddleware.php @@ -6,9 +6,21 @@ use WC_Order; +/** + * Middleware to handle Card Token in the request. + */ class CardTokenMiddleware implements RequestMiddlewareInterface { - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @param callable $next The next middleware to call. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, $next): array { $cardToken = mollieWooCommerceCardToken(); if ($cardToken && isset($requestData['payment']) && $context === 'order') { diff --git a/src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php b/src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php index 9be0d720..1600436b 100644 --- a/src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php +++ b/src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php @@ -4,16 +4,36 @@ use WC_Order; +/** + * Middleware to handle customer birthdate in the request. + */ class CustomerBirthdateMiddleware implements RequestMiddlewareInterface { + /** + * @var array The payment methods. + */ private array $paymentMethods; + /** + * Constructor. + * + * @param array $paymentMethods The payment methods. + */ public function __construct(array $paymentMethods) { $this->paymentMethods = $paymentMethods; } - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @param callable $next The next middleware to call. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, $next): array { $gateway = wc_get_payment_gateway_by_order($order); if (!$gateway || !isset($gateway->id)) { @@ -28,7 +48,7 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, $ $methodId = $additionalFields && in_array('birthdate', $additionalFields, true); if ($methodId) { $optionName = 'billing_birthdate_' . $paymentMethod->getProperty('id'); - //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $fieldPosted = wc_clean(wp_unslash($_POST[$optionName] ?? '')); if ($fieldPosted === '' || !is_string($fieldPosted)) { return $requestData; diff --git a/src/Payment/Request/Middleware/MiddlewareHandler.php b/src/Payment/Request/Middleware/MiddlewareHandler.php index a22b370d..1a733825 100644 --- a/src/Payment/Request/Middleware/MiddlewareHandler.php +++ b/src/Payment/Request/Middleware/MiddlewareHandler.php @@ -4,22 +4,52 @@ use WC_Order; +/** + * Class MiddlewareHandler + * + * This class is responsible for managing and executing a sequence of middleware components. + * Each middleware component can modify the request data before it is processed further. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class MiddlewareHandler { + /** + * @var array The list of middleware. + */ private array $middlewares; + /** + * MiddlewareHandler constructor. + * + * @param array $middlewares The list of middleware. + */ public function __construct(array $middlewares) { $this->middlewares = $middlewares; } - public function handle(array $requestData, WC_Order $order, $context = null): array + /** + * Handle the request data through the middleware chain. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @return array The modified request data. + */ + public function handle(array $requestData, WC_Order $order, $context): array { $middlewareChain = $this->createMiddlewareChain($this->middlewares); return $middlewareChain($requestData, $order, $context); } + /** + * Create a chain of middleware. + * + * @param array $middlewares The list of middleware. + * @return callable The middleware chain. + */ private function createMiddlewareChain(array $middlewares): callable { return array_reduce( diff --git a/src/Payment/Request/Middleware/OrderLinesMiddleware.php b/src/Payment/Request/Middleware/OrderLinesMiddleware.php index ebe5931f..33539e1e 100644 --- a/src/Payment/Request/Middleware/OrderLinesMiddleware.php +++ b/src/Payment/Request/Middleware/OrderLinesMiddleware.php @@ -7,18 +7,47 @@ use Mollie\WooCommerce\Payment\OrderLines; use WC_Order; +/** + * Class OrderLinesMiddleware + * + * Middleware to handle order lines in the request. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class OrderLinesMiddleware implements RequestMiddlewareInterface { + /** + * @var OrderLines The order lines handler. + */ private OrderLines $orderLines; + + /** + * @var string The default category for vouchers. + */ private string $voucherDefaultCategory; - public function __construct($orderLines, $voucherDefaultCategory) + /** + * OrderLinesMiddleware constructor. + * + * @param OrderLines $orderLines The order lines handler. + * @param string $voucherDefaultCategory The default category for vouchers. + */ + public function __construct(OrderLines $orderLines, string $voucherDefaultCategory) { $this->orderLines = $orderLines; $this->voucherDefaultCategory = $voucherDefaultCategory; } - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @param callable $next The next middleware to call. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, $next): array { $orderLines = $this->orderLines->order_lines($order, $this->voucherDefaultCategory); $requestData['lines'] = $orderLines['lines']; diff --git a/src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php b/src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php index 1cb29597..1b449157 100644 --- a/src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php +++ b/src/Payment/Request/Middleware/PaymentDescriptionMiddleware.php @@ -6,16 +6,40 @@ use WC_Order; +/** + * Class PaymentDescriptionMiddleware + * + * Middleware to handle payment description in the request. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class PaymentDescriptionMiddleware implements RequestMiddlewareInterface { + /** + * @var mixed The data helper instance. + */ private $dataHelper; + /** + * PaymentDescriptionMiddleware constructor. + * + * @param mixed $dataHelper The data helper instance. + */ public function __construct($dataHelper) { $this->dataHelper = $dataHelper; } - public function __invoke(array $requestData, WC_Order $order, string $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data. + * @param WC_Order $order The WooCommerce order object. + * @param string $context The context of the request. + * @param callable $next The next middleware to call. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, string $context, $next): array { $optionName = $this->dataHelper->getPluginId() . '_' . 'api_payment_description'; $option = get_option($optionName); @@ -25,75 +49,74 @@ public function __invoke(array $requestData, WC_Order $order, string $context = return $next($requestData, $order, $context); } + /** + * Get the payment description. + * + * @param WC_Order $order The WooCommerce order object. + * @param mixed $option The option value. + * @return string The payment description. + */ private function getPaymentDescription(WC_Order $order, $option): string { $description = !$option ? '' : trim($option); $description = !$description ? '{orderNumber}' : $description; switch ($description) { - // Support for old deprecated options. - // TODO: remove when deprecated case '{orderNumber}': - $description = - /* translators: do not translate between {} */ - _x( - 'Order {orderNumber}', - 'Payment description for {orderNumber}', - 'mollie-payments-for-woocommerce' - ); + $description = _x( + 'Order {orderNumber}', + 'Payment description for {orderNumber}', + 'mollie-payments-for-woocommerce' + ); $description = $this->replaceTagsDescription($order, $description); break; case '{storeName}': - $description = - /* translators: do not translate between {} */ - _x( - 'StoreName {storeName}', - 'Payment description for {storeName}', - 'mollie-payments-for-woocommerce' - ); + $description = _x( + 'StoreName {storeName}', + 'Payment description for {storeName}', + 'mollie-payments-for-woocommerce' + ); $description = $this->replaceTagsDescription($order, $description); break; case '{customer.firstname}': - $description = - /* translators: do not translate between {} */ - _x( - 'Customer Firstname {customer.firstname}', - 'Payment description for {customer.firstname}', - 'mollie-payments-for-woocommerce' - ); + $description = _x( + 'Customer Firstname {customer.firstname}', + 'Payment description for {customer.firstname}', + 'mollie-payments-for-woocommerce' + ); $description = $this->replaceTagsDescription($order, $description); break; case '{customer.lastname}': - $description = - /* translators: do not translate between {} */ - _x( - 'Customer Lastname {customer.lastname}', - 'Payment description for {customer.lastname}', - 'mollie-payments-for-woocommerce' - ); + $description = _x( + 'Customer Lastname {customer.lastname}', + 'Payment description for {customer.lastname}', + 'mollie-payments-for-woocommerce' + ); $description = $this->replaceTagsDescription($order, $description); break; case '{customer.company}': - $description = - /* translators: do not translate between {} */ - _x( - 'Customer Company {customer.company}', - 'Payment description for {customer.company}', - 'mollie-payments-for-woocommerce' - ); + $description = _x( + 'Customer Company {customer.company}', + 'Payment description for {customer.company}', + 'mollie-payments-for-woocommerce' + ); $description = $this->replaceTagsDescription($order, $description); break; - // Support for custom string with interpolation. default: - // Replace available description tags. $description = $this->replaceTagsDescription($order, $description); break; } - // Fall back on default if description turns out empty. return !$description ? __('Order', 'woocommerce') . ' ' . $order->get_order_number() : $description; } + /** + * Replace tags in the description with actual values. + * + * @param WC_Order $order The WooCommerce order object. + * @param string $description The description with tags. + * @return string The description with tags replaced. + */ private function replaceTagsDescription(WC_Order $order, string $description): string { $replacement_tags = [ diff --git a/src/Payment/Request/Middleware/RequestMiddlewareInterface.php b/src/Payment/Request/Middleware/RequestMiddlewareInterface.php index 51ff8a95..52e595f6 100644 --- a/src/Payment/Request/Middleware/RequestMiddlewareInterface.php +++ b/src/Payment/Request/Middleware/RequestMiddlewareInterface.php @@ -6,5 +6,14 @@ interface RequestMiddlewareInterface { - public function __invoke(array $requestData, WC_Order $order, string $context = null, callable $next): array; + /** + * Invoke the middleware. + * + * @param array $requestData The request data to be modified. + * @param WC_Order $order The WooCommerce order object. + * @param string $context Additional context for the middleware. + * @param callable $next The next middleware to be called. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, string $context, callable $next): array; } diff --git a/src/Payment/Request/Middleware/SelectedIssuerMiddleware.php b/src/Payment/Request/Middleware/SelectedIssuerMiddleware.php index b889d36f..5dc30659 100644 --- a/src/Payment/Request/Middleware/SelectedIssuerMiddleware.php +++ b/src/Payment/Request/Middleware/SelectedIssuerMiddleware.php @@ -6,21 +6,43 @@ use WC_Order; +/** + * Class SelectedIssuerMiddleware + * + * Middleware to handle the selection of the payment issuer. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class SelectedIssuerMiddleware implements RequestMiddlewareInterface { + /** + * @var string The plugin ID. + */ private $pluginId; - public function __construct($pluginId) + /** + * SelectedIssuerMiddleware constructor. + * + * @param string $pluginId The plugin ID. + */ + public function __construct(string $pluginId) { $this->pluginId = $pluginId; } - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data to be modified. + * @param WC_Order $order The WooCommerce order object. + * @param string $context Additional context for the middleware. + * @param callable $next The next middleware to be called. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, string $context, callable $next): array { - $gateway = wc_get_payment_gateway_by_order($order); if (!$gateway) { - return $next($requestData, $order, $context); } @@ -29,6 +51,7 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, $ if (empty($selectedIssuer)) { return $next($requestData, $order, $context); } + if ($context === 'order') { $requestData['payment']['issuer'] = $selectedIssuer; } elseif ($context === 'payment') { @@ -38,10 +61,16 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, $ return $next($requestData, $order, $context); } + /** + * Get the selected issuer. + * + * @param string $gatewayId The payment gateway ID. + * @return string The selected issuer. + */ private function getSelectedIssuer(string $gatewayId): string { $issuer_id = $this->pluginId . '_issuer_' . $gatewayId; - //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $postedIssuer = wc_clean(wp_unslash($_POST[$issuer_id] ?? '')); return !empty($postedIssuer) ? $postedIssuer : ''; } diff --git a/src/Payment/Request/Middleware/StoreCustomerMiddleware.php b/src/Payment/Request/Middleware/StoreCustomerMiddleware.php index 375c6bbf..45e24cef 100644 --- a/src/Payment/Request/Middleware/StoreCustomerMiddleware.php +++ b/src/Payment/Request/Middleware/StoreCustomerMiddleware.php @@ -7,15 +7,40 @@ use Mollie\WooCommerce\Settings\Settings; use WC_Order; +/** + * Class StoreCustomerMiddleware + * + * Middleware to handle the storage of customer information. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class StoreCustomerMiddleware implements RequestMiddlewareInterface { + /** + * @var Settings The settings helper instance. + */ private Settings $settingsHelper; + + /** + * StoreCustomerMiddleware constructor. + * + * @param Settings $settingsHelper The settings helper instance. + */ public function __construct(Settings $settingsHelper) { $this->settingsHelper = $settingsHelper; } - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data to be modified. + * @param WC_Order $order The WooCommerce order object. + * @param string $context Additional context for the middleware. + * @param callable $next The next middleware to be called. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, string $context, callable $next): array { $storeCustomer = $this->settingsHelper->shouldStoreCustomer(); if (!$storeCustomer) { diff --git a/src/Payment/Request/Middleware/UrlMiddleware.php b/src/Payment/Request/Middleware/UrlMiddleware.php index 82d03b1a..464b9285 100644 --- a/src/Payment/Request/Middleware/UrlMiddleware.php +++ b/src/Payment/Request/Middleware/UrlMiddleware.php @@ -6,21 +6,47 @@ use WC_Order; +/** + * Class UrlMiddleware + * + * Middleware to handle URL modifications for payment requests. + * + * @package Mollie\WooCommerce\Payment\Request\Middleware + */ class UrlMiddleware implements RequestMiddlewareInterface { + /** + * @var string The plugin ID. + */ private $pluginId; + /** - * @var mixed + * @var mixed The logger instance. */ private $logger; + /** + * UrlMiddleware constructor. + * + * @param string $pluginId The plugin ID. + * @param mixed $logger The logger instance. + */ public function __construct($pluginId, $logger) { $this->pluginId = $pluginId; $this->logger = $logger; } - public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array + /** + * Invoke the middleware. + * + * @param array $requestData The request data to be modified. + * @param WC_Order $order The WooCommerce order object. + * @param string $context Additional context for the middleware. + * @param callable $next The next middleware to be called. + * @return array The modified request data. + */ + public function __invoke(array $requestData, WC_Order $order, $context, $next): array { $gateway = wc_get_payment_gateway_by_order($order); if ($gateway) { @@ -35,15 +61,15 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, $ } /** - * Get the url to return to on Mollie return - * saves the return redirect and failed redirect, so we save the page language in case there is one set - * For example 'http://mollie-wc.docker.myhost/wc-api/mollie_return/?order_id=89&key=wc_order_eFZyH8jki6fge' + * Get the URL to return to on Mollie return. + * Saves the return redirect and failed redirect, so we save the page language in case there is one set. + * For example 'http://mollie-wc.docker.myhost/wc-api/mollie_return/?order_id=89&key=wc_order_eFZyH8jki6fge'. * - * @param WC_Order $order The order processed - * - * @return string The url with order id and key as params + * @param WC_Order $order The order processed. + * @param string $returnUrl The base return URL. + * @return string The URL with order ID and key as params. */ - private function getReturnUrl($order, $returnUrl) + private function getReturnUrl(WC_Order $order, string $returnUrl): string { $returnUrl = untrailingslashit($returnUrl); $returnUrl = $this->asciiDomainName($returnUrl); @@ -64,14 +90,14 @@ private function getReturnUrl($order, $returnUrl) } /** - * Get the webhook url - * For example 'http://mollie-wc.docker.myhost/wc-api/mollie_return/mollie_wc_gateway_bancontact/?order_id=89&key=wc_order_eFZyH8jki6fge' - * - * @param WC_Order $order The order processed + * Get the webhook URL. + * For example 'http://mollie-wc.docker.myhost/wc-api/mollie_return/mollie_wc_gateway_bancontact/?order_id=89&key=wc_order_eFZyH8jki6fge'. * - * @return string The url with gateway and order id and key as params + * @param WC_Order $order The order processed. + * @param string $gatewayId The payment gateway ID. + * @return string The URL with gateway and order ID and key as params. */ - public function getWebhookUrl($order, $gatewayId) + public function getWebhookUrl(WC_Order $order, string $gatewayId): string { $webhookUrl = WC()->api_request_url($gatewayId); $webhookUrl = untrailingslashit($webhookUrl); @@ -91,11 +117,12 @@ public function getWebhookUrl($order, $gatewayId) } /** - * @param $url + * Convert the domain name in a URL to ASCII. * - * @return string + * @param string $url The URL to convert. + * @return string The URL with the domain name in ASCII. */ - protected function asciiDomainName($url): string + protected function asciiDomainName(string $url): string { $parsed = wp_parse_url($url); $scheme = isset($parsed['scheme']) ? $parsed['scheme'] : ''; @@ -113,15 +140,17 @@ protected function asciiDomainName($url): string return $url; } + /** - * @param $order_id - * @param $order_key - * @param $webhook_url - * @param string $filterFlag + * Append order arguments to a URL. * - * @return string + * @param int $order_id The order ID. + * @param string $order_key The order key. + * @param string $webhook_url The base webhook URL. + * @param string $filterFlag An optional filter flag. + * @return string The URL with appended order arguments. */ - protected function appendOrderArgumentsToUrl($order_id, $order_key, $webhook_url, $filterFlag = '') + protected function appendOrderArgumentsToUrl(int $order_id, string $order_key, string $webhook_url, string $filterFlag = ''): string { $webhook_url = add_query_arg( [ @@ -133,17 +162,18 @@ protected function appendOrderArgumentsToUrl($order_id, $order_key, $webhook_url ); return $webhook_url; } + /** - * @param $domain - * @return false|mixed|string + * Encode a domain name to ASCII. + * + * @param string $domain The domain name to encode. + * @return string The encoded domain name. */ - protected function idnEncodeDomain($domain) + protected function idnEncodeDomain(string $domain): string { if ( defined('IDNA_NONTRANSITIONAL_TO_ASCII') - && defined( - 'INTL_IDNA_VARIANT_UTS46' - ) + && defined('INTL_IDNA_VARIANT_UTS46') ) { $domain = idn_to_ascii( $domain, diff --git a/src/Payment/Request/RequestFactory.php b/src/Payment/Request/RequestFactory.php index a581daff..1f67f4ce 100644 --- a/src/Payment/Request/RequestFactory.php +++ b/src/Payment/Request/RequestFactory.php @@ -6,23 +6,38 @@ use Psr\Container\ContainerInterface; use WC_Order; +/** + * Class RequestFactory + * + * This class is responsible for creating payment requests based on the provided type. + * + * @package Mollie\WooCommerce\Payment\Request + */ class RequestFactory { - private $container; + /** + * @var ContainerInterface The container interface for dependency injection. + */ + private ContainerInterface $container; + /** + * RequestFactory constructor. + * + * @param ContainerInterface $container The container interface for dependency injection. + */ public function __construct(ContainerInterface $container) { - $this->container = $container; } /** * Create a request based on the type. * - * @param string $type 'order' or 'payment'. + * @param string $type The type of request ('order' or 'payment'). * @param WC_Order $order The WooCommerce order object. - * @param string|null $customerId Customer ID for the request. + * @param string|null $customerId The customer ID for the request. * @return array The generated request data. + * @throws \InvalidArgumentException If the strategy for the given type is invalid. */ public function createRequest(string $type, WC_Order $order, $customerId): array { diff --git a/src/Payment/Request/Strategies/OrderRequestStrategy.php b/src/Payment/Request/Strategies/OrderRequestStrategy.php index 6d96719c..390c0fc5 100644 --- a/src/Payment/Request/Strategies/OrderRequestStrategy.php +++ b/src/Payment/Request/Strategies/OrderRequestStrategy.php @@ -2,26 +2,56 @@ 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; +/** + * Class OrderRequestStrategy + * + * This class handles the creation of payment requests for the Mollie orders API. + * + * @package Mollie\WooCommerce\Payment\Request\Strategies + */ class OrderRequestStrategy implements RequestStrategyInterface { + /** + * @var Data + */ private Data $dataHelper; + + /** + * @var Settings + */ private Settings $settingsHelper; + + /** + * @var MiddlewareHandler + */ private MiddlewareHandler $middlewareHandler; + /** + * OrderRequestStrategy constructor. + * + * @param Data $dataHelper + * @param Settings $settingsHelper + * @param MiddlewareHandler $middlewareHandler + */ public function __construct($dataHelper, $settingsHelper, MiddlewareHandler $middlewareHandler) { - $this->dataHelper = $dataHelper; $this->settingsHelper = $settingsHelper; $this->middlewareHandler = $middlewareHandler; } + /** + * Create a payment request for the given order. + * + * @param WC_Order $order The order to create a request for. + * @param string $customerId The customer ID. + * @return array The request data. + */ public function createRequest(WC_Order $order, $customerId): array { $settingsHelper = $this->settingsHelper; @@ -34,7 +64,6 @@ public function createRequest(WC_Order $order, $customerId): array $methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); $paymentLocale = $settingsHelper->getPaymentLocale(); - // Build the Mollie order data $requestData = [ 'amount' => [ 'currency' => $this->dataHelper->getOrderCurrency($order), diff --git a/src/Payment/Request/Strategies/PaymentRequestStrategy.php b/src/Payment/Request/Strategies/PaymentRequestStrategy.php index 0ff4e94d..749923e2 100644 --- a/src/Payment/Request/Strategies/PaymentRequestStrategy.php +++ b/src/Payment/Request/Strategies/PaymentRequestStrategy.php @@ -4,45 +4,74 @@ use Inpsyde\PaymentGateway\PaymentGateway; use Mollie\WooCommerce\Payment\Request\Middleware\MiddlewareHandler; +use Mollie\WooCommerce\Settings\Settings; +use Mollie\WooCommerce\Shared\Data; use WC_Order; +/** + * Class PaymentRequestStrategy + * + * This class handles the creation of payment requests for the Mollie payments API. + * + * @package Mollie\WooCommerce\Payment\Request\Strategies + */ class PaymentRequestStrategy implements RequestStrategyInterface { - private $dataHelper; - private $settingsHelper; + /** + * @var Data Helper for data operations. + */ + private Data $dataHelper; + + /** + * @var Settings Helper for settings operations. + */ + private Settings $settingsHelper; + + /** + * @var MiddlewareHandler Handler for middleware operations. + */ private MiddlewareHandler $middlewareHandler; + /** + * PaymentRequestStrategy constructor. + * + * @param Data $dataHelper Helper for data operations. + * @param Settings $settingsHelper Helper for settings operations. + * @param MiddlewareHandler $middlewareHandler Handler for middleware operations. + */ public function __construct($dataHelper, $settingsHelper, MiddlewareHandler $middlewareHandler) { - $this->dataHelper = $dataHelper; $this->settingsHelper = $settingsHelper; $this->middlewareHandler = $middlewareHandler; } + /** + * Create a payment request for the given order. + * + * @param WC_Order $order The order to create a request for. + * @param string $customerId The customer ID. + * @return array The request data. + */ public function createRequest(WC_Order $order, $customerId): array { - $gateway = wc_get_payment_gateway_by_order($order); if (!$gateway || !(mollieWooCommerceIsMollieGateway($gateway->id))) { return ['result' => 'failure']; } + $settingsHelper = $this->settingsHelper; $methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1); $paymentLocale = $settingsHelper->getPaymentLocale(); $requestData = [ 'amount' => [ - 'currency' => $this->dataHelper - ->getOrderCurrency($order), - 'value' => $this->dataHelper - ->formatCurrencyValue( - $order->get_total(), - $this->dataHelper->getOrderCurrency( - $order - ) - ), + 'currency' => $this->dataHelper->getOrderCurrency($order), + 'value' => $this->dataHelper->formatCurrencyValue( + $order->get_total(), + $this->dataHelper->getOrderCurrency($order) + ), ], 'method' => $methodId, 'locale' => $paymentLocale, diff --git a/src/Payment/Request/Strategies/RequestStrategyInterface.php b/src/Payment/Request/Strategies/RequestStrategyInterface.php index 288af22f..631ecb75 100644 --- a/src/Payment/Request/Strategies/RequestStrategyInterface.php +++ b/src/Payment/Request/Strategies/RequestStrategyInterface.php @@ -4,7 +4,21 @@ use WC_Order; +/** + * Interface RequestStrategyInterface + * + * Defines the contract for creating payment requests. + * + * @package Mollie\WooCommerce\Payment\Request\Strategies + */ interface RequestStrategyInterface { - public function createRequest(WC_Order $order, $customerId): array; + /** + * Creates a payment request. + * + * @param WC_Order $order The WooCommerce order object. + * @param string $customerId The customer ID. + * @return array The payment request data. + */ + public function createRequest(WC_Order $order, string $customerId): array; }