From 0aba342b036eb2de215bcaeedc4ad8a888c01cf7 Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Thu, 31 Aug 2023 14:13:34 +0200 Subject: [PATCH 1/6] Implement In3 fields using PHP --- resources/js/mollieIn3.js | 44 ---------- src/Assets/AssetsModule.php | 12 --- src/Payment/MollieOrder.php | 10 ++- src/PaymentMethods/In3.php | 2 +- .../In3FieldsStrategy.php | 84 +++++++++++++++++++ webpack.config.js | 1 - 6 files changed, 94 insertions(+), 59 deletions(-) delete mode 100644 resources/js/mollieIn3.js create mode 100644 src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php diff --git a/resources/js/mollieIn3.js b/resources/js/mollieIn3.js deleted file mode 100644 index 0905a1a8..00000000 --- a/resources/js/mollieIn3.js +++ /dev/null @@ -1,44 +0,0 @@ -import {maybeRequireField, saveOriginalField} from "./wooCheckoutFieldsUtility"; - -( - function ({jQuery}) { - let positionField = 'li.wc_payment_method.payment_method_mollie_wc_gateway_in3'; - let gateway = 'mollie_wc_gateway_in3'; - let inputPhoneName = 'billing_phone'; - let originalPhone = saveOriginalField(inputPhoneName, {}); - let phoneId = 'billing_phone_field'; - let phoneField = jQuery('form[name="checkout"] p#billing_phone_field'); - let phoneMarkup = '

' - + '' - + '' - + '' - + '' - + '

' - let inputBirthName = 'billing_birthdate'; - let originalBirth = saveOriginalField(inputBirthName, {}); - let birthId = 'billing_birthdate_field'; - let birthField = jQuery('form[name="checkout"] p#billing_birthdate_field'); - let birthMarkup = '

' - + '' - + '' - + '' - + '' - + '

' - jQuery(function () { - jQuery('body') - .on('updated_checkout payment_method_selected', function () { - phoneField = maybeRequireField(phoneField, positionField, phoneMarkup, inputPhoneName, phoneId, originalPhone, gateway); - birthField = maybeRequireField(birthField, positionField, birthMarkup, inputBirthName, birthId, originalBirth, gateway); - }); - }); - } -)( - window -) - - - diff --git a/src/Assets/AssetsModule.php b/src/Assets/AssetsModule.php index 17ed4a53..6595d7bb 100644 --- a/src/Assets/AssetsModule.php +++ b/src/Assets/AssetsModule.php @@ -291,13 +291,6 @@ protected function registerFrontendScripts(string $pluginUrl, string $pluginPath (string) filemtime($this->getPluginPath($pluginPath, '/public/js/mollieBillie.min.js')), true ); - wp_register_script( - 'mollie-in3-classic-handles', - $this->getPluginUrl($pluginUrl, '/public/js/mollieIn3.min.js'), - ['underscore', 'jquery'], - (string) filemtime($this->getPluginPath($pluginPath, '/public/js/mollieIn3.min.js')), - true - ); } public function registerBlockScripts(string $pluginUrl, string $pluginPath): void @@ -330,11 +323,6 @@ public function enqueueFrontendScripts($container) if ($isBillieEnabled && $isBillieEnabledAtMollie) { wp_enqueue_script('mollie-billie-classic-handles'); } - $isIn3Enabled = mollieWooCommerceIsGatewayEnabled('mollie_wc_gateway_in3_settings', 'enabled'); - $isIn3EnabledAtMollie = in_array('in3', $allMethodsEnabledAtMollie, true); - if ($isIn3Enabled && $isIn3EnabledAtMollie) { - wp_enqueue_script('mollie-in3-classic-handles'); - } $applePayGatewayEnabled = mollieWooCommerceIsGatewayEnabled('mollie_wc_gateway_applepay_settings', 'enabled'); $isAppleEnabledAtMollie = in_array('applepay', $allMethodsEnabledAtMollie, true); diff --git a/src/Payment/MollieOrder.php b/src/Payment/MollieOrder.php index 6d01d3ed..e13c6c99 100644 --- a/src/Payment/MollieOrder.php +++ b/src/Payment/MollieOrder.php @@ -944,13 +944,21 @@ protected function createBillingAddress($order) self::MAXIMAL_LENGHT_REGION ); $billingAddress->organizationName = $this->billingCompanyField($order); - $phone = !empty($order->get_billing_phone()) ? $order->get_billing_phone() : $order->get_shipping_phone(); + $phone = $this->getPhoneNumber($order); $billingAddress->phone = (ctype_space($phone)) ? null : $this->getFormatedPhoneNumber($phone); return $billingAddress; } + protected function getPhoneNumber($order){ + $phone = !empty($order->get_billing_phone()) ? $order->get_billing_phone() : $order->get_shipping_phone(); + if(empty($phone)){ + $phone = wc_clean(wp_unslash($_POST["billing_phone"] ?? '')); + } + return $phone; + } + /** * @param $order * @return stdClass diff --git a/src/PaymentMethods/In3.php b/src/PaymentMethods/In3.php index 1253ee9e..be4b7ec3 100644 --- a/src/PaymentMethods/In3.php +++ b/src/PaymentMethods/In3.php @@ -13,7 +13,7 @@ public function getConfig(): array 'defaultTitle' => __('in3', 'mollie-payments-for-woocommerce'), 'settingsDescription' => '', 'defaultDescription' => __('Pay in 3 instalments, 0% interest', 'mollie-payments-for-woocommerce'), - 'paymentFields' => false, + 'paymentFields' => true, 'instructions' => false, 'supports' => [ 'products', diff --git a/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php new file mode 100644 index 00000000..ab366db8 --- /dev/null +++ b/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php @@ -0,0 +1,84 @@ +getOrderIdOnPayForOrderPage(); + $showPhoneField = empty($order->get_billing_phone()); + $showBirthdateField = true; + } + + if (is_checkout() && !is_checkout_pay_page()) { + $checkoutFields = WC()->checkout()->get_checkout_fields(); + + if (!isset($checkoutFields["billing"][self::FIELD_PHONE])) { + $showPhoneField = true; + } + + if (!isset($checkoutFields["billing"][self::FIELD_BIRTHDATE])) { + $showBirthdateField = true; + } + } + + if ($showPhoneField) { + $this->phoneNumber(); + } + + if ($showBirthdateField) { + $this->dateOfBirth(); + } + } + + protected function getOrderIdOnPayForOrderPage() + { + global $wp; + $orderId = absint($wp->query_vars['order-pay']); + return wc_get_order($orderId); + } + + protected function dateOfBirth() + { + ?> +

+ + + +

+ +

+ + + + +

+ Date: Thu, 31 Aug 2023 15:31:09 +0200 Subject: [PATCH 2/6] Esc output. --- src/Payment/MollieOrder.php | 9 ++++++--- .../PaymentFieldsStrategies/In3FieldsStrategy.php | 13 +++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Payment/MollieOrder.php b/src/Payment/MollieOrder.php index e13c6c99..7c505e98 100644 --- a/src/Payment/MollieOrder.php +++ b/src/Payment/MollieOrder.php @@ -951,10 +951,13 @@ protected function createBillingAddress($order) return $billingAddress; } - protected function getPhoneNumber($order){ + protected function getPhoneNumber($order) + { + $phone = !empty($order->get_billing_phone()) ? $order->get_billing_phone() : $order->get_shipping_phone(); - if(empty($phone)){ - $phone = wc_clean(wp_unslash($_POST["billing_phone"] ?? '')); + if (empty($phone)) { + //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + $phone = wc_clean(wp_unslash($_POST['billing_phone'] ?? '')); } return $phone; } diff --git a/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php index ab366db8..122a770c 100644 --- a/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php +++ b/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php @@ -52,15 +52,12 @@ protected function dateOfBirth() { ?>

-

-

Date: Thu, 11 Jan 2024 06:42:00 +0100 Subject: [PATCH 4/6] Add birthdate check on the pay for order page for in3 payment method --- src/Gateway/GatewayModule.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index 9d997e07..f564543e 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -6,6 +6,7 @@ namespace Mollie\WooCommerce\Gateway; +use Automattic\WooCommerce\Admin\Overrides\Order; use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; use Mollie\WooCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule; use Mollie\WooCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait; @@ -58,6 +59,8 @@ class GatewayModule implements ServiceModule, ExecutableModule */ protected $pluginId; + const FIELD_IN3_BIRTHDATE = 'billing_birthdate'; + public function services(): array { return [ @@ -255,6 +258,11 @@ static function () { 11, 2 ); + add_action( + 'woocommerce_before_pay_action', + [$this, 'in3FieldsMandatoryPayForOrder'], + 11 + ); } // Set order to paid and processed when eventually completed without Mollie add_action('woocommerce_payment_complete', [$this, 'setOrderPaidByOtherGateway'], 10, 1); @@ -622,12 +630,32 @@ public function in3FieldsMandatory($fields, $errors) { $gatewayName = "mollie_wc_gateway_in3"; $phoneField = 'billing_phone'; - $birthdateField = 'billing_birthdate'; + $birthdateField = self::FIELD_IN3_BIRTHDATE; $paymentMethodName = 'in3'; $fields = $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $phoneField, $errors, $paymentMethodName); return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $birthdateField, $errors, $paymentMethodName); } + /** + * @param Order $order + */ + public function in3FieldsMandatoryPayForOrder(Order $order) + { + $birthdateValue = filter_input(INPUT_POST, self::FIELD_IN3_BIRTHDATE, FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + if (!$birthdateValue) { + wc_add_notice( + sprintf( + __( + 'Error processing %1$s payment, the birthdate field is required.', + 'mollie-payments-for-woocommerce' + ), + $order->get_payment_method_title() + ), + 'error' + ); + } + } + /** * @param string $id * @param IconFactory $iconFactory From 9ff37e4f98517f937f2e4fdf19d30dc953601ae6 Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Thu, 11 Jan 2024 06:53:25 +0100 Subject: [PATCH 5/6] Prevent birth field check for other payment methods --- src/Gateway/GatewayModule.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index f564543e..feff1e63 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -60,6 +60,7 @@ class GatewayModule implements ServiceModule, ExecutableModule protected $pluginId; const FIELD_IN3_BIRTHDATE = 'billing_birthdate'; + const GATEWAY_NAME_IN3 = "mollie_wc_gateway_in3"; public function services(): array { @@ -641,15 +642,18 @@ public function in3FieldsMandatory($fields, $errors) */ public function in3FieldsMandatoryPayForOrder(Order $order) { + $paymentMethod = filter_input(INPUT_POST, 'payment_method', FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + + if ($paymentMethod !== self::GATEWAY_NAME_IN3) { + return; + } + $birthdateValue = filter_input(INPUT_POST, self::FIELD_IN3_BIRTHDATE, FILTER_SANITIZE_SPECIAL_CHARS) ?? false; if (!$birthdateValue) { wc_add_notice( - sprintf( - __( - 'Error processing %1$s payment, the birthdate field is required.', - 'mollie-payments-for-woocommerce' - ), - $order->get_payment_method_title() + __( + 'Error processing the payment, the birthdate field is required.', + 'mollie-payments-for-woocommerce' ), 'error' ); From 9b74dc758732e75d54e7dc5f7304914457ee64c7 Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Thu, 11 Jan 2024 07:58:25 +0100 Subject: [PATCH 6/6] Unify error messages for required fields --- src/Gateway/GatewayModule.php | 44 ++++++++++++----------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index feff1e63..e0a9bdb3 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -623,8 +623,8 @@ public function BillieFieldsMandatory($fields, $errors) { $gatewayName = "mollie_wc_gateway_billie"; $field = 'billing_company'; - $paymentMethodName = 'Billie'; - return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $field, $errors, $paymentMethodName); + $companyLabel = __('Company', 'mollie-payments-for-woocommerce'); + return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $field, $companyLabel, $errors); } public function in3FieldsMandatory($fields, $errors) @@ -632,9 +632,10 @@ public function in3FieldsMandatory($fields, $errors) $gatewayName = "mollie_wc_gateway_in3"; $phoneField = 'billing_phone'; $birthdateField = self::FIELD_IN3_BIRTHDATE; - $paymentMethodName = 'in3'; - $fields = $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $phoneField, $errors, $paymentMethodName); - return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $birthdateField, $errors, $paymentMethodName); + $phoneLabel = __('Phone', 'mollie-payments-for-woocommerce'); + $birthDateLabel = __('Birthdate', 'mollie-payments-for-woocommerce'); + $fields = $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $phoneField, $phoneLabel, $errors); + return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $birthdateField, $birthDateLabel, $errors); } /** @@ -649,11 +650,13 @@ public function in3FieldsMandatoryPayForOrder(Order $order) } $birthdateValue = filter_input(INPUT_POST, self::FIELD_IN3_BIRTHDATE, FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + $birthDateLabel = __('Birthdate', 'mollie-payments-for-woocommerce'); + if (!$birthdateValue) { wc_add_notice( - __( - 'Error processing the payment, the birthdate field is required.', - 'mollie-payments-for-woocommerce' + sprintf( + __('%s is a required field.', 'woocommerce'), + "$birthDateLabel" ), 'error' ); @@ -696,10 +699,9 @@ public function buildPaymentMethod( * @param string $gatewayName * @param string $field * @param $errors - * @param string $paymentMethodName * @return mixed */ - public function addPaymentMethodMandatoryFields($fields, string $gatewayName, string $field, $errors, string $paymentMethodName) + public function addPaymentMethodMandatoryFields($fields, string $gatewayName, string $field, string $fieldLabel, $errors) { if ($fields['payment_method'] !== $gatewayName) { return $fields; @@ -712,29 +714,13 @@ public function addPaymentMethodMandatoryFields($fields, string $gatewayName, st $errors->add( 'validation', sprintf( - __( - 'Error processing %1$s payment, the %2$s field is required.', - 'mollie-payments-for-woocommerce' - ), - $paymentMethodName, - $field + __('%s is a required field.', 'woocommerce'), + "$fieldLabel" ) ); } } - if ($fields[$field] === '') { - $errors->add( - 'validation', - sprintf( - __( - 'Please enter your %1$s, this is required for %2$s payments', - 'mollie-payments-for-woocommerce' - ), - $field, - $paymentMethodName - ) - ); - } + return $fields; } }