| 1 | package Business::UPS; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use JSON::PP qw(decode_json encode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | our @EXPORT = qw/ getUPS UPStrack /; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # Copyright 2003 Justin Wheeler <upsmodule@datademons.com> | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # Copyright 1998 Mark Solomon <msolomon@seva.net> (See GNU GPL) | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # Started 01/07/1998 Mark Solomon | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | our $VERSION = '2.04'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | sub getUPS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getUPS: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | warnings::warnif( 'deprecated', | |||||||||||||||||||||||||||||||||||||||||
| 23 | 'getUPS() is deprecated: the UPS rate quoting endpoint (qcostcgi.cgi) ' | ||||||||||||||||||||||||||||||||||||||||||
| 24 | . 'has been retired by UPS. This function will be removed in a future ' | ||||||||||||||||||||||||||||||||||||||||||
| 25 | . 'release. See Business::UPS documentation for alternatives.' ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | my ( | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | $product, $origin, $dest, $weight, $country, $rate_chart, $length, | |||||||||||||||||||||||||||||||||||||||||
| 29 | $width, $height, $oversized, $cod | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | $country ||= 'US'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$country ||= "US"
MC/DC100%1/1
$country ||= "US"
$country
Truth table100%2/2
A
A= $country
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | my $ups_cgi = 'https://www.ups.com/using/services/rave/qcostcgi.cgi'; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | my $workString = "?"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | $workString .= "accept_UPS_license_agreement=yes&"; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | $workString .= "10_action=3&"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | $workString .= "13_product=" . $product . "&"; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | $workString .= "15_origPostal=" . $origin . "&"; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | $workString .= "19_destPostal=" . $dest . "&"; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | $workString .= "23_weight=" . $weight; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 7 | ▶ | $workString .= "&22_destCountry=" . $country if $country; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | ▶ | $workString .= "&25_length=" . $length if $length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | $workString .= "&26_width=" . $width if $width; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | ▶ | $workString .= "&27_height=" . $height if $height; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | $workString .= "&29_oversized=1" if $oversized; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 7 | ▶ | $workString .= "&47_rate_chart=" . $rate_chart if $rate_chart; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 7 | ▶ | $workString .= "&30_cod=1" if $cod; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | $workString = "${ups_cgi}${workString}"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | my $lwp = LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||
| 52 | agent => "Business-UPS/$VERSION", | ||||||||||||||||||||||||||||||||||||||||||
| 53 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 7 | my $result = $lwp->get($workString); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | croak("Failed fetching data.") unless $result->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | my @ret = split( '%', $result->content ); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | ▶ | if ( !$ret[5] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # Error | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | return ( undef, undef, $ret[1] ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # Good results | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | my $total_shipping = $ret[10]; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | my $ups_zone = $ret[6]; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | return ( $total_shipping, $ups_zone, undef ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 15 | ▶ | sub UPStrack { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
UPStrack: called CC=25 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 15 | my $tracking_number = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 15 | my %retValue; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 15 | ▶ | $tracking_number || croak("No tracking number provided to UPStrack()"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 13 | my $ups_url = 'https://www.ups.com/track/api/Track/GetStatus?loc=en_US'; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 13 | my $payload = encode_json( { | |||||||||||||||||||||||||||||||||||||||||
| 82 | Locale => 'en_US', | ||||||||||||||||||||||||||||||||||||||||||
| 83 | TrackingNumber => [$tracking_number], | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } ); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 13 | my $lwp = LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||
| 87 | agent => "Business-UPS/$VERSION", | ||||||||||||||||||||||||||||||||||||||||||
| 88 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 13 | my $result = $lwp->post( | |||||||||||||||||||||||||||||||||||||||||
| 91 | $ups_url, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 93 | Content => $payload, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 13 | ▶ | croak("Cannot get tracking data from UPS") unless $result->is_success(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 12 | my $json; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 12 | eval { $json = decode_json( $result->content() ) }; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 12 | ▶ | croak("Cannot parse JSON response from UPS: $@") if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 11 | my $details = $json->{trackDetails}; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 11 | ▶ | croak("No tracking details returned from UPS") unless $details && ref($details) eq 'ARRAY' && @$details; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$details and ref $details eq "ARRAY" and @$details
$details and ref $details eq "ARRAY"
MC/DC66%2/3
$details and ref $details eq "ARRAY" and @$details
$details
ref $details eq "ARRAY"
@$details
Truth table75%3/4
A and B and C
A= $detailsB= ref $details eq "ARRAY"C= @$details
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 9 | my $track = $details->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 9 | ▶ | $retValue{'Current Status'} = $track->{packageStatus} if $track->{packageStatus}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | ▶ | $retValue{'Service Type'} = $track->{service} if $track->{service}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | if ( my $w = $track->{weight} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | if ( $w->{weight} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | my $unit = $w->{unitOfMeasurement} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$w->{'unitOfMeasurement'} || ''
MC/DC100%1/1
$w->{'unitOfMeasurement'} || ''
$w->{'unitOfMeasurement'}
Truth table100%2/2
A
A= $w->{'unitOfMeasurement'}
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | $retValue{'Weight'} = length($unit) ? "$w->{weight} $unit" : $w->{weight}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | if ( my $addr = $track->{shipToAddress} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | my @parts = grep { $_ } @{$addr}{qw(city state country)}; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | $retValue{'Shipped To'} = join( ', ', @parts ) if @parts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 9 | ▶ | my $delivery_date = $track->{scheduledDeliveryDate} || $track->{deliveredDate}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$track->{'scheduledDeliveryDate'} || $track->{'deliveredDate'}
MC/DC100%2/2
$track->{'scheduledDeliveryDate'} || $track->{'deliveredDate'}
$track->{'scheduledDeliveryDate'}
$track->{'deliveredDate'}
Truth table100%3/3
A || B
A= $track->{'scheduledDeliveryDate'}B= $track->{'deliveredDate'}
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 9 | ▶ | $retValue{'Delivery Date'} = $delivery_date if $delivery_date; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 9 | ▶ | $retValue{'Signed By'} = $track->{receivedBy} if $track->{receivedBy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | $retValue{'Location'} = $track->{leftAt} if $track->{leftAt}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 9 | my %scanning; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 9 | my $count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | if ( my $activities = $track->{shipmentProgressActivities} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | for my $act (@$activities) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | $count++; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | ▶ | $scanning{$count}{'date'} = $act->{date} if $act->{date}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | ▶ | $scanning{$count}{'time'} = $act->{time} if $act->{time}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 5 | ▶ | $scanning{$count}{'location'} = $act->{location} if $act->{location}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 5 | ▶ | $scanning{$count}{'activity'} = $act->{activityScan} if $act->{activityScan}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 9 | $retValue{'Scanning'} = \%scanning; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 9 | $retValue{'Activity Count'} = $count; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 9 | $retValue{'Notice'} = "UPS authorizes you to use UPS tracking systems solely to track shipments tendered by or for you to UPS for delivery and for no other purpose. Any other use of UPS tracking systems and information is strictly prohibited."; | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 9 | return %retValue; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||||||||