File Coverage

lib/Business/Shipping/UPS_XML.pm
Criterion Covered Total %
statement 12 92 13.0
branch 0 12 0.0
condition 0 6 0.0
subroutine 4 12 33.3
pod 3 7 42.8
total 19 129 14.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             ################################################################################
3             #
4             # Script Name : UPS_XML.pm
5             # Version : 1
6             # Company : Down Home Web Design, Inc
7             # Author : Duane Hinkley ( duane@dhwd.com )
8             # Website : www.DownHomeWebDesign.com
9             #
10             # Description: A custom self contained module to calculate UPS rates using the
11             # newer XML method. This module properly calulates rates between
12             # and within other non-US countries including Canada.
13             #
14             # Copyright (c) 2003-2004 Down Home Web Design, Inc. All rights reserved.
15             #
16             # $Header: /home/cvs/interchange_upsxml/lib/Business/Shipping/UPS_XML.pm,v 1.1 2004/06/27 13:59:11 dlhinkley Exp $
17             #
18             # $Log: UPS_XML.pm,v $
19             # Revision 1.1 2004/06/27 13:59:11 dlhinkley
20             # Rename module
21             #
22             # Revision 1.6 2004/06/27 13:53:20 dlhinkley
23             # Rename module to UPS_XML
24             #
25             # Revision 1.5 2004/06/16 00:52:45 dlhinkley
26             # Clean up docs and add capability of multiple quantity of packages
27             #
28             # Revision 1.4 2004/06/15 14:56:34 dlhinkley
29             # Added sending dimensions for multiple packages
30             #
31             # Revision 1.3 2004/06/10 02:03:16 dlhinkley
32             # Fixed bugs from breaking up code and putting in CPAN format
33             #
34             # Revision 1.2 2004/06/01 02:48:25 dlhinkley
35             # Changes to make work
36             #
37             # Revision 1.5 2004/05/21 21:28:36 dlhinkley
38             # Add Currency
39             #
40             # Revision 1.4 2004/04/20 01:28:00 dlhinkley
41             # Added option for dimensions
42             #
43             # Revision 1.3 2004/03/14 18:50:31 dlhinkley
44             # Working version
45             #
46             #
47             ################################################################################
48              
49             package Business::Shipping::UPS_XML;
50 2     2   4421 use strict;
  2         4  
  2         70  
51              
52 2     2   12 use vars qw($VERSION);
  2         3  
  2         132  
53              
54             $VERSION = "0.07";
55              
56 2     2   22468 use Net::SSLeay qw(post_https make_form make_headers);
  2         48034  
  2         3498  
57              
58 2     2   3049 use Business::Shipping::UPS_XML::Parser;
  2         6  
  2         2441  
59              
60             sub new {
61              
62 0     0 1   my $type = shift;
63 0           my ($userid,$userid_pass,$access_key,$origin_country) = @_;
64 0           my $self = {};
65 0           $self->{'_userid'} = $userid;
66 0           $self->{'_userid_pass'} = $userid_pass;
67 0           $self->{'_access_key'} = $access_key;
68 0           $self->{'_from_country'} = $origin_country;
69 0           $self->{'package_type'} = '02';
70 0           $self->{'residential'} = "1";
71 0           $self->{'package_count'} = 0;
72              
73 0           bless $self, $type;
74             }
75            
76             sub query_ups {
77              
78 0     0 0   my $self = shift;
79 0           my ($xml) = @_;
80              
81 0           my ($page, $response, %reply_headers) = post_https('www.ups.com', '443', '/ups.app/xml/Rate',
82              
83             make_headers(
84            
85             'content-type' => 'application/x-www-form-urlencoded',
86             ),
87            
88             $xml
89             );
90              
91 0           return ($page,$response);
92             }
93             sub set_dimensions {
94            
95 0     0 1   my $self = shift;
96 0           my ( $width, $height, $length, $unit_of_measure, $weight) = @_;
97              
98 0           $self->{'package_count'}++;
99              
100 0           $self->{'length'}->[ $self->{'package_count'} ] = $length;
101 0           $self->{'width'}->[ $self->{'package_count'} ] = $width;
102 0           $self->{'height'}->[ $self->{'package_count'} ] = $height;
103 0           $self->{'unit_of_measure'}->[ $self->{'package_count'} ] = $unit_of_measure;
104 0           $self->{'weight'}->[ $self->{'package_count'} ] = $weight;
105             }
106             sub clr_dimensions {
107            
108 0     0 0   my $self = shift;
109              
110 0           $self->{'length'} = undef;
111 0           $self->{'width'} = undef;
112 0           $self->{'height'} = undef;
113 0           $self->{'unit_of_measure'} = undef;
114 0           $self->{'package_count'} = 0;
115             }
116             sub getUPS {
117            
118 0     0 1   my $self = shift;
119 0           my ( $service_code, $from_zip, $to_zip, $to_country, $weight) = @_;
120              
121 0           my $access_key = $self->{'_access_key'};
122 0           my $userid = $self->{'_userid'};
123 0           my $userid_pass = $self->{'_userid_pass'};
124 0           my $from_country = $self->{'_from_country'};
125 0           my $package_type = $self->{'package_type'};
126 0           my $residential = $self->{'residential'};
127 0           $self->{'single_weight'} = $weight;
128 0           my $error = "";
129 0           my $maxcost;
130             my $response;
131 0           my $zone;
132 0           my $x;
133 0           my $currency;
134 0           $self->{'send_xml'} = undef;
135 0           $self->{'rcv_xml'} = undef;
136              
137              
138              
139              
140 0           my $xml = "
141            
142             $access_key
143             $userid
144             $userid_pass
145            
146            
147            
148            
149            
150             Rating and Service
151             1.0001
152            
153             Rate
154             Rate
155            
156            
157             01
158            
159            
160            
161            
162            
163             $from_zip
164             $from_country
165            
166            
167            
168            
169            
170             $to_zip
171             $to_country
172             $residential
173            
174            
175            
176             $service_code
177            
178             " . $self->_packages() . "
179            
180            
181             ";
182              
183             #print "$xml\n";
184 0           $self->{'send_xml'} = $xml;
185 0           ($xml,$response) = $self->query_ups($xml);
186 0           $self->{'rcv_xml'} = $xml;
187              
188              
189             #print "$response\n\n";
190              
191 0           $x = new Business::Shipping::UPS_XML::Parser($xml);
192 0           $x->parse();
193              
194 0           my ($method,$return,$status) = split(/ /,$response);
195              
196             # Check for error
197             #
198 0 0         if ( $return ne "200" ) {
    0          
199              
200 0           $error = "HTML $return '$response'";
201             }
202             elsif ( $x->{'RatingServiceSelectionResponse'}->{'Response'}->{'ResponseStatusDescription'} eq 'Failure' ) {
203              
204 0           $error = "UPS XML Error: " . $x->{'RatingServiceSelectionResponse'}->{'Response'}->{'Error'}->{'ErrorDescription'};
205             }
206             else {
207              
208 0           $maxcost = $x->{'RatingServiceSelectionResponse'}->{'RatedShipment'}->{'TotalCharges'}->{'MonetaryValue'};
209 0           $currency = $x->{'RatingServiceSelectionResponse'}->{'RatedShipment'}->{'TotalCharges'}->{'CurrencyCode'};
210             }
211              
212 0           $self->clr_dimensions();
213              
214 0           return ($maxcost, $zone, $error,$currency);
215             }
216             sub _packages {
217            
218 0     0     my $self = shift;
219 0           my $xml;
220             my $l;
221              
222             # Do packages with dimensions
223             #
224 0 0         if ( $self->{'package_count'} > 0 ) {
225              
226              
227 0           for ($l = 1; $l <= $self->{'package_count'}; $l++) {
228              
229 0           $xml .= "
230            
231            
232             " . $self->{'package_type'} . "
233             Package
234             ";
235              
236 0 0 0       if ( $self->{'length'}->[ $l ] && $self->{'width'}->[ $l ] && $self->{'height'}->[ $l ] ) {
      0        
237              
238 0           $xml .= "
239            
240            
241             " . $self->{'unit_of_measure'}->[ $l ] . "
242            
243             " . $self->{'length'}->[ $l ] . "
244             " . $self->{'width'}->[ $l ] . "
245             " . $self->{'height'}->[ $l ] . "
246             ";
247             }
248              
249 0           $xml .= "
250             Rate Shopping";
251              
252 0 0         if ( $self->{'weight'}->[ $l ] ) {
253              
254 0           $xml .= "
255            
256             ". $self->{'weight'}->[ $l ] . "
257             ";
258             }
259              
260 0           $xml .= "
261            
262             ";
263              
264             }
265            
266             }
267             else { # A single package no dimensions
268              
269 0           $xml .= "
270            
271            
272             " . $self->{'package_type'} . "
273             Package
274             ";
275              
276 0           $xml .= "
277             Rate Shopping";
278              
279 0 0         if ( $self->{'single_weight'} > 0 ) {
280              
281 0           $xml .= "
282            
283             ". $self->{'single_weight'} . "
284             ";
285             }
286              
287 0           $xml .= "
288            
289             ";
290              
291             }
292              
293 0           return $xml;
294             }
295             sub send_xml {
296              
297 0     0 0   my $self = shift;
298              
299 0           return $self->{'send_xml'};
300             }
301             sub rcv_xml {
302              
303 0     0 0   my $self = shift;
304              
305 0           return $self->{'rcv_xml'};
306             }
307             #########################################################################################33
308             # End of class
309              
310             1;
311             __END__