EuVat for Magento Store Developers

Requires EuVat 0.3.4 or higher

EuVat provides the following events that allow to tie into the EuVat classification process:

Customer updates

Events

euvat_update_customer_before
euvat_update_customer_after

Description

Triggered before and after a Customer Model is being updated.

Models passed to Observer

Customer - Customer Model Mage_Customer_Model_Customer
Request - (Only update is related to Form Post Action, otherwise false)


VAT Number check against VIES

Events

euvat_check_taxvat_before
euvat_check_taxvat_after

Description

Triggered before and after the VIES Service is being requested.

Data passed to Obeserver

Result - Data Array representing the verification data returned by the cache or the VIES query (Read only).
Request - Object with the Elements of the EU VIES Request (Modifiable, only for euvat_check_taxvat_before).
StoreId - Store ID of the current store (Read only).

Events

euvat_vies_response_after

Description

Triggered with the response from the SOAP Request to EU VIES.

Data passed to Obeserver

Result - Data Array representing the verification data returned by the VIES query (Read only).


Validating Customers before creation

The following event is introduced when using the option to limit creation of new customer accounts to valid EU Businesses. To use it you need to apply the changes related to that option to use this Event:

Event

customer_validate_before

Description

Triggered before a new Customer Account is being validated. May get used to introduce your own customer validation code.

Data passed to Obeserver

Customer - Customer Model Mage_Customer_Model_Customer extended to Pisc_Euvat_Model_Customer_Customer


Sample code for Event 'customer_validate_before'

/*
 * Customer Validate Event
 * see also "Pisc_Euvat_Model_Observer"
 */
public function eventCustomerValidateBefore($observer)
{
	$config = Mage::getModel('euvat/config');
	$euvat = Mage::app()->getHelper('euvat');
 
	$customer = $observer->getEvent()->getCustomer();
 
	// Prevent registration for non EU-Businesses if configured
	if ($config->isEuvat() && $config->isCustomerCreateAccountEubusinessOnly()) {
	  $euvat->updateCustomer($customer);
	  if (!$euvat->isEuvat_Business($customer)) {
	    $customer->addError(Mage::helper('euvat')->__('Registration is for EU Businesses only. If being a EU Business provide your valid Address and EU VAT Number.'));
	  }
	}
 
	// Prevent registration if Tax/VAT number is checking invalid
	if ($config->isEuvat()) {
	  $check = $euvat->isEuVat_TaxvatValid($customer);
	  if ($check===false) {
	    $customer->addError(Mage::helper('euvat')->__('The EU VAT number provided does proove as invalid. Please correct the entered EU VAT number or ommit it.'));
	  }
	}
 
}



JavaScript Event for Frontend Taxvat Field

The following JavaScript Browser Event is triggered then a VAT ID Validation has received response data from the server side VAT ID validation process:

Event

euvat:validationResponse

Description

Triggered after the AJAX Request for VAT ID Validation has successfully completed.

Data passed to Obeserver

data - Validation result data

data = {
    html: <string>, <!-- The HTML validation response message shown in the Browser -->
    result: { <!-- The VAT validation result data -->
        countryCode: <string>, <!-- VAT ID Country Code -->
        vatNumber: <string>, <!-- VAT ID Number part -->
        requestDate: <string>, <!-- Timestamp of the request -->
        valid: <boolean>, <!-- VAT ID is valid (true) or invalid (false) -->
        name: <string>, <!-- VAT ID Owner Name if returned from EU VIES -->
        address: <string> <!-- VAT ID Owner Address if returned from EU VIES -->
    }
}

Validation Data object passed by Event



Example

Add a observer to the Browser Window to receive the event:

document.observe("euvat:validationResponse", function(event) {
  responseData = event.memo.data;
});

Event Observer using Prototype JS

See also the related PrototypeJS documentation for more on how to use this Event.

Limit creation of new customer accounts to valid EU Businesses

When using EuVat together with MasterPassword skip the following modifications. The required Events are then already introduced by MasterPassword.

This is the required preparation to use the Limit Creation of New Customer Accounts to valid EU Businesses configuration option of EuVAT.

For this function to be operative, EuVat requires a new event to be added to the customer/customer Model. This is performed by overriding the Core Customer Model with the Customer Model provided by EuVat.

Locate the Configuration File of EuVat in app/code/local/Pisc/Euvat/etc/config.xml and uncomment the following part to override the Core customer/customer Model:

app/code/local/Pisc/Euvat/etc/config.xml

<!-- Optional, uncomment this when using "Limit creation of new customer accounts for valid EU Businesses"
<customer>
	<rewrite>
	  <customer>Pisc_Euvat_Model_Customer_Customer</customer>
	</rewrite>
</customer>
-->
These changes will get overwritten when performing a EuVat Upgrade, and need to be reestablished after a future upgrade of EuVat.


If the Customer Model is already being overridden by another Extension then the following function (or its content of), taken from app/code/local/Pisc/Euvat/Model/Customer/Customer.php is required to be added to the Customer Model being in use:

app/code/local/Pisc/Euvat/Model/Customer/Customer.php

    /*
     * Customer Validation with added event
     */
    public function validate()
    {
        Mage::dispatchEvent('customer_validate_before', array('customer'=>$this));
 
        $errors = $this->getErrors();
        $validation = parent::validate();
        if (is_array($validation)) {
        	$errors = array_merge($errors, $validation);
        }
        if (empty($errors)) {
        	return true;
        }
 
    	return $errors;
    }