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AKD,SAAS4E,sBACpB,IAAK,MAEJ,MACD,IAAK,gBACJF,GAAgBzE,KAAK4E,+BAMxB,IAAIC,EAAkB7E,KAAKD,SAAS+E,eAA+C,+BACnFtE,OAAOP,EAASwD,0BACdlB,KAAK,6BACLwC,KAAKF,EAAgBG,QAAQ,oBAAqBxE,OAAO,oCAAoCyE,SAE5FR,EACFjE,OAAOP,EAASwD,0BAA0BvB,SAG1C1B,OAAOP,EAASwD,0BAA0BtB,SA/B3C,CAiCD,EAQQ,YAAAyC,6BAAR,W,UACKM,EAAqC,CAAC,EAEpCC,EAAkB3E,OAAO,oBAAoBE,MAInD,GAHAwE,EAAcC,IAAkD,QAA9B,EAAAD,EAAcC,UAAgB,QAAI,GAAK,EAGtEnF,KAAKD,SAASqF,0BACb5E,OAAO,uCAAuC8B,GAAG,aAAgB9B,OAAO,qBAAqBmB,OAAS,EAAI,CAC5G,IAAM0D,EAAmB7E,OAAO,qBAAqBE,MACrDwE,EAAcG,IAAoD,QAA/B,EAAAH,EAAcG,UAAiB,QAAI,GAAK,CAC5E,CAKD,IAAI,IAAIC,KAFRJ,EAAclF,KAAKD,SAASwF,qBAAsE,QAA/C,EAAAL,EAAclF,KAAKD,SAASwF,2BAAmB,QAAI,GAAK,EAErFL,EAGrB,GAAkB,IAAdI,GAAsBJ,EAAcI,IAAe,EACtD,OAAO,EAGT,OAAO,CACR,EACD,EAreA,GCoBgBzF,EAWbI,EAASoD,KAXcvD,EAWR0F,8BAVU,WAAvBjE,SAASkE,WAGZ5F,EAASC,GAKTyB,SAASmE,iBAAiB,oBAAoB,WAAM,OAAA7F,EAASC,EAAT,G","sources":["webpack://aelia_eu_vat_assistant_checkout_bundle/./src/classes/class.checkout.ts","webpack://aelia_eu_vat_assistant_checkout_bundle/./src/index.ts"],"sourcesContent":["import { IEUVA_Checkout_Params } from '../interfaces/ieuva_checkout_params';\nimport { IDictionary } from '../interfaces/idictionary';\nexport class Checkout {\n\t/**\n\t * The singleton instance of this class.\n\t *\n\t * @var Checkout\n\t */\n\tprotected static _instance;\n\n\t/**\n\t * The settings passed to the class.\n\t *\n\t * @var IEUVA_Checkout_Params\n\t */\n\tprotected settings: IEUVA_Checkout_Params;\n\n\t/**\n\t * The \"VAT number field\" HTML element displayed on the checkout page.\n\t *\n\t * Note\n\t * Despite WooCommerce calling it \"field\", this is the wrapper element\n\t * around the actual field.\n\t *\n\t * @var HTMLElement\n\t */\n\tprotected static vat_number_field: HTMLElement;\n\n\t/**\n\t * The \"VAT number\" HTML element displayed on the checkout page,\n\t * containing the actual VAT number. This element is wrapped within the\n\t * \"vat_number_field\" element.\n\t *\n\t * @var HTMLInputElement\n\t * @see Checkout.vat_number_field\n\t */\n\tprotected static vat_number_input: HTMLInputElement;\n\n\t/**\n\t * The \"self certificatin field\" HTML element displayed on the checkout page.\n\t *\n\t * Note\n\t * Despite WooCommerce calling it \"field\", this is the wrapper element\n\t * around the actual field.\n\t *\n\t * @var HTMLElement\n\t */\n\tprotected static self_certification_field: HTMLElement;\n\n\n\t/**\n\t * Stores the last VAT number processed. This is used to prevent\n\t * validating the same VAT number twice.\n\t *\n\t * @var string\n\t */\n\tprivate last_vat_number: string;\n\n\t/**\n\t * Returns the selector to be used to intercept the \"change\" event\n\t * on the checkout form.\n\t *\n\t * @returns string\n\t */\n\tprotected get_onchange_event_selector(): string {\n\t\tlet event_selector = '';\n\t\tswitch(this.settings.tax_based_on) {\n\t\t\tcase 'billing':\n\t\t\t\tevent_selector = 'select#billing_country';\n\t\t\t\tbreak;\n\t\t\tcase 'shipping':\n\t\t\t\tevent_selector = 'select#billing_country, select#shipping_country, input#ship-to-different-address-checkbox';\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tevent_selector = 'select#billing_country';\n\t\t}\n\t\treturn event_selector + ', #billing_company, #vat_number';\n\t}\n\n\t/**\n\t * Returns the country that will be used for tax calculation.\n\t *\n\t * @returns string\n\t */\n\tprotected get_vat_country(): string {\n\t\tlet country_field_selector = '#billing_country';\n\t\tif((this.settings.tax_based_on === 'shipping') && jQuery('#ship-to-different-address input[type=\"checkbox\"]').prop('checked')) {\n\t\t\tcountry_field_selector = '#shipping_country';\n\t\t}\n\n\t\treturn jQuery(country_field_selector).val();\n\t}\n\n\t/**\n\t * Indicates if the VAT number field must be hidden (the field could have to\n\t * be hidden because the customer in in shop's base country, and the admin\n\t * configured the system to hide the VAT field in such case.\n\t *\n\t * @param string country\n\t * @returns boolean\n\t */\n\tprotected must_hide_vat_number_field_for_base_country(country: string): boolean {\n\t\treturn !this.settings.show_eu_vat_number_for_base_country &&\n\t\t\t\t\t\t(country == this.settings.shop_base_country);\n\t}\n\n\t/**\n\t * Indicates there is a VAT number validator available for a country.\n\t *\n\t * @param {string} country\n\t */\n\tprotected country_has_vat_number_validator(country: string): boolean {\n\t\treturn this.settings.vat_number_countries.includes(country);\n\t}\n\n\t/**\n\t * Indicates if the specified country is part of the EU.\n\t *\n\t * @param {string} country\n\t */\n\tprotected is_eu_vat_country(country: string): boolean {\n\t\treturn this.settings.eu_vat_countries.includes(country);\n\t}\n\n\t/**\n\t * Indicates if the VAT number field should be displayed. The field is\n\t * displayed if the following criteria is satisfied:\n\t * - The specified country belongs to the list of countries for which\n\t * VAT numbers can be handled\n\t * AND\n\t * - If the country matches shop's base country, option \"show VAT number\n\t * for base country\" is DISABLED\n\t *\n\t * @param {string} country\n\t * @returns boolean\n\t */\n\tprotected should_show_vat_number_for_country(country: string): boolean {\n\t\t// Show it for all countries for which VAT numbers can be\n\t\t// handled, except base country\n\t\treturn !this.must_hide_vat_number_field_for_base_country(country) &&\n\t\t\t\t\t this.country_has_vat_number_validator(country);\n\t}\n\n\t/**\n\t * Indicates if the EU VAT field is required. The field is normally optional,\n\t * but it can be required in several cases:\n\t * - If it's configured as \"required\".\n\t * - If it's configured as \"required for the EU\" and the billing country is in\n\t * the EU.\n\t * - If it's configured as \"required only if company is filled\" and the company\n\t * field is filled.\n\t *\n\t * @param string country The VAT country selected at checkout.\n\t * @returns boolean\n\t * @since 1.5.6.151230\n\t */\n\tprotected is_vat_number_required(country): boolean {\n\t\t// If the VAT number field must be hidden, then it cannot be required\n\t\tif(this.must_hide_vat_number_field_for_base_country(country)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst company_field = document.getElementById('billing_company');\n\t\tconst company_name = (company_field ? company_field['value'].trim() : '');\n\t\tlet company_field_filled = (company_name.length > 0);\n\t\tlet is_eu_vat_country = this.is_eu_vat_country(country);\n\n\t\tlet result =\n\t\t\t// Required in all cases\n\t\t\t(this.settings.vat_number_field_required === 'required') ||\n\t\t\t// Required for EU only, and country is in the EU\n\t\t\t((this.settings.vat_number_field_required === 'required_for_eu_only') && is_eu_vat_country) ||\n\t\t\t// Required if company is filled, and company is filled\n\t\t\t((this.settings.vat_number_field_required === 'required_if_company_field_filled') && company_field_filled) ||\n\t\t\t// Required if company is filled, and company is filled, but only for EU\n\t\t\t((this.settings.vat_number_field_required === 'required_if_company_field_filled_eu_only') && company_field_filled && is_eu_vat_country);\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Shows or hides the VAT number field, depending on the data entered\n\t * on the checkout page.\n\t *\n\t * @returns void\n\t */\n\tpublic update_vat_number_field_visibility(): void {\n\t\tlet vat_country = this.get_vat_country();\n\t\tlet vat_number_required = this.is_vat_number_required(vat_country);\n\t\tlet $vat_number_field = jQuery(Checkout.vat_number_field);\n\n\t\t// Hide or show the EU VAT element, depending on the country\n\t\t// @see Checkout.should_show_vat_number_for_country()\n\t\tif(vat_country &&\n\t\t\t\tthis.should_show_vat_number_for_country(vat_country) ||\n\t\t\t\t// Show VAT number if it's required\n\t\t\t\tvat_number_required\n\t\t\t) {\n\t\t\t$vat_number_field.fadeIn();\n\t\t}\n\t\telse {\n\t\t\t$vat_number_field.fadeOut();\n\t\t\tCheckout.vat_number_input.value = '';\n\t\t}\n\n\t\t// Flag the VAT number field as \"required\", via CSS, if needed. This will allow\n\t\t// WooCommerce to highlight it with a red border if it's required and\n\t\t// left empty\n\t\t// @since 1.5.6.151230\n\t\tif($vat_number_field.is(':visible') && vat_number_required) {\n\t\t\t$vat_number_field.find('.form-row').addClass('validate-required');\n\t\t}\n\t\telse {\n\t\t\t$vat_number_field.find('.form-row').removeClass('validate-required');\n\t\t}\n\t}\n\n\t/**\n\t * Sets the handlers required for the validation of the VAT number field.\n\t *\n\t * @returns void\n\t */\n\tpublic set_vat_number_field_handlers(): void {\n\t\tvar checkout_handler = this;\n\t\tjQuery(this.get_onchange_event_selector()).on('change', () => {\n\t\t\t// Hide or show the VAT number, depending on the selected country\n\t\t\tcheckout_handler.update_vat_number_field_visibility();\n\t\t\t// Validate the VAT number, if entered. This call also updates the visibility of the self-certification\n\t\t\t// field, if that field is displayed\n\t\t\tcheckout_handler.validate_vat_number(checkout_handler.get_vat_country(), Checkout.vat_number_input.value);\n\t\t});\n\n\t\t// TODO Verify the possibility of validating the VAT number when the \"update_checkout\" event is triggered.\n\t\t// IMPORTANT: check that this handling would not result in infinite loops, or multiple unnecessary checks in a row.\n\t\t// jQuery(document.body).on('update_checkout', () => {\n\t\t// \tcheckout_handler.validate_vat_number(checkout_handler.get_vat_country(), Checkout.vat_number_input.value);\n\t\t// });\n\n\t\t// Validate the VAT number on the fly, as the customer types in\n\t\tjQuery(Checkout.vat_number_input).on('blur', () => {\n\t\t\tcheckout_handler.validate_vat_number(checkout_handler.get_vat_country(), Checkout.vat_number_input.value);\n\t\t});\n\t}\n\n\t/**\n\t * Sets the handlers required to show or hide the self-certification field.\n\t *\n\t * @returns void\n\t * @since 2.0.8.210112\n\t */\n\tpublic set_self_certification_field_handlers(): void {\n\t\tvar checkout_handler = this;\n\n\t\t// Update the display of the self certification element when the checkout form changes\n\t\tjQuery(this.get_onchange_event_selector()).on('change', function() {\n\t\t\tcheckout_handler.update_self_certification_field();\n\t\t});\n\n\t\t// Update the display of the self certification element when the VAT number changes (even if invalid)\n\t\tjQuery(document).on('wc_aelia_euva_eu_vat_number_validation_number_incomplete', function(event, country, vat_number) {\n\t\t\tcheckout_handler.update_self_certification_field();\n\t\t});\n\n\t\t// Update the display of the self certification element when the VAT number has been validate\n\t\tjQuery(document).on('wc_aelia_euva_eu_vat_number_validation_complete', function(event, validation_response) {\n\t\t\tcheckout_handler.update_self_certification_field();\n\t\t});\n\t}\n\n\t/**\n\t * Constructor.\n\t *\n\t * @param settings IEUVA_Checkout_Params\n\t */\n\tpublic constructor(settings: IEUVA_Checkout_Params) {\n\t\tthis.settings = settings;\n\n\t\t// Store the instance of the class, so that it can be fetched\n\t\t// later by calling Checkout.instance()\n\t\t// @since 2.0.0.201201\n\t\tCheckout._instance = this;\n\t}\n\n\t/**\n\t * Returns the instance of this class.\n\t *\n\t * @returns Checkout\n\t */\n\tpublic static instance(): Checkout {\n\t\t// If the class has not been instantiated, throw an exception. The handler must be\n\t\t// initialised first, as it needs a couple of settings to get ready.\n\t\t// @since 2.0.0.201201\n\t\tif(!Checkout._instance) {\n\t\t\tthrow new Error('Checkout handler has not been instantiated. Please call `Checkout.init()` before calling `Checkout.instance()`.');\n\t\t}\n\t\treturn Checkout._instance;\n\t}\n\n\t/**\n\t * Initialises the class and its event handlers.\n\t *\n\t * @param settings IEUVA_Checkout_Params\n\t * @return void\n\t */\n\tpublic static init(settings: IEUVA_Checkout_Params): void {\n\t\tif(document.querySelectorAll('.woocommerce form.checkout').length <= 0) {\n\t\t\tconsole.log('Checkout form not found. EU VAT Assistant scripts not loaded.');\n\t\t\treturn;\n\t\t}\n\n\t\t// Initialise the Checkout handler. We can't call Checkout.instance here,\n\t\t// as the handler must be initialised with the settings first\n\t\tlet checkout_handler = new Checkout(settings);\n\n\t\t// Set the handlers for the VAT number field, if present\n\t\tif((Checkout.vat_number_field = document.getElementById('vat_number_field')) &&\n\t\t\t (Checkout.vat_number_input = (document.getElementById('vat_number') as HTMLInputElement))) {\n\t\t\t\tcheckout_handler.set_vat_number_field_handlers();\n\t\t}\n\n\t\t// Set the handlers for the self-certification field, if present\n\t\t// @since 1.15.0.201125\n\t\tif(Checkout.self_certification_field = document.getElementById('customer_location_self_certified_field')) {\n\t\t\tlet $self_certification_description = jQuery(Checkout.self_certification_field).find('.description');\n\n\t\t\t// Display the description of the self certification field before the checkbox\n\t\t\tjQuery(Checkout.self_certification_field).find('.woocommerce-input-wrapper').before($self_certification_description);\n\t\t\tcheckout_handler.update_self_certification_field();\n\n\t\t\t// @since 2.0.8.210112\n\t\t\tcheckout_handler.set_self_certification_field_handlers();\n\t\t}\n\n\t\t// Trigger an update of the checkout form to display the EU VAT field\n\t\tjQuery('select#billing_country').change();\n\t}\n\n\t/**\n\t * Validates the VAT number entered by the customer and updates the checkout\n\t * page according to the result.\n\t *\n\t * @param {string} country\n\t * @param {string} vat_number\n\t */\n\tprotected validate_vat_number(country: string, vat_number: string): void {\n\t\t// Remove the country prefix, if present\n\t\tif(vat_number.includes(country)) {\n\t\t\tvat_number = vat_number.slice(country.length);\n\t\t}\n\n\t\t// Remove whitespace, to reduce payload and avoid checking the same number twice,\n\t\t// just because it has extra spaces\n\t\tvat_number = vat_number.trim();\n\n\t\t// If the number has not changed since last time, skip the validation\n\t\tif((country + vat_number) === this.last_vat_number) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Reset the \"invalid value\" CSS from the VAT number, before retying the validation\n\t\t// @since 2.0.14.210317\n\t\tjQuery(Checkout.vat_number_field).removeClass('woocommerce-invalid');\n\n\t\t// Don't bother sending an Ajax request when the required values are\n\t\t// empty\n\t\tif((country === '') || (vat_number.trim() === '')) {\n\t\t\t// Set the VAT number as \"invalid\". It can't be valid, if either the\n\t\t\t// country or the number is empty\n\t\t\t// @since 1.9.2.181212\n\t\t\tjQuery(Checkout.vat_number_input).prop('valid', false);\n\n\t\t\t// Only display an empty VAT number as \"invalid\" if it's\n\t\t\t// required\n\t\t\t// @since 1.9.3.181217\n\t\t\tif(this.is_vat_number_required(country)) {\n\t\t\t\tjQuery(Checkout.vat_number_field).addClass('woocommerce-invalid');\n\t\t\t}\n\n\t\t\tjQuery(document).trigger('wc_aelia_euva_eu_vat_number_validation_number_incomplete', country, vat_number);\n\n\t\t\treturn;\n\t\t}\n\n\t\t// Store the last validated number\n\t\tthis.last_vat_number = country + vat_number;\n\n\t\tlet ajax_args = {\n\t\t\t'action': 'validate_vat_number',\n\t\t\t'country': country,\n\t\t\t'vat_number': vat_number,\n\t\t\t'_ajax_nonce': this.settings._ajax_nonce,\n\t\t}\n\n\t\tjQuery.get(this.settings.ajax_url, ajax_args, function(response) {\n\t\t\t// Tag the field to indicate if it's valid or not\n\t\t\tvar vat_number_valid = response ? response.valid : false;\n\t\t\tjQuery(Checkout.vat_number_input).prop('valid', vat_number_valid);\n\n\t\t\t// Highlight the VAT number field when the entered VAT number is not valid\n\t\t\t// @since 1.8.4.181009\n\t\t\tjQuery(Checkout.vat_number_field).toggleClass('woocommerce-invalid', !vat_number_valid);\n\n\t\t\t// Trigger an event upon completion of validation\n\t\t\t// @since 1.7.15.171106\n\t\t\tjQuery(document).trigger('wc_aelia_euva_eu_vat_number_validation_complete', response);\n\t\t})\n\t\t// Track unexpected errors in the VAT validation request\n\t\t// @since 1.9.0.181022\n\t\t.fail(function(response){\n\t\t\tconsole.log(\"VAT validation request failed\", response);\n\t\t});\n\t}\n\n\t/**\n\t * Shows/hides the self-certification field, depending on the data entered on\n\t * checkout page and the plugin settings.\n\t */\n\tpublic update_self_certification_field(): void {\n\t\t// If the self certification field is not present, stop here\n\t\tif(!Checkout.self_certification_field) {\n\t\t\treturn;\n\t\t}\n\n\t\t// By default, assume that we should show the self-certification element\n\t\tlet show_element = true;\n\t\t// If VAT number is valid, and option \"hide self-certification when\n\t\t// VAT number is valid\" is enabled, the self-certification field must\n\t\t// be hidden. It will be ignored at checkout\n\t\tif(jQuery(Checkout.vat_number_input).prop('valid') && (this.settings.hide_self_cert_field_with_valid_vat == 1)) {\n\t\t\tshow_element = false;\n\t\t}\n\t\telse {\n\t\t\tswitch(this.settings.show_self_cert_field) {\n\t\t\t\tcase 'yes':\n\t\t\t\t\t// No action needed, take the initial value\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'conflict-only':\n\t\t\t\t\tshow_element = !this.sufficient_location_evidence();\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace tokens in the self certification box, if any\n\t\tlet self_cert_label = this.settings.user_interface['self_certification_field_title'];\n\t\tjQuery(Checkout.self_certification_field)\n\t\t\t.find('.self_certification_label')\n\t\t\t.html(self_cert_label.replace('{billing_country}', jQuery('#billing_country option:selected').text()));\n\n\t\tif(show_element) {\n\t\t\tjQuery(Checkout.self_certification_field).fadeIn();\n\t\t}\n\t\telse {\n\t\t\tjQuery(Checkout.self_certification_field).fadeOut();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if there is enough evidence about customer's location to satisfy\n\t * EU requirements.\n\t *\n\t * @returns boolean\n\t */\n\tprivate sufficient_location_evidence(): boolean {\n\t\tlet country_count: IDictionary = {};\n\n\t\tconst billing_country = jQuery('#billing_country').val();\n\t\tcountry_count[billing_country] = (country_count[billing_country] ?? 0) + 1;\n\n\t\t// Take shipping country as evidence only if explicitly told so\n\t\tif(this.settings.use_shipping_as_evidence) {\n\t\t\tif(jQuery('#ship-to-different-address-checkbox').is(':checked') && (jQuery('#shipping_country').length > 0)) {\n\t\t\t\tconst shipping_country = jQuery('#shipping_country').val();\n\t\t\t\tcountry_count[shipping_country] = (country_count[shipping_country] ?? 0) + 1;\n\t\t\t}\n\t\t}\n\t\t// Increase the count for the country matching customer's IP address\n\t\tcountry_count[this.settings.ip_address_country] = (country_count[this.settings.ip_address_country] ?? 0) + 1;\n\n\t\tfor(let country_id in country_count) {\n\t\t\t// We have sufficient evidence as long as we have at least a count of 2\n\t\t\t// for any given, non empty country\n\t\t\tif((country_id != '') && (country_count[country_id] >= 2)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}","import apiFetch from '@wordpress/api-fetch';\nimport axios from 'axios';\n\nimport { IEUVA_Checkout_Params } from './interfaces/ieuva_checkout_params';\nimport { Checkout } from './classes/class.checkout';\n\n// apiFetch.setFetchHandler( ( options ) => {\n// const { url, path, data, method } = options;\n\n// return axios( {\n// url: url || path,\n// method,\n// data,\n// } );\n// } );\n\n// Add a reference to the global parameters\ndeclare global {\n\tvar aelia_eu_vat_assistant_params: IEUVA_Checkout_Params;\n\tvar jQuery: any;\n}\n\n(function ready(callback, args) {\n if(document.readyState != 'loading'){\n\t\t// If this function is called after the document has finished loading,\n\t\t// invoke the callback immediately\n\t\tcallback(args);\n\t}\n\telse {\n\t\t// If the document is still loading, trigger the callback on the\n\t\t// \"DOM loaded\" event\n\t\tdocument.addEventListener('DOMContentLoaded', () => callback(args));\n }\n})(Checkout.init, aelia_eu_vat_assistant_params);\n"],"names":["callback","args","settings","this","Checkout","_instance","get_onchange_event_selector","event_selector","tax_based_on","get_vat_country","country_field_selector","jQuery","prop","val","must_hide_vat_number_field_for_base_country","country","show_eu_vat_number_for_base_country","shop_base_country","country_has_vat_number_validator","vat_number_countries","includes","is_eu_vat_country","eu_vat_countries","should_show_vat_number_for_country","is_vat_number_required","company_field","document","getElementById","company_field_filled","trim","length","vat_number_field_required","update_vat_number_field_visibility","vat_country","vat_number_required","$vat_number_field","vat_number_field","fadeIn","fadeOut","vat_number_input","value","is","find","addClass","removeClass","set_vat_number_field_handlers","checkout_handler","on","validate_vat_number","set_self_certification_field_handlers","update_self_certification_field","event","vat_number","validation_response","instance","Error","init","querySelectorAll","console","log","self_certification_field","$self_certification_description","before","change","slice","last_vat_number","trigger","ajax_args","_ajax_nonce","get","ajax_url","response","vat_number_valid","valid","toggleClass","fail","show_element","hide_self_cert_field_with_valid_vat","show_self_cert_field","sufficient_location_evidence","self_cert_label","user_interface","html","replace","text","country_count","billing_country","use_shipping_as_evidence","shipping_country","country_id","ip_address_country","aelia_eu_vat_assistant_params","readyState","addEventListener"],"sourceRoot":""}