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/company billie #971

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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'),
"<strong>$fieldLabel</strong>"
)
);
}
}

return $fields;
}

public function addPaymentMethodMandatoryFieldsPhoneVerification(
$fields,
string $gatewayName,
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Payment/MollieObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
68 changes: 67 additions & 1 deletion src/PaymentMethods/Billie.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function getConfig(): array
'products',
'refunds',
],
'filtersOnBuild' => false,
'filtersOnBuild' => true,
'confirmationDelayed' => false,
'SEPA' => false,
'orderMandatory' => true,
Expand All @@ -35,11 +35,77 @@ 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]);
unset($generalFormFields['allowed_countries']);

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it because of the interface that most of the methods here don't use type hints or return types?

{
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'),
"<strong>$fieldLabel</strong>"
)
);
}
}

return $fields;
}
}
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";
const FIELD_COMPANY = "billing_company_billie";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to use the single quotes for the simple strings, also can add the visibility to constant.

Suggested change
const FIELD_COMPANY = "billing_company_billie";
public const FIELD_COMPANY = 'billing_company_billie';


public function execute($gateway, $dataHelper)
{
Expand Down
Loading