X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FMisc%2FGeo.pm;h=6b3d6ca71759b84f9c5b4d7c1f878c2cae7b2ca0;hb=2a56c671635687bf2648eb3a7cf4bce228101af3;hp=aa4e55e36761d469521d77897fb1989a0359ac8b;hpb=dd003d59f56742f9374cec309ad81d527e88c846;p=freeside.git diff --git a/FS/FS/Misc/Geo.pm b/FS/FS/Misc/Geo.pm index aa4e55e36..6b3d6ca71 100644 --- a/FS/FS/Misc/Geo.pm +++ b/FS/FS/Misc/Geo.pm @@ -6,6 +6,7 @@ use vars qw( $DEBUG @EXPORT_OK $conf ); use LWP::UserAgent; use HTTP::Request; use HTTP::Request::Common qw( GET POST ); +use IO::Socket::SSL; use HTML::TokeParser; use Cpanel::JSON::XS; use URI::Escape 3.31; @@ -209,13 +210,13 @@ sub wa_sales { if ( lc($text) eq 'location code' ) { $p->get_tag('td'); # skip to the next column undef $u; - $u = $p->get_token until $u->[0] eq 'T'; # and then skip non-text + $u = $p->get_token until ($u->[0] || '') eq 'T'; # and then skip non-text $return->{'district'} = $u->[1]; } elsif ( lc($text) eq 'total tax rate' ) { $p->get_tag('td'); undef $u; - $u = $p->get_token until $u->[0] eq 'T'; + $u = $p->get_token until ($u->[0] || '') eq 'T'; $return->{'tax'} = $u->[1]; } } # get_token @@ -642,6 +643,50 @@ sub standardize_melissa { } } +sub standardize_freeside { + my $class = shift; + my $location = shift; + + my $url = 'https://ws.freeside.biz/normalize'; + + #free freeside.biz normalization only for US + if ( $location->{country} ne 'US' ) { + # soft failure + #why? something else could have cleaned it $location->{addr_clean} = ''; + return $location; + } + + my $ua = LWP::UserAgent->new( + 'ssl_opts' => { + verify_hostname => 0, + SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, + }, + ); + my $response = $ua->request( POST $url, [ + 'support-key' => scalar($conf->config('support-key')), + %$location, + ]); + + die "Address normalization error: ". $response->message + unless $response->is_success; + + local $@; + my $content = eval { decode_json($response->content) }; + if ( $@ ) { + warn $response->content; + die "Address normalization JSON error : $@\n"; + } + + die $content->{error}."\n" + if $content->{error}; + + { 'addr_clean' => 'Y', + map { $_ => $content->{$_} } + qw( address1 address2 city state zip country ) + }; + +} + =back =cut