Skip to content

Commit

Permalink
Move fields to payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Dec 24, 2024
1 parent 84dbc00 commit 07f9443
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 62 deletions.
61 changes: 0 additions & 61 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,6 @@ static function () {
}
}
);
$isBillieEnabled = $container->get('gateway.isBillieEnabled');
if ($isBillieEnabled) {
add_filter(
'woocommerce_after_checkout_validation',
[$this, 'BillieFieldsMandatory'],
11,
2
);
add_action(
'woocommerce_checkout_posted_data',
[$this, 'switchFields'],
11
);
}
$isIn3Enabled = mollieWooCommerceIsGatewayEnabled('mollie_wc_gateway_in3_settings', 'enabled');
if ($isIn3Enabled) {
add_filter(
Expand Down Expand Up @@ -659,14 +645,6 @@ protected function instantiatePaymentMethods($container): array
return $paymentMethods;
}

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 in3FieldsMandatory($fields, $errors)
{
$gatewayName = "mollie_wc_gateway_in3";
Expand Down Expand Up @@ -725,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 @@ -794,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
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)
{
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;
}
}

0 comments on commit 07f9443

Please sign in to comment.