Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/piwoo 469 phone and birth not required #909

Merged
merged 12 commits into from
May 22, 2024
5 changes: 0 additions & 5 deletions .idea/WooCommerce.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions inc/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,17 @@ function mollieDeleteWPTranslationFiles()
}
}
}

function transformPhoneToNLFormat($phone)
{
$startsWith06 = preg_match('/^06/', $phone);
if ($startsWith06) {
$prefix = '+316';
$phone = substr($phone, 2);
if (!$phone) {
return null;
}
$phone = $prefix . $phone;
}
return $phone;
}
42 changes: 7 additions & 35 deletions resources/js/blocks/molliePaymentMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,6 @@ const MollieComponent = (props) => {

}, [activePaymentMethod, onCheckoutValidation, billing.billingData, item, companyNameString, inputCompany]);

useEffect(() => {
let phoneLabel = getPhoneField()?.labels?.[0] ?? null;
if (!phoneLabel || phoneLabel.length === 0) {
return
}
if (activePaymentMethod === 'mollie_wc_gateway_in3') {
phoneLabel.innerText = item.phonePlaceholder
} else {
if (phoneString !== false) {
phoneLabel.innerText = phoneString
}
}
let isPhoneEmpty = (billing.billingData.phone === '' && shippingData.shippingAddress.phone === '') && inputPhone === '';
let isBirthdateEmpty = inputBirthdate === ''
const unsubscribeProcessing = onCheckoutValidation(

() => {
if (activePaymentMethod === 'mollie_wc_gateway_in3' && (isPhoneEmpty || isBirthdateEmpty)) {
return {
errorMessage: item.errorMessage,
};
}
}
);
return () => {
unsubscribeProcessing()
};

}, [activePaymentMethod, onCheckoutValidation, billing.billingData, shippingData.shippingAddress, item, phoneString, inputBirthdate, inputPhone]);

onSubmitLocal = onSubmit
const updateIssuer = ( changeEvent ) => {
selectIssuer( changeEvent.target.value )
Expand All @@ -215,12 +185,14 @@ const MollieComponent = (props) => {

function fieldMarkup(id, fieldType, label, action, value) {
const className = "wc-block-components-text-input wc-block-components-address-form__" + id;
return <div>
<input type={fieldType} name={id} id={id} value={value} onChange={action}></input><label htmlFor={id} dangerouslySetInnerHTML={{ __html: label }}></label></div>
return <div class="custom-input">
<label htmlFor={id} dangerouslySetInnerHTML={{__html: label}}></label>
<input type={fieldType} name={id} id={id} value={value} onChange={action}></input>
</div>
}

if (item.name === "mollie_wc_gateway_billie"){
if(isCompanyFieldVisible) {
if (item.name === "mollie_wc_gateway_billie") {
if (isCompanyFieldVisible) {
return;
}
const companyField = item.companyPlaceholder ? item.companyPlaceholder : "Company name";
Expand All @@ -231,7 +203,7 @@ const MollieComponent = (props) => {
let fields = [];
const birthdateField = item.birthdatePlaceholder ? item.birthdatePlaceholder : "Birthdate";
fields.push(fieldMarkup("billing-birthdate", "date", birthdateField, updateBirthdate, inputBirthdate));
if (!isPhoneFieldVisible) {
if (isPhoneFieldVisible === false) {
const phoneField = item.phonePlaceholder ? item.phonePlaceholder : "Phone";
fields.push(fieldMarkup("billing-phone-in3", "tel", phoneField, updatePhone, inputPhone));
}
Expand Down
8 changes: 5 additions & 3 deletions resources/js/mollieBlockIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import molliePaymentMethod from './blocks/molliePaymentMethod'

function getPhoneField()
{
const shippingPhone = shipping_address.phone ?? false;
const billingPhone = billing_address.phone ?? false
return billingPhone || shippingPhone;
const phoneFieldDataset = document.querySelector('[data-show-phone-field]');
if (!phoneFieldDataset) {
return true;
}
return phoneFieldDataset.dataset.showPhoneField !== "false"
}

const isCompanyFieldVisible = getCompanyField();
Expand Down
28 changes: 28 additions & 0 deletions resources/scss/mollie-block-custom-field.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Style the container to match WooCommerce styling */
.custom-input {
margin-bottom: 1.5em;
display: flex;
flex-direction: column;
}

.custom-input label {
margin-bottom: 0.5em;
font-weight: bold;
}

#billing-birthdate,
#billing-phone-in3 {
width: 100%;
padding: 0.75em;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 1em;
margin-bottom: 1.25em;
}

#billing-birthdate {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
8 changes: 8 additions & 0 deletions src/Assets/AssetsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function enqueueBlockCheckoutScripts(Data $dataService, array $gatewayIns
}
wp_enqueue_script(MollieCheckoutBlocksSupport::getScriptHandle());
wp_enqueue_style('mollie-gateway-icons');
wp_enqueue_style('mollie-block-custom-field');

MollieCheckoutBlocksSupport::localizeWCBlocksData($dataService, $gatewayInstances);
}
Expand Down Expand Up @@ -288,6 +289,13 @@ public function registerBlockScripts(string $pluginUrl, string $pluginPath): voi
(string) filemtime($this->getPluginPath($pluginPath, '/public/js/mollieBlockIndex.min.js')),
true
);
wp_register_style(
'mollie-block-custom-field',
$this->getPluginUrl($pluginUrl, '/public/css/mollie-block-custom-field.min.css'),
[],
(string) filemtime($this->getPluginPath($pluginPath, '/public/css/mollie-block-custom-field.min.css')),
'screen'
);
}

/**
Expand Down
87 changes: 23 additions & 64 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,13 @@ static function ($paymentContext) {
}
);
add_action('add_meta_boxes_woocommerce_page_wc-orders', [$this, 'addShopOrderMetabox'], 10);
add_filter('woocommerce_form_field_args', static function ($args, $key, $value) use ($container) {
if ($key !== 'billing_phone') {
return $args;
}
if ($args['required'] === true) {
update_option('mollie_wc_is_phone_required_flag', true);
} else {
add_filter('woocommerce_checkout_fields', static function ($fields) use ($container) {
if (!isset($fields['billing']['billing_phone'])) {
update_option('mollie_wc_is_phone_required_flag', false);
} else {
update_option('mollie_wc_is_phone_required_flag', true);
}
return $args;
return $fields;
}, 10, 3);
return true;
}
Expand Down Expand Up @@ -655,11 +652,8 @@ public function in3FieldsMandatory($fields, $errors)
{
$gatewayName = "mollie_wc_gateway_in3";
$phoneField = 'billing_phone_in3';
$birthdateField = self::FIELD_IN3_BIRTHDATE;
$phoneLabel = __('Phone', 'mollie-payments-for-woocommerce');
$birthDateLabel = __('Birthdate', 'mollie-payments-for-woocommerce');
$fields = $this->addPaymentMethodMandatoryFieldsPhoneVerification($fields, $gatewayName, $phoneField, $phoneLabel, $errors);
return $this->addPaymentMethodMandatoryFields($fields, $gatewayName, $birthdateField, $birthDateLabel, $errors);
return $this->addPaymentMethodMandatoryFieldsPhoneVerification($fields, $gatewayName, $phoneField, $phoneLabel, $errors);
}

/**
Expand All @@ -673,31 +667,11 @@ public function in3FieldsMandatoryPayForOrder($order)
return;
}

$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(
sprintf(
__('%s is a required field.', 'woocommerce'),
"<strong>$birthDateLabel</strong>"
),
'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');
$phoneValue = transformPhoneToNLFormat($phoneValue);
$phoneValid = $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 ($phoneValid) {
$order->set_billing_phone($phoneValue);
}
}
Expand Down Expand Up @@ -773,33 +747,20 @@ public function addPaymentMethodMandatoryFieldsPhoneVerification(
if ($fields['payment_method'] !== $gatewayName) {
return $fields;
}
if (isset($fields['billing_phone']) && $this->isPhoneValid($fields['billing_phone'])) {
if (!empty($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>"
)
);
if (!empty($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 (!$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 {
if ($fieldPosted && !$this->isPhoneValid($fieldPosted)) {
$fields['billing_phone'] = $fieldPosted;
return $fields;
}
$fields['billing_phone'] = null;
return $fields;
}

Expand All @@ -822,7 +783,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}$|^06\d{9,13}$/', $billing_phone);
}

public function addPhoneWhenRest($arrayContext)
Expand All @@ -835,17 +796,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/Gateway/MolliePaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ public function onOrderReceivedText($text, $order)
public function getSelectedIssuer(): ?string
{
$issuer_id = $this->pluginId . '_issuer_' . $this->id;

$postedIssuer = filter_input(INPUT_POST, $issuer_id, FILTER_SANITIZE_SPECIAL_CHARS);
//phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$postedIssuer = wc_clean(wp_unslash($_POST[$issuer_id] ?? ''));
return !empty($postedIssuer) ? $postedIssuer : null;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Payment/MollieOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,9 +1226,14 @@ protected function getFormatedPhoneNumber(string $phone)
{
//remove whitespaces and all non numerical characters except +
$phone = preg_replace('/[^0-9+]+/', '', $phone);
if (!is_string($phone)) {
return null;
}
//check if phone starts with 06 and replace with +316
$phone = transformPhoneToNLFormat($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 (preg_match('/^\+[1-9]\d{10,13}$|^[1-9]\d{9,13}$/', $phone)) {
return $phone;
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/PaymentMethods/In3.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function getConfig(): array
'confirmationDelayed' => false,
'orderMandatory' => true,
'errorMessage' => __(
'Required field is empty. Phone and birthdate fields are required.',
'Required field is empty or invalid. Phone (+316xxxxxxxx) and birthdate fields are required.',
'mollie-payments-for-woocommerce'
),
'phonePlaceholder' => __('Please enter your phone here. +00..', 'mollie-payments-for-woocommerce'),
'phonePlaceholder' => __('Please enter your phone here. +316xxxxxxxx', 'mollie-payments-for-woocommerce'),
'birthdatePlaceholder' => __('Please enter your birthdate here.', 'mollie-payments-for-woocommerce'),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function phoneNumber($phoneValue)
</label>
<span class="woocommerce-input-wrapper">
<input type="tel" class="input-text " name="<?= esc_attr(self::FIELD_PHONE); ?>" id="<?= esc_attr(self::FIELD_PHONE); ?>"
placeholder="+00000000000"
placeholder="+39xxxxxxxxx"
value="<?= esc_attr($phoneValue); ?>" autocomplete="phone">
</span>
</p>
Expand Down
Loading
Loading