/
fedex_query.tag
376 lines (332 loc) · 12.4 KB
/
fedex_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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# Copyright 2002 Interchange Development Group (http://www.icdevgroup.org/)
# Licensed under the GNU GPL v2. See file LICENSE for details.
# $Id: fedex_query.tag,v 1.4 2005-02-10 14:38:39 docelic Exp $
UserTag fedex-query Order mode weight
UserTag fedex-query attrAlias origin_zip origin
UserTag fedex-query addAttr
UserTag fedex-query Version $Revision: 1.4 $
UserTag fedex-query Documentation <<EOD
Required Variables
Construct a Rate request using the URL, variables, and values shown
below. If a value is not predetermined, the maximum length is shown in
parenthesis:
http://grd.fedex.com/cgi-bin/rrr2010.exe
Variable Name
Value
?func
=
Rate
Screen = Ground or HomeD
OriginZip = U.S. or Canada origin postal code.
OriginCountryCode = Origin country code:
US for United States
CA for Canada
DestZip = U.S., Canada, or Mexico destination postal
code.
DestCountryCode = Destination country code:
US for United States
CA for Canada
MX for Mexico
Weight = Weight, in pounds or kilograms, rounded to
the nearest whole number.
WeightUnit = The Unit of measure for the given weight:
KGS for kilograms
LBS for pounds (The default value is
Lbs)
Length = Optional: Length, in inches or centimeters,
rounded to the nearest whole number. To
calculate dimensional weight, values must be
entered for length, width, and height.
Width = Optional: Width, in inches or centimeters,
rounded to the nearest whole number. To
calculate dimensional weight, values must be
entered for length, width, and height.
Height = Optional: Height, in inches or centimeters,
rounded to the nearest whole number. To
calculate dimensional weight, values must be
entered for length, width, and height.
DimUnit = Optional: The Unit of measure for the given
dimensions (Length, Width, Height):
IN for Inches (The default value is IN)
CM for centimeters
AccessReturn = Optional: The number of accessorials
included in the request, plus the accessorial
description(s), plus =1, except for the
declared value accessorial, where the 1 is
replaced by the amount. Use a semicolon to
separate the number of accessorials included
from the first desciption, and a semicolon to
separate accessorials. The following are valid
accessorial values (values are
case-sensitive):
U.S. to U.S.
USCOD: C.O.D.or E.C.O.D. collection
USCT: Call tag
USECT: Electronic call tag
USAOD: Acknowledgement of delivery
USHazMat: Hazardous material
USDecVal: Declared value, each
additional $100
USRS: Residential surcharge
USANAC: Not in appropriate container
or single dimension greater than 60
inches
USAPOD: Auto proof of delivery
U.S. to Canada
USCODC: C.O.D. collection to Canada
USAOD: Acknowlegement of delivery
USDecVal: Declared value, each
additional $100
USANAC: Not in appropriate container
or single dimension greater than 60
inches
USRS: Residential surcharge
USAPOD: Auto proof of delivery
U.S. to Mexico
USDecVal: Declared value, each
additional $100
USRS: Residential surcharge
USANAC: Not in appropriate container
or single dimension greater than 60
inches
Canada to Canada
CACOD: C.O.D. or E.C.O.D. collection
CACT: Call tag
CAAOD: Acknowlegement of delivery
CADecVal: Declared value, each
additional $100
CARS: Residential surcharge
CAANAC: Not in appropriate container
or single dimension greater than 60
inches
CAAPOD: Auto proof of delivery
Canada to U.S.
CACOD: C.O.D. collection
CADecVal: Declared value, each
additional $100
CAANAC: Not in appropriate container
or single dimension greater than 60
inches
CAAPOD: Auto proof of delivery
U.S. to U.S. - Home Delivery
USFHDAC: Address Correction
USFHDANAC: Not in Approp. Container
or Single Dim. > 60 in.
USFHDAOD: Acknowledgement of
Delivery
USFHDDV: Declared Value Each
Additional $100
USFHDGAD: FedEx Appointment Home
Delivery
USFHDGADAPOD: FedEx Appointment
Home Delivery and Auto POD
USFHDGED: FedEx Evening Home
Delivery
USFHDGEDS: FedEx Evening Home
Delivery with Signature
USFHDGEDSAP: FedEx Evening Home
Delivery with Signature and Auto POD
USFHDGSDD: FedEx Date Certain
Home Delivery
USFHDGSDDS: FedEx Date Certain
Day Home Delivery with Signature
USFHDGSDDSAP: FedEx Date Certain
Day Home Delivery with Signature and
Auto POD
USFHDGSS: FedEx Signature Home
Service
USFHDGSSAPOD: FedEx Signature
Home Service and Auto POD
Top
Example
A URL for a Rate request without dimensional weight, oversize, or
accessorials would be constructed as follows:
!!! Line breaks are used here for clarity; URLs cannot include line breaks or
spaces.
http://grd.fedex.com/cgi-bin/rrr2010.exe
?func=Rate
&Screen=Ground
&OriginZip=44429
&OriginCountryCode=US
&DestZip=C1C1C1
&DestCountryCode=CA
&Weight=50
The URL for a Rate request that includes dimensions, oversize indicator,
and accessorials would be as follows:
http://grd.fedex.com/cgi-bin/rrr2010.exe
?func=Rate
&Screen=Ground
&OriginZip=C1C1C1
&OriginCountryCode=CA
&DestZip=44429
&DestCountryCode=US
&Weight=50
&WeightUnit=KGS
&Length=36
&Width=36
&Height=30
&DimUnit=CM
&AccessReturn=2;USCODC=1;USDecVal=500
EOD
UserTag fedex-query Routine <<EOR
my $can_do_ground;
my $can_do_express;
sub {
my( $mode, $weight, $opt) = @_;
BEGIN {
eval {
require LWP::Simple;
$can_do_ground = 1;
};
};
BEGIN {
eval {
require Business::Fedex;
$can_do_express = 1;
};
};
my $die = sub {
my ($msg, @args) = @_;
$msg = ::errmsg($msg, @args);
$Vend::Session->{ship_message} .= " $msg";
return 0;
};
my $fed;
$opt->{target_url} ||= 'http://grd.fedex.com/cgi-bin/rrr2010.exe';
$opt->{origin_country} ||= $::Variable->{COUNTRY} || 'US';
$opt->{origin} ||= $::Variable->{UPS_ORIGIN};
$opt->{zip} ||= $::Values->{$::Variable->{UPS_POSTCODE_FIELD}};
$opt->{country} ||= $::Values->{$::Variable->{UPS_COUNTRY_FIELD}};
$opt->{country} = uc $opt->{country};
if($can_do_express and (! $opt->{cache} || ! $Vend::fedex_object) ) {
eval {
$Vend::fedex_object = new Business::Fedex (
orig_country => $opt->{origin_country},
orig_zip => $opt->{origin},
weight => $opt->{weight},
dest_country => $opt->{country},
dest_zip => $opt->{zip},
packaging => $opt->{packaging} || 'My Packaging',
);
$Vend::fedex_object->getrate;
};
# if there's a problem here with express lookups, log the error
# but don't actually return so ground lookups can still be done
$die->($@) if $@;
}
$fed = $Vend::fedex_object if $can_do_express;
my %is_express = (
'FPO' => 1,
'FSO' => 1,
'F2D' => 1,
'FES' => 1,
'FIE' => 1,
'FIP' => 1,
);
my %fe_map = (
'FedEx Ground' => 'FEG',
'FedEx Home Delivery' => 'FEH',
'FedEx Priority Overnight' => 'FPO',
'FedEx Standard Overnight' => 'FSO',
'FedEx 2-Day' => 'F2D',
'FedEx Express Saver' => 'FES',
'FedEx International Priority' => 'FIP',
'FedEx International Economy' => 'FIE',
);
@fe_map{values %fe_map} = @fe_map{keys %fe_map};
#Debug("fed=" . ::uneval($fed));
my @services;
#Debug("can_ground=$can_do_ground country=$opt->{country} orig_country=$opt->{origin_country}");
if($opt->{services}) {
if(
$can_do_ground
and ($opt->{country} eq 'US' or $opt->{country} eq 'CA')
and $opt->{origin_country} eq 'US'
)
{
push @services, 'FEG';
push @services, 'FEH';
}
if($fed) {
for ( $fed->services() ) {
push @services, $fe_map{$_->{service}};
}
}
return join ( ($opt->{joiner} || ' '), @services);
}
if($fed and $is_express{$opt->{mode}}) {
for ( $fed->services() ) {
next unless $fe_map{$_->{service}} eq $opt->{mode};
return $_->{total};
}
return 0;
}
if($opt->{mode} eq 'FEH') {
$opt->{mode} = 'HomeD';
}
else {
$opt->{mode} = 'Ground';
}
my @required = qw/
function
mode
origin
origin_country
zip
country
weight
/;
my @opt = qw/
length
height
width
dimunit
weightunit
accessorial
/;
my %map = qw/
function func
zip DestZip
country DestCountryCode
weight Weight
mode Screen
origin OriginZip
origin_country OriginCountryCode
length Length
height Height
width Width
dimunit DimUnit
weightunit WeightUnit
accessorial AccessReturn
/;
$opt->{function} = 'Rate'
unless length $opt->{function};
my @parms;
for(@required) {
return $die->("Fedex mode %s: required parameter %s missing", $mode, $_)
unless length $opt->{$_};
push @parms, "$map{$_}=" . Vend::Util::hexify($opt->{$_});
}
for(@opt) {
next unless length $opt->{$_};
push @parms, "$map{$_}=" . Vend::Util::hexify($opt->{$_});
}
my $url = $opt->{target_url} . '?' . join('&', @parms);
return $url if $opt->{test};
my $return = LWP::Simple::get($url);
return $die->('Unable to access Fedex calculator.')
if ! length($return);
my %result;
while( $return =~ m{<!(\w+)>(.*)<!/\1>}gs ) {
$result{$1} = $2;
}
return $Vend::Interpolate::Tmp->{$opt->{hashref}} = \%result
if $opt->{hashref};
if(! $result{TotalCharges}) {
return $die->("Error on Fedex calculation: %s", $result{Error});
}
return $result{TransitTime} if $opt->{transit_time};
#Debug("mode=$opt->{mode} total=$result{TotalCharges}");
return $result{TotalCharges};
}
EOR