Let the ddlECCountry and ddlResponseLanguage be two drop-down
lists, populated with items to represent respective European Union Member Countries and Languages. Since in the example below we will use the values of the selected list-items directly in
function calls, it is important that they are populated with valid European Union Member Country and Language Codes, which are available in the VAT Registration Number Validator Reference
Manual. Alternatively these drop-lists can also be dynamically populated using:
public static string[] VATValidator.GetMemberStates(); and
public static string[] VATValidator.GetLanguages();
We will also assume that textVATNumber is a text box in which the visitor will enter their VAT number, and that
the checkObtainReference is a check box which is used to indicate whether a Consultation Reference Number is required or not.
We will place the overall validation result in the LabelVATValidationStatus label field, and the obtained registrant data
in two strings AllItemsData and AllKnownItemsData.
Then an eCommerce website using the VAT Registration Number Validator can use similar code to obtain the details of a VAT number.
|
using MBBSoftware;
[some code to ensure that there is a valid selection in ddlECCountry]
[some code to ensure that there is a text (VAT number) entered in the textVATNumber]
[some code to ensure that there is a valid selection in ddlResponseLanguage]
// Dictionary which will be used to contain the VIES data for the queried VAT number.
Dictionary< VATValidator.DataItem, string > dictVATNumber = new Dictionary< VATValidator.DataItem, string >();
// Get the selected state and language in separate variables for example clarity. Note that both
// GetMemberState( param ) and GetLanguage( param ) throw exceptions when invalid arguments are supplied.
VATValidator.MemberState state = VATValidator.GetMemberState( ddlECCountry.SelectedValue );
VATValidator.Language language = VATValidator.GetLanguage( ddlResponseLanguage.SelectedValue );
// Variables for the results and any possible warnings that need to be sent to the webmaster.
string AllItemsData = "";
string AllKnownItemsData = "";
string WebMasterMessage = "";
try
{
// Now create a VAT validator instance which will be used to acquire the data. Use the default
// constructor if you do not require a Consultation reference number. Use the parameters overload
// constructor to supply your own VAT number as parameterized if you require a Consultation reference number.
VATValidator validatorVAT = checkObtainReference.Checked ?
new VATValidator( VATValidator.MemberState.UnitedKingdom, "YourVATNnumber" ) :
new VATValidator();
if( validatorVAT.IsValidVAT( state, textVATNumber.Text, language, ref dictVATNumber, ref WebMasterMessage ) )
{
// Valid VAT Number.
LabelVATValidationStatus.Text = dictVATNumber[VATValidator.DataItem.Status];
// Example: build a html string for all VAT validation data items.
foreach( VATValidator.DataItem dataFileld in VATValidator.GetAllItems() )
{
AllItemsData += string.Format( "{0}: {1}<br />",
VATValidator.GetItemName( dataFileld, VATValidator.Language.English ),
dictVATNumber.ContainsKey( dataFileld ) ?
dictVATNumber[dataFileld] :
"[VAT-Validator: No Available Data]" );
}
// Example: build a html string only for the data items delivered through the request.
foreach( KeyValuePair< VATValidator.DataItem, string > dataFileld in dictVATNumber )
{
// Ignore any empty or containing only "-" data items.
if( !string.IsNullOrEmpty( dataFileld.Value.Trim( '-' ).Trim() ) )
{
AllKnownItemsData += string.Format( "{0}: {1}<br />",
VATValidator.GetItemName( dataFileld.Key, VATValidator.Language.English ),
dataFileld.Value );
}
}
}
else
{
// Invalid/Unconfirmed VAT Number. This message can be either "No, ..." for invalid VAT number or
// ["Explanation what went wrong & why the VAT number could not be confirmed."]; use the coma as an indicator.
LabelVATValidationStatus.Text = -1 != dictVATNumber[VATValidator.DataItem.Status].Substring(0,5).IndexOf(',') ?
dictVATNumber[VATValidator.DataItem.Status] :
("The entered VAT number could not be validated. Response: " +
dictVATNumber[VATValidator.DataItem.Status]);
}
}
catch( Exception ex )
{
LabelVATValidationStatus.Text =
"The following exception occurred while trying to obtain the requested VAT registration data: " + ex.Message;
}
if( !string.IsNullOrEmpty( WebMasterMessage ) )
{
// There is a warning message from the VAT Registration Number Validator. Send an email to the webmaster.
SendWarningNotification( "Warning from the VAT Registration Number Validator - [function name]",
string.Format( "Warning from the VAT-Validation assembly. Message: {0}",
WebMasterMessage ) );
}
|