diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index b8987f10..777e0d84 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -261,15 +261,6 @@ static function () { } } ); - $isBillieEnabled = $container->get('gateway.isBillieEnabled'); - if ($isBillieEnabled) { - add_filter( - 'woocommerce_after_checkout_validation', - [$this, 'BillieFieldsMandatory'], - 11, - 2 - ); - } $isIn3Enabled = mollieWooCommerceIsGatewayEnabled('mollie_wc_gateway_in3_settings', 'enabled'); if ($isIn3Enabled) { add_filter( @@ -654,14 +645,6 @@ protected function instantiatePaymentMethods($container): array return $paymentMethods; } - public function BillieFieldsMandatory($fields, $errors) - { - $gatewayName = "mollie_wc_gateway_billie"; - $field = 'billing_company'; - $companyLabel = __('Company', 'mollie-payments-for-woocommerce'); - return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $field, $companyLabel, $errors); - } - public function in3FieldsMandatory($fields, $errors) { $gatewayName = "mollie_wc_gateway_in3"; @@ -720,39 +703,6 @@ public function buildPaymentMethod( return $paymentMethod; } - - /** - * Some payment methods require mandatory fields, this function will add them to the checkout fields array - * @param $fields - * @param string $gatewayName - * @param string $field - * @param $errors - * @return mixed - */ - public function addPaymentMethodMandatoryFields($fields, string $gatewayName, string $field, string $fieldLabel, $errors) - { - if ($fields['payment_method'] !== $gatewayName) { - return $fields; - } - if (!isset($fields[$field])) { - $fieldPosted = filter_input(INPUT_POST, $field, FILTER_SANITIZE_SPECIAL_CHARS) ?? false; - if ($fieldPosted) { - $fields[$field] = $fieldPosted; - } else { - $errors->add( - 'validation', - sprintf( - /* translators: Placeholder 1: field name. */ - __('%s is a required field.', 'woocommerce'), - "$fieldLabel" - ) - ); - } - } - - return $fields; - } - public function addPaymentMethodMandatoryFieldsPhoneVerification( $fields, string $gatewayName, @@ -789,12 +739,6 @@ public function switchFields($data) $data['billing_phone'] = !empty($fieldPosted) ? $fieldPosted : $data['billing_phone']; } } - if (isset($data['payment_method']) && $data['payment_method'] === 'mollie_wc_gateway_billie') { - $fieldPosted = filter_input(INPUT_POST, 'billing_company_billie', FILTER_SANITIZE_SPECIAL_CHARS) ?? false; - if ($fieldPosted) { - $data['billing_company'] = !empty($fieldPosted) ? $fieldPosted : $data['billing_company']; - } - } return $data; } diff --git a/src/Payment/MollieObject.php b/src/Payment/MollieObject.php index d2f44480..e4ea3529 100644 --- a/src/Payment/MollieObject.php +++ b/src/Payment/MollieObject.php @@ -1109,7 +1109,7 @@ private function checkBillieCompanyField($order) $isBillieMethodId = $gateway->id === 'mollie_wc_gateway_billie'; if ($isBillieMethodId) { //phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - $fieldPosted = wc_clean(wp_unslash($_POST["billing_company"] ?? '')); + $fieldPosted = wc_clean(wp_unslash($_POST["billing_company_billie"] ?? '')); if ($fieldPosted === '' || !is_string($fieldPosted)) { return null; } diff --git a/src/PaymentMethods/Billie.php b/src/PaymentMethods/Billie.php index 0269b9da..c8d8f9d1 100644 --- a/src/PaymentMethods/Billie.php +++ b/src/PaymentMethods/Billie.php @@ -22,7 +22,7 @@ protected function getConfig(): array 'products', 'refunds', ], - 'filtersOnBuild' => false, + 'filtersOnBuild' => true, 'confirmationDelayed' => false, 'SEPA' => false, 'orderMandatory' => true, @@ -35,6 +35,21 @@ protected function getConfig(): array ]; } + public function filtersOnBuild() + { + add_filter( + 'woocommerce_after_checkout_validation', + [$this, 'BillieFieldsMandatory'], + 11, + 2 + ); + add_action( + 'woocommerce_checkout_posted_data', + [$this, 'switchFields'], + 11 + ); + } + public function getFormFields($generalFormFields): array { unset($generalFormFields[1]); @@ -42,4 +57,55 @@ public function getFormFields($generalFormFields): array return $generalFormFields; } + + public function BillieFieldsMandatory($fields, $errors) + { + $gatewayName = "mollie_wc_gateway_billie"; + $field = 'billing_company_billie'; + $companyLabel = __('Company', 'mollie-payments-for-woocommerce'); + return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $field, $companyLabel, $errors); + } + + public function switchFields($data) + { + if (isset($data['payment_method']) && $data['payment_method'] === 'mollie_wc_gateway_billie') { + $fieldPosted = filter_input(INPUT_POST, 'billing_company_billie', FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + if ($fieldPosted) { + $data['billing_company'] = !empty($fieldPosted) ? $fieldPosted : $data['billing_company']; + } + } + return $data; + } + + /** + * Some payment methods require mandatory fields, this function will add them to the checkout fields array + * @param $fields + * @param string $gatewayName + * @param string $field + * @param $errors + * @return mixed + */ + public function addPaymentMethodMandatoryFields($fields, string $gatewayName, string $field, string $fieldLabel, $errors) + { + if ($fields['payment_method'] !== $gatewayName) { + return $fields; + } + if (!isset($fields[$field])) { + $fieldPosted = filter_input(INPUT_POST, $field, FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + if ($fieldPosted) { + $fields[$field] = $fieldPosted; + } else { + $errors->add( + 'validation', + sprintf( + /* translators: Placeholder 1: field name. */ + __('%s is a required field.', 'woocommerce'), + "$fieldLabel" + ) + ); + } + } + + return $fields; + } } diff --git a/src/PaymentMethods/PaymentFieldsStrategies/BillieFieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/BillieFieldsStrategy.php index 3b8ac4bf..06a4e7a9 100644 --- a/src/PaymentMethods/PaymentFieldsStrategies/BillieFieldsStrategy.php +++ b/src/PaymentMethods/PaymentFieldsStrategies/BillieFieldsStrategy.php @@ -6,7 +6,7 @@ class BillieFieldsStrategy implements PaymentFieldsStrategyI { - const FIELD_COMPANY = "billing_company"; + const FIELD_COMPANY = "billing_company_billie"; public function execute($gateway, $dataHelper) {