Skip to content

Commit

Permalink
Fix Billie company with dedicated id
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Jan 9, 2025
1 parent ff8b999 commit 78a8d77
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 58 deletions.
22 changes: 14 additions & 8 deletions resources/js/blocks/molliePaymentMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let activePaymentMethodLocal
let cachedAvailableGateways
let creditCardSelected = new Event("mollie_creditcard_component_selected", {bubbles: true});
const MollieComponent = (props) => {
let {onSubmit, activePaymentMethod, billing, item, useEffect, ajaxUrl, jQuery, emitResponse, eventRegistration, requiredFields, shippingData, isCompanyFieldVisible, isPhoneFieldVisible} = props
let {onSubmit, activePaymentMethod, billing, item, useEffect, ajaxUrl, jQuery, emitResponse, eventRegistration, requiredFields, shippingData, isPhoneFieldVisible} = props
const { responseTypes } = emitResponse;
const {onPaymentSetup, onCheckoutValidation} = eventRegistration;
const [ selectedIssuer, selectIssuer ] = wp.element.useState('');
Expand Down Expand Up @@ -114,7 +114,7 @@ const MollieComponent = (props) => {
payment_method_title: item.title,
[issuerKey]: selectedIssuer,
billing_phone: inputPhone,
billing_company: inputCompany,
billing_company_billie: inputCompany,
billing_birthdate: inputBirthdate,
cardToken: tokenVal,
}
Expand All @@ -133,7 +133,7 @@ const MollieComponent = (props) => {

useEffect(() => {
let companyLabel = jQuery('div.wc-block-components-text-input.wc-block-components-address-form__company > label')
if (companyLabel.length === 0) {
if (companyLabel.length === 0 || item.hideCompanyField === true) {
return
}

Expand Down Expand Up @@ -219,14 +219,20 @@ const MollieComponent = (props) => {
}

if (item.name === "mollie_wc_gateway_billie") {
if (isCompanyFieldVisible) {
return;
const billingCompanyField = document.querySelector('#billing-company');
const shippingCompanyField = document.querySelector('#shipping-company');
const isBillingCompanyRequired = billingCompanyField?.hasAttribute('required');
const isShippingCompanyRequired = shippingCompanyField?.hasAttribute('required');

if ((billingCompanyField && isBillingCompanyRequired) || (shippingCompanyField && isShippingCompanyRequired) || item.hideCompanyField === true) {
return;
}

const companyField = item.companyPlaceholder ? item.companyPlaceholder : "Company name";
return (
<>
<div><p>{item.content}</p></div>
{fieldMarkup("billing-company","text", companyField, updateCompany, inputCompany)}
{fieldMarkup("billing_company_billie","text", companyField, updateCompany, inputCompany)}
</>
);
}
Expand Down Expand Up @@ -271,7 +277,7 @@ const MollieComponent = (props) => {
return <div><p>{item.content}</p></div>
}

const molliePaymentMethod = (useEffect, ajaxUrl, filters, gatewayData, availableGateways, item, jQuery, requiredFields, isCompanyFieldVisible, isPhoneFieldVisible) =>{
const molliePaymentMethod = (useEffect, ajaxUrl, filters, gatewayData, availableGateways, item, jQuery, requiredFields, isPhoneFieldVisible) =>{
let billingCountry = filters.billingCountry
let cartTotal = filters.cartTotal
cachedAvailableGateways = availableGateways
Expand All @@ -282,7 +288,7 @@ const molliePaymentMethod = (useEffect, ajaxUrl, filters, gatewayData, available
return {
name: item.name,
label: <div dangerouslySetInnerHTML={{__html: item.label}}/>,
content: <MollieComponent item={item} useEffect={useEffect} ajaxUrl={ajaxUrl} jQuery={jQuery} requiredFields={requiredFields} isCompanyFieldVisible={isCompanyFieldVisible} isPhoneFieldVisible={isPhoneFieldVisible}/>,
content: <MollieComponent item={item} useEffect={useEffect} ajaxUrl={ajaxUrl} jQuery={jQuery} requiredFields={requiredFields} isPhoneFieldVisible={isPhoneFieldVisible}/>,
edit: <div>{item.edit}</div>,
paymentMethodId: item.paymentMethodId,
canMakePayment: ({cartTotals, billingData}) => {
Expand Down
21 changes: 2 additions & 19 deletions resources/js/mollieBlockIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,10 @@ import ApplePayButtonEditorComponent from './blocks/ApplePayButtonEditorComponen

window.onload = (event) => {
const { registerPaymentMethod } = wc.wcBlocksRegistry;
const { checkoutData, defaultFields } = wc.wcSettings.allSettings;
let billing_address, shipping_address;

if (checkoutData) {
({ billing_address, shipping_address } = checkoutData);
} else {
billing_address = {};
shipping_address = {};
}
const { defaultFields } = wc.wcSettings.allSettings;
const { ajaxUrl, filters, gatewayData, availableGateways } = mollieBlockData.gatewayData;
const {useEffect} = wp.element;
const isAppleSession = typeof window.ApplePaySession === "function"
const isBlockEditor = !!wp?.blockEditor;

function getCompanyField() {
let shippingCompany = shipping_address.company ?? false;
let billingCompany = billing_address.company ?? false;
return shippingCompany ? shippingCompany : billingCompany;
}

function getPhoneField()
{
const phoneFieldDataset = document.querySelector('[data-show-phone-field]');
Expand All @@ -39,7 +23,6 @@ import ApplePayButtonEditorComponent from './blocks/ApplePayButtonEditorComponen
return phoneFieldDataset.dataset.showPhoneField !== "false"
}

const isCompanyFieldVisible = getCompanyField();
const companyNameString = defaultFields.company.label
const isPhoneFieldVisible = getPhoneField();
const phoneString = defaultFields.phone.label
Expand All @@ -48,7 +31,7 @@ import ApplePayButtonEditorComponent from './blocks/ApplePayButtonEditorComponen
'phoneString': phoneString,
}
gatewayData.forEach(item => {
let register = () => registerPaymentMethod(molliePaymentMethod(useEffect, ajaxUrl, filters, gatewayData, availableGateways, item, jQuery, requiredFields, isCompanyFieldVisible, isPhoneFieldVisible));
let register = () => registerPaymentMethod(molliePaymentMethod(useEffect, ajaxUrl, filters, gatewayData, availableGateways, item, jQuery, requiredFields, isPhoneFieldVisible));
if (item.name === 'mollie_wc_gateway_applepay') {
const {isExpressEnabled} = item;
if ((isAppleSession && window.ApplePaySession.canMakePayments())) {
Expand Down
2 changes: 2 additions & 0 deletions src/Assets/MollieCheckoutBlocksSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static function gatewayDataForWCBlocks(Data $dataService, array $gatewayI
'AT' => '+43xxxxxxxxx',
];
$country = WC()->customer ? WC()->customer->get_billing_country() : '';
$hideCompanyFieldFilter = apply_filters('mollie_wc_hide_company_field', false);
$phonePlaceholder = in_array($country, array_keys($countryCodes)) ? $countryCodes[$country] : $countryCodes['NL'];
$gatewayData[] = [
'name' => $gatewayKey,
Expand All @@ -165,6 +166,7 @@ public static function gatewayDataForWCBlocks(Data $dataService, array $gatewayI
'phonePlaceholder' => $phonePlaceholder,
'birthdatePlaceholder' => $method->getProperty('birthdatePlaceholder'),
'isExpressEnabled' => $gatewayId === 'applepay' && $method->getProperty('mollie_apple_pay_button_enabled_express_checkout') === 'yes',
'hideCompanyField' => $hideCompanyFieldFilter,
];
}
$dataToScript['gatewayData'] = $gatewayData;
Expand Down
12 changes: 3 additions & 9 deletions src/Payment/MollieObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,15 +1108,9 @@ 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_billie"] ?? ''));
if ($fieldPosted === '' || !is_string($fieldPosted)) {
return null;
}
return $this->maximalFieldLengths(
$fieldPosted,
self::MAXIMAL_LENGTH_ADDRESS
);
$company = wc_clean(wp_unslash($_POST["billing_company_billie"] ?? '')) ?: $order->get_billing_company(
) ?: $order->get_shipping_company();
return $company ? $this->maximalFieldLengths($company, self::MAXIMAL_LENGTH_ADDRESS) : null;
}
return null;
}
Expand Down
81 changes: 60 additions & 21 deletions src/PaymentMethods/Billie.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

namespace Mollie\WooCommerce\PaymentMethods;

/**
* Class Billie
*
* Handles the Billie payment method for WooCommerce.
*/
class Billie extends AbstractPaymentMethod implements PaymentMethodI
{
/**
* Get the configuration for the Billie payment method.
*
* @return array
*/
protected function getConfig(): array
{
return [
Expand Down Expand Up @@ -35,6 +45,10 @@ protected function getConfig(): array
];
}

/**
* Add filters and actions for the Billie payment method.
* This will be added during constructor
*/
public function filtersOnBuild()
{
add_filter(
Expand All @@ -50,60 +64,85 @@ public function filtersOnBuild()
);
}

public function getFormFields($generalFormFields): array
/**
* Modify the general form fields for the Billie payment method.
*
* @param array $generalFormFields
* @return array
*/
public function getFormFields(array $generalFormFields): array
{
unset($generalFormFields[1]);
unset($generalFormFields['allowed_countries']);

return $generalFormFields;
}

public function BillieFieldsMandatory($fields, $errors)
/**
* Validate mandatory fields for the Billie payment method.
*
* @param array $fields
* @param array $errors
*/
public function BillieFieldsMandatory(array $fields, array $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)
/**
* Switch fields for the Billie payment method.
*
* @param array $data
* @return array
*/
public function switchFields(array $data): array
{
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'];
if (!empty($fieldPosted) && is_string($fieldPosted)) {
$data['billing_company'] = $fieldPosted;
}
}
return $data;
}

/**
* Some payment methods require mandatory fields, this function will add them to the checkout fields array
* @param $fields
* @param array $fields
* @param string $gatewayName
* @param string $field
* @param $errors
* @param array $errors
* @return mixed
*/
public function addPaymentMethodMandatoryFields($fields, string $gatewayName, string $field, string $fieldLabel, $errors)
public function addPaymentMethodMandatoryFields(array $fields, string $gatewayName, string $field, string $fieldLabel, array $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>"
)
);
$fieldPosted = filter_input(INPUT_POST, $field, FILTER_SANITIZE_SPECIAL_CHARS) ?? false;

if (!empty($fieldPosted) && is_string($fieldPosted)) {
if (isset($fields['billing_company']) && $fields['billing_company'] === $fieldPosted) {
return $fields;
}
if (isset($fields['shipping_company']) && $fields['shipping_company'] === $fieldPosted) {
return $fields;
}
$fields['billing_company'] = $fieldPosted;
}

if (empty($fields['billing_company']) && empty($fields['shipping_company'])) {
$errors->add(
'validation',
sprintf(
/* translators: Placeholder 1: field name. */
__('%s is a required field.', 'woocommerce'),
"<strong>$fieldLabel</strong>"
)
);
}

return $fields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public function execute($gateway, $dataHelper)
$showCompanyField = empty($order->get_billing_company());
}

if (is_checkout() && !is_checkout_pay_page()) {
$companyFieldIsRequiredByWoo = $this->isCompanyFieldIsRequiredByWoo();
$hideCompanyFieldFilter = apply_filters('mollie_wc_hide_company_field', false);

if (is_checkout() && !is_checkout_pay_page() && !$companyFieldIsRequiredByWoo && !$hideCompanyFieldFilter) {
$showCompanyField = true;
}

Expand Down Expand Up @@ -53,4 +56,16 @@ public function getFieldMarkup($gateway, $dataHelper)
{
return "";
}

/**
*
* @return bool
*/
public function isCompanyFieldIsRequiredByWoo(): bool
{
$checkoutFields = WC()->checkout()->get_checkout_fields();
$billingCompanyFieldIsRequiredByWoo = isset($checkoutFields['billing']['billing_company']['required']) && ($checkoutFields['billing']['billing_company']['required'] === true);
$shippingCompanyFieldIsRequiredByWoo = isset($checkoutFields['shipping']['shipping_company']['required']) && ($checkoutFields['shipping']['shipping_company']['required'] === true);
return $billingCompanyFieldIsRequiredByWoo || $shippingCompanyFieldIsRequiredByWoo;
}
}

0 comments on commit 78a8d77

Please sign in to comment.