Skip to content

Commit

Permalink
PIWOO-414 Check phone is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Mar 11, 2024
1 parent ddd7453 commit cf4a786
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
75 changes: 66 additions & 9 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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);
}


Expand All @@ -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'),
"<strong>$phoneLabel</strong>"
),
'error'
);
} else {
$order->set_billing_phone($phoneValue);
}
}

/**
Expand All @@ -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'),
"<strong>$phoneLabel</strong>"
),
'error'
);
} else {
$order->set_billing_phone($phoneValue);
}
}

Expand Down Expand Up @@ -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'),
"<strong>$fieldLabel</strong>"
)
);
return $fields;
}

if (!$this->isPhoneValid($fieldPosted)) {
$errors->add(
'validation',
sprintf(
__('%s is not a valid phone number. Valid phone format +00000000000', 'woocommerce'),
"<strong>$fieldLabel</strong>"
)
);
return $fields;
} else {
$fields['billing_phone'] = $fieldPosted;
}
return $fields;
}

public function switchFields($data)
{
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Payment/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class BillieFieldsStrategy implements PaymentFieldsStrategyI
{
const FIELD_COMPANY = "billing_company_billie";
const FIELD_COMPANY = "billing_company";

public function execute($gateway, $dataHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit cf4a786

Please sign in to comment.