| 1 | package WebService::Avalara::AvaTax; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ABSTRACT: Avalara SOAP interface as compiled Perl methods | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | our $VERSION = '0.020'; # VERSION | |||||||||||||
| 9 | 4 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | #pod =head1 SYNOPSIS | ||||||||||||||
| 12 | #pod | ||||||||||||||
| 13 | #pod use WebService::Avalara::AvaTax; | ||||||||||||||
| 14 | #pod my $avatax = WebService::Avalara::AvaTax->new( | ||||||||||||||
| 15 | #pod username => 'avalara@example.com', | ||||||||||||||
| 16 | #pod password => 'sekrit', | ||||||||||||||
| 17 | #pod ); | ||||||||||||||
| 18 | #pod my $answer_ref = $avatax->ping; | ||||||||||||||
| 19 | #pod | ||||||||||||||
| 20 | #pod =head1 DESCRIPTION | ||||||||||||||
| 21 | #pod | ||||||||||||||
| 22 | #pod This class provides a Perl method API for | ||||||||||||||
| 23 | #pod Avalara AvaTax (C<http://developer.avalara.com/api-docs/soap>) | ||||||||||||||
| 24 | #pod web services. The first call to any AvaTax SOAP operation uses | ||||||||||||||
| 25 | #pod L<XML::Compile::WSDL11|XML::Compile::WSDL11> | ||||||||||||||
| 26 | #pod to compile and execute against the specified Avalara AvaTax service; | ||||||||||||||
| 27 | #pod subsequent calls can vary the parameters but will use the same compiled code. | ||||||||||||||
| 28 | #pod | ||||||||||||||
| 29 | #pod =cut | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 4 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 4 | ▶ | use Const::Fast; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | 4 | ▶ | use DateTime; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 4 | ▶ | use DateTime::Format::XSD; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 4 | ▶ | use English '-no_match_vars'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 4 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 4 | ▶ | use Package::Stash; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 4 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | 4 | ▶ | use Sys::Hostname; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 4 | ▶ | use Types::Standard qw(ArrayRef Bool HashRef InstanceOf Str); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 4 | ▶ | use WebService::Avalara::AvaTax::Service::Address; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 4 | ▶ | use WebService::Avalara::AvaTax::Service::Tax; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 4 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | 4 | with 'WebService::Avalara::AvaTax::Role::Connection'; | |||||||||||||
| 45 | |||||||||||||||
| 46 | #pod =method new | ||||||||||||||
| 47 | #pod | ||||||||||||||
| 48 | #pod Builds a new AvaTax web service client. Since this class consumes the | ||||||||||||||
| 49 | #pod L<WebService::Avalara::AvaTax::Role::Connection|WebService::Avalara::AvaTax::Role::Connection> | ||||||||||||||
| 50 | #pod role, please consult that module's documentation for a full list of attributes | ||||||||||||||
| 51 | #pod that can be set at construction. | ||||||||||||||
| 52 | #pod | ||||||||||||||
| 53 | #pod =cut | ||||||||||||||
| 54 | |||||||||||||||
| 55 | #pod =attr services | ||||||||||||||
| 56 | #pod | ||||||||||||||
| 57 | #pod This module is really just a convenience wrapper around instances of | ||||||||||||||
| 58 | #pod L<WebService::Avalara::AvaTax::Service::Address|WebService::Avalara::AvaTax::Service::Address> | ||||||||||||||
| 59 | #pod and | ||||||||||||||
| 60 | #pod L<WebService::Avalara::AvaTax::Service::Tax|WebService::Avalara::AvaTax::Service::Tax> | ||||||||||||||
| 61 | #pod modules. As such this attribute is used to keep an array reference to | ||||||||||||||
| 62 | #pod instances of both classes, with the following attributes from L</new> | ||||||||||||||
| 63 | #pod passed to both: | ||||||||||||||
| 64 | #pod | ||||||||||||||
| 65 | #pod =over | ||||||||||||||
| 66 | #pod | ||||||||||||||
| 67 | #pod =item L<username|WebService::Avalara::AvaTax::Role::Connection/username> | ||||||||||||||
| 68 | #pod | ||||||||||||||
| 69 | #pod =item L<password|WebService::Avalara::AvaTax::Role::Connection/password> | ||||||||||||||
| 70 | #pod | ||||||||||||||
| 71 | #pod =item L<use_wss|WebService::Avalara::AvaTax::Role::Connection/use_wss> | ||||||||||||||
| 72 | #pod | ||||||||||||||
| 73 | #pod =item L<is_production|WebService::Avalara::AvaTax::Role::Connection/is_production> | ||||||||||||||
| 74 | #pod | ||||||||||||||
| 75 | #pod =item L<user_agent|WebService::Avalara::AvaTax::Role::Connection/user_agent> | ||||||||||||||
| 76 | #pod | ||||||||||||||
| 77 | #pod =item L<debug|WebService::Avalara::AvaTax::Role::Connection/debug> | ||||||||||||||
| 78 | #pod | ||||||||||||||
| 79 | #pod =back | ||||||||||||||
| 80 | #pod | ||||||||||||||
| 81 | #pod =cut | ||||||||||||||
| 82 | |||||||||||||||
| 83 | has services => ( | ||||||||||||||
| 84 | is => 'lazy', | ||||||||||||||
| 85 | isa => ArrayRef, | ||||||||||||||
| 86 | init_arg => undef, | ||||||||||||||
| 87 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 88 | 0 | [ map { $_[0]->_new_service($_) } qw(Address Tax) ]; | |||||||||||||
| 89 | }, | ||||||||||||||
| 90 | 4 | ); | |||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | sub _new_service { | ||||||||||||
|
Subroutine
_new_service: not called CC=1 SCAR=6.9
| |||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||
| 94 | 0 | my $class = __PACKAGE__ . '::Service::' . shift; | |||||||||||||
| 95 | return $class->new( | ||||||||||||||
| 96 | 0 | map { ( $_ => $self->$_ ) } | |||||||||||||
| 97 | qw(username password use_wss is_production user_agent debug), | ||||||||||||||
| 98 | ); | ||||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | #pod =head1 METHODS | ||||||||||||||
| 102 | #pod | ||||||||||||||
| 103 | #pod Aside from the L</new> method, L</services> attribute and | ||||||||||||||
| 104 | #pod other attributes and methods consumed from | ||||||||||||||
| 105 | #pod L<WebService::Avalara::AvaTax::Role::Connection|WebService::Avalara::AvaTax::Role::Connection>, | ||||||||||||||
| 106 | #pod available method names are dynamically loaded from each | ||||||||||||||
| 107 | #pod L</services>' C<wsdl> | ||||||||||||||
| 108 | #pod attribute and can be passed either a hash or reference to a hash with the | ||||||||||||||
| 109 | #pod necessary parameters. In scalar context they return a reference to a hash | ||||||||||||||
| 110 | #pod containing the results of the SOAP call; in list context they return the | ||||||||||||||
| 111 | #pod results hashref and an | ||||||||||||||
| 112 | #pod L<XML::Compile::SOAP::Trace|XML::Compile::SOAP::Trace> | ||||||||||||||
| 113 | #pod object suitable for debugging and exception handling. | ||||||||||||||
| 114 | #pod | ||||||||||||||
| 115 | #pod If there is no result then you should check the trace object for why. | ||||||||||||||
| 116 | #pod | ||||||||||||||
| 117 | #pod Please consult the | ||||||||||||||
| 118 | #pod Avalara SOAP API reference (C<http://developer.avalara.com/api-reference>) | ||||||||||||||
| 119 | #pod for semantic details on the methods, parameters and results available for each | ||||||||||||||
| 120 | #pod of the methods listed below. Note that in order to make this interface easier | ||||||||||||||
| 121 | #pod and more Perl-ish, the following changes have been made: | ||||||||||||||
| 122 | #pod | ||||||||||||||
| 123 | #pod =over | ||||||||||||||
| 124 | #pod | ||||||||||||||
| 125 | #pod =item * | ||||||||||||||
| 126 | #pod | ||||||||||||||
| 127 | #pod SOAP operation names have been transformed from C<CamelCase> to | ||||||||||||||
| 128 | #pod C<lowercase_with_underscores>. For example, C<GetTax> is now | ||||||||||||||
| 129 | #pod L</get_tax>. If you do not like this behavior then use | ||||||||||||||
| 130 | #pod L<<< C<< orthodox => 1 >>|/orthodox >>> when calling L</new>. | ||||||||||||||
| 131 | #pod | ||||||||||||||
| 132 | #pod =item * | ||||||||||||||
| 133 | #pod | ||||||||||||||
| 134 | #pod Parameters do not need to be enclosed in C<{parameters}{FooRequest}{ ... }> | ||||||||||||||
| 135 | #pod hashes of hashes. These will be automatically added for you, along with all | ||||||||||||||
| 136 | #pod necessary SOAP headers. The examples below reflect this. | ||||||||||||||
| 137 | #pod | ||||||||||||||
| 138 | #pod =item * | ||||||||||||||
| 139 | #pod | ||||||||||||||
| 140 | #pod Similarly, results are not enclosed in C<{parameters}{FooResult}{ ... }> | ||||||||||||||
| 141 | #pod hashes of hashes. They are, however, returned as a hash reference, not a | ||||||||||||||
| 142 | #pod simple hash. If you want access to other aspects of the SOAP response, make | ||||||||||||||
| 143 | #pod the call in list context and the second value in the list will be an | ||||||||||||||
| 144 | #pod L<XML::Compile::SOAP::Trace|XML::Compile::SOAP::Trace> | ||||||||||||||
| 145 | #pod instance with methods for retrieving the full request and response as | ||||||||||||||
| 146 | #pod L<HTTP::Request|HTTP::Request> and | ||||||||||||||
| 147 | #pod L<HTTP::Response|HTTP::Response> objects, | ||||||||||||||
| 148 | #pod along with other methods for doing things like retrieving the parsed | ||||||||||||||
| 149 | #pod L<XML::LibXML::Document|XML::LibXML::Document> DOM | ||||||||||||||
| 150 | #pod node of the response. | ||||||||||||||
| 151 | #pod | ||||||||||||||
| 152 | #pod =back | ||||||||||||||
| 153 | #pod | ||||||||||||||
| 154 | #pod =attr orthodox | ||||||||||||||
| 155 | #pod | ||||||||||||||
| 156 | #pod When set to true at construction, the generated methods will exactly match | ||||||||||||||
| 157 | #pod their C<CamelCase> SOAP operation names. | ||||||||||||||
| 158 | #pod | ||||||||||||||
| 159 | #pod =cut | ||||||||||||||
| 160 | |||||||||||||||
| 161 | 4 | has orthodox => ( is => 'ro', isa => Bool, default => 0 ); | |||||||||||||
| 162 | |||||||||||||||
| 163 | #pod =for Pod::Coverage BUILD | ||||||||||||||
| 164 | #pod | ||||||||||||||
| 165 | #pod =cut | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 168 | 0 | my $self = shift; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 0 | for my $service ( @{ $self->services } ) { | |||||||||||||
| 171 | 0 | ▶ | while ( my ( $operation_name, $client ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | 0 | = each %{ $service->clients } ) | |||||||||||||
| 173 | { | ||||||||||||||
| 174 | 0 | my $method_name = $operation_name; | |||||||||||||
| 175 | 0 | ▶ | if ( not $self->orthodox ) { # normalize operation name | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | 0 | $method_name | |||||||||||||
| 177 | =~ s/ (?<= [[:alnum:]] ) ( [[:upper:]] ) /_\l$1/xmsg; | ||||||||||||||
| 178 | 0 | $method_name = lcfirst $method_name; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | $self->_stash->add_symbol( "&$method_name" => | |||||||||||||
| 182 | _method_closure( $service, $operation_name, $client ) ); | ||||||||||||||
| 183 | } | ||||||||||||||
| 184 | } | ||||||||||||||
| 185 | 0 | return; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | has _stash => ( | ||||||||||||||
| 189 | is => 'lazy', | ||||||||||||||
| 190 | isa => InstanceOf ['Package::Stash'], | ||||||||||||||
| 191 | 0 | ▶ | default => sub { Package::Stash->new(__PACKAGE__) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 192 | 4 | ); | |||||||||||||
| 193 | |||||||||||||||
| 194 | const my %OPERATION_PARAMETER => ( | ||||||||||||||
| 195 | Ping => 'Message', | ||||||||||||||
| 196 | IsAuthorized => 'Operations', | ||||||||||||||
| 197 | 4 | map { ( $_ => "${_}Request" ) } | |||||||||||||
| 198 | qw( | ||||||||||||||
| 199 | GetTax | ||||||||||||||
| 200 | GetTaxHistory | ||||||||||||||
| 201 | PostTax | ||||||||||||||
| 202 | CommitTax | ||||||||||||||
| 203 | CancelTax | ||||||||||||||
| 204 | ReconcileTaxHistory | ||||||||||||||
| 205 | AdjustTax | ||||||||||||||
| 206 | ApplyPayment | ||||||||||||||
| 207 | TaxSummaryFetch | ||||||||||||||
| 208 | Validate | ||||||||||||||
| 209 | ), | ||||||||||||||
| 210 | ); | ||||||||||||||
| 211 | |||||||||||||||
| 212 | 0 | ▶ | sub _method_closure { | ||||||||||||
|
Subroutine
_method_closure: not called CC=1 SCAR=6.9
| |||||||||||||||
| 213 | 0 | my ( $service, $operation_name, $client ) = @_; | |||||||||||||
| 214 | 0 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||
| 215 | 0 | my ( $self, @parameters ) = @_; | |||||||||||||
| 216 | |||||||||||||||
| 217 | 0 | $service->_current_operation_name($operation_name); | |||||||||||||
| 218 | 0 | ▶ | if ( 'GetTax' eq $operation_name ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | 0 | @parameters = _today_to_docdate(@parameters); | |||||||||||||
| 220 | } | ||||||||||||||
| 221 | |||||||||||||||
| 222 | 0 | my $client_version = "$PROGRAM_NAME,"; | |||||||||||||
| 223 | 0 | my $adapter_version = __PACKAGE__ . q{,}; | |||||||||||||
| 224 | 0 | ▶ | if ($main::VERSION) { $client_version .= $main::VERSION } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 225 | 0 | ▶ | if ($VERSION) { $adapter_version .= $VERSION } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 226 | my ( $answer_ref, $trace ) = $client->( | ||||||||||||||
| 227 | Profile => { | ||||||||||||||
| 228 | Client => $client_version, | ||||||||||||||
| 229 | Adapter => $adapter_version, | ||||||||||||||
| 230 | Machine => hostname(), | ||||||||||||||
| 231 | }, | ||||||||||||||
| 232 | parameters => { | ||||||||||||||
| 233 | $OPERATION_PARAMETER{$operation_name} => @parameters % 2 | ||||||||||||||
| 234 | ? "@parameters" | ||||||||||||||
| 235 | : {@parameters}, | ||||||||||||||
| 236 | }, | ||||||||||||||
| 237 | $service->use_wss ? () | ||||||||||||||
| 238 | : ( Security => { | ||||||||||||||
| 239 | UsernameToken => { | ||||||||||||||
| 240 | 0 | ▶ | map { ( "\u$_" => $service->$_ ) } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 241 | qw(username password), | ||||||||||||||
| 242 | }, | ||||||||||||||
| 243 | }, | ||||||||||||||
| 244 | ), | ||||||||||||||
| 245 | ); | ||||||||||||||
| 246 | 0 | ▶ | if ( 'HASH' eq ref $answer_ref ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 247 | ## no critic (ValuesAndExpressions::ProhibitAccessOfPrivateData) | ||||||||||||||
| 248 | $answer_ref | ||||||||||||||
| 249 | 0 | = $answer_ref->{parameters}{"${operation_name}Result"}; | |||||||||||||
| 250 | } | ||||||||||||||
| 251 | 0 | ▶ | return wantarray ? ( $answer_ref, $trace ) : $answer_ref; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 252 | 0 | }; | |||||||||||||
| 253 | } | ||||||||||||||
| 254 | |||||||||||||||
| 255 | 0 | ▶ | sub _today_to_docdate { | ||||||||||||
|
Subroutine
_today_to_docdate: not called CC=2 SCAR=17.9
| |||||||||||||||
| 256 | 0 | my %parameters = @_; | |||||||||||||
| 257 | 0 | ▶ | if ( not defined $parameters{DocDate} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 258 | $parameters{DocDate} | ||||||||||||||
| 259 | 0 | = DateTime::Format::XSD->format_datetime( DateTime->today ); | |||||||||||||
| 260 | 0 | $parameters{DocDate} =~ s/ T .* \z//xms; | |||||||||||||
| 261 | } | ||||||||||||||
| 262 | 0 | return %parameters; | |||||||||||||
| 263 | } | ||||||||||||||
| 264 | |||||||||||||||
| 265 | 4 | 1; | |||||||||||||
| 266 | |||||||||||||||
| 267 | __END__ | ||||||||||||||