diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index 83ad0163..8981bee3 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -255,11 +255,6 @@ static function () { 11, 2 ); - add_action( - 'woocommerce_checkout_posted_data', - [$this, 'switchFields'], - 11 - ); } $isIn3Enabled = mollieWooCommerceIsGatewayEnabled('mollie_wc_gateway_in3_settings', 'enabled'); if ($isIn3Enabled) { @@ -656,7 +651,7 @@ protected function instantiatePaymentMethods($container): array public function BillieFieldsMandatory($fields, $errors) { $gatewayName = "mollie_wc_gateway_billie"; - $field = 'billing_company_billie'; + $field = 'billing_company'; $companyLabel = __('Company', 'mollie-payments-for-woocommerce'); return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $field, $companyLabel, $errors); } @@ -668,7 +663,7 @@ public function in3FieldsMandatory($fields, $errors) $birthdateField = self::FIELD_IN3_BIRTHDATE; $phoneLabel = __('Phone', 'mollie-payments-for-woocommerce'); $birthDateLabel = __('Birthdate', 'mollie-payments-for-woocommerce'); - $fields = $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $phoneField, $phoneLabel, $errors); + $fields = $this->addPaymentMethodMandatoryFieldsPhoneVerification($fields, $gatewayName, $phoneField, $phoneLabel, $errors); return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $birthdateField, $birthDateLabel, $errors); } @@ -677,7 +672,7 @@ public function bancomatpayFieldsMandatory($fields, $errors) $gatewayName = "mollie_wc_gateway_bancomatpay"; $phoneField = 'billing_phone_bancomatpay'; $phoneLabel = __('Phone', 'mollie-payments-for-woocommerce'); - return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $phoneField, $phoneLabel, $errors); + return $this->addPaymentMethodMandatoryFieldsPhoneVerification($fields, $gatewayName, $phoneField, $phoneLabel, $errors); } @@ -704,6 +699,21 @@ public function in3FieldsMandatoryPayForOrder($order) 'error' ); } + $phoneValue = filter_input(INPUT_POST, 'billing_phone_in3', FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + $phoneValue = $phoneValue && $this->isPhoneValid($phoneValue) ? $phoneValue : false; + $phoneLabel = __('Phone', 'mollie-payments-for-woocommerce'); + + if (!$phoneValue) { + wc_add_notice( + sprintf( + __('%s is a required field. Valid phone format +000000000', 'mollie-payments-for-woocommerce'), + "$phoneLabel" + ), + 'error' + ); + } else { + $order->set_billing_phone($phoneValue); + } } /** @@ -718,16 +728,19 @@ public function bancomatpayFieldsMandatoryPayForOrder($order) } $phoneValue = filter_input(INPUT_POST, 'billing_phone_bancomatpay', FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + $phoneValue = $phoneValue && $this->isPhoneValid($phoneValue) ? $phoneValue : false; $phoneLabel = __('Phone', 'mollie-payments-for-woocommerce'); if (!$phoneValue) { wc_add_notice( sprintf( - __('%s is a required field.', 'woocommerce'), + __('%s is a required field. Valid phone format +00000000000', 'mollie-payments-for-woocommerce'), "$phoneLabel" ), 'error' ); + } else { + $order->set_billing_phone($phoneValue); } } @@ -792,6 +805,45 @@ public function addPaymentMethodMandatoryFields($fields, string $gatewayName, st return $fields; } + public function addPaymentMethodMandatoryFieldsPhoneVerification( + $fields, + string $gatewayName, + string $field, + string $fieldLabel, + $errors + ) { + if ($fields['payment_method'] !== $gatewayName) { + return $fields; + } + if (isset($fields['billing_phone']) && $this->isPhoneValid($fields['billing_phone'])) { + return $fields; + } + $fieldPosted = filter_input(INPUT_POST, $field, FILTER_SANITIZE_SPECIAL_CHARS) ?? false; + if (!$fieldPosted) { + $errors->add( + 'validation', + sprintf( + __('%s is a required field.', 'woocommerce'), + "$fieldLabel" + ) + ); + return $fields; + } + + if (!$this->isPhoneValid($fieldPosted)) { + $errors->add( + 'validation', + sprintf( + __('%s is not a valid phone number. Valid phone format +00000000000', 'woocommerce'), + "$fieldLabel" + ) + ); + return $fields; + } else { + $fields['billing_phone'] = $fieldPosted; + } + return $fields; + } public function switchFields($data) { @@ -815,4 +867,9 @@ public function switchFields($data) } return $data; } + + private function isPhoneValid($billing_phone) + { + return preg_match('/^\+[1-9]\d{1,14}$/', $billing_phone); + } } diff --git a/src/Payment/PaymentService.php b/src/Payment/PaymentService.php index 50127fdb..f866de5f 100644 --- a/src/Payment/PaymentService.php +++ b/src/Payment/PaymentService.php @@ -491,7 +491,7 @@ protected function processAsMolliePayment( : '', ]; - $this->logger->debug($apiCallLog); + $this->logger->debug(json_encode($apiCallLog, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); // Try as simple payment $paymentObject = $this->apiHelper->getApiClient( diff --git a/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php index a274e451..447ff85d 100644 --- a/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php +++ b/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php @@ -14,7 +14,7 @@ public function execute($gateway, $dataHelper) if (is_checkout_pay_page()) { $order = $this->getOrderIdOnPayForOrderPage(); - $showPhoneField = empty($order->get_billing_phone()); + $showPhoneField = empty($order->get_billing_phone()) || !$this->isPhoneValid($order->get_billing_phone()); } if (is_checkout() && !is_checkout_pay_page()) { @@ -53,4 +53,9 @@ public function getFieldMarkup($gateway, $dataHelper) { return ""; } + + private function isPhoneValid(string $get_billing_phone) + { + return preg_match('/^\+[0-9]{11,13}$/', $get_billing_phone) === 1; + } } diff --git a/src/PaymentMethods/PaymentFieldsStrategies/BillieFieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/BillieFieldsStrategy.php index df2ca0ae..4cab3e16 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_billie"; + const FIELD_COMPANY = "billing_company"; public function execute($gateway, $dataHelper) { diff --git a/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php index 6985d0f7..f12d41a6 100644 --- a/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php +++ b/src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php @@ -16,7 +16,7 @@ public function execute($gateway, $dataHelper) if (is_checkout_pay_page()) { $order = $this->getOrderIdOnPayForOrderPage(); - $showPhoneField = empty($order->get_billing_phone()); + $showPhoneField = empty($order->get_billing_phone()) || !$this->isPhoneValid($order->get_billing_phone()); $showBirthdateField = true; } @@ -76,4 +76,9 @@ public function getFieldMarkup($gateway, $dataHelper) { return ""; } + + private function isPhoneValid(string $get_billing_phone) + { + return preg_match('/^\+[0-9]{11,13}$/', $get_billing_phone) === 1; + } }