diff --git a/lib/WebGUI/Shop/TaxDriver/EU.pm b/lib/WebGUI/Shop/TaxDriver/EU.pm index e734cf1ba..11355983f 100644 --- a/lib/WebGUI/Shop/TaxDriver/EU.pm +++ b/lib/WebGUI/Shop/TaxDriver/EU.pm @@ -176,7 +176,20 @@ sub addVATNumber { 0, ] ); - return $numberIsValid ? undef : $i18n->get('vies unavailable'); + if ( $numberIsValid ) { + return undef; + } + else { + my $workflow = WebGUI::Workflow::Instance->create( $self->session, { + workflowId => 'taxeurecheckworkflow01', + parameters => { + userId => $user->userId, + vatNumber => $number, + }, + } )->start(); + + return $i18n->get('vies unavailable'); + } } #------------------------------------------------------------------- @@ -823,6 +836,45 @@ sub isUsableVATNumber { #------------------------------------------------------------------- +=head2 recheckVATNumber ( vatNumber, user ) + +=cut + +sub recheckVATNumber { + my $self = shift; + my $number = shift; + my $user = shift || $self->session->user; + + my $validator = Business::Tax::VAT::Validation->new; + + my $isValid = $validator->check( $number ); + my $errorCode = $validator->get_last_error_code; + + if ( $isValid ) { + $self->session->db->write( + 'update tax_eu_vatNumbers set viesValidated=?, viesErrorCode=? where vatNumber=? and userId=?', + [ + 1, + undef, + $number, + $user->userId, + ], + ); + + return 'VALID'; + } + elsif ( $errorCode < 17 ) { + $self->deleteVATNumber( $number, $user ); + + return 'INVALID'; + } + else { + return 'UNKNOWN'; + } +} + +#------------------------------------------------------------------- + =head2 skuFormDefinition ( ) Returns a hash ref containing the form definition for the per sku options for this tax driver. diff --git a/lib/WebGUI/Workflow/Activity/RecheckVATNumber.pm b/lib/WebGUI/Workflow/Activity/RecheckVATNumber.pm new file mode 100644 index 000000000..f4a604577 --- /dev/null +++ b/lib/WebGUI/Workflow/Activity/RecheckVATNumber.pm @@ -0,0 +1,94 @@ +package WebGUI::Workflow::Activity::RecheckVATNumber; + + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2009 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; +use WebGUI::Shop::TaxDriver::EU; +use base 'WebGUI::Workflow::Activity'; + +=head1 NAME + +Package WebGUI::Workflow::Activity::RecheckVATNumber + +=head1 DESCRIPTION + +Rechecks VAT number trhough the EU VIES service that could not be checked at the time they were submitted. + +=head1 SYNOPSIS + +See WebGUI::Workflow::Activity for details on how to use any activity. + +=head1 METHODS + +These methods are available from this class: + +=cut + + +#------------------------------------------------------------------- + +=head2 definition ( session, definition ) + +See WebGUI::Workflow::Activity::defintion() for details. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift; + my $i18n = WebGUI::International->new($session, "Activity_RecheckVATNumber"); + + push ( @{ $definition }, { + name =>$i18n->get("topicName"), + } ); + + return $class->SUPER::definition( $session, $definition ); +} + +#------------------------------------------------------------------- + +=head2 execute ( [ object ] ) + +See WebGUI::Workflow::Activity::execute() for details. + +=cut + +sub execute { + my $self = shift; + my $object = shift; + my $instance = shift; + + my $params = $instance->get('parameters'); + my $user = WebGUI::User->new( $self->session, $params->{ userId } ); + my $taxDriver = WebGUI::Shop::TaxDriver::EU->new( $self->session ); + + my $result = $taxDriver->recheckVATNumber( $params->{ vatNumber }, $user ); +$self->session->log->warn( "Checked $params->{ vatNumber } for user $params->{ userId }, result: $result"); + + + # If the validity of the number is known we're finished. + if ( $result eq 'VALID' || $result eq 'INVALID' ) { + return $self->COMPLETE; + } + + # Otherwise, try again in an hour. + return $self->WAITING( 3600 ); +} + +1; + +#vim:ft=perl diff --git a/lib/WebGUI/i18n/English/Activity_RecheckVATNumber.pm b/lib/WebGUI/i18n/English/Activity_RecheckVATNumber.pm new file mode 100644 index 000000000..e2686f390 --- /dev/null +++ b/lib/WebGUI/i18n/English/Activity_RecheckVATNumber.pm @@ -0,0 +1,14 @@ +package WebGUI::i18n::English::Activity_RecheckVATNumber; + +use strict; + +our $I18N = { + 'topicName' => { + message => q|Recheck VAT number|, + lastUpdated => 0, + context => q|Title of the VAT number recheck workflow activity|, + }, +}; + +1; + diff --git a/lib/WebGUI/i18n/English/TaxDriver_EU.pm b/lib/WebGUI/i18n/English/TaxDriver_EU.pm index acccf1369..064075880 100644 --- a/lib/WebGUI/i18n/English/TaxDriver_EU.pm +++ b/lib/WebGUI/i18n/English/TaxDriver_EU.pm @@ -9,7 +9,8 @@ our $I18N = { }, 'vies unavailable' => { - message => q|Number validation currently not available. Check later.|, + message => q|Number validation is currently not available. Your number will be rechecked automatically +after some time.|, lastUpdated => 0, context => q|An error message|, }, diff --git a/t/Shop/TaxDriver/EU.t b/t/Shop/TaxDriver/EU.t index c41029672..d1dcfa1b6 100644 --- a/t/Shop/TaxDriver/EU.t +++ b/t/Shop/TaxDriver/EU.t @@ -18,6 +18,7 @@ use strict; use lib "$FindBin::Bin/../../lib"; use Test::More; use Test::Deep; +use Test::MockObject::Extends; use Exception::Class; use Data::Dumper; @@ -46,7 +47,7 @@ my $cart; #---------------------------------------------------------------------------- # Tests -my $tests = 55; +my $tests = 58; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -114,8 +115,14 @@ SKIP: { $session->user( {userId=>$taxUser->userId} ); + # Mock the Validation module + my $validator = Test::MockObject::Extends->new( Business::Tax::VAT::Validation->new ); + local *Business::Tax::VAT::Validation::new; + $validator->fake_new( 'Business::Tax::VAT::Validation' ); + my $testVAT_NL = 'NL123456789B12'; my $testVAT_BE = 'BE0123456789'; + my $noServiceVAT= 'NotGonnaWork'; my $invalidVAT = 'ByNoMeansAllowed'; my $visitorUser = WebGUI::User->new( $session, 1 ); @@ -134,13 +141,31 @@ SKIP: { isa_ok( $e, 'WebGUI::Error::InvalidParam', 'User may not be visitor' ); is( $e, 'Visitor cannot add VAT numbers', 'addVATNumber returns correct message when user is visitor' ); - my $response = $taxer->addVATNumber( $invalidVAT, $taxUser, 1 ); + #----- invalid vat number + $validator->set_always( 'check', 0 ); + $validator->set_always( 'get_last_error_code', 1 ); + my $response = $taxer->addVATNumber( $invalidVAT, $taxUser ); is( $response, 'The entered VAT number is invalid.', 'Invalid VAT numbers return an error message' ); - my $responseNL = $taxer->addVATNumber( $testVAT_NL, $taxUser, 1 ); - my $responseBE = $taxer->addVATNumber( $testVAT_BE, $taxUser, 1 ); + #----- service unavailable + $validator->set_always( 'check', 0 ); + $validator->set_always( 'get_last_error_code', 17 ); + my $response = $taxer->addVATNumber( $noServiceVAT, $taxUser ); + ok( $response =~ m{^Number validation is currently not available.}, 'When VIES is down a message is returned' ); + + my $workflows = WebGUI::Workflow::Instance->getAllInstances( $session ); + my ($workflow) = grep { $_->get('parameters')->{ vatNumber } eq $noServiceVAT } @{ $workflows }; + ok( defined $workflow , 'A recheck workflow has been fired' ); + + #----- valid number + $validator->set_always( 'check', 1 ); + $validator->set_always( 'get_last_error_code', undef ); + my $response = $taxer->addVATNumber( $testVAT_NL, $taxUser ); + ok( !defined $response , 'Valid VAT numbers return undef.' ); + + # Add another number for later testing purposes. + my $responseBE = $taxer->addVATNumber( $testVAT_BE, $taxUser ); - ok( !defined $responseNL && !defined $responseBE, 'Valid VAT numbers return undef.' ); ####################################################################### # diff --git a/t/Workflow/Activity/RecheckVATNumber.t b/t/Workflow/Activity/RecheckVATNumber.t new file mode 100644 index 000000000..04d2d6010 --- /dev/null +++ b/t/Workflow/Activity/RecheckVATNumber.t @@ -0,0 +1,120 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use Test::More; +use Test::Deep; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; + +use WebGUI::Shop::TaxDriver::EU; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 9; # Increment this number for each test you create + + +#---------------------------------------------------------------------------- +{ + my @args; + my $called = 0; + my $return = ''; + local *WebGUI::Shop::TaxDriver::EU::recheckVATNumber = sub { + my $self = shift; + @args = @_; + $called++; + return $return; + }; + + my $number = 'NL34567890'; + my $user = WebGUI::User->new( $session, 'new' ); + my $userId = $user->userId; + addToCleanup( $user ); + + # --- valid number ---------------- + $return = 'VALID'; + my $instance = createInstance( $session, $number, $userId ); + + my $response = $instance->run; + is( $response, 'complete', 'Activity completes when recheckVATNumber found a valid VAT number' ); + $response = $instance->run; + is( $response, 'done', 'Workflow finishes on a valid number' ); + + cmp_ok( scalar( @args ), '==', 2, 'recheckVATNumber is passed 2 params' ); + cmp_ok( $args[0], 'eq', $number, 'first passed param is VATNumber' ); + cmp_ok( $args[1]->getId, 'eq', $userId, 'second passed param is correct user obect' ); + + cmp_ok( $called, '==', 1, 'recheckVATNumber is only called once per iteration' ); + + # --- Invalid number -------------- + $return = 'INVALID'; + my $instance = createInstance( $session, $number, $userId ); + + my $response = $instance->run; + is( $response, 'complete', 'Activity completes when recheckVATNumber found an invalid VAT number' ); + $response = $instance->run; + is( $response, 'done', 'Workflow finishes on an invalid number' ); + + # --- Connection problem ---------- + $return = 'UNKNOWN'; + my $instance = createInstance( $session, $number, $userId ); + + my $response = $instance->run; + is( $response, 'waiting 3600', 'Activity waits for an hour when VIES is unavailable.' ); +} + + +#---------------------------------------------------------------------------- +sub createInstance { + my $session = shift; + my $number = shift; + my $userId = shift; + + my $workflow = WebGUI::Workflow->create($session, { + enabled => 1, + objectType => 'None', + mode => 'realtime', + } ); + my $activity = $workflow->addActivity( 'WebGUI::Workflow::Activity::RecheckVATNumber' ); + + addToCleanup( $workflow ); + + my $instance = WebGUI::Workflow::Instance->create( $session, { + workflowId => $workflow->getId, + skipSpectreNotification => 1, + parameters => { + vatNumber => $number, + userId => $userId, + } + } ); + + return $instance; +}; + + + +#---------------------------------------------------------------------------- +# Cleanup +END { + +} +#vim:ft=perl