Skip to content

Commit

Permalink
v2.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kousikmukherjee committed Mar 26, 2024
1 parent cd6bcb3 commit 01e4589
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 49 deletions.
6 changes: 3 additions & 3 deletions eu-vat-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: EU/UK VAT for WooCommerce
Plugin URI: https://wpfactory.com/item/eu-vat-for-woocommerce/
Description: Manage EU VAT in WooCommerce. Beautifully.
Version: 2.10.0
Version: 2.10.1
Author: WPFactory
Author URI: https://wpfactory.com/
Text Domain: eu-vat-for-woocommerce
Expand All @@ -22,7 +22,7 @@
* Main Alg_WC_EU_VAT Class
*
* @class Alg_WC_EU_VAT
* @version 2.10.0
* @version 2.10.1
* @since 1.0.0
*/
final class Alg_WC_EU_VAT {
Expand All @@ -33,7 +33,7 @@ final class Alg_WC_EU_VAT {
* @var string
* @since 1.0.0
*/
public $version = '2.10.0';
public $version = '2.10.1';
public $core = null;
public $settings = array();

Expand Down
28 changes: 27 additions & 1 deletion includes/class-alg-wc-eu-vat-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ function filter_available_payment_gateways_allowed( $available_gateways ) {
/**
* checkout_validate_vat.
*
* @version 1.7.1
* @version 2.10.1
* @since 1.0.0
* @todo [dev] (important) simplify the code
*/
Expand Down Expand Up @@ -1390,6 +1390,7 @@ function checkout_validate_vat( $_posted ) {
$_posted[ alg_wc_eu_vat_get_field_id() ] != alg_wc_eu_vat_session_get( 'alg_wc_eu_vat_to_check' )
)
) {

$is_valid = false;
if (
'yes' === get_option( 'alg_wc_eu_vat_force_checkout_recheck', 'no' ) &&
Expand All @@ -1400,6 +1401,31 @@ function checkout_validate_vat( $_posted ) {
( isset( $_posted['billing_country'] ) ? $_posted['billing_country'] : '' ),
( isset( $_posted['billing_company'] ) ? $_posted['billing_company'] : '' )
);
} else {

$vat_number = $_posted[ alg_wc_eu_vat_get_field_id() ];
$billing_country = isset( $_posted['billing_country'] ) ? $_posted['billing_country'] : '';
$billing_company = isset( $_posted['billing_company'] ) ? $_posted['billing_company'] : '';
$vat_number = preg_replace('/\s+/', '', $vat_number);
$eu_vat_number = alg_wc_eu_vat_parse_vat( $vat_number, $billing_country );

/* Vat validate manually presaved number */
if( 'yes' === get_option( 'alg_wc_eu_vat_manual_validation_enable', 'no' ) ) {
if( '' != ( $manual_validation_vat_numbers = get_option( 'alg_wc_eu_vat_manual_validation_vat_numbers', '' ) ) ) {
$prevalidated_VAT_numbers = array();
$prevalidated_VAT_numbers = explode( ',', $manual_validation_vat_numbers );
$sanitized_vat_numbers = array_map('trim', $prevalidated_VAT_numbers);

$conjuncted_vat_number = $billing_country . '' . $eu_vat_number['number'];
if( isset($sanitized_vat_numbers[0] ) ){
if ( in_array( $conjuncted_vat_number, $sanitized_vat_numbers ) ) {
alg_wc_eu_vat_maybe_log( $eu_vat_number['country'], $eu_vat_number['number'], $billing_company, '', __( 'Success (checkout): VAT ID valid. Matched with prevalidated VAT numbers.', 'eu-vat-for-woocommerce' ) );
$is_valid = true;

}
}
}
}
}

if ( 'no' != ( $preserve_option_value = get_option( 'alg_wc_eu_vat_preserv_vat_for_different_shipping', 'no' ) ) ) {
Expand Down
92 changes: 88 additions & 4 deletions includes/functions/alg-wc-eu-vat-functions-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function alg_wc_eu_vat_parse_vat( $full_vat_number, $billing_country ) {
/**
* alg_wc_eu_vat_validate_vat_no_soap.
*
* @version 2.9.16
* @version 2.10.1
* @since 1.0.0
* @return mixed: bool on successful checking, null otherwise
*/
Expand Down Expand Up @@ -88,6 +88,10 @@ function alg_wc_eu_vat_validate_vat_no_soap( $country_code, $vat_number, $billin
if ( isset( $decoded_result['isValid'] ) && $decoded_result['isValid'] ) {
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, $method,
__( 'Success: VAT ID is valid', 'eu-vat-for-woocommerce' ) );

// store result to session
alg_wc_eu_vat_store_validation_session( $country_code, $vat_number, true, $billing_company );

return true;
} else {
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, $method,
Expand Down Expand Up @@ -130,6 +134,10 @@ function alg_wc_eu_vat_validate_vat_no_soap( $country_code, $vat_number, $billin
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, $method,
__( 'Success: VAT ID is valid', 'eu-vat-for-woocommerce' ) );
}

// store result to session
alg_wc_eu_vat_store_validation_session( $country_code, $vat_number, $return, $billing_company );

return $return;
}
}
Expand All @@ -138,7 +146,7 @@ function alg_wc_eu_vat_validate_vat_no_soap( $country_code, $vat_number, $billin
/**
* alg_wc_eu_vat_validate_vat_soap.
*
* @version 2.9.15
* @version 2.10.1
* @since 1.0.0
* @return mixed: bool on successful checking, null otherwise
*/
Expand Down Expand Up @@ -198,15 +206,21 @@ function alg_wc_eu_vat_validate_vat_soap( $country_code, $vat_number, $billing_c
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, 'soap',
__( 'Success: VAT ID is valid', 'eu-vat-for-woocommerce' ) );
}

// store result to session
alg_wc_eu_vat_store_validation_session( $country_code, $vat_number, $return, $billing_company );

return $return;
} else {
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, 'soap',
__( 'Error: SoapClient class does not exist', 'eu-vat-for-woocommerce' ) );

return null;
}
} catch( Exception $exception ) {
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, 'soap',
sprintf( __( 'Error: Exception: %s', 'eu-vat-for-woocommerce' ), $exception->getMessage() ) );

return null;
}
}
Expand Down Expand Up @@ -242,7 +256,7 @@ function alg_wc_eu_vat_validate_vat_with_method( $country_code, $vat_number, $bi
/**
* alg_wc_eu_vat_validate_vat.
*
* @version 2.9.18
* @version 2.10.1
* @since 1.0.0
* @return mixed: bool on successful checking, null otherwise
* @todo [dev] (maybe) check for minimal length
Expand Down Expand Up @@ -276,6 +290,15 @@ function alg_wc_eu_vat_validate_vat( $country_code, $vat_number, $billing_compan
}
}
}

/* First validate from session value */
$validate_status = alg_wc_eu_vat_validate_from_session( $country_code, $vat_number, $billing_company );

if( $validate_status ) {
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, 'ValidateFromStoredSession', __( 'Success: VAT ID is valid', 'eu-vat-for-woocommerce' ) );
return true;
}

/* Vat validate manually presaved number end */

$methods = array();
Expand Down Expand Up @@ -305,7 +328,7 @@ function alg_wc_eu_vat_validate_vat( $country_code, $vat_number, $billing_compan
/**
* alg_wc_eu_vat_validate_vat_uk.
*
* @version 2.9.15
* @version 2.10.1
* @since 1.0.0
* @return mixed: bool on successful checking, null otherwise
* @todo [dev] (maybe) check for minimal length
Expand Down Expand Up @@ -397,6 +420,67 @@ function alg_wc_eu_vat_validate_vat_uk( $country_code, $vat_number, $billing_com
alg_wc_eu_vat_maybe_log( $country_code, $vat_number, $billing_company, $method,
__( 'Success: VAT ID is valid', 'eu-vat-for-woocommerce' ) );
}

// store result to session
alg_wc_eu_vat_store_validation_session( $country_code, $vat_number, $return, $billing_company );

return $return;
}
}

if ( ! function_exists( 'alg_wc_eu_vat_store_validation_session' ) ) {
/**
* alg_wc_eu_vat_store_validation_session.
*
* @version 2.10.1
* @since 2.9.22
* @return mixed: array of stored value
* @todo [dev] store vat number validation result to reduce request number to VIES
*/
function alg_wc_eu_vat_store_validation_session( $country_code, $vat_number, $status, $billing_company = '' ) {
$store_array_each = array('country_code' => $country_code, 'vat_number' => $vat_number, 'billing_company' => $billing_company, 'status' => $status );

if( 'yes' !== get_option( 'alg_wc_eu_vat_reduce_concurrent_request_enable', 'no' ) ){
return $store_array_each;
}

$validation_saved = alg_wc_eu_vat_session_get( 'alg_wc_eu_vat_validation_storage', array() );

if( empty( $validation_saved ) ) {
$validation_saved = array();
}

if( is_array( $validation_saved ) && isset( $validation_saved[$vat_number] ) ) {
return $store_array_each;
}

$validation_saved[$vat_number] = $store_array_each;

alg_wc_eu_vat_session_set( 'alg_wc_eu_vat_validation_storage', $validation_saved );
return $store_array;
}
}

if ( ! function_exists( 'alg_wc_eu_vat_validate_from_session' ) ) {
/**
* alg_wc_eu_vat_validate_from_session.
*
* @version 2.10.1
* @since 2.9.22
* @return mixed: bool on successful checking, null otherwise
* @todo [dev] validate vat number from session to reduce request number to VIES
*/
function alg_wc_eu_vat_validate_from_session( $country_code, $vat_number, $billing_company = '' ) {
if( 'yes' !== get_option( 'alg_wc_eu_vat_reduce_concurrent_request_enable', 'no' ) ){
return false;
}

$validation_results = alg_wc_eu_vat_session_get('alg_wc_eu_vat_validation_storage', array());
if( !empty($vat_number) && !empty( $validation_results ) && is_array( $validation_results ) && isset( $validation_results[$vat_number] ) ) {
if( $validation_results[$vat_number]['status'] == '1' ) {
return true;
}
}
return false;
}
}
14 changes: 12 additions & 2 deletions includes/settings/class-alg-wc-eu-vat-settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* EU VAT for WooCommerce - Admin Section Settings
*
* @version 2.9.21
* @version 2.10.1
* @since 1.5.0
* @author WPFactory
*/
Expand Down Expand Up @@ -30,7 +30,7 @@ function __construct() {
/**
* get_settings.
*
* @version 2.9.21
* @version 2.10.1
* @since 1.5.0
* @todo [dev] (important) set "Session type" default value to "WC session (recommended)"
*/
Expand Down Expand Up @@ -149,6 +149,16 @@ function get_settings() {
'default' => 'no',
'type' => 'checkbox',
),

array(
'title' => __( 'Reduce concurrent request to VIES', 'eu-vat-for-woocommerce' ),
'desc' => __( 'Enable', 'eu-vat-for-woocommerce' ),
'desc_tip' => __( 'Enable if validation fails multiple times and you encounter the "MS_MAX_CONCURRENT_REQ" error after debugging.', 'eu-vat-for-woocommerce' ),
'id' => 'alg_wc_eu_vat_reduce_concurrent_request_enable',
'default' => 'no',
'type' => 'checkbox',
),

array(
'type' => 'sectionend',
'id' => 'alg_wc_eu_vat_advanced_options',
Expand Down
8 changes: 4 additions & 4 deletions includes/settings/class-alg-wc-eu-vat-settings-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function get_settings() {
),

array(
'title' => __( 'Exempt VAT for valid numbers', 'eu-vat-for-woocommerce' ),
'title' => __( 'Remove VAT for validated numbers', 'eu-vat-for-woocommerce' ),
'desc_tip' => __( 'Enables/disabled VAT exemption.', 'eu-vat-for-woocommerce' ),
'desc' => __( 'Yes', 'eu-vat-for-woocommerce' ),
'id' => 'alg_wc_eu_vat_disable_for_valid',
Expand All @@ -97,16 +97,16 @@ function get_settings() {
),

array(
'title' => __( 'Preserve VAT if shipping country is different from billing country', 'eu-vat-for-woocommerce' ),
'desc_tip' => __( 'Enables for preserve VAT if shipping country is different from billing country.', 'eu-vat-for-woocommerce' ),
'title' => __( 'Keep VAT if shipping country is different from billing country', 'eu-vat-for-woocommerce' ),
'desc_tip' => __( 'Enables for keep VAT if shipping country is different from billing country.', 'eu-vat-for-woocommerce' ),
'desc' => __( 'Yes', 'eu-vat-for-woocommerce' ),
'id' => 'alg_wc_eu_vat_preserv_vat_for_different_shipping',
'default' => 'no',
'type' => 'checkbox',
),

array(
'title' => __( 'Preserve VAT in selected countries', 'eu-vat-for-woocommerce' ),
'title' => __( 'Keep VAT in selected countries', 'eu-vat-for-woocommerce' ),
'desc_tip' => __( 'This will validate the VAT, but won\'t exempt VAT for selected countries.', 'eu-vat-for-woocommerce' ) . ' ' .
sprintf( __( 'Country for "Base (i.e. store) country" option is set in "%s".', 'eu-vat-for-woocommerce' ),
__( 'WooCommerce > Settings > General > Store Address', 'eu-vat-for-woocommerce' ) ),
Expand Down
Loading

0 comments on commit 01e4589

Please sign in to comment.