use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common qw( GET POST );
-use JSON;
+use IO::Socket::SSL;
+use HTML::TokeParser;
+use Cpanel::JSON::XS;
use URI::Escape 3.31;
use Data::Dumper;
use FS::Conf;
+use FS::Log;
use Locale::Country;
FS::UID->install_callback( sub {
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
sub standardize_uscensus {
my $self = shift;
my $location = shift;
+ my $log = FS::Log->new('FS::Misc::Geo::standardize_uscensus');
+ $log->debug(join("\n", @{$location}{'address1', 'city', 'state', 'zip'}));
eval "use Geo::USCensus::Geocoding";
die $@ if $@;
my $result = Geo::USCensus::Geocoding->query($request);
if ( $result->is_match ) {
# unfortunately we get the address back as a single line
+ $log->debug($result->address);
if ($result->address =~ /^(.*), (.*), ([A-Z]{2}), (\d{5}.*)$/) {
return +{
address1 => $1,
censustract => $result->censustract,
};
} else {
- die "can't parse address '".$result->address."'";
+ die "Geocoding returned '".$result->address."', which does not seem to be a valid address.\n";
}
+ } elsif ( $result->match_level eq 'Tie' ) {
+ die "Geocoding was not able to identify a unique matching address.\n";
+ } elsif ( $result->match_level ) {
+ die "Geocoding did not find a matching address.\n";
} else {
- warn Dumper($result) if $DEBUG;
- die $result->error_message;
+ $log->error($result->error_message);
+ return; # for internal errors, don't return anything
}
}
}
}
+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