/
usps_query.tag
346 lines (269 loc) · 9.5 KB
/
usps_query.tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright 2002 Interchange Development Group (http://www.icdevgroup.org/)
# Licensed under the GNU GPL v2. See file LICENSE for details.
# $Id: usps_query.tag,v 1.4 2005-02-10 14:38:39 docelic Exp $
UserTag usps-query Order service weight
UserTag usps-query addAttr
UserTag usps-query Version $Revision: 1.4 $
UserTag usps-query Routine <<EOR
sub {
my ($service, $weight, $opt) = @_;
my ($rate, $resp, $xml, $mailtype, @intl, $m_rep, $m_mod);
my %supported_services = (
'EXPRESS' => 1,
'FIRST CLASS' => 1,
'PRIORITY' => 1,
'PARCEL' => 1,
'BPM' => 1,
'LIBRARY' => 1,
'MEDIA' => 1,
'GLOBAL EXPRESS GUARANTEED DOCUMENT SERVICE' => 1,
'GLOBAL EXPRESS GUARANTEED NON-DOCUMENT SERVICE' => 1,
'GLOBAL EXPRESS MAIL (EMS)' => 1,
'AIRMAIL PARCEL POST' => 1,
'ECONOMY (SURFACE) PARCEL POST' => 1,
'POSTCARDS - AIRMAIL' => 1,
'AEROGRAMMES - AIRMAIL' => 1,
'MATTER FOR THE BLIND - ECONOMY MAIL' => 1,
);
my %package_sizes = (
'REGULAR' => 1,
'LARGE' => 1,
'OVERSIZE' => 1,
);
my %mailtypes = (
'package' => 1,
'postcards or aerogrammes' => 1,
'matter for the blind' => 1,
'envelope' => 1,
);
my $error_msg = 'USPS: ';
my $origin = $opt->{origin} || $::Variable->{USPS_ORIGIN} || $::Variable->{UPS_ORIGIN};
my $destination = $opt->{destination} || $::Values->{zip} || $::Variable->{SHIP_DEFAULT_ZIP};
my $userid = $opt->{userid} || $::Variable->{USPS_ID};
my $passwd = $opt->{passwd} || $::Variable->{USPS_PASSWORD};
my $url = $opt->{url} || $::Variable->{USPS_URL} || 'http://Production.ShippingAPIs.com/ShippingAPI.dll';
my $container = $opt->{container} || $::Variable->{USPS_CONTAINER} || 'None';
my $machinable = $opt->{machinable} || $::Variable->{USPS_MACHINABLE} || 'False';
$service = uc $service;
if (! $supported_services{$service}) {
$error_msg .= "unknown service type $service.";
return;
}
my $size = uc ($opt->{size} || $::Variable->{USPS_SIZE} || 'REGULAR');
if (! $package_sizes{$size}) {
$error_msg .= "unknown package size $size.";
return
}
if ($service eq 'PARCEL') {
if ($weight < .375 or $weight > 35) {
$machinable = 'False';
}
}
if ($opt->{country}) {
$mailtype = lc ($opt->{mailtype} || $::Variable->{USPS_MAILTYPE} || 'package');
unless ($mailtypes{$mailtype}) {
$error_msg = "unknown mail type '$mailtype'.";
return;
}
}
my $modulo = $opt->{modulo} || $::Variable->{USPS_MODULO};
if ($modulo and ($modulo < $weight)) {
$m_rep = int $weight / $modulo;
$m_mod = $weight % $modulo;
$weight = $modulo;
}
RATEQUOTE: {
my $ounces = int(($weight - int($weight)) * 16);
$weight = int $weight;
if ($opt->{country}) {
$xml = qq{API=IntlRate\&XML=<IntlRateRequest USERID="$userid" PASSWORD="$passwd">};
$xml .= <<EOXML;
<Package ID="0">
<Pounds>$weight</Pounds>
<Ounces>$ounces</Ounces>
<MailType>$mailtype</MailType>
<Country>$opt->{country}</Country>
</Package>
</IntlRateRequest>
EOXML
}
else {
$xml = qq{API=Rate\&XML=<RateRequest USERID="$userid" PASSWORD="$passwd">};
$xml .= <<EOXML;
<Package ID="0">
<Service>$service</Service>
<ZipOrigination>$origin</ZipOrigination>
<ZipDestination>$destination</ZipDestination>
<Pounds>$weight</Pounds>
<Ounces>$ounces</Ounces>
<Container>$container</Container>
<Size>$size</Size>
<Machinable>$machinable</Machinable>
</Package>
</RateRequest>
EOXML
}
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request 'POST', "$url";
$req->content_type('application/x-www-form-urlencoded');
$req->content($xml);
my $response = $ua->request($req);
$error_msg = 'USPS: ';
if ($response->is_success) {
$resp = $response->content;
}
else {
$error_msg .= 'Error obtaining rate quote from usps.com.';
}
if ($resp =~ /<Error>/i) {
$resp =~ m|<Description>(.+)</Description>|;
$error_msg .= $1;
}
else {
if ($opt->{country}) {
@intl = split /<Service/, $resp;
foreach (@intl) {
m|<SvcDescription>(.+)</SvcDescription>|;
$resp = uc $1;
if ($resp eq $service) {
m|<Postage>(.+)</Postage>|;
$rate += $1;
undef $error_msg;
last;
}
}
}
else {
$resp =~ m|<Postage>(.+)</Postage>|;
$rate += $1;
undef $error_msg;
}
}
}
if ($m_rep) {
$rate *= $m_rep; undef $m_rep;
}
if ($m_mod) {
$weight = $m_mod; undef $m_mod;
goto RATEQUOTE;
}
$::Session->{ship_message} .= " $error_msg" if $error_msg;
return $rate;
}
EOR
UserTag usps-query Documentation <<EOD
=head1 NAME
usps-query tag -- calculate USPS costs via www
=head1 SYNOPSIS
[usps-query
service="service name"
weight="NNN"
userid="USPS webtools user id"*
passwd="USPS webtools password"*
origin="NNNNN"*
destination="NNNNN"*
url="applet URL"*
container="container type"*
size="package size"*
machinable="True/False"*
mailtype="mailing type"*
country="Country name"*
modulo="NN"*
]
=head1 DESCRIPTION
Calculates USPS costs via the WWW using the United States Postal Service Rate
Rate Calculator API. You *MUST* register with USPS in order to use this service.
Visit http://www.usps.com/webtools and follow the link(s) to register. You will
receive a confirmation email upon completing the registration process. You
*MUST* follow the instructions in this email to obtain access to the production
rate quote server. THIS USERTAG WILL NOT WORK WITH USPS's TEST SERVER.
=head1 PARAMETERS
=head2 Base Parameters (always required):
=over 4
=item service
The USPS service you wish to get a rate quote for. Services currently supported:
EXPRESS
FIRST CLASS
PRIORITY
PARCEL
BPM
LIBRARY
MEDIA
GLOBAL EXPRESS GUARANTEED DOCUMENT SERVICE
GLOBAL EXPRESS GUARANTEED NON-DOCUMENT SERVICE
GLOBAL EXPRESS MAIL (EMS)
AIRMAIL PARCEL POST
ECONOMY (SURFACE) PARCEL POST
POSTCARDS - AIRMAIL
AEROGRAMMES - AIRMAIL
MATTER FOR THE BLIND - ECONOMY MAIL
=item weight
The total weight of the items to be mailed/shipped.
=item userid
Your USPS webtools userid, which was obtained by registering.
This will default to $Variable->{USPS_ID}, which is the preferred
way to set this parameter.
=item passwd
Your USPS webtools passwd, which was obtained by registering.
This will default to $Variable->{USPS_PASSWORD}, which is the
preferred way to set this parameter.
=back 4
=head2 Extended Parameters (domestic and international services)
=over 4
=item url
The URL of the USPS rate quote API. The default is $Variable->{USPS_URL}
or 'http://Production.ShippingAPIs.com/ShippingAPI.dll'.
=item modulo
Enables a rudimentary method of obtaining rate quotes for multi-box shipments.
'modulo' is a number which represents the maximum weight per box; the default
is $Variable->{USPS_MODULO}. When modulo > 0, the shipping weight will be divided
into the number of individual parcels of max. weight 'modulo' which will accommodate
the whole shipment, and the total rate will be calculated accordingly.
Example: with modulo = 10, a 34.5lbs. shipment will be calculated as 3 parcels
weighing 10lbs. each, plus one parcel weighing 4lbs. 8oz.
=back 4
=head2 Extended Parameters for domestic (U.S.) services only
=over 4
=item origin
Origin zip code. Default is $Variable->{USPS_ORIGIN} or $Variable->{UPS_ORIGIN}.
=item destination
Destination zip code. Default is $Values->{zip} or $Variable->{SHIP_DEFAULT_ZIP}.
=item container
The USPS-defined container type for the shipment. Default is
Variable->{USPS_CONTAINER} or 'None". Please see the Technical Guide to the
Domestic Rates Calculator Application Programming Interface for a complete
list of container types.
=item size
The USPS-defined package size for the shipment. Valid choices are
'REGULAR', 'LARGE', and 'OVERSIZE'. The default is $Variable->{USPS_SIZE} or
'REGULAR'. Please see the Technical Guide to the Domestic Rates Calculator
Application Programming Interface for a definition of package sizes.
=item machinable (for PARCEL service only)
Possible value are 'True' and 'False'. Indicates whether or not the shipment
qualifies for machine processing by UPS. Default is $Variable->{USPS_MACHINABLE}
or 'False". Consult the USPS service guides for more info on this subject.
=back 4
=head2 Extended parameters for International services only
=over 4
=item mailtype
The USPS-defined mail type for the shipment. Valid choices are:
package
postcards or aerogrammes
matter for the blind
envelope
Default is $Variable->{USPS_MAILTYPE} or 'package'. See the USPS international
service guides for more information on this topic.
=item country (required for international services)
Destination country. No default. You must pass the name of the country, not the ISO
code or abbreviation (i.e. 'Canada', not 'CA'). Note that USPS maintains a table of
valid country names which does not necessarily match all entries in the country
table which is distributed with the standard demo, so modifications may be needed
if you intend to use USPS international services. Consult the USPS International
Services guide for more information.
=back 4
=head1 BUGS
We shall see....
=head1 AUTHORS
Ed LaFrance <edl@newmediaems.com>.
=cut
EOD