Skip to content

Commit

Permalink
Remove front validation
Browse files Browse the repository at this point in the history
Pass null when the phone is not right
  • Loading branch information
mmaymo committed May 15, 2024
1 parent 9a923b5 commit c87dbd6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
41 changes: 14 additions & 27 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,9 @@ public function in3FieldsMandatoryPayForOrder($order)
);
}
$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');
$phoneValue = $phoneValue && $this->isPhoneValid($phoneValue) ? $phoneValue : null;

if (!$phoneValue) {
wc_add_notice(
sprintf(
__('%s is a required field. Valid phone format +000000000', 'mollie-payments-for-woocommerce'),
"<strong>$phoneLabel</strong>"
),
'error'
);
} else {
if ($phoneValue) {
$order->set_billing_phone($phoneValue);
}
}
Expand Down Expand Up @@ -797,6 +788,10 @@ public function addPaymentMethodMandatoryFieldsPhoneVerification(
if (isset($fields['billing_phone']) && $this->isPhoneValid($fields['billing_phone'])) {
return $fields;
}
if (isset($fields['billing_phone']) && !$this->isPhoneValid($fields['billing_phone'])) {
$fields['billing_phone'] = null;
return $fields;
}
$fieldPosted = filter_input(INPUT_POST, $field, FILTER_SANITIZE_SPECIAL_CHARS) ?? false;
if (!$fieldPosted) {
$errors->add(
Expand All @@ -810,13 +805,7 @@ public function addPaymentMethodMandatoryFieldsPhoneVerification(
}

if (!$this->isPhoneValid($fieldPosted)) {
$errors->add(
'validation',
sprintf(
__('%s is not a valid phone number. Valid phone format +00000000000', 'woocommerce'),
"<strong>$fieldLabel</strong>"
)
);
$fields['billing_phone'] = null;
return $fields;
} else {
$fields['billing_phone'] = $fieldPosted;
Expand Down Expand Up @@ -883,7 +872,7 @@ public function switchFields($data)

private function isPhoneValid($billing_phone)
{
return preg_match('/^\+[1-9]\d{10,13}$/', $billing_phone);
return preg_match('/^\+[1-9]\d{10,13}$|^[1-9]\d{9,13}$/', $billing_phone);
}

private function isBirthValid($billing_birthdate)
Expand All @@ -906,17 +895,15 @@ public function addPhoneWhenRest($arrayContext)
if (!empty($billingPhone) && $this->isPhoneValid($billingPhone)) {
return;
}
if (!empty($billingPhone) && !$this->isPhoneValid($billingPhone)) {
$context->order->set_billing_phone(null);
$context->order->save();
return;
}
$billingPhone = $context->payment_data['billing_phone'];
if ($billingPhone) {
if ($billingPhone && $this->isPhoneValid($billingPhone)) {
$context->order->set_billing_phone($billingPhone);
$context->order->save();
} else {
$message = __('Please introduce a valid phone number. +00000000000', 'mollie-payments-for-woocommerce');
throw new RouteException(
'woocommerce_rest_checkout_process_payment_error',
$message,
402
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Payment/MollieObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,8 @@ protected function getFormatedPhoneNumber(string $phone)
//remove whitespaces and all non numerical characters except +
$phone = preg_replace('/[^0-9+]+/', '', $phone);

//check that $phone is in E164 format
if ($phone !== null && preg_match('/^\+[1-9]\d{1,14}$/', $phone)) {
//check that $phone is in E164 format or can be changed by api
if ($phone !== null && preg_match('/^\+[1-9]\d{10,13}$|^[1-9]\d{9,13}$/', $phone)) {
return $phone;
}
return null;
Expand Down

0 comments on commit c87dbd6

Please sign in to comment.